How to parse QUERY_STRING?

This is a discussion on How to parse QUERY_STRING? within the C Programming forums, part of the General Programming Boards category; Hello! :-) How to parse query string like this: user=john&office=35&location=france QUERY_STRING can contain not more than three pairs user=john office=35 ...

  1. #1
    Unregistered
    Guest

    Question How to parse QUERY_STRING?

    Hello! :-)

    How to parse query string like this:
    user=john&office=35&location=france

    QUERY_STRING can contain not more than three pairs
    user=john
    office=35
    location=france

    some importances can be not, like only
    location=france
    or only
    user=john&office=35
    ...

    order can be violated:
    office=35&location=france&user=john

    Sorry for stupid question but my head cannot to do it! :-)

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Think about a map... keys correspond to values.

    Read up to the equal sign. The string you read (minus the =) is your key. Now read to the ?, that value you read (minus the ?) is the value.

    Assign the value you read to the correct variable based on the key, and it's done. Just repeat the process while there is more stuff to parse.

    You have to maintain a map of key value pairs. I could implement it fiarly easy in C++ with <map>.. but I don't think there is any similair such tool with standard C.
    Last edited by SilentStrike; 03-24-2002 at 12:19 AM.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    But what if

    char *text = "office=35&user=john&location=france",

    Then your method fails. You have to map the key to the value to this... because the order in which they occur is meaningless.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Unregistered
    Guest
    try this, i work well for me.

    #include <stdio.h>
    #include <stdlib.h>

    // Helper macro to convert two-character hex strings to character value
    #define ToHex(Y) (Y>='0'&&Y<='9'?Y-'0':Y-'A'+10)

    char InputData[4096];

    void getAllParams() {
    // Determing if it is a POST or GET method
    if( getenv( "REQUEST_METHOD" ) == 0 ) {
    printf("No REQUEST_METHOD, must be running in DOS mode");
    return;
    } else if (strcmp( getenv("REQUEST_METHOD"), "POST") == 0) {
    // If POST
    char *endptr; // quite useless, but required
    char *len1 = getenv("CONTENT_LENGTH");
    int contentlength = strtol(len1, &endptr, 10);
    fread(InputData , contentlength, 1, stdin);
    } else {
    // If GET
    strcpy(InputData, getenv("QUERY_STRING"));
    }
    }

    void getParam(const char *Name, char *Value) {
    char *pos1 = strstr(InputData, Name);

    if (pos1) {
    pos1 += strlen(Name);

    if (*pos1 == '=') { // Make sure there is an '=' where we expect it
    pos1++;

    while (*pos1 && *pos1 != '&') {
    if (*pos1 == '%') { // Convert it to a single ASCII character and store at our Valueination
    *Value++ = (char)ToHex(pos1[1]) * 16 + ToHex(pos1[2]);
    pos1 += 3;
    } else if( *pos1=='+' ) { // If it's a '+', store a space at our Valueination
    *Value++ = ' ';
    pos1++;
    } else {
    *Value++ = *pos1++; // Otherwise, just store the character at our Valueination
    }
    }

    *Value++ = '\0';
    return;
    }

    }

    strcpy(Value, "undefine"); // If param not found, then use default parameter
    return;
    }


    int main() {

    char myName[100] = "";
    char myAddress[100] = "";

    printf("Content-Type:text/html \n\n");
    getAllParams();
    getParam("Name", myName);
    getParam("Address", myAddress);

    printf("QueryString: %s", InputData);
    printf("<br>");
    printf("Name: %s", myName);
    printf("<br>");
    printf("Address: %s", myAddress);


    return 0;
    }

  5. #5
    Unregistered
    Guest

    Smile

    Thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21