Simple data structures / objects in plain C Snapshot
Classes | Defines | Typedefs | Functions
PROPERTIES

implements the commonly used idiom of a map from names to values (strings to strings), a hash with a signle values (not multimap). More...

Classes

struct  tagNAMEVALUE_HASH_ENTRY
struct  tagPROPERTIES

Defines

#define MAKE_PROPERTIES_DECIMAL_INT_GET(type)
#define MAKE_PROPERTIES_HEX_INT_GET(type)

Typedefs

typedef struct
tagNAMEVALUE_HASH_ENTRY 
NAMEVALUE_HASH_ENTRY
typedef struct tagPROPERTIES PROPERTIES

Functions

int PROPERTIES_init (PROPERTIES *props, int num_buckets)
 initialise the object
int PROPERTIES_load (PROPERTIES *props, const char *file_name)
 loads properties from file, a java properties text file.
void PROPERTIES_free (PROPERTIES *props)
 free a properties object
int PROPERTIES_put (PROPERTIES *props, const char *name, const char *value)
 add a name to value mapping
const char * PROPERTIES_get (PROPERTIES *props, const char *name)
 get the value for a name, return 0 if no such mapping exists.

Detailed Description

implements the commonly used idiom of a map from names to values (strings to strings), a hash with a signle values (not multimap).

Also supported is reading the properties from a file ( format just like java.lang.Properties )


Define Documentation

#define MAKE_PROPERTIES_DECIMAL_INT_GET (   type)
Value:
M_INLINE int PROPERTIES_dec_##type (PROPERTIES *props, const char *name, type *rret) \
{ \
  const char *value; \
  char *eptr; \
  type ret; \
  \
  value = PROPERTIES_get( props, name ); \
  if (!value) { \
    return -1; \
  } \
  for( ;isspace(*value); ++value ); \
 \
  errno = 0; \
   \
  ret = strto_##type ( value, &eptr, 10 ); \
 \
  if (errno) {  \
    return -2; \
  } \
 \
  if (*eptr == '\0' || isspace(*eptr)) { \
    *rret = ( type ) ret; \
    return 0; \
  } \
  return -3; \
}

Definition at line 58 of file properties.h.

#define MAKE_PROPERTIES_HEX_INT_GET (   type)
Value:
M_INLINE int PROPERTIES_hex_##type (PROPERTIES *props, const char *name, type *rret) \
{ \
  const char *value; \
  char *eptr; \
  type ret; \
  \
  value = PROPERTIES_get( props, name ); \
  if (!value) { \
    return -1; \
  } \
  for( ;isspace(*value); ++value ); \
 \
  errno = 0; \
   \
  ret = strto_##type ( value, &eptr, 16 ); \
 \
  if (errno) {  \
    return -2; \
  } \
  if (*eptr == '\0' || isspace(*eptr)) { \
    *rret = ( type ) ret; \
    return 0; \
  } \
  return -3; \
 \
}

Definition at line 93 of file properties.h.


Typedef Documentation

typedef struct tagPROPERTIES PROPERTIES

Function Documentation

void PROPERTIES_free ( PROPERTIES props)

free a properties object

Definition at line 34 of file properties.c.

{
   HASH_Entry *cur;
   NAMEVALUE_HASH_ENTRY *entry;

   HASH_DELETEALL(cur, &props->props);
     entry = (NAMEVALUE_HASH_ENTRY *) cur;
     free(entry->name);
     free(entry->value);
     free(entry);
   HASH_DELETEALL_END
}
const char* PROPERTIES_get ( PROPERTIES props,
const char *  name 
)

get the value for a name, return 0 if no such mapping exists.

Definition at line 82 of file properties.c.

{
  NAMEVALUE_HASH_ENTRY *value;

  value = (NAMEVALUE_HASH_ENTRY *) HASH_find( &props->props, (void *) name, -1 );
  if (!value) {
    return 0;
  }
  return value->value;
}
int PROPERTIES_init ( PROPERTIES props,
int  num_buckets 
)

initialise the object

Definition at line 29 of file properties.c.

{
  return HASH_init( &props->props, num_buckets, 0, props_compare_key, 0 );
}
int PROPERTIES_load ( PROPERTIES props,
const char *  file_name 
)

loads properties from file, a java properties text file.

Definition at line 96 of file properties.c.

{
   if (PROPERTIES_init( props, 30 )) {
     return -1;
   }
   return load_properties( props, file_name );
}
int PROPERTIES_put ( PROPERTIES props,
const char *  name,
const char *  value 
)

add a name to value mapping

Definition at line 48 of file properties.c.

{
  NAMEVALUE_HASH_ENTRY *val;

  val = (NAMEVALUE_HASH_ENTRY *) malloc( sizeof(  NAMEVALUE_HASH_ENTRY ) ); 
  if (!val) {
    return -1;
  }
  val->value = val->name = 0;

  val->name = strdup( name );
  if (!val->name) {
    goto err;
  }
  val->value = strdup( value );
  if (!val->value) {
    goto err;
  }
  if (HASH_insert( &props->props, &val->entry, (void *)  name, -1 )) {
    goto err;
  }
  return 0;
err:
  if (val->value) {
    free( val->value );
  }
  if (val->name) {
    free( val->name );
  }
  free( val );
  return 0;

}