#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include "iniparser.h"

Go to the source code of this file.
Defines | |
| #define | ASCIILINESZ 1024 |
| #define | ASCIILINESZ 1024 |
| #define | DICT_INVALID_KEY ((char*)-1) |
| #define | DICTMINSZ 128 |
| #define | INI_INVALID_KEY ((char*)-1) |
| #define | MAXVALSZ 1024 |
| Remove blanks at the beginning and the end of a string. | |
Functions | |
| static void | dictionary_del (dictionary *d) |
| Delete a dictionary object. | |
| static void | dictionary_dump (dictionary *d, FILE *f) |
| Dump a dictionary to an opened file pointer. | |
| static char * | dictionary_get (dictionary *d, char *key, char *def) |
| Get a value from a dictionary. | |
| static unsigned | dictionary_hash (char *key) |
| Compute the hash key for a string. | |
| static dictionary * | dictionary_new (int size) |
| Create a new dictionary object. | |
| static void | dictionary_set (dictionary *d, char *key, char *val) |
| Set a value in a dictionary. | |
| static void | dictionary_unset (dictionary *d, char *key) |
| Delete a key in a dictionary. | |
| static void | iniparser_add_entry (dictionary *d, char *sec, char *key, char *val) |
| void | iniparser_dump (dictionary *d, FILE *f) |
| Dump a dictionary to an opened file pointer. | |
| void | iniparser_dump_ini (dictionary *d, FILE *f) |
| Save a dictionary to a loadable ini file. | |
| int | iniparser_find_entry (dictionary *ini, char *entry) |
| Finds out if a given entry exists in a dictionary. | |
| void | iniparser_free (dictionary *d) |
| Free all memory associated to an ini dictionary. | |
| int | iniparser_getboolean (dictionary *d, char *key, int notfound) |
| Get the string associated to a key, convert to a boolean. | |
| double | iniparser_getdouble (dictionary *d, char *key, double notfound) |
| Get the string associated to a key, convert to a double. | |
| int | iniparser_getint (dictionary *d, char *key, int notfound) |
| Get the string associated to a key, convert to an int. | |
| int | iniparser_getnsec (dictionary *d) |
| Get number of sections in a dictionary. | |
| char * | iniparser_getsecname (dictionary *d, int n) |
| Get name for section n in a dictionary. | |
| char * | iniparser_getstr (dictionary *d, char *key) |
| Get the string associated to a key, return NULL if not found. | |
| char * | iniparser_getstring (dictionary *d, char *key, char *def) |
| Get the string associated to a key. | |
| dictionary * | iniparser_new (char *ininame) |
| Parse an ini file and return an allocated dictionary object. | |
| int | iniparser_setstr (dictionary *ini, char *entry, char *val) |
| Set an entry in a dictionary. | |
| void | iniparser_unset (dictionary *ini, char *entry) |
| Delete an entry in a dictionary. | |
| static void * | mem_double (void *ptr, int size) |
| static char * | strcrop (char *s) |
| Remove blanks at the end of a string. | |
| static char * | strlwc (char *s) |
| Convert a string to lowercase. | |
| static char * | strskp (char *s) |
| Convert a string to uppercase. | |
| #define ASCIILINESZ 1024 |
Definition at line 550 of file iniparser.c.
| #define ASCIILINESZ 1024 |
Definition at line 550 of file iniparser.c.
Referenced by iniparser_add_entry(), iniparser_dump_ini(), iniparser_new(), strcrop(), and strlwc().
| #define DICT_INVALID_KEY ((char*)-1) |
Invalid key token
Definition at line 244 of file iniparser.c.
| #define DICTMINSZ 128 |
Minimal allocated number of entries in a dictionary
Definition at line 241 of file iniparser.c.
Referenced by dictionary_new().
| #define INI_INVALID_KEY ((char*)-1) |
Definition at line 551 of file iniparser.c.
Referenced by iniparser_find_entry(), iniparser_getboolean(), iniparser_getdouble(), and iniparser_getint().
| #define MAXVALSZ 1024 |
Remove blanks at the beginning and the end of a string.
| s | String to parse. |
Definition at line 238 of file iniparser.c.
| static void dictionary_del | ( | dictionary * | d | ) | [static] |
Delete a dictionary object.
| d | dictionary object to deallocate. |
Definition at line 336 of file iniparser.c.
References _dictionary_::hash, _dictionary_::key, NULL, _dictionary_::size, and _dictionary_::val.
Referenced by iniparser_free().
| static void dictionary_dump | ( | dictionary * | d, | |
| FILE * | f | |||
| ) | [static] |
Dump a dictionary to an opened file pointer.
| d | Dictionary to dump | |
| f | Opened file pointer. |
[Key]=[Value], one per line. It is Ok to provide stdout or stderr as output file pointers.
Definition at line 529 of file iniparser.c.
References _dictionary_::key, NULL, _dictionary_::size, and _dictionary_::val.
Referenced by iniparser_dump().
| static char* dictionary_get | ( | dictionary * | d, | |
| char * | key, | |||
| char * | def | |||
| ) | [static] |
Get a value from a dictionary.
| d | dictionary object to search. | |
| key | Key to look for in the dictionary. | |
| def | Default value to return if key not found. |
Definition at line 371 of file iniparser.c.
References dictionary_hash(), _dictionary_::hash, hash, _dictionary_::key, NULL, _dictionary_::size, and _dictionary_::val.
Referenced by iniparser_getstring().
| static unsigned dictionary_hash | ( | char * | key | ) | [static] |
Compute the hash key for a string.
| key | Character string to use for key. |
Definition at line 278 of file iniparser.c.
Referenced by dictionary_get(), dictionary_set(), and dictionary_unset().
| static dictionary* dictionary_new | ( | int | size | ) | [static] |
Create a new dictionary object.
| size | Optional initial size of the dictionary. |
Definition at line 309 of file iniparser.c.
References DICTMINSZ, _dictionary_::hash, _dictionary_::key, _dictionary_::size, and _dictionary_::val.
Referenced by iniparser_new().
| static void dictionary_set | ( | dictionary * | d, | |
| char * | key, | |||
| char * | val | |||
| ) | [static] |
Set a value in a dictionary.
| d | dictionary object to modify. | |
| key | Key to modify or add. | |
| val | Value to add. |
It is Ok to provide a NULL value for val, but NULL values for the dictionary or the key are considered as errors: the function will return immediately in such a case.
Notice that if you dictionary_set a variable to NULL, a call to dictionary_get will return a NULL value: the variable will be found, and its value (NULL) is returned. In other words, setting the variable content to NULL is equivalent to deleting the variable from the dictionary. It is not possible (in this implementation) to have a key in the dictionary without value.
Definition at line 417 of file iniparser.c.
References dictionary_hash(), _dictionary_::hash, hash, _dictionary_::key, mem_double(), _dictionary_::n, NULL, _dictionary_::size, and _dictionary_::val.
Referenced by iniparser_add_entry(), and iniparser_setstr().
| static void dictionary_unset | ( | dictionary * | d, | |
| char * | key | |||
| ) | [static] |
Delete a key in a dictionary.
| d | dictionary object to modify. | |
| key | Key to remove. |
Definition at line 482 of file iniparser.c.
References dictionary_hash(), _dictionary_::hash, hash, _dictionary_::key, _dictionary_::n, NULL, _dictionary_::size, and _dictionary_::val.
Referenced by iniparser_unset().
| static void iniparser_add_entry | ( | dictionary * | d, | |
| char * | sec, | |||
| char * | key, | |||
| char * | val | |||
| ) | [static] |
Definition at line 554 of file iniparser.c.
References ASCIILINESZ, dictionary_set(), and NULL.
Referenced by iniparser_new().
| void iniparser_dump | ( | dictionary * | d, | |
| FILE * | f | |||
| ) |
Dump a dictionary to an opened file pointer.
| d | Dictionary to dump. | |
| f | Opened file pointer to dump to. |
stderr or stdout as output files. This function is meant for debugging purposes mostly.
Definition at line 663 of file iniparser.c.
References dictionary_dump().
| void iniparser_dump_ini | ( | dictionary * | d, | |
| FILE * | f | |||
| ) |
Save a dictionary to a loadable ini file.
| d | Dictionary to dump | |
| f | Opened file pointer to dump to |
stderr or stdout as output files.
Definition at line 681 of file iniparser.c.
References ASCIILINESZ, iniparser_getnsec(), iniparser_getsecname(), _dictionary_::key, NULL, _dictionary_::size, and _dictionary_::val.
| int iniparser_find_entry | ( | dictionary * | ini, | |
| char * | entry | |||
| ) |
Finds out if a given entry exists in a dictionary.
Definition at line 886 of file iniparser.c.
References INI_INVALID_KEY, and iniparser_getstring().
Referenced by mod_init().
| void iniparser_free | ( | dictionary * | d | ) |
Free all memory associated to an ini dictionary.
| d | Dictionary to free |
Definition at line 1018 of file iniparser.c.
References dictionary_del().
| int iniparser_getboolean | ( | dictionary * | d, | |
| char * | key, | |||
| int | notfound | |||
| ) |
Get the string associated to a key, convert to a boolean.
| d | Dictionary to search | |
| key | Key string to look for | |
| notfound | Value to return in case of error |
A true boolean is found if one of the following is matched:
A false boolean is found if one of the following is matched:
The notfound value returned if no boolean is identified, does not necessarily have to be 0 or 1.
Definition at line 855 of file iniparser.c.
References INI_INVALID_KEY, and iniparser_getstring().
Referenced by add_ld_session().
| double iniparser_getdouble | ( | dictionary * | d, | |
| char * | key, | |||
| double | notfound | |||
| ) |
Get the string associated to a key, convert to a double.
| d | Dictionary to search | |
| key | Key string to look for | |
| notfound | Value to return in case of error |
Definition at line 812 of file iniparser.c.
References INI_INVALID_KEY, and iniparser_getstring().
| int iniparser_getint | ( | dictionary * | d, | |
| char * | key, | |||
| int | notfound | |||
| ) |
Get the string associated to a key, convert to an int.
| d | Dictionary to search | |
| key | Key string to look for | |
| notfound | Value to return in case of error |
Definition at line 789 of file iniparser.c.
References INI_INVALID_KEY, and iniparser_getstring().
Referenced by add_ld_session().
| int iniparser_getnsec | ( | dictionary * | d | ) |
Get number of sections in a dictionary.
| d | Dictionary to examine |
This clearly fails in the case a section name contains a colon, but this should simply be avoided.
This function returns -1 in case of error.
Definition at line 594 of file iniparser.c.
References _dictionary_::key, NULL, and _dictionary_::size.
Referenced by child_init(), iniparser_dump_ini(), and mod_init().
| char* iniparser_getsecname | ( | dictionary * | d, | |
| int | n | |||
| ) |
Get name for section n in a dictionary.
| d | Dictionary to examine | |
| n | Section number (from 0 to nsec-1). |
This function returns NULL in case of error.
Definition at line 627 of file iniparser.c.
References _dictionary_::key, NULL, and _dictionary_::size.
Referenced by child_init(), iniparser_dump_ini(), and mod_init().
| char* iniparser_getstr | ( | dictionary * | d, | |
| char * | key | |||
| ) |
Get the string associated to a key, return NULL if not found.
| d | Dictionary to search | |
| key | Key string to look for |
This function is only provided for backwards compatibility with previous versions of iniparser. It is recommended to use iniparser_getstring() instead.
Definition at line 739 of file iniparser.c.
References iniparser_getstring(), and NULL.
| char* iniparser_getstring | ( | dictionary * | d, | |
| char * | key, | |||
| char * | def | |||
| ) |
Get the string associated to a key.
| d | Dictionary to search | |
| key | Key string to look for | |
| def | Default value to return if key not found. |
Definition at line 760 of file iniparser.c.
References dictionary_get(), NULL, and strlwc().
Referenced by add_ld_session(), iniparser_find_entry(), iniparser_getboolean(), iniparser_getdouble(), iniparser_getint(), and iniparser_getstr().
| dictionary* iniparser_new | ( | char * | ininame | ) |
Parse an ini file and return an allocated dictionary object.
| ininame | Name of the ini file to read. |
The returned dictionary must be freed using iniparser_free().
Definition at line 951 of file iniparser.c.
References ASCIILINESZ, dictionary_new(), iniparser_add_entry(), NULL, strcrop(), strlwc(), and strskp().
Referenced by mod_init().
| int iniparser_setstr | ( | dictionary * | ini, | |
| char * | entry, | |||
| char * | val | |||
| ) |
Set an entry in a dictionary.
| ini | Dictionary to modify. | |
| entry | Entry to modify (entry name) | |
| val | New value to associate to the entry. |
Definition at line 914 of file iniparser.c.
References dictionary_set(), and strlwc().
| void iniparser_unset | ( | dictionary * | ini, | |
| char * | entry | |||
| ) |
Delete an entry in a dictionary.
Definition at line 930 of file iniparser.c.
References dictionary_unset(), and strlwc().
| static void* mem_double | ( | void * | ptr, | |
| int | size | |||
| ) | [static] |
| static char* strcrop | ( | char * | s | ) | [static] |
Remove blanks at the end of a string.
| s | String to parse. |
Definition at line 178 of file iniparser.c.
References ASCIILINESZ, and NULL.
Referenced by iniparser_new().
| static char* strlwc | ( | char * | s | ) | [static] |
Convert a string to lowercase.
| s | String to convert. |
Definition at line 91 of file iniparser.c.
References ASCIILINESZ, and NULL.
Referenced by iniparser_getstring(), iniparser_new(), iniparser_setstr(), and iniparser_unset().
| static char* strskp | ( | char * | s | ) | [static] |
Convert a string to uppercase.
| s | String to convert. |
| s | String to parse. |
Definition at line 153 of file iniparser.c.
References NULL.
Referenced by iniparser_new().
1.5.6