diff options
author | Wilmer van der Gaast <wilmer@gaast.net> | 2008-12-25 11:05:11 +0000 |
---|---|---|
committer | Wilmer van der Gaast <wilmer@gaast.net> | 2008-12-25 11:05:11 +0000 |
commit | 489f996fe52c6969a9574d5ca562a4f5cb5c2585 (patch) | |
tree | 52356e203d9fa6c595e8b640b4b2cae11b52d09e | |
parent | 72b6783ebf2af43a384364e62eab71b1c5e6f9c1 (diff) |
Simplified ini parser code a bit more. Not using strtok() after all since
I can't find a guarantee that it's okay with me further mutilating the
strings. :-)
-rw-r--r-- | conf.c | 6 | ||||
-rw-r--r-- | lib/ini.c | 51 |
2 files changed, 27 insertions, 30 deletions
@@ -307,15 +307,15 @@ static int conf_loadini( conf_t *conf, char *file ) } else { - fprintf( stderr, "Error: Unknown setting `%s` in configuration file.\n", ini->key ); + fprintf( stderr, "Error: Unknown setting `%s` in configuration file (line %d).\n", ini->key, ini->line ); return 0; /* For now just ignore unknown keys... */ } } else if( g_strcasecmp( ini->section, "defaults" ) != 0 ) { - fprintf( stderr, "Error: Unknown section [%s] in configuration file. " - "BitlBee configuration must be put in a [settings] section!\n", ini->section ); + fprintf( stderr, "Error: Unknown section [%s] in configuration file (line %d). " + "BitlBee configuration must be put in a [settings] section!\n", ini->section, ini->line ); return 0; } } @@ -52,6 +52,23 @@ ini_t *ini_open( char *file ) return NULL; } +/* Strips leading and trailing whitespace and returns a pointer to the first + non-ws character of the given string. */ +static char *ini_strip_whitespace( char *in ) +{ + char *e; + + while( isspace( *in ) ) + in++; + + e = in + strlen( in ) - 1; + while( e > in && isspace( *e ) ) + e--; + e[1] = 0; + + return in; +} + int ini_read( ini_t *file ) { char *s; @@ -61,33 +78,25 @@ int ini_read( ini_t *file ) char *e, *next; file->line++; - - /* Leading whitespace */ - while( *file->cur == ' ' || *file->cur == '\t' ) - file->cur++; /* Find the end of line */ if( ( e = strchr( file->cur, '\n' ) ) != NULL ) { + *e = 0; next = e + 1; } else { /* No more lines. */ - e = file->cur + strlen( file->cur ) - 1; + e = file->cur + strlen( file->cur ); next = NULL; } /* Comment? */ if( ( s = strchr( file->cur, '#' ) ) != NULL ) - e = s - 1; - - /* And kill trailing whitespace. */ - while( isspace( *e ) && e > file->cur ) - e--; - e[1] = 0; + *s = 0; - printf( "Stripped line: '%s'\n", file->cur ); + file->cur = ini_strip_whitespace( file->cur ); if( *file->cur == '[' ) { @@ -96,22 +105,13 @@ int ini_read( ini_t *file ) { *s = 0; file->c_section = file->cur; - - printf( "Section started: %s\n", file->c_section ); } } else if( ( s = strchr( file->cur, '=' ) ) != NULL ) { *s = 0; - file->value = s + 1; - while( isspace( *file->value ) ) - file->value++; - - s--; - while( isspace( *s ) && s > file->cur ) - s--; - s[1] = 0; - file->key = file->cur; + file->key = ini_strip_whitespace( file->cur ); + file->value = ini_strip_whitespace( s + 1 ); if( ( s = strchr( file->key, '.' ) ) != NULL ) { @@ -125,12 +125,9 @@ int ini_read( ini_t *file ) } file->cur = next; - - printf( "%s.%s = '%s'\n", file->section, file->key, file->value ); - return 1; } - /* else: noise, but let's just ignore it. */ + /* else: noise/comment/etc, let's just ignore it. */ file->cur = next; } |