0,0 → 1,180 |
/* |
* Copyright (c) 2008 Lukas Mejdrech |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
|
/** @addtogroup net |
* @{ |
*/ |
|
/** @file |
*/ |
|
#include <errno.h> |
#include <malloc.h> |
#include <unistd.h> |
#include <string.h> |
|
#include "char_map.h" |
|
#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 ); |
|
int char_map_add( char_map_ref map, const char * identifier, const int value ){ |
if( char_map_is_valid( map ) && ( * 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 ); |
}else{ |
if( map->items[ index ]->value != CHAR_MAP_NULL ) return EEXISTS; |
map->items[ index ]->value = value; |
return EOK; |
} |
} |
} |
return char_map_add_item( map, identifier, value ); |
} |
return EINVAL; |
} |
|
int char_map_add_item( char_map_ref map, const char * identifier, const int value ){ |
if( map->next == ( map->size - 1 )){ |
char_map_ref * tmp; |
|
tmp = ( char_map_ref * ) malloc( sizeof( char_map_ref ) * 2 * map->size ); |
if( ! tmp ) return ENOMEM; |
map->size *= 2; |
memcpy( tmp, map->items, sizeof( char_map_ref ) * map->next ); |
free( map->items ); |
map->items = tmp; |
} |
map->items[ map->next ] = ( char_map_ref ) malloc( sizeof( char_map_t )); |
if( ! map->items[ map->next ] ) return ENOMEM; |
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 ); |
}else{ |
map->items[ map->next ]->value = value; |
} |
++ map->next; |
return EOK; |
} |
|
void char_map_destroy( char_map_ref map ){ |
if( char_map_is_valid( map )){ |
int index; |
|
map->magic = 0; |
for( index = 0; index < map->next; ++ index ){ |
char_map_destroy( map->items[ index ] ); |
} |
free( map->items ); |
} |
} |
|
int char_map_exclude( char_map_ref map, const char * identifier ){ |
char_map_ref node; |
|
node = char_map_find_node( map, identifier ); |
if( node ){ |
int value; |
|
value = node->value; |
node->value = CHAR_MAP_NULL; |
return value; |
} |
return CHAR_MAP_NULL; |
} |
|
int char_map_find( char_map_ref map, const char * identifier ){ |
char_map_ref node; |
|
node = char_map_find_node( map, identifier ); |
return node ? node->value : CHAR_MAP_NULL; |
} |
|
char_map_ref char_map_find_node( char_map_ref map, const char * identifier ){ |
if( ! char_map_is_valid( map )) return NULL; |
if( * 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 NULL; |
} |
return map; |
} |
|
int char_map_get_value( char_map_ref map ){ |
return char_map_is_valid( map ) ? map->value : CHAR_MAP_NULL; |
} |
|
int char_map_initialize( char_map_ref map ){ |
if( ! map ) return EINVAL; |
map->c = 0; |
map->value = CHAR_MAP_NULL; |
map->size = 2; |
map->next = 0; |
map->items = malloc( sizeof( char_map_ref ) * map->size ); |
if( ! map->items ) return ENOMEM; |
map->items[ map->next ] = NULL; |
map->magic = CHAR_MAP_MAGIC_VALUE; |
return EOK; |
} |
|
int char_map_is_valid( 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 ){ |
char_map_ref node; |
|
// if( ! char_map_is_valid( map )) return EINVAL; |
node = char_map_find_node( map, identifier ); |
if( node ){ |
node->value = value; |
return EOK; |
}else{ |
return char_map_add( map, identifier, value ); |
} |
} |
|
/** @} |
*/ |
Property changes: |
Added: svn:eol-style |
+native |
\ No newline at end of property |