source: lib/ini.c @ 2ea8736

Last change on this file since 2ea8736 was 489f996, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-12-25T11:05:11Z

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

  • Property mode set to 100644
File size: 3.1 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2008 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* INI file reading code                                                */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23  Suite 330, Boston, MA  02111-1307  USA
24*/
25#define BITLBEE_CORE
26#include "bitlbee.h"
27
28ini_t *ini_open( char *file )
29{
30        int fd;
31        ini_t *ini = NULL;
32        struct stat fi;
33       
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 )
39        {
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;
46        }
47       
48        g_free( ini );
49        if( fd >= 0 )
50                close( fd );
51
52        return NULL;
53}
54
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
72int ini_read( ini_t *file )
73{
74        char *s;
75       
76        while( file->cur && file->cur < file->file + file->size )
77        {
78                char *e, *next;
79               
80                file->line++;
81
82                /* Find the end of line */
83                if( ( e = strchr( file->cur, '\n' ) ) != NULL )
84                {
85                        *e = 0;
86                        next = e + 1;
87                }
88                else
89                {
90                        /* No more lines. */
91                        e = file->cur + strlen( file->cur );
92                        next = NULL;
93                }
94               
95                /* Comment? */
96                if( ( s = strchr( file->cur, '#' ) ) != NULL )
97                        *s = 0;
98               
99                file->cur = ini_strip_whitespace( file->cur );
100               
101                if( *file->cur == '[' )
102                {
103                        file->cur++;
104                        if( ( s = strchr( file->cur, ']' ) ) != NULL )
105                        {
106                                *s = 0;
107                                file->c_section = file->cur;
108                        }
109                }
110                else if( ( s = strchr( file->cur, '=' ) ) != NULL )
111                {
112                        *s = 0;
113                        file->key = ini_strip_whitespace( file->cur );
114                        file->value = ini_strip_whitespace( s + 1 );
115                       
116                        if( ( s = strchr( file->key, '.' ) ) != NULL )
117                        {
118                                *s = 0;
119                                file->section = file->key;
120                                file->key = s + 1;
121                        }
122                        else
123                        {
124                                file->section = file->c_section;
125                        }
126                       
127                        file->cur = next;
128                        return 1;
129                }
130                /* else: noise/comment/etc, let's just ignore it. */
131
132                file->cur = next;
133        }
134       
135        return 0;
136}
137
138void ini_close( ini_t *file )
139{
140        g_free( file );
141}
Note: See TracBrowser for help on using the repository browser.