Robot Agent  1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Macros | Functions
dictionary.c File Reference

Implements a dictionary for string variables. More...

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

Go to the source code of this file.

Macros

#define MAXVALSZ   1024
 
#define DICTMINSZ   128
 
#define DICT_INVALID_KEY   ((char*)-1)
 

Functions

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 *d)
 Delete a dictionary object. More...
 
char * dictionary_get (dictionary *d, const char *key, char *def)
 Get a value from a dictionary. More...
 
int dictionary_set (dictionary *d, 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

Implements a dictionary for string variables.

Author
N. Devillard This module implements a simple dictionary object, i.e. a list of string/string associations. This object is useful to store e.g. informations retrieved from a configuration file (ini files).

Definition in file dictionary.c.

Macro Definition Documentation

#define DICT_INVALID_KEY   ((char*)-1)

Invalid key token

Definition at line 30 of file dictionary.c.

#define DICTMINSZ   128

Minimal allocated number of entries in a dictionary

Definition at line 27 of file dictionary.c.

#define MAXVALSZ   1024

Maximum value size for integers and doubles.

Definition at line 24 of file dictionary.c.

Function Documentation

void dictionary_del ( dictionary d)

Delete a dictionary object.

Parameters
ddictionary object to deallocate.
Returns
void

Deallocate a dictionary object and all memory associated to it.

Definition at line 143 of file dictionary.c.

void dictionary_dump ( dictionary d,
FILE *  out 
)

Dump a dictionary to an opened file pointer.

Parameters
dDictionary to dump
fOpened 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 337 of file dictionary.c.

char* dictionary_get ( dictionary d,
const char *  key,
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 175 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 88 of file dictionary.c.

dictionary* dictionary_new ( int  size)

Create a new dictionary object.

Parameters
sizeOptional initial size of the dictionary.
Returns
1 newly allocated dictionary objet.

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 117 of file dictionary.c.

int dictionary_set ( dictionary d,
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 221 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 288 of file dictionary.c.