Ignore:
Timestamp:
2010-03-07T22:51:38Z (14 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
60ab6c3
Parents:
b5cbff4 (diff), 31c80a5 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge from lp:~lukasmejdrech/helenos/network.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/structures/char_map.c

    rb5cbff4 r71b00dcc  
    5757 *  @returns EEXIST if the key character string is already used.
    5858 */
    59 int     char_map_add_item( char_map_ref map, const char * identifier, size_t length, const int value );
     59int char_map_add_item(char_map_ref map, const char * identifier, size_t length, const int value);
    6060
    6161/** Returns the node assigned to the key from the map.
     
    6666 *  @returns NULL if the key is not assigned a&nbsp;node.
    6767 */
    68 char_map_ref    char_map_find_node( const char_map_ref map, const char * identifier, const size_t length );
     68char_map_ref char_map_find_node(const char_map_ref map, const char * identifier, const size_t length);
    6969
    7070/** Returns the value assigned to the map.
     
    7373 *  @returns CHAR_MAP_NULL if the map is not assigned a&nbsp;value.
    7474 */
    75 int     char_map_get_value( const char_map_ref map );
     75int char_map_get_value(const char_map_ref map);
    7676
    7777/** Checks if the map is valid.
     
    8080 *  @returns FALSE otherwise.
    8181 */
    82 int     char_map_is_valid( const char_map_ref map );
    83 
    84 int char_map_add( char_map_ref map, const char * identifier, size_t length, const int value ){
    85         if( char_map_is_valid( map ) && ( identifier ) && (( length ) || ( * identifier ))){
    86                 int     index;
    87 
    88                 for( index = 0; index < map->next; ++ index ){
    89                         if( map->items[ index ]->c == * identifier ){
     82int char_map_is_valid(const char_map_ref map);
     83
     84int char_map_add(char_map_ref map, const char * identifier, size_t length, const int value){
     85        if(char_map_is_valid(map) && (identifier) && ((length) || (*identifier))){
     86                int index;
     87
     88                for(index = 0; index < map->next; ++ index){
     89                        if(map->items[index]->c == * identifier){
    9090                                ++ identifier;
    91                                 if(( length > 1 ) || (( length == 0 ) && ( * identifier ))){
    92                                         return char_map_add( map->items[ index ], identifier, length ? length - 1 : 0, value );
     91                                if((length > 1) || ((length == 0) && (*identifier))){
     92                                        return char_map_add(map->items[index], identifier, length ? length - 1 : 0, value);
    9393                                }else{
    94                                         if( map->items[ index ]->value != CHAR_MAP_NULL ) return EEXISTS;
    95                                         map->items[ index ]->value = value;
     94                                        if(map->items[index]->value != CHAR_MAP_NULL){
     95                                                return EEXISTS;
     96                                        }
     97                                        map->items[index]->value = value;
    9698                                        return EOK;
    9799                                }
    98100                        }
    99101                }
    100                 return char_map_add_item( map, identifier, length, value );
     102                return char_map_add_item(map, identifier, length, value);
    101103        }
    102104        return EINVAL;
    103105}
    104106
    105 int char_map_add_item( char_map_ref map, const char * identifier, size_t length, const int value ){
    106         if( map->next == ( map->size - 1 )){
    107                 char_map_ref    * tmp;
    108 
    109                 tmp = ( char_map_ref * ) realloc( map->items, sizeof( char_map_ref ) * 2 * map->size );
    110                 if( ! tmp ) return ENOMEM;
     107int char_map_add_item(char_map_ref map, const char * identifier, size_t length, const int value){
     108        if(map->next == (map->size - 1)){
     109                char_map_ref *tmp;
     110
     111                tmp = (char_map_ref *) realloc(map->items, sizeof(char_map_ref) * 2 * map->size);
     112                if(! tmp){
     113                        return ENOMEM;
     114                }
    111115                map->size *= 2;
    112116                map->items = tmp;
    113117        }
    114         map->items[ map->next ] = ( char_map_ref ) malloc( sizeof( char_map_t ));
    115         if( ! map->items[ map->next ] ) return ENOMEM;
    116         if( char_map_initialize( map->items[ map->next ] ) != EOK ){
    117                 free( map->items[ map->next ] );
    118                 map->items[ map->next ] = NULL;
     118        map->items[map->next] = (char_map_ref) malloc(sizeof(char_map_t));
     119        if(! map->items[map->next]){
    119120                return ENOMEM;
    120121        }
    121         map->items[ map->next ]->c = * identifier;
     122        if(char_map_initialize(map->items[map->next]) != EOK){
     123                free(map->items[map->next]);
     124                map->items[map->next] = NULL;
     125                return ENOMEM;
     126        }
     127        map->items[map->next]->c = * identifier;
    122128        ++ identifier;
    123129        ++ map->next;
    124         if(( length > 1 ) || (( length == 0 ) && ( * identifier ))){
    125                 map->items[ map->next - 1 ]->value = CHAR_MAP_NULL;
    126                 return char_map_add_item( map->items[ map->next - 1 ], identifier, length ? length - 1 : 0, value );
     130        if((length > 1) || ((length == 0) && (*identifier))){
     131                map->items[map->next - 1]->value = CHAR_MAP_NULL;
     132                return char_map_add_item(map->items[map->next - 1], identifier, length ? length - 1 : 0, value);
    127133        }else{
    128                 map->items[ map->next - 1 ]->value = value;
     134                map->items[map->next - 1]->value = value;
    129135        }
    130136        return EOK;
    131137}
    132138
    133 void char_map_destroy( char_map_ref map ){
    134         if( char_map_is_valid( map )){
    135                 int     index;
     139void char_map_destroy(char_map_ref map){
     140        if(char_map_is_valid(map)){
     141                int index;
    136142
    137143                map->magic = 0;
    138                 for( index = 0; index < map->next; ++ index ){
    139                         char_map_destroy( map->items[ index ] );
    140                 }
    141                 free( map->items );
     144                for(index = 0; index < map->next; ++ index){
     145                        char_map_destroy(map->items[index]);
     146                }
     147                free(map->items);
    142148                map->items = NULL;
    143149        }
    144150}
    145151
    146 int char_map_exclude( char_map_ref map, const char * identifier, size_t length ){
    147         char_map_ref    node;
    148 
    149         node = char_map_find_node( map, identifier, length );
    150         if( node ){
    151                 int     value;
     152int char_map_exclude(char_map_ref map, const char * identifier, size_t length){
     153        char_map_ref node;
     154
     155        node = char_map_find_node(map, identifier, length);
     156        if(node){
     157                int value;
    152158
    153159                value = node->value;
     
    158164}
    159165
    160 int char_map_find( const char_map_ref map, const char * identifier, size_t length ){
    161         char_map_ref    node;
    162 
    163         node = char_map_find_node( map, identifier, length );
     166int char_map_find(const char_map_ref map, const char * identifier, size_t length){
     167        char_map_ref node;
     168
     169        node = char_map_find_node(map, identifier, length);
    164170        return node ? node->value : CHAR_MAP_NULL;
    165171}
    166172
    167 char_map_ref char_map_find_node( const char_map_ref map, const char * identifier, size_t length ){
    168         if( ! char_map_is_valid( map )) return NULL;
    169         if( length || ( * identifier )){
    170                 int     index;
    171 
    172                 for( index = 0; index < map->next; ++ index ){
    173                         if( map->items[ index ]->c == * identifier ){
     173char_map_ref char_map_find_node(const char_map_ref map, const char * identifier, size_t length){
     174        if(! char_map_is_valid(map)){
     175                return NULL;
     176        }
     177        if(length || (*identifier)){
     178                int index;
     179
     180                for(index = 0; index < map->next; ++ index){
     181                        if(map->items[index]->c == * identifier){
    174182                                ++ identifier;
    175                                 if( length == 1 ) return map->items[ index ];
    176                                 return char_map_find_node( map->items[ index ], identifier, length ? length - 1 : 0 );
     183                                if(length == 1){
     184                                        return map->items[index];
     185                                }
     186                                return char_map_find_node(map->items[index], identifier, length ? length - 1 : 0);
    177187                        }
    178188                }
     
    182192}
    183193
    184 int char_map_get_value( const char_map_ref map ){
    185         return char_map_is_valid( map ) ? map->value : CHAR_MAP_NULL;
    186 }
    187 
    188 int char_map_initialize( char_map_ref map ){
    189         if( ! map ) return EINVAL;
     194int char_map_get_value(const char_map_ref map){
     195        return char_map_is_valid(map) ? map->value : CHAR_MAP_NULL;
     196}
     197
     198int char_map_initialize(char_map_ref map){
     199        if(! map){
     200                return EINVAL;
     201        }
    190202        map->c = '\0';
    191203        map->value = CHAR_MAP_NULL;
    192204        map->size = 2;
    193205        map->next = 0;
    194         map->items = malloc( sizeof( char_map_ref ) * map->size );
    195         if( ! map->items ){
     206        map->items = malloc(sizeof(char_map_ref) * map->size);
     207        if(! map->items){
    196208                map->magic = 0;
    197209                return ENOMEM;
    198210        }
    199         map->items[ map->next ] = NULL;
     211        map->items[map->next] = NULL;
    200212        map->magic = CHAR_MAP_MAGIC_VALUE;
    201213        return EOK;
    202214}
    203215
    204 int char_map_is_valid( const char_map_ref map ){
    205         return map && ( map->magic == CHAR_MAP_MAGIC_VALUE );
    206 }
    207 
    208 int char_map_update( char_map_ref map, const char * identifier, const size_t length, const int value ){
    209         char_map_ref    node;
    210 
    211 //      if( ! char_map_is_valid( map )) return EINVAL;
    212         node = char_map_find_node( map, identifier, length );
    213         if( node ){
     216int char_map_is_valid(const char_map_ref map){
     217        return map && (map->magic == CHAR_MAP_MAGIC_VALUE);
     218}
     219
     220int char_map_update(char_map_ref map, const char * identifier, const size_t length, const int value){
     221        char_map_ref node;
     222
     223//      if(! char_map_is_valid(map)) return EINVAL;
     224        node = char_map_find_node(map, identifier, length);
     225        if(node){
    214226                node->value = value;
    215227                return EOK;
    216228        }else{
    217                 return char_map_add( map, identifier, length, value );
     229                return char_map_add(map, identifier, length, value);
    218230        }
    219231}
Note: See TracChangeset for help on using the changeset viewer.