38 #define ASCIILINESZ (1024)
39 #define INI_INVALID_KEY ((char*) NULL)
64 static char *strlwc(
const char *s)
66 static char l[ASCIILINESZ+1];
72 for (i = 0; s[i] && i < ASCIILINESZ; i++)
90 static char *strstrip(
const char *s)
92 static char l[ASCIILINESZ+1];
93 unsigned int i, numspc;
101 for (i = numspc = 0; s[i] && i < ASCIILINESZ; i++) {
108 l[i - numspc] =
'\0';
121 line_status ciniparser_line(
char *input_line,
char *section,
122 char *key,
char *value)
125 char line[ASCIILINESZ+1];
128 strcpy(line, strstrip(input_line));
129 len = (int) strlen(line);
134 }
else if (line[0] ==
'#') {
137 }
else if (line[0] ==
'[' && line[len-1] ==
']') {
139 sscanf(line,
"[%[^]]", section);
140 strcpy(section, strstrip(section));
141 strcpy(section, strlwc(section));
143 }
else if (sscanf (line,
"%[^=] = \"%[^\"]\"", key, value) == 2
144 || sscanf (line,
"%[^=] = '%[^\']'", key, value) == 2
145 || sscanf (line,
"%[^=] = %[^;#]", key, value) == 2) {
147 strcpy(key, strstrip(key));
148 strcpy(key, strlwc(key));
149 strcpy(value, strstrip(value));
154 if (!strcmp(value,
"\"\"") || (!strcmp(value,
"''"))) {
158 }
else if (sscanf(line,
"%[^=] = %[;#]", key, value) == 2
159 || sscanf(line,
"%[^=] %[=]", key, value) == 2) {
166 strcpy(key, strstrip(key));
167 strcpy(key, strlwc(key));
188 for (i = 0; i < d->size; i++) {
189 if (d->key[i] == NULL)
191 if (strchr(d->key[i],
':') == NULL) {
204 if (d == NULL || n < 0)
212 for (i = 0; i < d->size; i++) {
213 if (d->key[i] == NULL)
215 if (! strchr(d->key[i],
':')) {
226 return (
char *) NULL;
233 if (d == NULL || f == NULL)
236 for (i = 0; i < d->size; i++) {
237 if (d->key[i] == NULL)
239 if (d->val[i] != NULL) {
240 fprintf(f,
"[%s]=[%s]\n", d->key[i], d->val[i]);
242 fprintf(f,
"[%s]=UNDEF\n", d->key[i]);
252 char keym[ASCIILINESZ+1];
257 if (d == NULL || f == NULL)
260 memset(keym, 0, ASCIILINESZ + 1);
265 for (i = 0; i < d->size; i++) {
266 if (d->key[i] == NULL)
268 fprintf(f,
"%s = %s\n", d->key[i], d->val[i]);
273 for (i = 0; i < nsec; i++) {
275 seclen = (int)strlen(secname);
276 fprintf(f,
"\n[%s]\n", secname);
277 snprintf(keym, ASCIILINESZ + 1,
"%s:", secname);
278 for (j = 0; j < d->size; j++) {
279 if (d->key[j] == NULL)
281 if (!strncmp(d->key[j], keym, seclen+1)) {
282 fprintf(f,
"%-30s = %s\n",
284 d->val[j] ? d->val[j] :
"");
298 if (d == NULL || key == NULL)
301 lc_key = strlwc(key);
313 if (str == INI_INVALID_KEY)
316 return (
int) strtol(str, NULL, 10);
325 if (str == INI_INVALID_KEY)
337 if (c == INI_INVALID_KEY)
341 case 'y':
case 'Y':
case '1':
case 't':
case 'T':
344 case 'n':
case 'N':
case '0':
case 'f':
case 'F':
379 char line[ASCIILINESZ+1];
380 char section[ASCIILINESZ+1];
381 char key[ASCIILINESZ+1];
382 char tmp[ASCIILINESZ+1];
383 char val[ASCIILINESZ+1];
384 int last = 0, len, lineno = 0, errs = 0;
387 if ((in = fopen(ininame,
"r")) == NULL) {
388 fprintf(stderr,
"ciniparser: cannot open %s\n (ignored)",
399 memset(line, 0, ASCIILINESZ + 1);
400 memset(section, 0, ASCIILINESZ + 1);
401 memset(key, 0, ASCIILINESZ + 1);
402 memset(val, 0, ASCIILINESZ + 1);
405 while (fgets(line+last, ASCIILINESZ-last, in)!=NULL) {
407 len = (int) strlen(line)-1;
409 if (line[len] !=
'\n') {
411 "ciniparser: input line too long in %s (%d)\n",
421 ((line[len] ==
'\n') || (isspace(line[len])))) {
427 if (len >= 0 && line[len] ==
'\\') {
433 switch (ciniparser_line(line, section, key, val)) {
443 snprintf(tmp, ASCIILINESZ + 1,
"%s:%s", section, key);
448 fprintf(stderr,
"ciniparser: syntax error in %s (%d):\n",
450 fprintf(stderr,
"-> %s\n", line);
457 memset(line, 0, ASCIILINESZ);
460 fprintf(stderr,
"ciniparser: memory allocation failure\n");
const char * ciniparser_getstring(dictionary *d, const char *key, char *def)
Get the string associated to a key.
int dictionary_set(dictionary *d, const char *key, const char *val)
Set a value in a dictionary.
dictionary * ciniparser_load(const char *ininame)
Parse an ini file and return an allocated dictionary object.
void ciniparser_dump_ini(dictionary *d, FILE *f)
Save a dictionary to a loadable ini file.
int ciniparser_getint(dictionary *d, const char *key, int notfound)
Get the string associated to a key, convert to an int.
const char * dictionary_get(dictionary *d, const char *key, const char *def)
Get a value from a dictionary.
dictionary * dictionary_new(int size)
Create a new dictionary object.
void dictionary_del(dictionary *d)
Delete a dictionary object.
void ciniparser_freedict(dictionary *d)
Free all memory associated to an ini dictionary.
int ciniparser_set(dictionary *d, const char *entry, const char *val)
Set an item in the dictionary.
int ciniparser_find_entry(dictionary *ini, const char *entry)
Finds out if a given entry exists in a dictionary.
void ciniparser_unset(dictionary *ini, char *entry)
Delete an entry in a dictionary.
double ciniparser_getdouble(dictionary *d, const char *key, double notfound)
Get the string associated to a key, convert to a double.
enum _line_status_ line_status
void dictionary_unset(dictionary *d, const char *key)
Delete a key in a dictionary.
const char * ciniparser_getsecname(dictionary *d, int n)
Get name for section n in a dictionary.
void ciniparser_dump(dictionary *d, FILE *f)
Dump a dictionary to an opened file pointer.
int ciniparser_getnsec(dictionary *d)
Get number of sections in a dictionary.
int ciniparser_getboolean(dictionary *d, const char *key, int notfound)
Get the string associated to a key, convert to a boolean.