-
Notifications
You must be signed in to change notification settings - Fork 5
/
qs_parse.c
272 lines (232 loc) · 7.18 KB
/
qs_parse.c
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
#include "qs_parse.h"
#include <stdio.h>
// TODO: implement sorting of the qs_kv array; for now ensure it's not compiled
#undef _qsSORTING
// isxdigit _is_ available in <ctype.h>, but let's avoid another header instead
#define ISHEX(x) ((((x)>='0'&&(x)<='9') || ((x)>='A'&&(x)<='F') || ((x)>='a'&&(x)<='f')) ? 1 : 0)
#define HEX2DEC(x) (((x)>='0'&&(x)<='9') ? (x)-48 : ((x)>='A'&&(x)<='F') ? (x)-55 : ((x)>='a'&&(x)<='f') ? (x)-87 : 0)
#define ISQSCHR(x) ((((x)=='=')||((x)=='#')||((x)=='&')||((x)=='\0')) ? 0 : 1)
int qs_strncmp(const char * s, const char * qs, register size_t n)
{
int i=0;
register unsigned char u1, u2, unyb, lnyb;
while(n-- > 0)
{
u1 = (unsigned char) *s++;
u2 = (unsigned char) *qs++;
if ( ! ISQSCHR(u1) ) { u1 = '\0'; }
if ( ! ISQSCHR(u2) ) { u2 = '\0'; }
if ( u1 == '+' ) { u1 = ' '; }
if ( u1 == '%' ) // easier/safer than scanf
{
unyb = (unsigned char) *s++;
lnyb = (unsigned char) *s++;
if ( ISHEX(unyb) && ISHEX(lnyb) )
u1 = (HEX2DEC(unyb) * 16) + HEX2DEC(lnyb);
else
u1 = '\0';
}
if ( u2 == '+' ) { u2 = ' '; }
if ( u2 == '%' ) // easier/safer than scanf
{
unyb = (unsigned char) *qs++;
lnyb = (unsigned char) *qs++;
if ( ISHEX(unyb) && ISHEX(lnyb) )
u2 = (HEX2DEC(unyb) * 16) + HEX2DEC(lnyb);
else
u2 = '\0';
}
if ( u1 != u2 )
return u1 - u2;
if ( u1 == '\0' )
return 0;
i++;
}
if ( ISQSCHR(*qs) )
return -1;
else
return 0;
}
int qs_parse(char * qs, char * qs_kv[], int qs_kv_size)
{
int i, j;
char * substr_ptr;
for(i=0; i<qs_kv_size; i++) qs_kv[i] = NULL;
// find the beginning of the k/v substrings
if ( (substr_ptr = strchr(qs, '?')) != NULL )
substr_ptr++;
else
substr_ptr = qs;
i=0;
while(i<qs_kv_size)
{
qs_kv[i] = substr_ptr;
j = strcspn(substr_ptr, "&");
if ( substr_ptr[j] == '\0' ) { break; }
substr_ptr += j + 1;
i++;
}
i++; // x &'s -> means x iterations of this loop -> means *x+1* k/v pairs
// we only decode the values in place, the keys could have '='s in them
// which will hose our ability to distinguish keys from values later
for(j=0; j<i; j++)
{
substr_ptr = qs_kv[j] + strcspn(qs_kv[j], "=&#");
if ( substr_ptr[0] == '&' ) // blank value: skip decoding
substr_ptr[0] = '\0';
else
qs_decode(++substr_ptr);
}
#ifdef _qsSORTING
// TODO: qsort qs_kv, using qs_strncmp() for the comparison
#endif
return i;
}
int qs_decode(char * qs)
{
int i=0, j=0;
while( ISQSCHR(qs[j]) )
{
if ( qs[j] == '+' ) { qs[i] = ' '; }
else if ( qs[j] == '%' ) // easier/safer than scanf
{
if ( ! ISHEX(qs[j+1]) || ! ISHEX(qs[j+2]) )
{
qs[i] = '\0';
return i;
}
qs[i] = (HEX2DEC(qs[j+1]) * 16) + HEX2DEC(qs[j+2]);
j+=2;
}
else
{
qs[i] = qs[j];
}
i++; j++;
}
qs[i] = '\0';
return i;
}
char * qs_k2v(const char * key, char * qs_kv[], int qs_kv_size)
{
int i;
size_t key_len, skip;
key_len = strlen(key);
#ifdef _qsSORTING
// TODO: binary search for key in the sorted qs_kv
#else // _qsSORTING
for(i=0; i<qs_kv_size; i++)
{
// we rely on the unambiguous '=' to find the value in our k/v pair
if ( qs_strncmp(key, qs_kv[i], key_len) == 0 )
{
skip = strcspn(qs_kv[i], "=");
if ( qs_kv[i][skip] == '=' )
skip++;
// return (zero-char value) ? ptr to trailing '\0' : ptr to value
return qs_kv[i] + skip;
}
}
#endif // _qsSORTING
return NULL;
}
char * qs_scanvalue(const char * key, const char * qs, char * val, size_t val_len)
{
int i, key_len;
char * tmp;
// find the beginning of the k/v substrings
if ( (tmp = strchr(qs, '?')) != NULL )
qs = tmp + 1;
key_len = strlen(key);
while(qs[0] != '#' && qs[0] != '\0')
{
if ( qs_strncmp(key, qs, key_len) == 0 )
break;
qs += strcspn(qs, "&") + 1;
}
if ( qs[0] == '\0' ) return NULL;
qs += strcspn(qs, "=&#");
if ( qs[0] == '=' )
{
qs++;
i = strcspn(qs, "&=#");
strncpy(val, qs, (val_len-1)<(i+1) ? (val_len-1) : (i+1));
qs_decode(val);
}
else
{
if ( val_len > 0 )
val[0] = '\0';
}
return val;
}
int hex2dcolor(char * color, double * r, double * g, double * b, double * a)
{
int i, j;
if ( color == NULL ) return 0;
i = strlen(color);
if ( i != 8 && i != 6 && i != 4 && i != 3 ) return 0;
for(j=0; j<i; j++) if ( ! ISHEX(color[j]) ) return 0;
switch(i)
{
// (H*16+H)/255 == H*17/255 == H/15
case 3:
*r = HEX2DEC(color[0]) / 15.0;
*g = HEX2DEC(color[1]) / 15.0;
*b = HEX2DEC(color[2]) / 15.0;
break;
case 4:
*r = HEX2DEC(color[0]) / 15.0;
*g = HEX2DEC(color[1]) / 15.0;
*b = HEX2DEC(color[2]) / 15.0;
*a = HEX2DEC(color[3]) / 15.0;
break;
case 6:
*r = ((HEX2DEC(color[0]) * 16) + HEX2DEC(color[1])) / 255.0;
*g = ((HEX2DEC(color[2]) * 16) + HEX2DEC(color[3])) / 255.0;
*b = ((HEX2DEC(color[4]) * 16) + HEX2DEC(color[5])) / 255.0;
break;
case 8:
*r = ((HEX2DEC(color[0]) * 16) + HEX2DEC(color[1])) / 255.0;
*g = ((HEX2DEC(color[2]) * 16) + HEX2DEC(color[3])) / 255.0;
*b = ((HEX2DEC(color[4]) * 16) + HEX2DEC(color[5])) / 255.0;
*a = ((HEX2DEC(color[6]) * 16) + HEX2DEC(color[7])) / 255.0;
break;
}
return i;
}
int hex2ccolor(char * color, unsigned char * r, unsigned char * g, unsigned char * b, unsigned char * a)
{
int i, j;
if ( color == NULL ) return 0;
i = strlen(color);
if ( i != 8 && i != 6 && i != 4 && i != 3 ) return 0;
for(j=0; j<i; j++) if ( ! ISHEX(color[j]) ) return 0;
switch(i)
{
// (H*16+H) == H*17
case 3:
*r = HEX2DEC(color[0]) * 17;
*g = HEX2DEC(color[1]) * 17;
*b = HEX2DEC(color[2]) * 17;
break;
case 4:
*r = HEX2DEC(color[0]) * 17;
*g = HEX2DEC(color[1]) * 17;
*b = HEX2DEC(color[2]) * 17;
*a = HEX2DEC(color[3]) * 17;
break;
case 6:
*r = (HEX2DEC(color[0]) * 16) + HEX2DEC(color[1]);
*g = (HEX2DEC(color[2]) * 16) + HEX2DEC(color[3]);
*b = (HEX2DEC(color[4]) * 16) + HEX2DEC(color[5]);
break;
case 8:
*r = (HEX2DEC(color[0]) * 16) + HEX2DEC(color[1]);
*g = (HEX2DEC(color[2]) * 16) + HEX2DEC(color[3]);
*b = (HEX2DEC(color[4]) * 16) + HEX2DEC(color[5]);
*a = (HEX2DEC(color[6]) * 16) + HEX2DEC(color[7]);
break;
}
return i;
}