In the last part we examined the way to extract out the Query String from the URL. However, as is known well, the query string in itself is useless. It is the keys and their values that matter! This post is just about that - how to extract the names of the keys and their values from the query string received on the URL (via GET request). We will not cover how to read the query string as we have done that already in the last program. Let us bring in a few new goodies into that program and introduce a way to extract and print the values in the URL. Have a look at the program here:
C is full of great functions. One of those is the strtok function. This function is designed to extract parts of string based on delimiters. If you have used PHP and are aware of the explode function, this one right here is a grand-daddy of that one. However it has got a few differences from explode function of PHP. strtok actually reads in the string, checks for the occurrence of the delimiting character(s) and return the part of string before the occurrence. The great thing is that it automatically replaces the delimiting character with a null character. So the value you get as a return is a valid string in C!
Now is the tricky part: if you want to continue reading that same string, pass NULL as the first argument (the string to be read) and it will go on reading the previous string itself! Yepp! It stores the string internally in its static variable! This is how we took out the program's name in the last example and this is the way we are going to extract the key-value pairs from the query string!
If you see the source code (of the current program), you will notice we have done that on line 44 (below the comment "Extract the program name" ; use the browser's find functionality to locate that!) and line 62. The main changes made to the program comparing the previous one begins with line number 62 (token = strtok (data,"&");)
What we are actually doing in there is to first extract the part of query string which contains the key-value pair then print the key-value pair as we received it, then extract the key and the value and print them and then repeat the process until we have printed them all! In case you are puzzled by how we are extracting the key name and the value, that is what we will talk about next.
The values were extracted using the sscanf function. This function is almost identical to scanf function.
|NOTE: You might have already used this to input multiple values on one line. If you have not used it, try this:
scanf("%d %d", &int_a, &int_b);
where both int_a and int_b are integers previously declared. Then try to input "12 20" at the prompt and see what you get in variables int_a and int_b respectively!
The only difference betweeb sscanf and scanf is: scanf reads from the keyboard input, sscanf reads from a string. The first parameter of sscanf takes in the string to read from. So we have supplied the token variable. Next, we know that the key name we received from the form and the value are separated by the '=' sign. What we need to do is to take out the part before the first '=' character and copy that to the 'key' and the rest of it to 'value'. That is exactly what this line does:
sscanf(token, "%[^=]=%65536s", key, value);
This might not be well known but the scanf (and thus sscanf) function's format specifier (the one you put in quotes) takes in a regular expression and parses that! What our regular expression does is to make sscanf expect thing in the order desired to extract the key and value. Here is the expression explained:
All well and good! Its done! Well, uhmm...we did not check the output. So let us see how it appears if we enter "Nicola" and "Tesla" on the form (you remember the last article right?). This is how it should look according to the new program (and yes, we changed the name of the file):