Rev 3666 | Rev 3886 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3666 | Rev 3846 | ||
---|---|---|---|
Line 25... | Line 25... | ||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup net |
29 | /** @addtogroup net |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** @file |
33 | /** @file |
- | 34 | * A character string to integer map implementation file. |
|
34 | */ |
35 | */ |
35 | 36 | ||
36 | #include <errno.h> |
37 | #include <errno.h> |
37 | #include <malloc.h> |
38 | #include <malloc.h> |
38 | #include <unistd.h> |
39 | #include <unistd.h> |
39 | #include <string.h> |
40 | #include <string.h> |
40 | 41 | ||
41 | #include "char_map.h" |
42 | #include "char_map.h" |
42 | 43 | ||
- | 44 | /** An internal magic value for a consistency check. |
|
- | 45 | */ |
|
43 | #define CHAR_MAP_MAGIC_VALUE 0x12345611 |
46 | #define CHAR_MAP_MAGIC_VALUE 0x12345611 |
44 | 47 | ||
- | 48 | /** Adds the value with the key to the map. |
|
- | 49 | * Creates new nodes to map the key. |
|
- | 50 | * @param map The character string to integer map. Input/output parameter. |
|
- | 51 | * @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. |
|
- | 52 | * @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. |
|
- | 53 | * @param value The integral value to be stored for the key character string. Input parameter. |
|
- | 54 | * @returns EOK on success. |
|
- | 55 | * @returns ENOMEM if there is no memory left. |
|
- | 56 | * @returns EEXIST if the key character string is already used. |
|
- | 57 | */ |
|
45 | int char_map_add_item( char_map_ref map, const char * identifier, const int value ); |
58 | int char_map_add_item( char_map_ref map, const char * identifier, size_t length, const int value ); |
- | 59 | ||
- | 60 | /** Returns the node assigned to the key from the map. |
|
- | 61 | * @param map The character string to integer map. Input parameter. |
|
- | 62 | * @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. |
|
- | 63 | * @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. |
|
- | 64 | * @returns The node holding the integral value assigned to the key character string. |
|
- | 65 | * @returns NULL if the key is not assigned a node. |
|
- | 66 | */ |
|
46 | char_map_ref char_map_find_node( char_map_ref map, const char * identifier ); |
67 | char_map_ref char_map_find_node( const char_map_ref map, const char * identifier, const size_t length ); |
- | 68 | ||
- | 69 | /** Checks if the map is valid. |
|
- | 70 | * @param map The character string to integer map. Input parameter. |
|
- | 71 | * @returns TRUE if the map is valid. |
|
- | 72 | * @returns FALSE otherwise. |
|
- | 73 | */ |
|
47 | int char_map_is_valid( char_map_ref map ); |
74 | int char_map_is_valid( const char_map_ref map ); |
48 | 75 | ||
49 | int char_map_add( char_map_ref map, const char * identifier, const int value ){ |
76 | int char_map_add( char_map_ref map, const char * identifier, size_t length, const int value ){ |
50 | if( char_map_is_valid( map ) && ( * identifier )){ |
77 | if( char_map_is_valid( map ) && ( identifier ) && (( length ) || ( * identifier ))){ |
51 | int index; |
78 | int index; |
52 | 79 | ||
53 | for( index = 0; index < map->next; ++ index ){ |
80 | for( index = 0; index < map->next; ++ index ){ |
54 | if( map->items[ index ]->c == * identifier ){ |
81 | if( map->items[ index ]->c == * identifier ){ |
55 | ++ identifier; |
82 | ++ identifier; |
- | 83 | if( length ) -- length; |
|
56 | if( * identifier ){ |
84 | if( length || ( * identifier )){ |
57 | return char_map_add( map->items[ index ], identifier, value ); |
85 | return char_map_add( map->items[ index ], identifier, length, value ); |
58 | }else{ |
86 | }else{ |
59 | if( map->items[ index ]->value != CHAR_MAP_NULL ) return EEXISTS; |
87 | if( map->items[ index ]->value != CHAR_MAP_NULL ) return EEXISTS; |
60 | map->items[ index ]->value = value; |
88 | map->items[ index ]->value = value; |
61 | return EOK; |
89 | return EOK; |
62 | } |
90 | } |
63 | } |
91 | } |
64 | } |
92 | } |
65 | return char_map_add_item( map, identifier, value ); |
93 | return char_map_add_item( map, identifier, length, value ); |
66 | } |
94 | } |
67 | return EINVAL; |
95 | return EINVAL; |
68 | } |
96 | } |
69 | 97 | ||
70 | int char_map_add_item( char_map_ref map, const char * identifier, const int value ){ |
98 | int char_map_add_item( char_map_ref map, const char * identifier, size_t length, const int value ){ |
71 | if( map->next == ( map->size - 1 )){ |
99 | if( map->next == ( map->size - 1 )){ |
72 | char_map_ref * tmp; |
100 | char_map_ref * tmp; |
73 | 101 | ||
74 | tmp = ( char_map_ref * ) malloc( sizeof( char_map_ref ) * 2 * map->size ); |
102 | tmp = ( char_map_ref * ) malloc( sizeof( char_map_ref ) * 2 * map->size ); |
75 | if( ! tmp ) return ENOMEM; |
103 | if( ! tmp ) return ENOMEM; |
Line 81... | Line 109... | ||
81 | map->items[ map->next ] = ( char_map_ref ) malloc( sizeof( char_map_t )); |
109 | map->items[ map->next ] = ( char_map_ref ) malloc( sizeof( char_map_t )); |
82 | if( ! map->items[ map->next ] ) return ENOMEM; |
110 | if( ! map->items[ map->next ] ) return ENOMEM; |
83 | char_map_initialize( map->items[ map->next ] ); |
111 | char_map_initialize( map->items[ map->next ] ); |
84 | map->items[ map->next ]->c = * identifier; |
112 | map->items[ map->next ]->c = * identifier; |
85 | ++ identifier; |
113 | ++ identifier; |
- | 114 | if( length ) -- length; |
|
- | 115 | ++ map->next; |
|
86 | if( * identifier ){ |
116 | if( length || ( * identifier )){ |
87 | map->items[ map->next ]->value = CHAR_MAP_NULL; |
117 | map->items[ map->next - 1 ]->value = CHAR_MAP_NULL; |
88 | char_map_add_item( map->items[ map->next ], identifier, value ); |
118 | return char_map_add_item( map->items[ map->next - 1 ], identifier, length, value ); |
89 | }else{ |
119 | }else{ |
90 | map->items[ map->next ]->value = value; |
120 | map->items[ map->next - 1 ]->value = value; |
91 | } |
121 | } |
92 | ++ map->next; |
- | |
93 | return EOK; |
122 | return EOK; |
94 | } |
123 | } |
95 | 124 | ||
96 | void char_map_destroy( char_map_ref map ){ |
125 | void char_map_destroy( char_map_ref map ){ |
97 | if( char_map_is_valid( map )){ |
126 | if( char_map_is_valid( map )){ |
Line 103... | Line 132... | ||
103 | } |
132 | } |
104 | free( map->items ); |
133 | free( map->items ); |
105 | } |
134 | } |
106 | } |
135 | } |
107 | 136 | ||
108 | int char_map_exclude( char_map_ref map, const char * identifier ){ |
137 | int char_map_exclude( char_map_ref map, const char * identifier, size_t length ){ |
109 | char_map_ref node; |
138 | char_map_ref node; |
110 | 139 | ||
111 | node = char_map_find_node( map, identifier ); |
140 | node = char_map_find_node( map, identifier, length ); |
112 | if( node ){ |
141 | if( node ){ |
113 | int value; |
142 | int value; |
114 | 143 | ||
115 | value = node->value; |
144 | value = node->value; |
116 | node->value = CHAR_MAP_NULL; |
145 | node->value = CHAR_MAP_NULL; |
117 | return value; |
146 | return value; |
118 | } |
147 | } |
119 | return CHAR_MAP_NULL; |
148 | return CHAR_MAP_NULL; |
120 | } |
149 | } |
121 | 150 | ||
122 | int char_map_find( char_map_ref map, const char * identifier ){ |
151 | int char_map_find( const char_map_ref map, const char * identifier, size_t length ){ |
123 | char_map_ref node; |
152 | char_map_ref node; |
124 | 153 | ||
125 | node = char_map_find_node( map, identifier ); |
154 | node = char_map_find_node( map, identifier, length ); |
126 | return node ? node->value : CHAR_MAP_NULL; |
155 | return node ? node->value : CHAR_MAP_NULL; |
127 | } |
156 | } |
128 | 157 | ||
129 | char_map_ref char_map_find_node( char_map_ref map, const char * identifier ){ |
158 | char_map_ref char_map_find_node( const char_map_ref map, const char * identifier, size_t length ){ |
130 | if( ! char_map_is_valid( map )) return NULL; |
159 | if( ! char_map_is_valid( map )) return NULL; |
- | 160 | if( length ) -- length; |
|
131 | if( * identifier ){ |
161 | if( length || ( * identifier )){ |
132 | int index; |
162 | int index; |
133 | 163 | ||
134 | for( index = 0; index < map->next; ++ index ){ |
164 | for( index = 0; index < map->next; ++ index ){ |
135 | if( map->items[ index ]->c == * identifier ){ |
165 | if( map->items[ index ]->c == * identifier ){ |
136 | ++ identifier; |
166 | ++ identifier; |
137 | return char_map_find_node( map->items[ index ], identifier ); |
167 | return char_map_find_node( map->items[ index ], identifier, length ); |
138 | } |
168 | } |
139 | } |
169 | } |
140 | return NULL; |
170 | return NULL; |
141 | } |
171 | } |
142 | return map; |
172 | return map; |
143 | } |
173 | } |
144 | 174 | ||
145 | int char_map_get_value( char_map_ref map ){ |
175 | int char_map_get_value( const char_map_ref map ){ |
146 | return char_map_is_valid( map ) ? map->value : CHAR_MAP_NULL; |
176 | return char_map_is_valid( map ) ? map->value : CHAR_MAP_NULL; |
147 | } |
177 | } |
148 | 178 | ||
149 | int char_map_initialize( char_map_ref map ){ |
179 | int char_map_initialize( char_map_ref map ){ |
150 | if( ! map ) return EINVAL; |
180 | if( ! map ) return EINVAL; |
Line 157... | Line 187... | ||
157 | map->items[ map->next ] = NULL; |
187 | map->items[ map->next ] = NULL; |
158 | map->magic = CHAR_MAP_MAGIC_VALUE; |
188 | map->magic = CHAR_MAP_MAGIC_VALUE; |
159 | return EOK; |
189 | return EOK; |
160 | } |
190 | } |
161 | 191 | ||
162 | int char_map_is_valid( char_map_ref map ){ |
192 | int char_map_is_valid( const char_map_ref map ){ |
163 | return map && ( map->magic == CHAR_MAP_MAGIC_VALUE ); |
193 | return map && ( map->magic == CHAR_MAP_MAGIC_VALUE ); |
164 | } |
194 | } |
165 | 195 | ||
166 | int char_map_update( char_map_ref map, const char * identifier, const int value ){ |
196 | int char_map_update( char_map_ref map, const char * identifier, const size_t length, const int value ){ |
167 | char_map_ref node; |
197 | char_map_ref node; |
168 | 198 | ||
169 | // if( ! char_map_is_valid( map )) return EINVAL; |
199 | // if( ! char_map_is_valid( map )) return EINVAL; |
170 | node = char_map_find_node( map, identifier ); |
200 | node = char_map_find_node( map, identifier, length ); |
171 | if( node ){ |
201 | if( node ){ |
172 | node->value = value; |
202 | node->value = value; |
173 | return EOK; |
203 | return EOK; |
174 | }else{ |
204 | }else{ |
175 | return char_map_add( map, identifier, value ); |
205 | return char_map_add( map, identifier, length, value ); |
176 | } |
206 | } |
177 | } |
207 | } |
178 | 208 | ||
179 | /** @} |
209 | /** @} |
180 | */ |
210 | */ |