#1
  1. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13

    returning specific part of string


    I'm making a config file, for a command line app.
    I'm trying to make it like the usual config files where the lines that start with a # are ignored and the other lines are the settings to the program. I'm having a little trouble getting getting the values of the directives. The config file looks something like this

    # pounds(#) start comments
    # format:
    #directive directive_value(s)

    # example 1) where the program will start looking from
    rootPath "c:\foo"

    # example 2) output verbosity level
    verbosity 3

    # example 3) files to be parsed
    fileTypes htm html php


    my question is, after I find the line that has a directive and identfy which directive it is how do I extract the values that come next. There are three types of directives that I must extract: 1) a string such as "c:\foo"(don't need quotes) 2) an integer 3) a variable number of strings -- look above for the examples.

    this is how I'm going about finding the keywords(directives). it works, I guess. I just don't know how to extract the values that follow. Also, if someone knows of a better way to do this, let me know.


    Code:
    while(fgets(line, MAX_PATH, fp)){
    
    // set the comment var the first char of line
    comment = line[0];
    		
    // if comment var is #  or an empty line skip
    if(comment == '#' || comment =='\n')
    continue;
    	
    // extract the command of the line found. command is the first word of the file.
    tok = strtok(line, " ");
    
    
    
     // ### from this point on we're checking for keywords. 
    	
    //root path
    if (strcmp(tok, "rootPath") == 0){
    printf("## rootPath keyword found \n");
    
    // code to extract the variable number string that comes after rootPath
    			
    continue;
    } //end  root path
    
    // verbosity level
    else if (strcmp(tok, "verbosity") == 0){
    printf("## verbosity keyword found \n");
    
    // code to extract the int that comes after verbosity directive
    
    continue;
    } //end verbosity
    
    
    // file types
    else if (strcmp(tok, "fileTypes") == 0){
    printf("## fileTypes keyword found \n");
    
    // code to extract the strings that comes after fileTypes directive
    
    continue;
    } //end verbosity
    
    
    }// end while
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    All you have to do is call strtok(NULL, " ") to get the next segment of the line and then assign it to the variable that you require.
  4. #3
  5. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13
    Ok. strtok returns a char *. How can I convert the char it returns to an int. For example, if i return the 3 in something like this
    verbosity 3
    the three will be treated as a char. If I use the atoi function I need a char *. What's a good way of converting that 3 to an int?
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    USA
    Posts
    286
    Rep Power
    13
    atoi()
    Jon Sagara

    "Me fail English? That's unpossible!"
  8. #5
  9. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    If it is a single character then just pass it to an int variable. If it is a string (char *) then use atoi/atol/atof
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Originally posted by Onslaught
    If it is a single character then just pass it to an int variable.
    Bad idea to do this.
    Code:
    char bar = '3';
    int foo = bar;
    This won't work because ASCII code for '3' is not equal to 3. If you try assigning directly, the value of foo will be 51 (ASCII code for '3'). If you want to convert a single char to an int var, you should do it like this:
    Code:
    char bar = '3';
    int foo = bar - '0';
    Of course, the code assumes that the ASCII (or EBCDIC or Unicode) values of '0' .. '9' are in sequence. This is a fairly safe assumption to make, because a lot of atoi() implementations depend on this to be true.
  12. #7
  13. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13
    I was encountering the problem that scorpion pointed out. I'm gonna give scropion's way a try. I'm sure it will work. I don't understand how that works, though.
  14. #8
  15. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    >> I don't understand how that works, though.
    It's pretty simple. First a few basic facts to note (The facts follow 0-based numbering, because this is a C forum ;)):
    0. Every character has an ASCII code associated with it (See http://www.asciitable.com/ for a list of characters and their ASCII code values)
    1. The ASCII code values for the characters '0', '1', '2' ... '9' are in sequence, with no other characters in between.
    2. In C, you can perform arithmetic operations on chars.

    Given these three basic facts, let's say the ASCII code for '0' is some number, x. From fact 1, it follows that the ASCII code for '1' is x + 1, ASCII code for '2' is x + 2, ASCII code for '3' is x + 3 .... ASCII code for '9' is x + 9.

    So now, if you want to convert a char to an int, all you have to do is take its ASCII code value and subtract the ASCII code value for '0' from it. This is possible in C because you can do arithmetic with the char type directly. E.g.
    ASCII code Value of '3' - ASCII code Value of '0' is (x+3) - x = 3 (which is what you're looking for).

    Instead of working things out in terms of 'x', we can play with real values now. From the link specified in fact 0, we see that the ASCII code value for '0' is 48 and ASCII code value for '3' is 51.
    So '3' - '0' translates to 51 - 48 = 3

    Hope this helps :)
    Last edited by Scorpions4ever; July 9th, 2003 at 12:01 AM.
  16. #9
  17. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13
    Scorpion, I bow down to you.
  18. #10
  19. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    Your right Scorpion, I had forgotten about that fact (been a while since I had tried such as that). :o

    Thanks for the correct. ;)
    Last edited by Onslaught; July 9th, 2003 at 07:51 AM.
  20. #11
  21. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13
    hey guys, you see the way that I'm checking for directives... is there a better way or is that one good enough?
  22. #12
  23. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Originally posted by Scorpions4ever
    Of course, the code assumes that the ASCII (or EBCDIC or Unicode) values of '0' .. '9' are in sequence. This is a fairly safe assumption to make, because a lot of atoi() implementations depend on this to be true.
    I'm pretty sure it's guaranteed by the standard, in both C and C++. No matter whether you're using ASCII or something else. The numbers 0 .. 9 must be in sequence.

    The same is not true for letters a .. z, though, and although they're in sequence in ASCII they're not in, e.g. EBCDIC.

IMN logo majestic logo threadwatch logo seochat tools logo