Changeset 489f996


Ignore:
Timestamp:
2008-12-25T11:05:11Z (15 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
9e768da
Parents:
72b6783e
Message:

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. :-)

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • conf.c

    r72b6783e r489f996  
    308308                        else
    309309                        {
    310                                 fprintf( stderr, "Error: Unknown setting `%s` in configuration file.\n", ini->key );
     310                                fprintf( stderr, "Error: Unknown setting `%s` in configuration file (line %d).\n", ini->key, ini->line );
    311311                                return 0;
    312312                                /* For now just ignore unknown keys... */
     
    315315                else if( g_strcasecmp( ini->section, "defaults" ) != 0 )
    316316                {
    317                         fprintf( stderr, "Error: Unknown section [%s] in configuration file. "
    318                                          "BitlBee configuration must be put in a [settings] section!\n", ini->section );
     317                        fprintf( stderr, "Error: Unknown section [%s] in configuration file (line %d). "
     318                                         "BitlBee configuration must be put in a [settings] section!\n", ini->section, ini->line );
    319319                        return 0;
    320320                }
  • lib/ini.c

    r72b6783e r489f996  
    5353}
    5454
     55/* Strips leading and trailing whitespace and returns a pointer to the first
     56   non-ws character of the given string. */
     57static char *ini_strip_whitespace( char *in )
     58{
     59        char *e;
     60
     61        while( isspace( *in ) )
     62                in++;
     63
     64        e = in + strlen( in ) - 1;
     65        while( e > in && isspace( *e ) )
     66                e--;
     67        e[1] = 0;
     68       
     69        return in;
     70}
     71
    5572int ini_read( ini_t *file )
    5673{
     
    6279               
    6380                file->line++;
    64                
    65                 /* Leading whitespace */
    66                 while( *file->cur == ' ' || *file->cur == '\t' )
    67                         file->cur++;
    6881
    6982                /* Find the end of line */
    7083                if( ( e = strchr( file->cur, '\n' ) ) != NULL )
    7184                {
     85                        *e = 0;
    7286                        next = e + 1;
    7387                }
     
    7589                {
    7690                        /* No more lines. */
    77                         e = file->cur + strlen( file->cur ) - 1;
     91                        e = file->cur + strlen( file->cur );
    7892                        next = NULL;
    7993                }
     
    8195                /* Comment? */
    8296                if( ( s = strchr( file->cur, '#' ) ) != NULL )
    83                         e = s - 1;
     97                        *s = 0;
    8498               
    85                 /* And kill trailing whitespace. */
    86                 while( isspace( *e ) && e > file->cur )
    87                         e--;
    88                 e[1] = 0;
    89                
    90                 printf( "Stripped line: '%s'\n", file->cur );
     99                file->cur = ini_strip_whitespace( file->cur );
    91100               
    92101                if( *file->cur == '[' )
     
    97106                                *s = 0;
    98107                                file->c_section = file->cur;
    99                                
    100                                 printf( "Section started: %s\n", file->c_section );
    101108                        }
    102109                }
     
    104111                {
    105112                        *s = 0;
    106                         file->value = s + 1;
    107                         while( isspace( *file->value ) )
    108                                 file->value++;
    109                        
    110                         s--;
    111                         while( isspace( *s ) && s > file->cur )
    112                                 s--;
    113                         s[1] = 0;
    114                         file->key = file->cur;
     113                        file->key = ini_strip_whitespace( file->cur );
     114                        file->value = ini_strip_whitespace( s + 1 );
    115115                       
    116116                        if( ( s = strchr( file->key, '.' ) ) != NULL )
     
    126126                       
    127127                        file->cur = next;
    128                        
    129                         printf( "%s.%s = '%s'\n", file->section, file->key, file->value );
    130                        
    131128                        return 1;
    132129                }
    133                 /* else: noise, but let's just ignore it. */
     130                /* else: noise/comment/etc, let's just ignore it. */
    134131
    135132                file->cur = next;
Note: See TracChangeset for help on using the changeset viewer.