CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2005
    Location
    Ellesmera
    Posts
    427

    String Parsing in C

    It has been a while since I did some parsing in C, so I am trying to get it all back

    I have a specific string that I need to parse. Basically get the specific string of length M starting at position X.

    Here is a sample of data format:

    Code:
    1   TestTeamCisco96279               68:7f:74:56:64:ec   WPA2PSK/AES            68       11b/g/n NONE   In
    I have a structure to to store this data.

    Right now I have parse until WPAPSK/AES and I wanted to separate the string before and after "/" .

    So here is what I did.

    Code:
    int get_data(char *ssid, char *mac , char *auth, char * enc, const char *data)
    {
    char p[150];
    char tmp[30];
     
     memset(p, 0, strlen(data) );
     memcpy(p, data, strlen(data));
    
     // get data starting at pos =57 length = 23
     memmove(tmp, p+57, 23);
     
     int pos = strcspn(tmp , "/");
     
      memmove(enc, tmp+pos+1, strlen(tmp)-pos);
      memmove(auth, p+57, pos);
     
     printf("Auth  :: %s \n", auth);
     printf("Encryption :: %s \n", enc);
    }
    The output is these:

    Code:
    Auth :: WPA2PSK
    Encryption :: AES            WPA2PSK
    If I changed the lenght of Encryption to 10, then the data is corrrectly displayed.

    This is something wrong with how I manipulated the pointers?

    englighten me please
    *** Con Tu Adios, Te Llevas, Mi Corazon***

    Traveling Encoder...

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: String Parsing in C

    strcspn() works with null terminated strings.

    But, there is no guarantee your strings are null terminated.
    Code:
     memset(p, 0, strlen(data) );
    memcpy(p, data, strlen(data));
    Let's suppose data is a string containing A, B, C, \0.
    strlen() will return 3, not 4 because strlen does not take into account the last null character.
    So memset(p, 0, strlen(data) ); will only write three zeros.
    Then memcpy(p, data, strlen(data)); will only copy A, B, and C.
    You end up with a string which is not null terminated.
    A possible solution is simply:
    Code:
    strcpy(p, data);

  3. #3
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: String Parsing in C

    you can you GNU Regular expressions Library

    http://www.gnu.org/s/hello/manual/li...pressions.html

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: String Parsing in C

    I don't know why you're using memcpy or memmove here. You could iterate the string looking for the slashes and use strncpy to extract the portion you want, you could use strtok to find the slashes, or you could use strstr to find the slashes and strncpy to extract the data between them.

  5. #5
    Join Date
    May 2005
    Location
    Ellesmera
    Posts
    427

    Re: String Parsing in C

    @ olivthill2 , thanks for the tip. strcspn , gave me the correct position of the character. I was not able to include it in the post, but the temporary variable "tmp" was initialized using memset and its size is greater than the actual number of size I extracted from the original string, so I think "tmp" is null terminated. correct me if am wrong.

    @aamir121a , thanks for the suggestion. I was actually looking at it too. I am making this program on a busybox so there is very limited libraries and functions available. I am not sure if regular expression included in the platform. I will check.

    @GCDEF .. strtok works its wonders again. thanks.

    thank you all for the suggestions.
    *** Con Tu Adios, Te Llevas, Mi Corazon***

    Traveling Encoder...

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured