Have this String (str):
ip=127.0.0.1&server=localhost&uri=/&vers=0.56&total_processed=21&total_blocked=20&config=learning&cscore0=$XSS&score0=16&zone0=ARGS&id0=1302&var_name0=a&zone1=ARGS&id1=1303&var_name1=a
Want to produce JSON out of it in pure C, like key:value ie ip: 127.0.0.1 etc
Tried this code:
struct key_value
{
char key[128];
char value[128];
};
int number_of_keys = 50;
struct key_value *kv = malloc(sizeof(struct key_value) * number_of_keys);
if (kv == NULL) {
perror("Malloc");
exit(EXIT_FAILURE);
}
char delim[] = "&";
char *ptr = strtok((char *)((ngx_str_t *)ostr->elts)[0].data, delim);
int j=0;
while(ptr != NULL)
{
sscanf(ptr,"%[^=]=%[^\n]",kv[j].key,kv[j].value);
ptr = strtok(NULL, delim);
j=j+1;
}
ngx_log_error(NGX_LOG_ERR, r->connection->log,
0, "{ %s:%s }", kv[0].key, kv[0].value);
But forgot how can I make a function to access/format it with unknown elements count??? Above I added only 2 %s ... but it can be more
Any improvements on speed, reliability for the code above?
Thanks,