Simple data structures / objects in plain C Snapshot
properties.h
Go to the documentation of this file.
00001 #ifndef __BHASHPROPS_H__
00002 #define __BHASHPROPS_H__
00003 
00004 #include <cutils/bhash.h>
00005 #include <cutils/strtoint.h>
00006 #include <ctype.h>
00007 
00008 /**
00009  * @defgroup PROPERTIES
00010  * @brief implements the commonly used idiom of a map from names to values (strings to strings), a hash with a signle values (not multimap).
00011  *
00012  * Also supported is reading the properties from a file ( format just like java.lang.Properties )
00013  * @{
00014  */
00015 
00016 typedef struct tagNAMEVALUE_HASH_ENTRY  {
00017   HASH_Entry entry;
00018 
00019   char *name;
00020   char *value;
00021 
00022 } NAMEVALUE_HASH_ENTRY;
00023 
00024 
00025 typedef struct tagPROPERTIES {
00026   HASH props;
00027 } PROPERTIES;
00028 
00029 /**
00030  * @brief initialise the object 
00031  */
00032 int PROPERTIES_init( PROPERTIES *props, int num_buckets );
00033 
00034 /**
00035  * @brief loads properties from file, a java properties text file. 
00036  */
00037 int PROPERTIES_load(PROPERTIES *props, const char *file_name);
00038 
00039 /**
00040  * @brief free a properties object
00041  */
00042 void PROPERTIES_free( PROPERTIES *props );
00043 
00044 
00045 /**
00046  * @brief add a name to value mapping
00047  */
00048 int PROPERTIES_put( PROPERTIES *props, const char *name , const char *value );
00049 
00050 
00051 
00052 /**
00053  * @brief get the value for a name, return 0 if no such mapping exists.
00054  */
00055 const char * PROPERTIES_get( PROPERTIES *props, const char *name );
00056 
00057 
00058 #define MAKE_PROPERTIES_DECIMAL_INT_GET( type ) \
00059 M_INLINE int PROPERTIES_dec_##type (PROPERTIES *props, const char *name, type *rret) \
00060 { \
00061   const char *value; \
00062   char *eptr; \
00063   type ret; \
00064   \
00065   value = PROPERTIES_get( props, name ); \
00066   if (!value) { \
00067     return -1; \
00068   } \
00069   for( ;isspace(*value); ++value ); \
00070  \
00071   errno = 0; \
00072    \
00073   ret = strto_##type ( value, &eptr, 10 ); \
00074  \
00075   if (errno) {  \
00076     return -2; \
00077   } \
00078  \
00079   if (*eptr == '\0' || isspace(*eptr)) { \
00080     *rret = ( type ) ret; \
00081     return 0; \
00082   } \
00083   return -3; \
00084 }
00085 
00086 MAKE_PROPERTIES_DECIMAL_INT_GET( int16_t )
00087 MAKE_PROPERTIES_DECIMAL_INT_GET( uint16_t )
00088 MAKE_PROPERTIES_DECIMAL_INT_GET( int32_t )
00089 MAKE_PROPERTIES_DECIMAL_INT_GET( uint32_t )
00090 MAKE_PROPERTIES_DECIMAL_INT_GET( int64_t )
00091 MAKE_PROPERTIES_DECIMAL_INT_GET( uint64_t )
00092 
00093 #define MAKE_PROPERTIES_HEX_INT_GET( type ) \
00094 M_INLINE int PROPERTIES_hex_##type (PROPERTIES *props, const char *name, type *rret) \
00095 { \
00096   const char *value; \
00097   char *eptr; \
00098   type ret; \
00099   \
00100   value = PROPERTIES_get( props, name ); \
00101   if (!value) { \
00102     return -1; \
00103   } \
00104   for( ;isspace(*value); ++value ); \
00105  \
00106   errno = 0; \
00107    \
00108   ret = strto_##type ( value, &eptr, 16 ); \
00109  \
00110   if (errno) {  \
00111     return -2; \
00112   } \
00113   if (*eptr == '\0' || isspace(*eptr)) { \
00114     *rret = ( type ) ret; \
00115     return 0; \
00116   } \
00117   return -3; \
00118  \
00119 }
00120 
00121 MAKE_PROPERTIES_HEX_INT_GET( int16_t )
00122 MAKE_PROPERTIES_HEX_INT_GET( uint16_t )
00123 MAKE_PROPERTIES_HEX_INT_GET( int32_t )
00124 MAKE_PROPERTIES_HEX_INT_GET( uint32_t )
00125 MAKE_PROPERTIES_HEX_INT_GET( int64_t )
00126 MAKE_PROPERTIES_HEX_INT_GET( uint64_t )
00127 
00128 
00129 
00130 /**
00131  * @}
00132  */
00133 
00134 #endif
00135 
00136