Elegant query string parsing in C

I am trying to parse a URL query string in C and I do not see how to do this elegantly. Any hints or suggestions would be greatly appreciated:

static void readParams(char * string, char * param, char * value) { char arg[100] = {0}; // Not elegant, brittle char value2[1024] = {0}; sscanf(string, "%[^=]=%s", arg, value2); strcpy(param, arg); strcpy(value, value2); } char * contents = "username=ted&age=25"; char * splitted = strtok (contents,"&"); char * username; char * age; while (splitted != NULL) { char param[100]; // Not elegant, brittle char value[100]; char * t_str = strdup(splitted); readParams(t_str, param, value); if (strcmp(param, "username") == 0) { username = strdup(value); } if (strcmp(param, "age") == 0) { age = strdup(value); // This is a string, can do atoi } splitted = strtok (NULL, "&"); } 

The problem I ran into is that because of the strtok everything that seemed more reasonable until the last strtok seemed to break the while loop.

+6
source share
3 answers

In general, strtok splits the original string for use by some other functions. Below is an example using strtok to tokenize a string

  #include <stdlib.h> #include <string.h> #include <stdio.h> #define MX_SPLIT 128 char **split( char **result, char *working, const char *src, const char *delim) { int i; strcpy(working, src); // working will get chppped up instead of src char *p=strtok(working, delim); for(i=0; p!=NULL && i < (MX_SPLIT -1); i++, p=strtok(NULL, delim) ) { result[i]=p; result[i+1]=NULL; // mark the end of result array } return result; } void foo(const char *somestring) { int i=0; char *result[MX_SPLIT]={NULL}; char working[256]={0x0}; // assume somestring is never bigger than 256 - a weak assumption char mydelim[]=" !@ #$%^&*()_-"; split(result, working, somestring, mydelim); while(result[i]!=NULL) printf("token # %d=%s\n", i, result[i]); } 
+1
source

You need to either adapt a comprehensive and efficient parser, or coordinate with the libraries that will do this for you.

uriparser should provide everything you need (plus it supports unicode).

+1
source

Assumptions are not so bad in general, and especially in fast, reliable, and secure code (note, for example, that your input line is in the wrong format).

In order to achieve the most flexible code, you need to manually allocate (and free after use!) Memory for strings that should be up to the full length of the input string (again, when a reasonable limit is required), since it is unknown (in general), how long are pairs of parameters and values.

0
source

Source: https://habr.com/ru/post/910468/


All Articles