source: mainline/uspace/lib/c/include/adt/generic_char_map.h@ 4edd39fc

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

Cstyle fixes of generic char map.

  • Property mode set to 100644
File size: 4.6 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#define GENERIC_CHAR_MAP_MAGIC_VALUE 0x12345622
49
50/** Character string to generic type map declaration.
51 * @param[in] name Name of the map.
52 * @param[in] type Inner object type.
53 */
54#define GENERIC_CHAR_MAP_DECLARE(name, type) \
55 GENERIC_FIELD_DECLARE(name##_items, type) \
56 \
57 typedef struct name name##_t; \
58 typedef name##_t *name##_ref; \
59 \
60 struct name { \
61 char_map_t names; \
62 name##_items_t values; \
63 int magic; \
64 }; \
65 \
66 int name##_add(name##_ref, const char *, const size_t, type *); \
67 int name##_count(name##_ref); \
68 void name##_destroy(name##_ref); \
69 void name##_exclude(name##_ref, const char *, const size_t); \
70 type *name##_find(name##_ref, const char *, const size_t); \
71 int name##_initialize(name##_ref); \
72 int name##_is_valid(name##_ref);
73
74/** Character string to generic type map implementation.
75 *
76 * Should follow declaration with the same parameters.
77 *
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 GENERIC_FIELD_IMPLEMENT(name##_items, type) \
83 \
84 int name##_add(name##_ref map, const char *name, const size_t length, \
85 type *value) \
86 { \
87 ERROR_DECLARE; \
88 int index; \
89 if (!name##_is_valid(map)) \
90 return EINVAL; \
91 index = name##_items_add(&map->values, value); \
92 if (index < 0) \
93 return index; \
94 if (ERROR_OCCURRED(char_map_add(&map->names, name, length, \
95 index))) { \
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) \
103 { \
104 return name##_is_valid(map) ? \
105 name##_items_count(&map->values) : -1; \
106 } \
107 \
108 void name##_destroy(name##_ref map) \
109 { \
110 if (name##_is_valid(map)) { \
111 char_map_destroy(&map->names); \
112 name##_items_destroy(&map->values); \
113 } \
114 } \
115 \
116 void name##_exclude(name##_ref map, const char *name, \
117 const size_t length) \
118 { \
119 if (name##_is_valid(map)) { \
120 int index; \
121 index = char_map_exclude(&map->names, name, length); \
122 if (index != CHAR_MAP_NULL) \
123 name##_items_exclude_index(&map->values, \
124 index); \
125 } \
126 } \
127 \
128 type *name##_find(name##_ref map, const char *name, \
129 const size_t length) \
130 { \
131 if (name##_is_valid(map)) { \
132 int index; \
133 index = char_map_find(&map->names, name, length); \
134 if( index != CHAR_MAP_NULL) \
135 return name##_items_get_index(&map->values, \
136 index); \
137 } \
138 return NULL; \
139 } \
140 \
141 int name##_initialize(name##_ref map) \
142 { \
143 ERROR_DECLARE; \
144 if (!map) \
145 return EINVAL; \
146 ERROR_PROPAGATE(char_map_initialize(&map->names)); \
147 if (ERROR_OCCURRED(name##_items_initialize(&map->values))) { \
148 char_map_destroy(&map->names); \
149 return ERROR_CODE; \
150 } \
151 map->magic = GENERIC_CHAR_MAP_MAGIC_VALUE; \
152 return EOK; \
153 } \
154 \
155 int name##_is_valid(name##_ref map) \
156 { \
157 return map && (map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE); \
158 }
159
160#endif
161
162/** @}
163 */
Note: See TracBrowser for help on using the repository browser.