forked from Yurik72/ESPHap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_params.c
64 lines (50 loc) · 1.33 KB
/
query_params.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
#include <stdlib.h>
#include <string.h>
#include "query_params.h"
query_param_t *query_params_parse(const char *s) {
query_param_t *params = NULL;
int i = 0;
while (1) {
int pos = i;
while (s[i] && s[i] != '=' && s[i] != '&' && s[i] != '#') i++;
if (i == pos) {
i++;
continue;
}
query_param_t *param = malloc(sizeof(query_param_t));
param->name = strndup(s+pos, i-pos);
param->value = NULL;
param->next = params;
params = param;
if (s[i] == '=') {
i++;
pos = i;
while (s[i] && s[i] != '&' && s[i] != '#') i++;
if (i != pos) {
param->value = strndup(s+pos, i-pos);
}
}
if (!s[i] || s[i] == '#')
break;
}
return params;
}
query_param_t *query_params_find(query_param_t *params, const char *name) {
while (params) {
if (!strcmp(params->name, name))
return params;
params = params->next;
}
return NULL;
}
void query_params_free(query_param_t *params) {
while (params) {
query_param_t *next = params->next;
if (params->name)
free(params->name);
if (params->value)
free(params->value);
free(params);
params = next;
}
}