Changes in uspace/srv/net/structures/char_map.c [aadf01e:21580dd] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/structures/char_map.c
raadf01e r21580dd 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){ 95 return EEXISTS; 96 } 97 map->items[index]->value = value; 94 if( map->items[ index ]->value != CHAR_MAP_NULL ) return EEXISTS; 95 map->items[ index ]->value = value; 98 96 return EOK; 99 97 } 100 98 } 101 99 } 102 return char_map_add_item( map, identifier, length, value);100 return char_map_add_item( map, identifier, length, value ); 103 101 } 104 102 return EINVAL; 105 103 } 106 104 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 } 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; 115 111 map->size *= 2; 116 112 map->items = tmp; 117 113 } 118 map->items[map->next] = (char_map_ref) malloc(sizeof(char_map_t)); 119 if(! map->items[map->next]){ 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; 120 119 return ENOMEM; 121 120 } 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; 121 map->items[ map->next ]->c = * identifier; 128 122 ++ identifier; 129 123 ++ map->next; 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);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 ); 133 127 }else{ 134 map->items[ map->next - 1]->value = value;128 map->items[ map->next - 1 ]->value = value; 135 129 } 136 130 return EOK; 137 131 } 138 132 139 void char_map_destroy( char_map_ref map){140 if( char_map_is_valid(map)){141 int 133 void char_map_destroy( char_map_ref map ){ 134 if( char_map_is_valid( map )){ 135 int index; 142 136 143 137 map->magic = 0; 144 for( index = 0; index < map->next; ++ index){145 char_map_destroy( map->items[index]);138 for( index = 0; index < map->next; ++ index ){ 139 char_map_destroy( map->items[ index ] ); 146 140 } 147 free( map->items);141 free( map->items ); 148 142 map->items = NULL; 149 143 } 150 144 } 151 145 152 int char_map_exclude( char_map_ref map, const char * identifier, size_t length){153 char_map_ref 154 155 node = char_map_find_node( map, identifier, length);156 if( node){157 int 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; 158 152 159 153 value = node->value; … … 164 158 } 165 159 166 int char_map_find( const char_map_ref map, const char * identifier, size_t length){167 char_map_ref 168 169 node = char_map_find_node( map, identifier, length);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 ); 170 164 return node ? node->value : CHAR_MAP_NULL; 171 165 } 172 166 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){ 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 ){ 182 174 ++ identifier; 183 if(length == 1){ 184 return map->items[index]; 185 } 186 return char_map_find_node(map->items[index], identifier, length ? length - 1 : 0); 175 if( length == 1 ) return map->items[ index ]; 176 return char_map_find_node( map->items[ index ], identifier, length ? length - 1 : 0 ); 187 177 } 188 178 } … … 192 182 } 193 183 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 } 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; 202 190 map->c = '\0'; 203 191 map->value = CHAR_MAP_NULL; 204 192 map->size = 2; 205 193 map->next = 0; 206 map->items = malloc( sizeof(char_map_ref) * map->size);207 if( ! map->items){194 map->items = malloc( sizeof( char_map_ref ) * map->size ); 195 if( ! map->items ){ 208 196 map->magic = 0; 209 197 return ENOMEM; 210 198 } 211 map->items[ map->next] = NULL;199 map->items[ map->next ] = NULL; 212 200 map->magic = CHAR_MAP_MAGIC_VALUE; 213 201 return EOK; 214 202 } 215 203 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 222 223 // if( ! char_map_is_valid(map)) return EINVAL;224 node = char_map_find_node( map, identifier, length);225 if( node){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 ){ 226 214 node->value = value; 227 215 return EOK; 228 216 }else{ 229 return char_map_add( map, identifier, length, value);217 return char_map_add( map, identifier, length, value ); 230 218 } 231 219 }
Note:
See TracChangeset
for help on using the changeset viewer.