LIRC libraries
LinuxInfraredRemoteControl
Ciniparser

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...
 
dictionaryciniparser_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...
 
dictionarydictionary_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...
 

Detailed Description

Macro Definition Documentation

#define MAXVALSZ   1024

Maximum value size for integers and doubles.

Definition at line 47 of file dictionary.c.

Typedef Documentation

typedef struct _dictionary_ dictionary

Dictionary object.

Parameters
nNumber of entries in the dictionary
sizeStorage size
valList of string values
keyList of string keys
hashList 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.

Function Documentation

void ciniparser_dump ( dictionary d,
FILE *  f 
)

Dump a dictionary to an opened file pointer.

Parameters
dDictionary to dump.
fOpened file pointer to dump to.
Returns
void

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.

Parameters
dDictionary to dump
fOpened file pointer to dump to
Returns
void

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.

Parameters
iniDictionary to search
entryName of the entry to look for
Returns
integer 1 if entry exists, 0 otherwise

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.

Parameters
dDictionary to free
Returns
void

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.

Parameters
dDictionary to search
keyKey string to look for
notfoundValue to return in case of error
Returns
integer

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 string starting with 'y'
  • A string starting with 'Y'
  • A string starting with 't'
  • A string starting with 'T'
  • A string starting with '1'

A false boolean is found if one of the following is matched:

  • A string starting with 'n'
  • A string starting with 'N'
  • A string starting with 'f'
  • A string starting with 'F'
  • A string starting with '0'

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.

Parameters
dDictionary to search
keyKey string to look for
notfoundValue to return in case of error
Returns
double

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.

Parameters
dDictionary to search
keyKey string to look for
notfoundValue to return in case of error
Returns
integer

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:

  • "42" -> 42
  • "042" -> 34 (octal -> decimal)
  • "0x42" -> 66 (hexa -> decimal)

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.

Parameters
dDictionary to examine
Returns
int Number of sections found in dictionary, -1 on error

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.

Parameters
dDictionary to examine
nSection number (from 0 to nsec-1).
Returns
Pointer to char string, NULL on error

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.

Parameters
dDictionary to search
keyKey string to look for
defDefault value to return if key not found.
Returns
pointer to statically allocated character string

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.

Parameters
ininameName of the ini file to read.
Returns
Pointer to newly allocated dictionary

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.

Parameters
dDictionary object created by ciniparser_load()
entryEntry in the dictionary to manipulate
valValue to assign to the entry
Returns
0 on success, -1 on error

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.

Parameters
iniDictionary to modify.
entryEntry to modify (entry name)
valNew value to associate to the entry.
Returns
int 0 if Ok, -1 otherwise.

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.

Parameters
iniDictionary to modify
entryEntry to delete (entry name)
Returns
void

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.

Parameters
ddictionary object to deallocate.
Returns
void

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.

Parameters
dDictionary to dump
outOpened file pointer
Returns
void

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.

Parameters
ddictionary object to search.
keyKey to look for in the dictionary.
defDefault value to return if key not found.
Returns
1 pointer to internally allocated character string.

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.

Parameters
keyCharacter string to use for key.
Returns
1 unsigned int on at least 32 bits.

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.

Parameters
sizeOptional initial size of the dictionary.
Returns
allocated dictionary object on success, NULL on failure

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.

Parameters
ddictionary object to modify.
keyKey to modify or add.
valValue to add.
Returns
int 0 if Ok, anything else otherwise

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.

Parameters
ddictionary object to modify.
keyKey to remove.
Returns
void

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.