14 #define ASCIILINESZ (1024)
15 #define INI_INVALID_KEY ((char*)-1)
44 const char *
strlwc(
const char * in,
char *out,
unsigned len)
48 if (in==NULL || out == NULL || len==0)
return NULL ;
50 while (in[i] !=
'\0' && i < len-1) {
51 out[i] = (char)tolower((
int)in[i]);
68 static char * xstrdup(
const char * s)
76 t = (
char*) malloc(len) ;
95 if (s==NULL)
return 0;
98 while (isspace((
int)*s) && *s) s++;
100 if (!isspace((
int)*(last-1)))
106 memmove(dest,s,last - s + 1);
133 if (d==NULL)
return -1 ;
135 for (i=0 ; i<d->
size ; i++) {
138 if (strchr(d->
key[i],
':')==NULL) {
164 if (d==NULL || n<0)
return NULL ;
166 for (i=0 ; i<d->
size ; i++) {
169 if (strchr(d->
key[i],
':')==NULL) {
198 if (d==NULL || f==NULL) return ;
199 for (i=0 ; i<d->
size ; i++) {
200 if (d->
key[i]==NULL) continue ;
201 fprintf(f,
"%s = [%s]\n", d->
key[i], d->
val[i]);
223 const char * secname ;
225 if (d==NULL || f==NULL) return ;
230 for (i=0 ; i<d->
size ; i++) {
233 fprintf(f,
"%s = %s\n", d->
key[i], d->
val[i]);
237 for (i=0 ; i<nsec ; i++) {
263 if (d==NULL || f==NULL) return ;
266 seclen = (int)strlen(s);
267 fprintf(f,
"\n[%s]\n", s);
268 sprintf(keym,
"%s:", s);
269 for (j=0 ; j<d->
size ; j++) {
272 if (!strncmp(d->
key[j], keym, seclen+1)) {
276 d->
val[j] ? d->
val[j] :
"");
299 if (d==NULL)
return nkeys;
302 seclen = (int)strlen(s);
303 sprintf(keym,
"%s:", s);
305 for (j=0 ; j<d->
size ; j++) {
308 if (!strncmp(d->
key[j], keym, seclen+1))
337 if (d==NULL || keys==NULL)
return NULL;
340 seclen = (int)strlen(s);
341 sprintf(keym,
"%s:", s);
345 for (j=0 ; j<d->
size ; j++) {
348 if (!strncmp(d->
key[j], keym, seclen+1)) {
374 const char * lc_key ;
378 if (d==NULL || key==NULL)
381 lc_key =
strlwc(key, tmp_str,
sizeof(tmp_str));
419 return (
int)strtol(str, NULL, 0);
483 if (c[0]==
'y' || c[0]==
'Y' || c[0]==
'1' || c[0]==
't' || c[0]==
'T') {
485 }
else if (c[0]==
'n' || c[0]==
'N' || c[0]==
'0' || c[0]==
'f' || c[0]==
'F') {
560 const char * input_line,
569 line = xstrdup(input_line);
576 }
else if (line[0]==
'#' || line[0]==
';') {
580 }
else if (line[0]==
'[') {
582 sscanf(line,
"[%[^]]", section);
584 strlwc(section, section, len);
586 }
else if (sscanf (line,
"%[^=] = \"%[^\"]\"", key, value) == 2
587 || sscanf (line,
"%[^=] = '%[^\']'", key, value) == 2) {
596 if (!strcmp(value,
"\"\"") || (!strcmp(value,
"''"))) {
600 }
else if (sscanf (line,
"%[^=] = %[^;#]", key, value) == 2) {
607 }
else if (sscanf(line,
"%[^=] = %[;#]", key, value)==2
608 || sscanf(line,
"%[^=] %[=]", key, value) == 2) {
659 if ((in=fopen(ininame,
"r"))==NULL) {
660 fprintf(stderr,
"iniparser: cannot open %s\n", ininame);
676 while (fgets(line+last,
ASCIILINESZ-last, in)!=NULL) {
678 len = (int)strlen(line)-1;
682 if (line[len]!=
'\n' && !feof(in)) {
684 "iniparser: input line too long in %s (%d)\n",
693 ((line[len]==
'\n') || (isspace(line[len])))) {
701 if (line[len]==
'\\') {
708 switch (iniparser_line(line, section, key, val)) {
718 sprintf(tmp,
"%s:%s", section, key);
723 fprintf(stderr,
"iniparser: syntax error in %s (%d):\n",
726 fprintf(stderr,
"-> %s\n", line);
736 fprintf(stderr,
"iniparser: memory allocation failure\n");
void dictionary_del(dictionary *d)
Delete a dictionary object.
void dictionary_unset(dictionary *d, const char *key)
Delete a key in a dictionary.
const char * dictionary_get(const dictionary *d, const char *key, const char *def)
Get a value from a dictionary.
int dictionary_set(dictionary *d, const char *key, const char *val)
Set a value in a dictionary.
dictionary * dictionary_new(size_t size)
Create a new dictionary object.
enum _line_status_ line_status
double iniparser_getdouble(const dictionary *d, const char *key, double notfound)
Get the string associated to a key, convert to a double.
void iniparser_unset(dictionary *ini, const char *entry)
Delete an entry in a dictionary.
const char ** iniparser_getseckeys(const dictionary *d, const char *s, const char **keys)
Get the number of keys in a section of a dictionary.
int iniparser_getsecnkeys(const dictionary *d, const char *s)
Get the number of keys in a section of a dictionary.
const char * strlwc(const char *in, char *out, unsigned len)
Convert a string to lowercase.
void iniparser_dumpsection_ini(const dictionary *d, const char *s, FILE *f)
Save a dictionary section to a loadable ini file.
void iniparser_dump(const dictionary *d, FILE *f)
Dump a dictionary to an opened file pointer.
unsigned strstrip(char *s)
Remove blanks at the beginning and the end of a string.
void iniparser_freedict(dictionary *d)
Free all memory associated to an ini dictionary.
int iniparser_getint(const dictionary *d, const char *key, int notfound)
Get the string associated to a key, convert to an int.
dictionary * iniparser_load(const char *ininame)
Parse an ini file and return an allocated dictionary object.
const char * iniparser_getsecname(const dictionary *d, int n)
Get name for section n in a dictionary.
int iniparser_find_entry(const dictionary *ini, const char *entry)
Finds out if a given entry exists in a dictionary.
int iniparser_getnsec(const dictionary *d)
Get number of sections in a dictionary.
int iniparser_set(dictionary *ini, const char *entry, const char *val)
Set an entry in a dictionary.
void iniparser_dump_ini(const dictionary *d, FILE *f)
Save a dictionary to a loadable ini file.
const char * iniparser_getstring(const dictionary *d, const char *key, const char *def)
Get the string associated to a key.
int iniparser_getboolean(const dictionary *d, const char *key, int notfound)
Get the string associated to a key, convert to a boolean.