parsing strings in c: what is the right way?

Discussion in 'Mac Programming' started by wala, Dec 26, 2006.

  1. macrumors member

    Joined:
    Jun 3, 2005
    #1
    What would be a normal way to, say, parse a string from a configuration file for a program written in C? Would it simply be a series of while loops, eg, in the case of getting past whitespace, something like:
    PHP:
    while(*string && isblank(*string))
        
    string++;
    Or are there standard libraries and tools which should be used to parse strings besides functions like strtok()?
     
  2. macrumors regular

    Joined:
    Nov 23, 2006
    #2
    I would say these would provide the combination of functions (and good memory management) to do what you want (and more)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <malloc.h>
     
  3. thread starter macrumors member

    Joined:
    Jun 3, 2005
    #3
    What is the current official recommendation regarding malloc.h? I haven't seen much code which uses that #include....
     
  4. macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #4
    It almost seems as though there as many "right" ways to do this as there are programs, everyone feels the need to reinvent this stuff. Once upon a time this kind of made sense, but there are good standardised alternatives today.

    On the Mac, you can use property lists, all the hairy code is already written. On Windows, use the registry. On "plain" Unixy systems, there are lots of nice libraries around that will handle XML, LanMan-style .ini files, and many more (and those would be fine with the Mac and Windows too, if you want a single implementation).
     
  5. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #5
    That's not surprising, because malloc, calloc, realloc and free are included in stdlib.h.
     
  6. edm
    macrumors newbie

    Joined:
    Dec 27, 2006
    Location:
    Austin, TX
    #6
    Regex

    Seems like there would be a Regular Expressions library available in C that would handle all all your parsing needs.
     
  7. macrumors 65816

    Joined:
    Jan 6, 2004
    #7
    I agree. Use regex!

    On OSX, just type "man regex" at a terminal prompt
     
  8. thread starter macrumors member

    Joined:
    Jun 3, 2005
    #8
    How would I use regexec's substring matching feature? (I cannot seem to find understandable documentation for it)
    Examples would be nice....
     

Share This Page