source: mainline/uspace/lib/c/include/adt/generic_char_map.h@ 7093a32

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7093a32 was 2544442, checked in by Jakub Jermar <jakub@…>, 15 years ago

Fix doxygen doc groups (libc vs. net) and header guards.

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/*
2 * Copyright (c) 2009 Lukas 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 libc
30 * @{
31 */
32
33/** @file
34 * Character string to generic type map.
35 */
36
37#ifndef LIBC_GENERIC_CHAR_MAP_H_
38#define LIBC_GENERIC_CHAR_MAP_H_
39
40#include <unistd.h>
41#include <errno.h>
42#include <err.h>
43
44#include <adt/char_map.h>
45#include <adt/generic_field.h>
46
47/** Internal magic value for a&nbsp;map consistency check.
48 */
49#define GENERIC_CHAR_MAP_MAGIC_VALUE 0x12345622
50
51/** Character string to generic type map declaration.
52 * @param[in] name Name of the map.
53 * @param[in] type Inner object type.
54 */
55#define GENERIC_CHAR_MAP_DECLARE(name, type) \
56 \
57GENERIC_FIELD_DECLARE(name##_items, type) \
58 \
59typedef struct name name##_t; \
60typedef name##_t * name##_ref; \
61 \
62struct name{ \
63 char_map_t names; \
64 name##_items_t values; \
65 int magic; \
66}; \
67 \
68int name##_add(name##_ref map, const char * name, const size_t length, type * value); \
69int name##_count(name##_ref map); \
70void name##_destroy(name##_ref map); \
71void name##_exclude(name##_ref map, const char * name, const size_t length); \
72type * name##_find(name##_ref map, const char * name, const size_t length); \
73int name##_initialize(name##_ref map); \
74int name##_is_valid(name##_ref map);
75
76/** Character string to generic type map implementation.
77 * Should follow declaration with the same parameters.
78 * @param[in] name Name of the map.
79 * @param[in] type Inner object type.
80 */
81#define GENERIC_CHAR_MAP_IMPLEMENT(name, type) \
82 \
83GENERIC_FIELD_IMPLEMENT(name##_items, type) \
84 \
85int name##_add(name##_ref map, const char * name, const size_t length, type * value){ \
86 ERROR_DECLARE; \
87 \
88 int index; \
89 \
90 if(! name##_is_valid(map)){ \
91 return EINVAL; \
92 } \
93 index = name##_items_add(&map->values, value); \
94 if(index < 0){ \
95 return index; \
96 } \
97 if(ERROR_OCCURRED(char_map_add(&map->names, name, length, index))){ \
98 name##_items_exclude_index(&map->values, index); \
99 return ERROR_CODE; \
100 } \
101 return EOK; \
102} \
103 \
104int name##_count(name##_ref map){ \
105 return name##_is_valid(map) ? name##_items_count(&map->values) : -1; \
106} \
107 \
108void name##_destroy(name##_ref map){ \
109 if(name##_is_valid(map)){ \
110 char_map_destroy(&map->names); \
111 name##_items_destroy(&map->values); \
112 } \
113} \
114 \
115void name##_exclude(name##_ref map, const char * name, const size_t length){ \
116 if(name##_is_valid(map)){ \
117 int index; \
118 \
119 index = char_map_exclude(&map->names, name, length); \
120 if(index != CHAR_MAP_NULL){ \
121 name##_items_exclude_index(&map->values, index); \
122 } \
123 } \
124} \
125 \
126type * name##_find(name##_ref map, const char * name, const size_t length){ \
127 if(name##_is_valid(map)){ \
128 int index; \
129 \
130 index = char_map_find(&map->names, name, length); \
131 if(index != CHAR_MAP_NULL){ \
132 return name##_items_get_index(&map->values, index); \
133 } \
134 } \
135 return NULL; \
136} \
137 \
138int name##_initialize(name##_ref map){ \
139 ERROR_DECLARE; \
140 \
141 if(! map){ \
142 return EINVAL; \
143 } \
144 ERROR_PROPAGATE(char_map_initialize(&map->names)); \
145 if(ERROR_OCCURRED(name##_items_initialize(&map->values))){ \
146 char_map_destroy(&map->names); \
147 return ERROR_CODE; \
148 } \
149 map->magic = GENERIC_CHAR_MAP_MAGIC_VALUE; \
150 return EOK; \
151} \
152 \
153int name##_is_valid(name##_ref map){ \
154 return map && (map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE); \
155}
156
157#endif
158
159/** @}
160 */
Note: See TracBrowser for help on using the repository browser.