summaryrefslogtreecommitdiff
path: root/mq_cmd.c
diff options
context:
space:
mode:
Diffstat (limited to 'mq_cmd.c')
-rw-r--r--mq_cmd.c192
1 files changed, 192 insertions, 0 deletions
diff --git a/mq_cmd.c b/mq_cmd.c
new file mode 100644
index 0000000..7f1887a
--- /dev/null
+++ b/mq_cmd.c
@@ -0,0 +1,192 @@
+#include "mq_cmd.h"
+
+#define MQ_CMD_MSG_SIZE 8192
+//last string element should be string ;]
+mq_cmd* mq_cmd_create(int id, char *cmd, size_t cmd_sz, char *param, size_t param_sz)
+{
+ mq_cmd *ret = NULL;
+
+ if (cmd == NULL)
+ return NULL;
+ if (param == NULL)
+ return NULL;
+
+ ret = malloc(sizeof(mq_cmd));
+ if (ret == NULL)
+ return NULL;
+
+ ret->id = id;
+ ret->sz = MQ_CMD_MSG_SIZE;
+ ret->buf = malloc(ret->sz);
+ if (!ret->buf)
+ {
+ free(ret);
+ return NULL;
+ }
+ //if not null string that everything will blow up pal
+ //cmd[cmd_sz-1] = 0x0;
+ //param[param_sz-1] = 0x0;
+ snprintf(ret->buf,ret->sz-1,":%d:%s: %s", id, cmd, param);
+
+ return ret;
+}
+
+int mq_cmd_id(mq_cmd *cmd, int *id)
+{
+ int i,j,strt,sz;
+ int ret=-1;
+ int id_parse;
+ char buf[16]; //fix this
+ /*:[ID]:*/
+
+ if (cmd == NULL)
+ return -1;
+ if (id == NULL)
+ return -1;
+
+ i = 0;
+ while ((cmd->buf[i] != 0) && (i<cmd->sz))
+ {
+ if (cmd->buf[i] == ':')
+ {
+ i++;
+ strt = i;
+ sz = 0;
+ while ((cmd->buf[i] != ':')&&
+ (cmd->buf[i] != '\0')&&
+ (i<cmd->sz))
+ {
+ sz++;
+ i++;
+ }
+ for (j=0;j<(strt+sz)||j<15;j++)
+ {
+ buf[j] = cmd->buf[strt+j];
+ }
+ buf[j] = 0x0;
+ id_parse = atoi(buf);
+ *id = id_parse;
+ return 0;
+ }
+ i++;
+ }
+ return -1;
+}
+
+//buf contains pointer to cmd
+//sz contains size of buf
+//%s formatting should work badly, writting to buf modifies cmd
+//be carefull
+char* mq_cmd_cmd(mq_cmd *cmd, char **buf, size_t *sz)
+{
+ size_t sz_parse;
+ int i,j,strt,cnt,l_c;
+ char *p;
+
+ if (cmd == NULL)
+ return NULL;
+ if (buf == NULL)
+ return NULL;
+ if (sz == NULL)
+ return NULL;
+
+ p = cmd->buf;
+
+ i = 0;
+ l_c = 0;
+ while ((cmd->buf[i] != 0)&&(i<cmd->sz))
+ {
+ if (cmd->buf[i] == ':')
+ {
+ l_c += 1;
+ if (l_c == 2)
+ {
+ i++;
+ break;
+
+ }
+ }
+ i++;
+ }
+ strt = i;
+ cnt = 0;
+ while ((cmd->buf[i] != 0)&&(i<cmd->sz))
+ {
+
+ if (cmd->buf[i] == ':')
+ break;
+ cnt++;
+ i++;
+ }
+ *buf = cmd->buf+strt;
+ *sz = cnt;
+
+ return 0;
+}
+
+int mq_cmd_param(mq_cmd *cmd, char **param, size_t *sz)
+{
+ size_t sz_parse;
+ int i,j,strt,cnt,l_c;
+ char *p;
+
+ p = cmd->buf;
+
+ i = 0;
+ l_c = 0;
+ while ((cmd->buf[i] != 0)&&(i<cmd->sz))
+ {
+ if (cmd->buf[i] == ':')
+ {
+ l_c += 1;
+ if (l_c == 3)
+ {
+ i++;
+ break;
+ }
+ }
+ i++;
+ }
+
+ while ((cmd->buf[i] != 0)&&(i<cmd->sz))
+ {
+ if (cmd->buf[i] != ' ') break;
+ i++;
+ }
+ strt = i;
+ *param = cmd->buf+strt;
+ *sz = cmd->sz - strt+1;
+
+ return 0;
+}
+
+size_t mq_cmd_size(mq_cmd *cmd)
+{
+ if (cmd == NULL)
+ {
+ return -1;
+ }
+ return cmd->sz;
+}
+
+char *mq_cmd_buf(mq_cmd *cmd)
+{
+ if (cmd == NULL)
+ {
+ return NULL;
+ }
+
+ return cmd->buf;
+}
+
+void mq_cmd_free(mq_cmd *mq)
+{
+ if (mq != NULL)
+ {
+ free(mq->buf);
+ mq->buf = NULL;
+ free(mq);
+ }
+
+}
+