aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilmer van der Gaast <wilmer@gaast.net>2008-12-25 11:05:11 +0000
committerWilmer van der Gaast <wilmer@gaast.net>2008-12-25 11:05:11 +0000
commit489f996fe52c6969a9574d5ca562a4f5cb5c2585 (patch)
tree52356e203d9fa6c595e8b640b4b2cae11b52d09e
parent72b6783ebf2af43a384364e62eab71b1c5e6f9c1 (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.c6
-rw-r--r--lib/ini.c51
2 files changed, 27 insertions, 30 deletions
diff --git a/conf.c b/conf.c
index 8e6b6c69..873aa0e7 100644
--- a/conf.c
+++ b/conf.c
@@ -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;
}
}
diff --git a/lib/ini.c b/lib/ini.c
index b758ef81..d9900aeb 100644
--- a/lib/ini.c
+++ b/lib/ini.c
@@ -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;
}