Update README for archival
[reddit.git] / scripts / traffic / utils.c
blob0469b0e570effbe72c00ccd2a7d8b53b0a8d7914
1 #include "utils.h"
3 #include <stdio.h>
4 #include <string.h>
5 #include <assert.h>
7 void split_fields(char *line, ...)
9 va_list args;
11 va_start(args, line);
12 char* c = line;
13 for (char** field = va_arg(args, char**); field != NULL;
14 field = va_arg(args, char**)) {
15 *field = c;
17 for (; *c != '\t' && *c != '\n'; c++) {
18 assert(*c != '\0');
21 *c = '\0';
22 c += 1;
24 va_end(args);
27 int url_decode(char* buffer)
29 char *c = buffer, *o = buffer;
30 int size = 0;
32 for (;*c != '\0';) {
33 if (*c == '%') {
34 int count = sscanf(c + 1, "%2hhx", o);
35 if (count != 1) {
36 size = -1;
37 break;
40 c += 3;
41 } else {
42 *o = *c;
43 c += 1;
46 o += 1;
47 size += 1;
50 if (size > 0)
51 *o = '\0';
53 return size;
56 int parse_query_param(char** query, char** key, char** value)
58 if (*query == NULL) {
59 return -1;
62 *key = *query;
63 *value = NULL;
65 bool string_ended = false;
66 int value_length = -1;
67 char *c;
69 for(c = *query; *c != '&'; c++) {
70 if (*c == '\0') {
71 string_ended = true;
72 break;
73 } else if (*value == NULL && *c == '=') {
74 *c = '\0';
75 *value = c + 1;
78 if (*value != NULL) {
79 value_length += 1;
83 *c = '\0';
85 if (!string_ended) {
86 *query = c + 1;
87 } else {
88 *query = NULL;
91 return value_length;