Parse a null delimited list of strings
Results 1 to 4 of 4

Thread: Parse a null delimited list of strings

  1. #1
    Senior Member
    Join Date
    Feb 2003
    Posts
    282

    Parse a null delimited list of strings

    I have a buffer formated as a list of strings delimited by null character. I am trying to use strtok to tokenize or parse this buffer into seperate strings.

    the formate of the buffer is as folows

    a:\<null>b:\<null>c:\<null><null>

    My Idea was to use strtok to tokenize it, since I have had luck tokenizeing other delimited lists.

    Problem is my program crashes at address 78025ff6 inside MSVCRT.DLL

    Turns out this is the Microsoft C library, And address 78025ff6 is just below following strtok. I disasembled it.

    So it apears strtok is my problem:

    GetLogicalDriveStrings(256, buffer);
    char *drive, status[50];
    drive = strtok(buffer, '\0');
    strcpy(status, drive);
    while(drive != NULL){
    drive = strtok(NULL, '\0');
    strcat(status, drive);
    }

    GetLogicalDriveStrings fills the buffer with the null delimited list format described above.

    Can someone help me, I tryed strtok with a space delimited list and no problem, so the problem seems to be the null character is crashing strtok.

  2. #2
    Junior Member
    Join Date
    Aug 2003
    Posts
    1
    Hi,

    Since C strings are \0 terminated, without a method of passing the length, how is strtok supposed to know about your overloaded meaning whereby 2 \0s is the end of the string? You can try something simple like:

    void parseStrings(char *abc)
    {
    int sidx=0;
    char *sList[128];
    char *lookback=abc;

    while(sidx < 128)
    {
    if(*abc == '\0')
    {
    if(abc > lookback)
    {
    sList[sidx] = lookback;
    sidx++;
    lookback = abc+1;
    }
    else
    {
    int i=0;
    while(i < sidx)
    printf("%s\n", sList[i++]);
    exit(0);
    }
    }
    abc++;
    }
    }

    main()
    {
    parseStrings("abc\000123\000xxx\000");
    }

  3. #3
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    Just out of curiosity, since you have a buffer, which you are storing as a *char and since those strings are null terminated...wouldn't that just be the same as an two dimensional array or **char? In otherwords, an array of strings?

    I'm hoping this jogs something loose in your memory about how to handle that, but one of your str functions should cover it...

    If it doesn't jar anything lose, let me know, I will pull out the books and try to refresh my memory.

    /nebulus
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  4. #4
    Senior Member
    Join Date
    Feb 2003
    Posts
    282
    tiocsti's works, I managed to use your code sample and index method to come up with a function, this way I learn, Instead of just copying your code and useing It I copyed it, tested, and then used it as a guide to create my own.


    nebulus, I was thinking the same thing. That an array of strings, would in memory be aranged as such, strings seperated with null. As you say. Then I was thinking, that I declared buffer as a pointer to a char array, and that GetLogicalDriveStrings filled the buffer as so. Then I thought about addressing the strings as such

    buffer[0], buffer[1]

    At first i had problems with this addressing method, but now ive also come acros an example loop that parses the buffer in this way, by addressing buffer by index.

    I think what triped me is the way buffer was declared, as GetLogicalDriveStrings wanted a pointer to an array of chars would trip if i passed a two dimensional array.

    And I think this is how it ended up working afterall.

    Thank you both for your help, it was greatly apreciated

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •