14 #define ASCIILINESZ (1024)
15 #define INI_INVALID_KEY ((char*)-1)
44 static char * strlwc(
const char * s)
49 if (s==NULL)
return NULL ;
53 l[i] = (char)tolower((
int)s[i]);
74 static char * strstrip(
const char * s)
79 if (s==NULL)
return NULL ;
81 while (isspace((
int)*s) && *s) s++;
86 if (!isspace((
int)*(last-1)))
117 if (d==NULL)
return -1 ;
119 for (i=0 ; i<d->
size ; i++) {
122 if (strchr(d->
key[i],
':')==NULL) {
148 if (d==NULL || n<0)
return NULL ;
150 for (i=0 ; i<d->
size ; i++) {
153 if (strchr(d->
key[i],
':')==NULL) {
182 if (d==NULL || f==NULL) return ;
183 for (i=0 ; i<d->
size ; i++) {
186 if (d->
val[i]!=NULL) {
187 fprintf(f,
"[%s]=[%s]\n", d->
key[i], d->
val[i]);
189 fprintf(f,
"[%s]=UNDEF\n", d->
key[i]);
212 if (d==NULL || f==NULL) return ;
217 for (i=0 ; i<d->
size ; i++) {
220 fprintf(f,
"%s = %s\n", d->
key[i], d->
val[i]);
224 for (i=0 ; i<nsec ; i++) {
250 if (d==NULL || f==NULL) return ;
253 seclen = (int)strlen(s);
254 fprintf(f,
"\n[%s]\n", s);
255 sprintf(keym,
"%s:", s);
256 for (j=0 ; j<d->
size ; j++) {
259 if (!strncmp(d->
key[j], keym, seclen+1)) {
263 d->
val[j] ? d->
val[j] :
"");
286 if (d==NULL)
return nkeys;
289 seclen = (int)strlen(s);
290 sprintf(keym,
"%s:", s);
292 for (j=0 ; j<d->
size ; j++) {
295 if (!strncmp(d->
key[j], keym, seclen+1))
328 if (d==NULL)
return keys;
333 keys = (
char**) malloc(nkeys*
sizeof(
char*));
335 seclen = (int)strlen(s);
336 sprintf(keym,
"%s:", s);
340 for (j=0 ; j<d->
size ; j++) {
343 if (!strncmp(d->
key[j], keym, seclen+1)) {
373 if (d==NULL || key==NULL)
376 lc_key = strlwc(key);
414 return (
int)strtol(str, NULL, 0);
478 if (c[0]==
'y' || c[0]==
'Y' || c[0]==
'1' || c[0]==
't' || c[0]==
'T') {
480 }
else if (c[0]==
'n' || c[0]==
'N' || c[0]==
'0' || c[0]==
'f' || c[0]==
'F') {
556 const char * input_line,
565 strcpy(line, strstrip(input_line));
566 len = (int)strlen(line);
572 }
else if (line[0]==
'#' || line[0]==
';') {
575 }
else if (line[0]==
'[' && line[len-1]==
']') {
577 sscanf(line,
"[%[^]]", section);
578 strcpy(section, strstrip(section));
579 strcpy(section, strlwc(section));
581 }
else if (sscanf (line,
"%[^=] = \"%[^\"]\"", key, value) == 2
582 || sscanf (line,
"%[^=] = '%[^\']'", key, value) == 2
583 || sscanf (line,
"%[^=] = %[^;#]", key, value) == 2) {
585 strcpy(key, strstrip(key));
586 strcpy(key, strlwc(key));
587 strcpy(value, strstrip(value));
592 if (!strcmp(value,
"\"\"") || (!strcmp(value,
"''"))) {
596 }
else if (sscanf(line,
"%[^=] = %[;#]", key, value)==2
597 || sscanf(line,
"%[^=] %[=]", key, value) == 2) {
604 strcpy(key, strstrip(key));
605 strcpy(key, strlwc(key));
646 if ((in=fopen(ininame,
"r"))==NULL) {
647 fprintf(stderr,
"iniparser: cannot open %s\n", ininame);
663 while (fgets(line+last,
ASCIILINESZ-last, in)!=NULL) {
665 len = (int)strlen(line)-1;
669 if (line[len]!=
'\n') {
671 "iniparser: input line too long in %s (%d)\n",
680 ((line[len]==
'\n') || (isspace(line[len])))) {
685 if (line[len]==
'\\') {
692 switch (iniparser_line(line, section, key, val)) {
702 sprintf(tmp,
"%s:%s", section, key);
707 fprintf(stderr,
"iniparser: syntax error in %s (%d):\n",
710 fprintf(stderr,
"-> %s\n", line);
720 fprintf(stderr,
"iniparser: memory allocation failure\n");
int iniparser_set(dictionary *ini, const char *entry, const char *val)
Set an entry in a dictionary.
char * iniparser_getstring(dictionary *d, const char *key, char *def)
Get the string associated to a key.
void iniparser_unset(dictionary *ini, const char *entry)
Delete an entry in a dictionary.
char * dictionary_get(dictionary *d, const char *key, char *def)
Get a value from a dictionary.
void dictionary_unset(dictionary *d, const char *key)
Delete a key in a dictionary.
dictionary * iniparser_load(const char *ininame)
Parse an ini file and return an allocated dictionary object.
double iniparser_getdouble(dictionary *d, const char *key, double notfound)
Get the string associated to a key, convert to a double.
int iniparser_getboolean(dictionary *d, const char *key, int notfound)
Get the string associated to a key, convert to a boolean.
void iniparser_dump(dictionary *d, FILE *f)
Dump a dictionary to an opened file pointer.
enum _line_status_ line_status
char * iniparser_getsecname(dictionary *d, int n)
Get name for section n in a dictionary.
dictionary * dictionary_new(int size)
Create a new dictionary object.
void dictionary_del(dictionary *d)
Delete a dictionary object.
void iniparser_dumpsection_ini(dictionary *d, char *s, FILE *f)
Save a dictionary section to a loadable ini file.
int iniparser_find_entry(dictionary *ini, const char *entry)
Finds out if a given entry exists in a dictionary.
int iniparser_getsecnkeys(dictionary *d, char *s)
Get the number of keys in a section of a dictionary.
int iniparser_getnsec(dictionary *d)
Get number of sections in a dictionary.
int dictionary_set(dictionary *d, const char *key, const char *val)
Set a value in a dictionary.
char ** iniparser_getseckeys(dictionary *d, char *s)
Get the number of keys in a section of a dictionary.
void iniparser_dump_ini(dictionary *d, FILE *f)
Save a dictionary to a loadable ini file.
void iniparser_freedict(dictionary *d)
Free all memory associated to an ini dictionary.
int iniparser_getint(dictionary *d, const char *key, int notfound)
Get the string associated to a key, convert to an int.