2 * Copyright 2006 Eduard Bloch
4 * Uses my config parser code and small wrappers to provide the old interface.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; see the file COPYING. If not, write to the Free Software
19 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 #define GETVAL_BUF_LEN 512
37 #define isUspace(x) isspace( (int) (unsigned char) x)
39 static FILE *glob_cfg_ptr = NULL;
42 * Warning, uses static line buffer, not reentrant. NULL returned if the key isn't found.
44 static char *get_value(FILE *srcfile, const char *key, int dorewind) {
45 static char linebuf[GETVAL_BUF_LEN];
48 return ((char *) NULL);
57 while(fgets(linebuf, sizeof(linebuf)-1, srcfile)) {
64 /* simple state machine, char position moved by the states (or not),
65 * state change is done by the state (or not) */
66 for( i=0 ; i<sizeof(linebuf) ; ) {
67 /* printf("key: %s, %s, s: %d\n", key, linebuf, s); */
71 if(isUspace(linebuf[i]))
73 else if(linebuf[i] == '#' || linebuf[i]=='\0')
81 case(KEYCOMPARE): /* compare the key */
83 if(key[i-keybeg]=='\0')
84 /* end of key, next state decides what to do on this position */
87 if(linebuf[i-keybeg]!=key[i-keybeg])
94 case(EQSIGNSEARCH): /* skip whitespace, stop on =, break on anything else */
96 if(isUspace(linebuf[i]))
98 else if(linebuf[i]=='=') {
106 case(VALBEGINSEARCH):
108 if(isUspace(linebuf[i]))
111 /* possible at EOF */
112 if(linebuf[i] == '\0')
115 lastchar=i-1; /* lastchar can be a space, see below */
121 case(LASTCHARSEARCH):
124 if(!isUspace(linebuf[i]))
127 else { /* got string end, terminate after the last seen char */
128 if(linebuf+lastchar < ret) /* no non-space found */
130 linebuf[lastchar+1]='\0';
142 int cfg_open(const char *name)
145 fclose(glob_cfg_ptr);
152 glob_cfg_ptr = fopen(name, "r");
153 return (glob_cfg_ptr ? 0 : -1);
161 r=fclose(glob_cfg_ptr);
168 get_value(glob_cfg_ptr, NULL, 1);
171 char *cfg_get(const char *key)
173 return get_value(glob_cfg_ptr, key, 1);
176 char *cfg_get_next(const char *key)
178 return get_value(glob_cfg_ptr, key, 0);