![]() |
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). | |
Data Structures | |
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 |
#define | DICTMINSZ 128 |
#define | DICT_INVALID_KEY ((char*)-1) |
Typedefs | |
typedef enum _line_status_ | line_status |
typedef struct _dictionary_ | dictionary |
Dictionary object. More... | |
Enumerations | |
enum | _line_status_ { LINE_UNPROCESSED, LINE_ERROR, LINE_EMPTY, LINE_COMMENT, LINE_SECTION, LINE_VALUE } |
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 DICT_INVALID_KEY ((char*)-1) |
Invalid key token
Definition at line 53 of file dictionary.c.
#define DICTMINSZ 128 |
Minimal allocated number of entries in a dictionary
Definition at line 50 of file dictionary.c.
#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.
typedef enum _line_status_ line_status |
This enum stores the status for each parsed line (internal use only).
enum _line_status_ |
This enum stores the status for each parsed line (internal use only).
Definition at line 44 of file ciniparser.c.
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 229 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 249 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 355 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 475 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 331 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 319 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 307 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 199 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 293 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 376 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 366 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 371 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 110 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 245 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 129 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 75 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 93 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 149 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 209 of file dictionary.c.