summaryrefslogtreecommitdiff
path: root/src/dictionary.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/dictionary.h')
-rw-r--r--src/dictionary.h133
1 files changed, 37 insertions, 96 deletions
diff --git a/src/dictionary.h b/src/dictionary.h
index b332680..965dfd6 100644
--- a/src/dictionary.h
+++ b/src/dictionary.h
@@ -3,8 +3,6 @@
/**
@file dictionary.h
@author N. Devillard
- @date Aug 2000
- @version $Revision: 1.11 $
@brief Implements a dictionary for string variables.
This module implements a simple dictionary object, i.e. a list
@@ -13,18 +11,11 @@
*/
/*--------------------------------------------------------------------------*/
-/*
- $Id: dictionary.h,v 1.11 2002-06-17 09:30:46 ndevilla Exp $
- $Author: ndevilla $
- $Date: 2002-06-17 09:30:46 $
- $Revision: 1.11 $
-*/
-
#ifndef _DICTIONARY_H_
#define _DICTIONARY_H_
/*---------------------------------------------------------------------------
- Includes
+ Includes
---------------------------------------------------------------------------*/
#include <stdio.h>
@@ -32,14 +23,18 @@
#include <string.h>
#include <unistd.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+
/*---------------------------------------------------------------------------
- New types
+ New types
---------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/**
- @brief Dictionary object
+ @brief Dictionary object
This object contains a list of string/string associations. Each
association is identified by a unique string key. Looking up values
@@ -48,16 +43,16 @@
*/
/*-------------------------------------------------------------------------*/
typedef struct _dictionary_ {
- int n ; /** Number of entries in dictionary */
- int size ; /** Storage size */
- char ** val ; /** List of string values */
- char ** key ; /** List of string keys */
- unsigned * hash ; /** List of hash values for keys */
+ int n ; /** Number of entries in dictionary */
+ int size ; /** Storage size */
+ char ** val ; /** List of string values */
+ char ** key ; /** List of string keys */
+ unsigned * hash ; /** List of hash values for keys */
} dictionary ;
/*---------------------------------------------------------------------------
- Function prototypes
+ Function prototypes
---------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
@@ -72,7 +67,7 @@ typedef struct _dictionary_ {
by comparing the key itself in last resort.
*/
/*--------------------------------------------------------------------------*/
-unsigned dictionary_hash(char * key);
+unsigned dictionary_hash(const char * key);
/*-------------------------------------------------------------------------*/
/**
@@ -85,7 +80,7 @@ unsigned dictionary_hash(char * key);
dictionary, give size=0.
*/
/*--------------------------------------------------------------------------*/
-dictionary * dictionary_new(int size);
+dictionary * dictionary_new(size_t size);
/*-------------------------------------------------------------------------*/
/**
@@ -112,52 +107,8 @@ void dictionary_del(dictionary * vd);
dictionary object, you should not try to free it or modify it.
*/
/*--------------------------------------------------------------------------*/
-char * dictionary_get(dictionary * d, char * key, char * def);
-
-
-/*-------------------------------------------------------------------------*/
-/**
- @brief Get a value from a dictionary, as a char.
- @param d dictionary object to search.
- @param key Key to look for in the dictionary.
- @param def Default value for the key if not found.
- @return char
-
- This function locates a key in a dictionary using dictionary_get,
- and returns the first char of the found string.
- */
-/*--------------------------------------------------------------------------*/
-char dictionary_getchar(dictionary * d, char * key, char def) ;
-
-/*-------------------------------------------------------------------------*/
-/**
- @brief Get a value from a dictionary, as an int.
- @param d dictionary object to search.
- @param key Key to look for in the dictionary.
- @param def Default value for the key if not found.
- @return int
-
- This function locates a key in a dictionary using dictionary_get,
- and applies atoi on it to return an int. If the value cannot be found
- in the dictionary, the default is returned.
- */
-/*--------------------------------------------------------------------------*/
-int dictionary_getint(dictionary * d, char * key, int def);
-
-/*-------------------------------------------------------------------------*/
-/**
- @brief Get a value from a dictionary, as a double.
- @param d dictionary object to search.
- @param key Key to look for in the dictionary.
- @param def Default value for the key if not found.
- @return double
+char * dictionary_get(dictionary * d, const char * key, char * def);
- This function locates a key in a dictionary using dictionary_get,
- and applies atof on it to return a double. If the value cannot be found
- in the dictionary, the default is returned.
- */
-/*--------------------------------------------------------------------------*/
-double dictionary_getdouble(dictionary * d, char * key, double def);
/*-------------------------------------------------------------------------*/
/**
@@ -165,7 +116,7 @@ double dictionary_getdouble(dictionary * d, char * key, double def);
@param d dictionary object to modify.
@param key Key to modify or add.
@param val Value to add.
- @return void
+ @return 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
@@ -181,9 +132,11 @@ double dictionary_getdouble(dictionary * d, char * key, double def);
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.
*/
/*--------------------------------------------------------------------------*/
-void dictionary_set(dictionary * vd, char * key, char * val);
+int dictionary_set(dictionary * vd, const char * key, const char * val);
/*-------------------------------------------------------------------------*/
/**
@@ -196,36 +149,8 @@ void dictionary_set(dictionary * vd, char * key, char * val);
key cannot be found.
*/
/*--------------------------------------------------------------------------*/
-void dictionary_unset(dictionary * d, char * key);
-
-
-/*-------------------------------------------------------------------------*/
-/**
- @brief Set a key in a dictionary, providing an int.
- @param d Dictionary to update.
- @param key Key to modify or add
- @param val Integer value to store (will be stored as a string).
- @return void
-
- This helper function calls dictionary_set() with the provided integer
- converted to a string using %d.
- */
-/*--------------------------------------------------------------------------*/
-void dictionary_setint(dictionary * d, char * key, int val);
-
-/*-------------------------------------------------------------------------*/
-/**
- @brief Set a key in a dictionary, providing a double.
- @param d Dictionary to update.
- @param key Key to modify or add
- @param val Double value to store (will be stored as a string).
- @return void
+void dictionary_unset(dictionary * d, const char * key);
- This helper function calls dictionary_set() with the provided double
- converted to a string using %g.
- */
-/*--------------------------------------------------------------------------*/
-void dictionary_setdouble(dictionary * d, char * key, double val);
/*-------------------------------------------------------------------------*/
/**
@@ -241,4 +166,20 @@ void dictionary_setdouble(dictionary * d, char * key, double val);
/*--------------------------------------------------------------------------*/
void dictionary_dump(dictionary * d, FILE * out);
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Duplicate a string
+ @param s String to duplicate
+ @return Pointer to a newly allocated string, to be freed with free()
+
+ This is a replacement for strdup(). This implementation is provided
+ for systems that do not have it.
+ */
+/*--------------------------------------------------------------------------*/
+char * xstrdup(const char * s);
+
+#ifdef __cplusplus
+}
+#endif
+
#endif