27,10 → 27,11 |
*/ |
|
/** @addtogroup net |
* @{ |
* @{ |
*/ |
|
/** @file |
* A character string to integer map implementation file. |
*/ |
|
#include <errno.h> |
40,21 → 41,48 |
|
#include "char_map.h" |
|
/** An internal magic value for a consistency check. |
*/ |
#define CHAR_MAP_MAGIC_VALUE 0x12345611 |
|
int char_map_add_item( char_map_ref map, const char * identifier, const int value ); |
char_map_ref char_map_find_node( char_map_ref map, const char * identifier ); |
int char_map_is_valid( char_map_ref map ); |
/** Adds the value with the key to the map. |
* Creates new nodes to map the key. |
* @param map The character string to integer map. Input/output parameter. |
* @param identifier The key zero ('\0') terminated character string. The key character string is processed until the first terminating zero ('\0') character after the given length is found. Input parameter. |
* @param length The key character string length. The parameter may be zero (0) which means that the string is processed until the terminating zero ('\0') character is found. Input parameter. |
* @param value The integral value to be stored for the key character string. Input parameter. |
* @returns EOK on success. |
* @returns ENOMEM if there is no memory left. |
* @returns EEXIST if the key character string is already used. |
*/ |
int char_map_add_item( char_map_ref map, const char * identifier, size_t length, const int value ); |
|
int char_map_add( char_map_ref map, const char * identifier, const int value ){ |
if( char_map_is_valid( map ) && ( * identifier )){ |
/** Returns the node assigned to the key from the map. |
* @param map The character string to integer map. Input parameter. |
* @param identifier The key zero ('\0') terminated character string. The key character string is processed until the first terminating zero ('\0') character after the given length is found. Input parameter. |
* @param length The key character string length. The parameter may be zero (0) which means that the string is processed until the terminating zero ('\0') character is found. Input parameter. |
* @returns The node holding the integral value assigned to the key character string. |
* @returns NULL if the key is not assigned a node. |
*/ |
char_map_ref char_map_find_node( const char_map_ref map, const char * identifier, const size_t length ); |
|
/** Checks if the map is valid. |
* @param map The character string to integer map. Input parameter. |
* @returns TRUE if the map is valid. |
* @returns FALSE otherwise. |
*/ |
int char_map_is_valid( const char_map_ref map ); |
|
int char_map_add( char_map_ref map, const char * identifier, size_t length, const int value ){ |
if( char_map_is_valid( map ) && ( identifier ) && (( length ) || ( * identifier ))){ |
int index; |
|
for( index = 0; index < map->next; ++ index ){ |
if( map->items[ index ]->c == * identifier ){ |
++ identifier; |
if( * identifier ){ |
return char_map_add( map->items[ index ], identifier, value ); |
if( length ) -- length; |
if( length || ( * identifier )){ |
return char_map_add( map->items[ index ], identifier, length, value ); |
}else{ |
if( map->items[ index ]->value != CHAR_MAP_NULL ) return EEXISTS; |
map->items[ index ]->value = value; |
62,12 → 90,12 |
} |
} |
} |
return char_map_add_item( map, identifier, value ); |
return char_map_add_item( map, identifier, length, value ); |
} |
return EINVAL; |
} |
|
int char_map_add_item( char_map_ref map, const char * identifier, const int value ){ |
int char_map_add_item( char_map_ref map, const char * identifier, size_t length, const int value ){ |
if( map->next == ( map->size - 1 )){ |
char_map_ref * tmp; |
|
83,13 → 111,14 |
char_map_initialize( map->items[ map->next ] ); |
map->items[ map->next ]->c = * identifier; |
++ identifier; |
if( * identifier ){ |
map->items[ map->next ]->value = CHAR_MAP_NULL; |
char_map_add_item( map->items[ map->next ], identifier, value ); |
if( length ) -- length; |
++ map->next; |
if( length || ( * identifier )){ |
map->items[ map->next - 1 ]->value = CHAR_MAP_NULL; |
return char_map_add_item( map->items[ map->next - 1 ], identifier, length, value ); |
}else{ |
map->items[ map->next ]->value = value; |
map->items[ map->next - 1 ]->value = value; |
} |
++ map->next; |
return EOK; |
} |
|
105,10 → 134,10 |
} |
} |
|
int char_map_exclude( char_map_ref map, const char * identifier ){ |
int char_map_exclude( char_map_ref map, const char * identifier, size_t length ){ |
char_map_ref node; |
|
node = char_map_find_node( map, identifier ); |
node = char_map_find_node( map, identifier, length ); |
if( node ){ |
int value; |
|
119,22 → 148,23 |
return CHAR_MAP_NULL; |
} |
|
int char_map_find( char_map_ref map, const char * identifier ){ |
int char_map_find( const char_map_ref map, const char * identifier, size_t length ){ |
char_map_ref node; |
|
node = char_map_find_node( map, identifier ); |
node = char_map_find_node( map, identifier, length ); |
return node ? node->value : CHAR_MAP_NULL; |
} |
|
char_map_ref char_map_find_node( char_map_ref map, const char * identifier ){ |
char_map_ref char_map_find_node( const char_map_ref map, const char * identifier, size_t length ){ |
if( ! char_map_is_valid( map )) return NULL; |
if( * identifier ){ |
if( length ) -- length; |
if( length || ( * identifier )){ |
int index; |
|
for( index = 0; index < map->next; ++ index ){ |
if( map->items[ index ]->c == * identifier ){ |
++ identifier; |
return char_map_find_node( map->items[ index ], identifier ); |
return char_map_find_node( map->items[ index ], identifier, length ); |
} |
} |
return NULL; |
142,7 → 172,7 |
return map; |
} |
|
int char_map_get_value( char_map_ref map ){ |
int char_map_get_value( const char_map_ref map ){ |
return char_map_is_valid( map ) ? map->value : CHAR_MAP_NULL; |
} |
|
159,20 → 189,20 |
return EOK; |
} |
|
int char_map_is_valid( char_map_ref map ){ |
int char_map_is_valid( const char_map_ref map ){ |
return map && ( map->magic == CHAR_MAP_MAGIC_VALUE ); |
} |
|
int char_map_update( char_map_ref map, const char * identifier, const int value ){ |
int char_map_update( char_map_ref map, const char * identifier, const size_t length, const int value ){ |
char_map_ref node; |
|
// if( ! char_map_is_valid( map )) return EINVAL; |
node = char_map_find_node( map, identifier ); |
node = char_map_find_node( map, identifier, length ); |
if( node ){ |
node->value = value; |
return EOK; |
}else{ |
return char_map_add( map, identifier, value ); |
return char_map_add( map, identifier, length, value ); |
} |
} |
|