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 | |
---|
28 | ini_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 | int ini_read( ini_t *file ) |
---|
56 | { |
---|
57 | char *s; |
---|
58 | |
---|
59 | while( file->cur && file->cur < file->file + file->size ) |
---|
60 | { |
---|
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 ) |
---|
71 | { |
---|
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 ) |
---|
96 | { |
---|
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; |
---|
121 | } |
---|
122 | else |
---|
123 | { |
---|
124 | file->section = file->c_section; |
---|
125 | } |
---|
126 | |
---|
127 | file->cur = next; |
---|
128 | |
---|
129 | printf( "%s.%s = '%s'\n", file->section, file->key, file->value ); |
---|
130 | |
---|
131 | return 1; |
---|
132 | } |
---|
133 | /* else: noise, but let's just ignore it. */ |
---|
134 | |
---|
135 | file->cur = next; |
---|
136 | } |
---|
137 | |
---|
138 | return 0; |
---|
139 | } |
---|
140 | |
---|
141 | void ini_close( ini_t *file ) |
---|
142 | { |
---|
143 | g_free( file ); |
---|
144 | } |
---|