Changes in uspace/srv/net/structures/char_map.c [21580dd:aadf01e] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/structures/char_map.c
r21580dd raadf01e 57 57 * @returns EEXIST if the key character string is already used. 58 58 */ 59 int char_map_add_item( char_map_ref map, const char * identifier, size_t length, const int value);59 int char_map_add_item(char_map_ref map, const char * identifier, size_t length, const int value); 60 60 61 61 /** Returns the node assigned to the key from the map. … … 66 66 * @returns NULL if the key is not assigned a node. 67 67 */ 68 char_map_ref char_map_find_node( const char_map_ref map, const char * identifier, const size_t length);68 char_map_ref char_map_find_node(const char_map_ref map, const char * identifier, const size_t length); 69 69 70 70 /** Returns the value assigned to the map. … … 73 73 * @returns CHAR_MAP_NULL if the map is not assigned a value. 74 74 */ 75 int char_map_get_value( const char_map_ref map);75 int char_map_get_value(const char_map_ref map); 76 76 77 77 /** Checks if the map is valid. … … 80 80 * @returns FALSE otherwise. 81 81 */ 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 87 88 for( index = 0; index < map->next; ++ index){89 if( map->items[ index ]->c == * identifier){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){ 90 90 ++ 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); 93 93 }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; 96 98 return EOK; 97 99 } 98 100 } 99 101 } 100 return char_map_add_item( map, identifier, length, value);102 return char_map_add_item(map, identifier, length, value); 101 103 } 102 104 return EINVAL; 103 105 } 104 106 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; 107 int 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 } 111 115 map->size *= 2; 112 116 map->items = tmp; 113 117 } 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]){ 119 120 return ENOMEM; 120 121 } 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; 122 128 ++ identifier; 123 129 ++ 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); 127 133 }else{ 128 map->items[ map->next - 1]->value = value;134 map->items[map->next - 1]->value = value; 129 135 } 130 136 return EOK; 131 137 } 132 138 133 void char_map_destroy( char_map_ref map){134 if( char_map_is_valid( map)){135 int 139 void char_map_destroy(char_map_ref map){ 140 if(char_map_is_valid(map)){ 141 int index; 136 142 137 143 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); 142 148 map->items = NULL; 143 149 } 144 150 } 145 151 146 int char_map_exclude( char_map_ref map, const char * identifier, size_t length){147 char_map_ref 148 149 node = char_map_find_node( map, identifier, length);150 if( node){151 int 152 int 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; 152 158 153 159 value = node->value; … … 158 164 } 159 165 160 int char_map_find( const char_map_ref map, const char * identifier, size_t length){161 char_map_ref 162 163 node = char_map_find_node( map, identifier, length);166 int 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); 164 170 return node ? node->value : CHAR_MAP_NULL; 165 171 } 166 172 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 ){ 173 char_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){ 174 182 ++ 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); 177 187 } 178 188 } … … 182 192 } 183 193 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; 194 int char_map_get_value(const char_map_ref map){ 195 return char_map_is_valid(map) ? map->value : CHAR_MAP_NULL; 196 } 197 198 int char_map_initialize(char_map_ref map){ 199 if(! map){ 200 return EINVAL; 201 } 190 202 map->c = '\0'; 191 203 map->value = CHAR_MAP_NULL; 192 204 map->size = 2; 193 205 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){ 196 208 map->magic = 0; 197 209 return ENOMEM; 198 210 } 199 map->items[ map->next] = NULL;211 map->items[map->next] = NULL; 200 212 map->magic = CHAR_MAP_MAGIC_VALUE; 201 213 return EOK; 202 214 } 203 215 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 210 211 // if( ! char_map_is_valid( map)) return EINVAL;212 node = char_map_find_node( map, identifier, length);213 if( node){216 int char_map_is_valid(const char_map_ref map){ 217 return map && (map->magic == CHAR_MAP_MAGIC_VALUE); 218 } 219 220 int 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){ 214 226 node->value = value; 215 227 return EOK; 216 228 }else{ 217 return char_map_add( map, identifier, length, value);229 return char_map_add(map, identifier, length, value); 218 230 } 219 231 }
Note:
See TracChangeset
for help on using the changeset viewer.