Rev 4505 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3666 | mejdrech | 1 | /* |
3912 | mejdrech | 2 | * Copyright (c) 2009 Lukas Mejdrech |
3666 | mejdrech | 3 | * All rights reserved. |
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
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 |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
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 |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
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 |
||
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 |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | /** @addtogroup net |
||
3846 | mejdrech | 30 | * @{ |
3666 | mejdrech | 31 | */ |
32 | |||
33 | /** @file |
||
4704 | mejdrech | 34 | * Character string to generic type map. |
3666 | mejdrech | 35 | */ |
36 | |||
37 | #ifndef __GENERIC_CHAR_MAP_H__ |
||
38 | #define __GENERIC_CHAR_MAP_H__ |
||
39 | |||
4505 | mejdrech | 40 | #include <errno.h> |
3666 | mejdrech | 41 | #include <unistd.h> |
42 | |||
3886 | mejdrech | 43 | #include "../err.h" |
44 | |||
3666 | mejdrech | 45 | #include "char_map.h" |
46 | #include "generic_field.h" |
||
47 | |||
4704 | mejdrech | 48 | /** Internal magic value for a map consistency check. |
49 | */ |
||
3666 | mejdrech | 50 | #define GENERIC_CHAR_MAP_MAGIC_VALUE 0x12345622 |
51 | |||
4704 | mejdrech | 52 | /** Character string to generic type map declaration. |
53 | * @param name Name of the map. Input parameter. |
||
54 | * @param type Inner object type. Input parameter |
||
55 | */ |
||
3846 | mejdrech | 56 | #define GENERIC_CHAR_MAP_DECLARE( name, type ) \ |
57 | \ |
||
58 | GENERIC_FIELD_DECLARE( name##_items, type ) \ |
||
59 | \ |
||
60 | typedef struct name name##_t; \ |
||
61 | typedef name##_t * name##_ref; \ |
||
62 | \ |
||
63 | struct name{ \ |
||
64 | char_map_t names; \ |
||
65 | name##_items_t values; \ |
||
66 | int magic; \ |
||
67 | }; \ |
||
68 | \ |
||
69 | int name##_add( name##_ref map, const char * name, const size_t length, type * value ); \ |
||
70 | int name##_count( name##_ref map ); \ |
||
71 | void name##_destroy( name##_ref map ); \ |
||
72 | void name##_exclude( name##_ref map, const char * name, const size_t length ); \ |
||
73 | type * name##_find( name##_ref map, const char * name, const size_t length ); \ |
||
74 | type * name##_get_index( name##_ref map, int index ); \ |
||
75 | int name##_initialize( name##_ref map ); \ |
||
3666 | mejdrech | 76 | int name##_is_valid( name##_ref map ); |
77 | |||
4704 | mejdrech | 78 | /** Character string to generic type map implementation. |
79 | * Should follow declaration with the same parameters. |
||
80 | * @param name Name of the map. Input parameter. |
||
81 | * @param type Inner object type. Input parameter |
||
82 | */ |
||
3846 | mejdrech | 83 | #define GENERIC_CHAR_MAP_IMPLEMENT( name, type ) \ |
84 | \ |
||
85 | GENERIC_FIELD_IMPLEMENT( name##_items, type ) \ |
||
86 | \ |
||
87 | int name##_add( name##_ref map, const char * name, const size_t length, type * value ){ \ |
||
88 | ERROR_DECLARE; \ |
||
89 | \ |
||
90 | int index; \ |
||
91 | \ |
||
92 | if( ! name##_is_valid( map )) return EINVAL; \ |
||
93 | index = name##_items_add( & map->values, value ); \ |
||
94 | if( index < 0 ) return index; \ |
||
3912 | mejdrech | 95 | if( ERROR_OCCURRED( char_map_add( & map->names, name, length, index ))){ \ |
3846 | mejdrech | 96 | name##_items_exclude_index( & map->values, index ); \ |
97 | return ERROR_CODE; \ |
||
98 | } \ |
||
99 | return EOK; \ |
||
100 | } \ |
||
101 | \ |
||
102 | int name##_count( name##_ref map ){ \ |
||
3666 | mejdrech | 103 | return name##_is_valid( map ) ? name##_items_count( & map->values ) : -1; \ |
3846 | mejdrech | 104 | } \ |
105 | \ |
||
106 | void name##_destroy( name##_ref map ){ \ |
||
107 | if( name##_is_valid( map )){ \ |
||
108 | char_map_destroy( & map->names ); \ |
||
109 | name##_items_destroy( & map->values ); \ |
||
110 | } \ |
||
111 | } \ |
||
112 | \ |
||
113 | void name##_exclude( name##_ref map, const char * name, const size_t length ){ \ |
||
114 | if( name##_is_valid( map )){ \ |
||
115 | int index; \ |
||
116 | \ |
||
117 | index = char_map_exclude( & map->names, name, length ); \ |
||
118 | if( index != CHAR_MAP_NULL ){ \ |
||
119 | name##_items_exclude_index( & map->values, index ); \ |
||
120 | } \ |
||
121 | } \ |
||
122 | } \ |
||
123 | \ |
||
124 | type * name##_find( name##_ref map, const char * name, const size_t length ){ \ |
||
125 | if( name##_is_valid( map )){ \ |
||
126 | int index; \ |
||
127 | \ |
||
128 | index = char_map_find( & map->names, name, length ); \ |
||
129 | if( index != CHAR_MAP_NULL ){ \ |
||
130 | return name##_items_get_index( & map->values, index ); \ |
||
131 | } \ |
||
132 | } \ |
||
133 | return NULL; \ |
||
134 | } \ |
||
135 | \ |
||
136 | int name##_initialize( name##_ref map ){ \ |
||
3912 | mejdrech | 137 | ERROR_DECLARE; \ |
138 | \ |
||
3846 | mejdrech | 139 | if( ! map ) return EINVAL; \ |
3912 | mejdrech | 140 | ERROR_PROPAGATE( char_map_initialize( & map->names )); \ |
141 | if( ERROR_OCCURRED( name##_items_initialize( & map->values ))){ \ |
||
142 | char_map_destroy( & map->names ); \ |
||
143 | return ERROR_CODE; \ |
||
144 | } \ |
||
3846 | mejdrech | 145 | map->magic = GENERIC_CHAR_MAP_MAGIC_VALUE; \ |
146 | return EOK; \ |
||
147 | } \ |
||
148 | \ |
||
149 | int name##_is_valid( name##_ref map ){ \ |
||
150 | return map && ( map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE ); \ |
||
3666 | mejdrech | 151 | } |
152 | |||
153 | #endif |
||
154 | |||
155 | /** @} |
||
156 | */ |