LIRC libraries
LinuxInfraredRemoteControl
|
Files | |
file | ciniparser.c |
Parser for ini files. | |
file | ciniparser.h |
Parser for ini files. | |
file | dictionary.c |
Implements a dictionary for string variables. | |
file | dictionary.h |
Implements a dictionary for string variables.This module implements a simple dictionary object, i.e. a list of string/string associations. This object is useful to store e.g. information retrieved from a configuration file (ini files). | |
Classes | |
struct | _dictionary_ |
Dictionary object. More... | |
Macros | |
#define | ASCIILINESZ (1024) |
#define | INI_INVALID_KEY ((char*)NULL) |
#define | ciniparser_getstr(d, k) ciniparser_getstring(d, k, NULL) |
#define | ciniparser_setstr ciniparser_setstring |
#define | MAXVALSZ 1024 |
Maximum value size for integers and doubles. More... | |
#define | DICTMINSZ 128 |
Minimal allocated number of entries in a dictionary. | |
#define | DICT_INVALID_KEY ((char*)-1) |
Invalid key token. | |
Typedefs | |
typedef enum _line_status_ | line_status |
This enum stores the status for each parsed line (internal use only). | |
typedef struct _dictionary_ | dictionary |
Dictionary object. More... | |
Enumerations | |
enum | _line_status_ { LINE_UNPROCESSED, LINE_ERROR, LINE_EMPTY, LINE_COMMENT, LINE_SECTION, LINE_VALUE } |
This enum stores the status for each parsed line (internal use only). | |
Functions | |
int | ciniparser_getnsec (dictionary *d) |
Get number of sections in a dictionary. More... | |
const char * | ciniparser_getsecname (dictionary *d, int n) |
Get name for section n in a dictionary. More... | |
void | ciniparser_dump (dictionary *d, FILE *f) |
Dump a dictionary to an opened file pointer. More... | |
void | ciniparser_dump_ini (dictionary *d, FILE *f) |
Save a dictionary to a loadable ini file. More... | |
const char * | ciniparser_getstring (dictionary *d, const char *key, char *def) |
Get the string associated to a key. More... | |
int | ciniparser_getint (dictionary *d, const char *key, int notfound) |
Get the string associated to a key, convert to an int. More... | |
double | ciniparser_getdouble (dictionary *d, const char *key, double notfound) |
Get the string associated to a key, convert to a double. More... | |
int | ciniparser_getboolean (dictionary *d, const char *key, int notfound) |
Get the string associated to a key, convert to a boolean. More... | |
int | ciniparser_find_entry (dictionary *ini, const char *entry) |
Finds out if a given entry exists in a dictionary. More... | |
int | ciniparser_set (dictionary *d, const char *entry, const char *val) |
Set an item in the dictionary. More... | |
void | ciniparser_unset (dictionary *ini, char *entry) |
Delete an entry in a dictionary. More... | |
dictionary * | ciniparser_load (const char *ininame) |
Parse an ini file and return an allocated dictionary object. More... | |
void | ciniparser_freedict (dictionary *d) |
Free all memory associated to an ini dictionary. More... | |
int | ciniparser_setstring (dictionary *ini, char const *entry, const char *val) |
Set an entry in a dictionary. More... | |
unsigned | dictionary_hash (const char *key) |
Compute the hash key for a string. More... | |
dictionary * | dictionary_new (int size) |
Create a new dictionary object. More... | |
void | dictionary_del (dictionary *vd) |
Delete a dictionary object. More... | |
const char * | dictionary_get (dictionary *d, const char *key, const char *def) |
Get a value from a dictionary. More... | |
int | dictionary_set (dictionary *vd, const char *key, const char *val) |
Set a value in a dictionary. More... | |
void | dictionary_unset (dictionary *d, const char *key) |
Delete a key in a dictionary. More... | |
void | dictionary_dump (dictionary *d, FILE *out) |
Dump a dictionary to an opened file pointer. More... | |
#define MAXVALSZ 1024 |
Maximum value size for integers and doubles.
Definition at line 47 of file dictionary.c.
typedef struct _dictionary_ dictionary |
Dictionary object.
n | Number of entries in the dictionary |
size | Storage size |
val | List of string values |
key | List of string keys |
hash | List of hash values for keys |
This object contains a list of string/string associations. Each association is identified by a unique string key. Looking up values in the dictionary is speeded up by the use of a (hopefully collision-free) hash function.
void ciniparser_dump | ( | dictionary * | d, |
FILE * | f | ||
) |
Dump a dictionary to an opened file pointer.
d | Dictionary to dump. |
f | Opened file pointer to dump to. |
This function prints out the contents of a dictionary, one element by line, onto the provided file pointer. It is OK to specify stderr
or stdout
as output files. This function is meant for debugging purposes mostly.
Definition at line 227 of file ciniparser.c.
void ciniparser_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 |
This function dumps a given dictionary into a loadable ini file. It is Ok to specify stderr
or stdout
as output files.
Definition at line 244 of file ciniparser.c.
int ciniparser_find_entry | ( | dictionary * | ini, |
const char * | entry | ||
) |
Finds out if a given entry exists in a dictionary.
ini | Dictionary to search |
entry | Name of the entry to look for |
Finds out if a given entry exists in the dictionary. Since sections are stored as keys with NULL associated values, this is the only way of querying for the presence of sections in a dictionary.
Definition at line 348 of file ciniparser.c.
void ciniparser_freedict | ( | dictionary * | d | ) |
Free all memory associated to an ini dictionary.
d | Dictionary to free |
Free all memory associated to an ini dictionary. It is mandatory to call this function before the dictionary object gets out of the current context.
Definition at line 468 of file ciniparser.c.
int ciniparser_getboolean | ( | dictionary * | d, |
const 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 |
This function queries a dictionary for a key. A key as read from an ini file is given as "section:key". If the key cannot be found, the notfound value is returned.
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 324 of file ciniparser.c.
double ciniparser_getdouble | ( | dictionary * | d, |
const 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 |
This function queries a dictionary for a key. A key as read from an ini file is given as "section:key". If the key cannot be found, the notfound value is returned.
Definition at line 312 of file ciniparser.c.
int ciniparser_getint | ( | dictionary * | d, |
const 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 |
This function queries a dictionary for a key. A key as read from an ini file is given as "section:key". If the key cannot be found, the notfound value is returned.
Supported values for integers include the usual C notation so decimal, octal (starting with 0) and hexadecimal (starting with 0x) are supported. Examples:
Warning: the conversion may overflow in various ways. Conversion is totally outsourced to strtol(), see the associated man page for overflow handling.
Credits: Thanks to A. Becker for suggesting strtol()
Definition at line 300 of file ciniparser.c.
int ciniparser_getnsec | ( | dictionary * | d | ) |
Get number of sections in a dictionary.
d | Dictionary to examine |
This function returns the number of sections found in a dictionary. The test to recognize sections is done on the string stored in the dictionary: a section name is given as "section" whereas a key is stored as "section:key", thus the test looks for entries that do not contain a colon.
This clearly fails in the case a section name contains a colon, but this should simply be avoided.
Definition at line 179 of file ciniparser.c.
const char * ciniparser_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 locates the n-th section in a dictionary and returns its name as a pointer to a string statically allocated inside the dictionary. Do not free or modify the returned string!
Definition at line 198 of file ciniparser.c.
const char * ciniparser_getstring | ( | dictionary * | d, |
const 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. |
This function queries a dictionary for a key. A key as read from an ini file is given as "section:key". If the key cannot be found, the pointer passed as 'def' is returned. The returned char pointer is pointing to a string allocated in the dictionary, do not free or modify it.
Definition at line 286 of file ciniparser.c.
dictionary * ciniparser_load | ( | const char * | ininame | ) |
Parse an ini file and return an allocated dictionary object.
ininame | Name of the ini file to read. |
This is the parser for ini files. This function is called, providing the name of the file to be read. It returns a dictionary object that should not be accessed directly, but through accessor functions instead.
The returned dictionary must be freed using ciniparser_freedict().
Definition at line 368 of file ciniparser.c.
int ciniparser_set | ( | dictionary * | d, |
const char * | entry, | ||
const char * | val | ||
) |
Set an item in the dictionary.
d | Dictionary object created by ciniparser_load() |
entry | Entry in the dictionary to manipulate |
val | Value to assign to the entry |
Remember that string values are converted by ciniparser_getboolean(), ciniparser_getdouble(), etc. It is also OK to set an entry to NULL.
Definition at line 358 of file ciniparser.c.
int ciniparser_setstring | ( | dictionary * | ini, |
char const * | entry, | ||
const 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. |
If the given entry can be found in the dictionary, it is modified to contain the provided value. If it cannot be found, -1 is returned. It is Ok to set val to NULL.
void ciniparser_unset | ( | dictionary * | ini, |
char * | entry | ||
) |
Delete an entry in a dictionary.
ini | Dictionary to modify |
entry | Entry to delete (entry name) |
If the given entry can be found, it is deleted from the dictionary.
Definition at line 363 of file ciniparser.c.
void dictionary_del | ( | dictionary * | vd | ) |
Delete a dictionary object.
d | dictionary object to deallocate. |
Deallocate a dictionary object and all memory associated to it.
Definition at line 109 of file dictionary.c.
void dictionary_dump | ( | dictionary * | d, |
FILE * | out | ||
) |
Dump a dictionary to an opened file pointer.
d | Dictionary to dump |
out | Opened file pointer |
Dumps a dictionary onto an opened file pointer. Key pairs are printed out as [Key]=[Value], one per line. It is Ok to provide stdout or stderr as output file pointers.
Definition at line 241 of file dictionary.c.
const char * dictionary_get | ( | dictionary * | d, |
const char * | key, | ||
const char * | def | ||
) |
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. |
This function locates a key in a dictionary and returns a pointer to its value, or the passed 'def' pointer if no such key can be found in dictionary. The returned character pointer points to data internal to the dictionary object, you should not try to free it or modify it.
Definition at line 128 of file dictionary.c.
unsigned dictionary_hash | ( | const char * | key | ) |
Compute the hash key for a string.
key | Character string to use for key. |
This hash function has been taken from an Article in Dr Dobbs Journal. This is normally a collision-free function, distributing keys evenly. The key is stored anyway in the struct so that collision can be avoided by comparing the key itself in last resort.
Definition at line 74 of file dictionary.c.
dictionary * dictionary_new | ( | int | size | ) |
Create a new dictionary object.
size | Optional initial size of the dictionary. |
This function allocates a new dictionary object of given size and returns it. If you do not know in advance (roughly) the number of entries in the dictionary, give size=0.
Definition at line 92 of file dictionary.c.
int dictionary_set | ( | dictionary * | vd, |
const char * | key, | ||
const char * | val | ||
) |
Set a value in a dictionary.
d | dictionary object to modify. |
key | Key to modify or add. |
val | Value to add. |
If the given key is found in the dictionary, the associated value is replaced by the provided one. If the key cannot be found in the dictionary, it is added to it.
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.
This function returns non-zero in case of failure.
Definition at line 147 of file dictionary.c.
void dictionary_unset | ( | dictionary * | d, |
const char * | key | ||
) |
Delete a key in a dictionary.
d | dictionary object to modify. |
key | Key to remove. |
This function deletes a key in a dictionary. Nothing is done if the key cannot be found.
Definition at line 206 of file dictionary.c.