Rev 3886 | Rev 4505 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3886 | Rev 3912 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Lukas Mejdrech |
2 | * Copyright (c) 2009 Lukas Mejdrech |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 | */ |
34 | */ |
35 | 35 | ||
36 | #ifndef __GENERIC_CHAR_MAP_H__ |
36 | #ifndef __GENERIC_CHAR_MAP_H__ |
37 | #define __GENERIC_CHAR_MAP_H__ |
37 | #define __GENERIC_CHAR_MAP_H__ |
38 | 38 | ||
39 | #include <unistd.h> |
39 | #include <unistd.h> |
40 | 40 | ||
41 | #include "../err.h" |
41 | #include "../err.h" |
42 | 42 | ||
43 | #include "char_map.h" |
43 | #include "char_map.h" |
44 | #include "generic_field.h" |
44 | #include "generic_field.h" |
45 | 45 | ||
46 | #define GENERIC_CHAR_MAP_MAGIC_VALUE 0x12345622 |
46 | #define GENERIC_CHAR_MAP_MAGIC_VALUE 0x12345622 |
47 | 47 | ||
48 | #define GENERIC_CHAR_MAP_DECLARE( name, type ) \ |
48 | #define GENERIC_CHAR_MAP_DECLARE( name, type ) \ |
49 | \ |
49 | \ |
50 | GENERIC_FIELD_DECLARE( name##_items, type ) \ |
50 | GENERIC_FIELD_DECLARE( name##_items, type ) \ |
51 | \ |
51 | \ |
52 | typedef struct name name##_t; \ |
52 | typedef struct name name##_t; \ |
53 | typedef name##_t * name##_ref; \ |
53 | typedef name##_t * name##_ref; \ |
54 | \ |
54 | \ |
55 | struct name{ \ |
55 | struct name{ \ |
56 | char_map_t names; \ |
56 | char_map_t names; \ |
57 | name##_items_t values; \ |
57 | name##_items_t values; \ |
58 | int magic; \ |
58 | int magic; \ |
59 | }; \ |
59 | }; \ |
60 | \ |
60 | \ |
61 | int name##_add( name##_ref map, const char * name, const size_t length, type * value ); \ |
61 | int name##_add( name##_ref map, const char * name, const size_t length, type * value ); \ |
62 | int name##_count( name##_ref map ); \ |
62 | int name##_count( name##_ref map ); \ |
63 | void name##_destroy( name##_ref map ); \ |
63 | void name##_destroy( name##_ref map ); \ |
64 | void name##_exclude( name##_ref map, const char * name, const size_t length ); \ |
64 | void name##_exclude( name##_ref map, const char * name, const size_t length ); \ |
65 | type * name##_find( name##_ref map, const char * name, const size_t length ); \ |
65 | type * name##_find( name##_ref map, const char * name, const size_t length ); \ |
66 | type * name##_get_index( name##_ref map, int index ); \ |
66 | type * name##_get_index( name##_ref map, int index ); \ |
67 | int name##_initialize( name##_ref map ); \ |
67 | int name##_initialize( name##_ref map ); \ |
68 | int name##_is_valid( name##_ref map ); |
68 | int name##_is_valid( name##_ref map ); |
69 | 69 | ||
70 | #define GENERIC_CHAR_MAP_IMPLEMENT( name, type ) \ |
70 | #define GENERIC_CHAR_MAP_IMPLEMENT( name, type ) \ |
71 | \ |
71 | \ |
72 | GENERIC_FIELD_IMPLEMENT( name##_items, type ) \ |
72 | GENERIC_FIELD_IMPLEMENT( name##_items, type ) \ |
73 | \ |
73 | \ |
74 | int name##_add( name##_ref map, const char * name, const size_t length, type * value ){ \ |
74 | int name##_add( name##_ref map, const char * name, const size_t length, type * value ){ \ |
75 | ERROR_DECLARE; \ |
75 | ERROR_DECLARE; \ |
76 | \ |
76 | \ |
77 | int index; \ |
77 | int index; \ |
78 | \ |
78 | \ |
79 | if( ! name##_is_valid( map )) return EINVAL; \ |
79 | if( ! name##_is_valid( map )) return EINVAL; \ |
80 | index = name##_items_add( & map->values, value ); \ |
80 | index = name##_items_add( & map->values, value ); \ |
81 | if( index < 0 ) return index; \ |
81 | if( index < 0 ) return index; \ |
82 | if( ERROR_OCCURED( char_map_add( & map->names, name, length, index ))){ \ |
82 | if( ERROR_OCCURRED( char_map_add( & map->names, name, length, index ))){ \ |
83 | name##_items_exclude_index( & map->values, index ); \ |
83 | name##_items_exclude_index( & map->values, index ); \ |
84 | return ERROR_CODE; \ |
84 | return ERROR_CODE; \ |
85 | } \ |
85 | } \ |
86 | return EOK; \ |
86 | return EOK; \ |
87 | } \ |
87 | } \ |
88 | \ |
88 | \ |
89 | int name##_count( name##_ref map ){ \ |
89 | int name##_count( name##_ref map ){ \ |
90 | return name##_is_valid( map ) ? name##_items_count( & map->values ) : -1; \ |
90 | return name##_is_valid( map ) ? name##_items_count( & map->values ) : -1; \ |
91 | } \ |
91 | } \ |
92 | \ |
92 | \ |
93 | void name##_destroy( name##_ref map ){ \ |
93 | void name##_destroy( name##_ref map ){ \ |
94 | if( name##_is_valid( map )){ \ |
94 | if( name##_is_valid( map )){ \ |
95 | char_map_destroy( & map->names ); \ |
95 | char_map_destroy( & map->names ); \ |
96 | name##_items_destroy( & map->values ); \ |
96 | name##_items_destroy( & map->values ); \ |
97 | } \ |
97 | } \ |
98 | } \ |
98 | } \ |
99 | \ |
99 | \ |
100 | void name##_exclude( name##_ref map, const char * name, const size_t length ){ \ |
100 | void name##_exclude( name##_ref map, const char * name, const size_t length ){ \ |
101 | if( name##_is_valid( map )){ \ |
101 | if( name##_is_valid( map )){ \ |
102 | int index; \ |
102 | int index; \ |
103 | \ |
103 | \ |
104 | index = char_map_exclude( & map->names, name, length ); \ |
104 | index = char_map_exclude( & map->names, name, length ); \ |
105 | if( index != CHAR_MAP_NULL ){ \ |
105 | if( index != CHAR_MAP_NULL ){ \ |
106 | name##_items_exclude_index( & map->values, index ); \ |
106 | name##_items_exclude_index( & map->values, index ); \ |
107 | } \ |
107 | } \ |
108 | } \ |
108 | } \ |
109 | } \ |
109 | } \ |
110 | \ |
110 | \ |
111 | type * name##_find( name##_ref map, const char * name, const size_t length ){ \ |
111 | type * name##_find( name##_ref map, const char * name, const size_t length ){ \ |
112 | if( name##_is_valid( map )){ \ |
112 | if( name##_is_valid( map )){ \ |
113 | int index; \ |
113 | int index; \ |
114 | \ |
114 | \ |
115 | index = char_map_find( & map->names, name, length ); \ |
115 | index = char_map_find( & map->names, name, length ); \ |
116 | if( index != CHAR_MAP_NULL ){ \ |
116 | if( index != CHAR_MAP_NULL ){ \ |
117 | return name##_items_get_index( & map->values, index ); \ |
117 | return name##_items_get_index( & map->values, index ); \ |
118 | } \ |
118 | } \ |
119 | } \ |
119 | } \ |
120 | return NULL; \ |
120 | return NULL; \ |
121 | } \ |
121 | } \ |
122 | \ |
122 | \ |
123 | int name##_initialize( name##_ref map ){ \ |
123 | int name##_initialize( name##_ref map ){ \ |
- | 124 | ERROR_DECLARE; \ |
|
- | 125 | \ |
|
124 | if( ! map ) return EINVAL; \ |
126 | if( ! map ) return EINVAL; \ |
125 | char_map_initialize( & map->names ); \ |
127 | ERROR_PROPAGATE( char_map_initialize( & map->names )); \ |
126 | name##_items_initialize( & map->values ); \ |
128 | if( ERROR_OCCURRED( name##_items_initialize( & map->values ))){ \ |
- | 129 | char_map_destroy( & map->names ); \ |
|
- | 130 | return ERROR_CODE; \ |
|
- | 131 | } \ |
|
127 | map->magic = GENERIC_CHAR_MAP_MAGIC_VALUE; \ |
132 | map->magic = GENERIC_CHAR_MAP_MAGIC_VALUE; \ |
128 | return EOK; \ |
133 | return EOK; \ |
129 | } \ |
134 | } \ |
130 | \ |
135 | \ |
131 | int name##_is_valid( name##_ref map ){ \ |
136 | int name##_is_valid( name##_ref map ){ \ |
132 | return map && ( map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE ); \ |
137 | return map && ( map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE ); \ |
133 | } |
138 | } |
134 | 139 | ||
135 | #endif |
140 | #endif |
136 | 141 | ||
137 | /** @} |
142 | /** @} |
138 | */ |
143 | */ |
139 | 144 |