Changeset 72b6783e


Ignore:
Timestamp:
2008-12-24T09:00:42Z (15 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
489f996
Parents:
5469952
Message:

First version of new ini parser. Will just attempt to simplify code a bit.

Location:
lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lib/ini.c

    r5469952 r72b6783e  
    22  * BitlBee -- An IRC to other IM-networks gateway                     *
    33  *                                                                    *
    4   * Copyright 2002-2005 Wilmer van der Gaast and others                *
     4  * Copyright 2002-2008 Wilmer van der Gaast and others                *
    55  \********************************************************************/
    66
     
    2828ini_t *ini_open( char *file )
    2929{
    30         ini_t *ini = g_new0( ini_t, 1 );
     30        int fd;
     31        ini_t *ini = NULL;
     32        struct stat fi;
    3133       
    32         if( ( ini->fp = fopen( file, "r" ) ) == NULL )
     34        if( ( fd = open( file, O_RDONLY ) ) != -1 &&
     35            fstat( fd, &fi ) == 0 &&
     36            fi.st_size <= 16384 &&
     37            ( ini = g_malloc( sizeof( ini_t ) + fi.st_size + 1 ) ) &&
     38            read( fd, ini->file, fi.st_size ) == fi.st_size )
    3339        {
    34                 g_free( ini );
    35                 return( NULL );
     40                memset( ini, 0, sizeof( ini_t ) );
     41                ini->size = fi.st_size;
     42                ini->file[ini->size] = 0;
     43                ini->cur = ini->file;
     44                ini->c_section = "";
     45                return ini;
    3646        }
    3747       
    38         return( ini );
     48        g_free( ini );
     49        if( fd >= 0 )
     50                close( fd );
     51
     52        return NULL;
    3953}
    4054
    4155int ini_read( ini_t *file )
    4256{
    43         char key[MAX_STRING], s[MAX_STRING], *t;
    44         int i;
     57        char *s;
    4558       
    46         while( !feof( file->fp ) )
     59        while( file->cur && file->cur < file->file + file->size )
    4760        {
    48                 *s = 0;
    49                 fscanf( file->fp, "%127[^\n#]s", s );
    50                 fscanf( file->fp, "%*[^\n]s" );
    51                 fgetc( file->fp );              /* Skip newline         */
    52                 file->line ++;
    53                 if( strchr( s, '=' ) )
     61                char *e, *next;
     62               
     63                file->line++;
     64               
     65                /* Leading whitespace */
     66                while( *file->cur == ' ' || *file->cur == '\t' )
     67                        file->cur++;
     68
     69                /* Find the end of line */
     70                if( ( e = strchr( file->cur, '\n' ) ) != NULL )
    5471                {
    55                         sscanf( s, "%[^ =]s", key );
    56                         if( ( t = strchr( key, '.' ) ) )
     72                        next = e + 1;
     73                }
     74                else
     75                {
     76                        /* No more lines. */
     77                        e = file->cur + strlen( file->cur ) - 1;
     78                        next = NULL;
     79                }
     80               
     81                /* Comment? */
     82                if( ( s = strchr( file->cur, '#' ) ) != NULL )
     83                        e = s - 1;
     84               
     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 );
     91               
     92                if( *file->cur == '[' )
     93                {
     94                        file->cur++;
     95                        if( ( s = strchr( file->cur, ']' ) ) != NULL )
    5796                        {
    58                                 *t = 0;
    59                                 strcpy( file->section, key );
    60                                 t ++;
     97                                *s = 0;
     98                                file->c_section = file->cur;
     99                               
     100                                printf( "Section started: %s\n", file->c_section );
     101                        }
     102                }
     103                else if( ( s = strchr( file->cur, '=' ) ) != NULL )
     104                {
     105                        *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;
     115                       
     116                        if( ( s = strchr( file->key, '.' ) ) != NULL )
     117                        {
     118                                *s = 0;
     119                                file->section = file->key;
     120                                file->key = s + 1;
    61121                        }
    62122                        else
    63123                        {
    64                                 strcpy( file->section, file->c_section );
    65                                 t = key;
     124                                file->section = file->c_section;
    66125                        }
    67                         sscanf( t, "%s", file->key );
    68                         t = strchr( s, '=' ) + 1;
    69                         for( i = 0; t[i] == ' '; i ++ );
    70                         strcpy( file->value, &t[i] );
    71                         for( i = strlen( file->value ) - 1; file->value[i] == 32; i -- )
    72                                 file->value[i] = 0;
    73126                       
    74                         return( 1 );
     127                        file->cur = next;
     128                       
     129                        printf( "%s.%s = '%s'\n", file->section, file->key, file->value );
     130                       
     131                        return 1;
    75132                }
    76                 else if( ( t = strchr( s, '[' ) ) )
    77                 {
    78                         strcpy( file->c_section, t + 1 );
    79                         t = strchr( file->c_section, ']' );
    80                         *t = 0;
    81                 }
     133                /* else: noise, but let's just ignore it. */
     134
     135                file->cur = next;
    82136        }
    83         return( 0 );
     137       
     138        return 0;
    84139}
    85140
    86141void ini_close( ini_t *file )
    87142{
    88         fclose( file->fp );
    89143        g_free( file );
    90144}
  • lib/ini.h

    r5469952 r72b6783e  
    2929typedef struct
    3030{
    31         FILE *fp;
    3231        int line;
    33         char c_section[MAX_STRING];
    34         char section[MAX_STRING];
    35         char key[MAX_STRING];
    36         char value[MAX_STRING];
     32        char *c_section;
     33        char *section;
     34        char *key;
     35        char *value;
     36        int size;
     37        char *cur, *tok;
     38        char file[];
    3739} ini_t;
    3840
Note: See TracChangeset for help on using the changeset viewer.