summaryrefslogtreecommitdiff
path: root/mq_cmd.c
blob: bc692a47f76a21bd1b7e331d2197ce52c76d85b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "mq_cmd.h"

#define MQ_CMD_MSG_SIZE 1024
//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)
	{
		PERM();
		return NULL;
	}
	if (param == NULL)
	{
		PERM();
		return NULL;
	}

	ret = malloc(sizeof(mq_cmd));
	if (ret == NULL)
	{
		ENL();
		return NULL;
	}
	memset(ret, 0, sizeof(mq_cmd));

	ret->id = id;
	ret->sz = MQ_CMD_MSG_SIZE;
	ret->buf = malloc(MQ_CMD_MSG_SIZE);
	memset(ret->buf, 0, MQ_CMD_MSG_SIZE); //speed? who cares
	if (ret->buf == NULL)
	{
		ENL();
		free(ret);
		return NULL;
	}
	//if not null string that everything will blow up pal
	//cmd[cmd_sz-1]     = 0x0;
	//param[param_sz-1] = 0x0;
	char *t_cmd = malloc(cmd_sz+1);
	memcpy(t_cmd, cmd, cmd_sz);
	t_cmd[cmd_sz]=0;
	
	char *t_param = malloc(param_sz+1);
	memcpy(t_param, param, param_sz);
	t_param[param_sz]=0;
	snprintf(ret->buf,ret->sz-1,":%d:%s: %s", id, t_cmd, t_param);

	free(t_cmd);
	free(t_param);

	return ret;
}

mq_cmd* mq_cmd_creates(char *str, size_t sz, int id)
{
	mq_cmd *ret = NULL;

	//just naive belive everything is fine in string
	ret = malloc(sizeof(mq_cmd)); //can blow up
	ret->buf = malloc(sz);
	if (ret->buf == NULL)
	{
		PERM();
		free(ret);
		return NULL;
	}
	memset(ret->buf, 0, MQ_CMD_MSG_SIZE);
	ret->sz = sz;
	memcpy(ret->buf,str,sz);
	ret->id = id;

	return ret;
}

int mq_cmd_id(mq_cmd *cmd)
{
	int i,j,strt,sz;
	int id_parse;
	char buf[16]; //fix this
	/*:[ID]:*/

	if (cmd == NULL)
	{
		PERM();
		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);
			return id_parse;
		}	
		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
int mq_cmd_cmd(mq_cmd *cmd, char **buf, size_t *sz)
{
	int i,strt,cnt,l_c;

	if (cmd == NULL)
	{
		PERM();
		return -1;
	}
	if (buf == NULL)
	{
		PERM();
		return -1;
	}
	if (sz == NULL)
	{
		PERM();
		return -1;
	}

	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)
{
	int i,strt,l_c;

	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;

	//buffer allways ends with zero, no worries
	*sz = strlen(*param);

	return 0;
}

size_t mq_cmd_size(mq_cmd *cmd)
{
	if (cmd == NULL)
	{
		PERM();
		return -1;
	}
	return cmd->sz;
}

char *mq_cmd_buf(mq_cmd *cmd)
{
	if (cmd == NULL)
	{
		PERM();
		return NULL;
	}

	return cmd->buf;
}

void mq_cmd_free(mq_cmd *mq)
{
	if (mq != NULL)
	{
		free(mq->buf);
		mq->buf = NULL;
		free(mq);
		mq = NULL;
	}
}


int mq_cmd_o_cmp_param(mq_cmd *cmd, char *str)
{
	int str_sz; //get strsize from strlen()
	size_t buf_sz;
	char *buf = NULL;

	if (cmd == NULL)
	{
		return -1;
	}

	if (str == NULL)
	{
		return -1;
	}

	if (-1 == mq_cmd_param(cmd, &buf, &buf_sz))
	{
		return -1;
	}

	str_sz = strlen(str);
	if (strncmp(buf, str, str_sz) != 0)
	{
		return -1;
	}

	return 0;
}

int mq_cmd_o_cmp_cmd(  mq_cmd *cmd, char *str)
{
	int str_sz; //get strsize from strlen()
	size_t buf_sz;
	char *buf = NULL;

	if (cmd == NULL)
	{
		PERM();
		return -1;
	}

	if (str == NULL)
	{
		PERM();
		return -1;
	}

	if (-1 == mq_cmd_cmd(cmd, &buf, &buf_sz))
	{
		return -1;
	}

	str_sz = strlen(str);
	if (strncmp(buf, str, str_sz) != 0)
	{
		return -1;
	}

	return 0;
}

int mq_cmd_o_cmp_id(   mq_cmd *cmd, int id)
{
	int fret;
	if (cmd == NULL)
	{
		PERM();
		return -1;
	}

	if (id < 0)
	{
		PERM();
		return -1;
	}

	if ((fret = mq_cmd_id(cmd)) != id)
	{
		ENL();
		return -1;
	}

	return 0;
}