[21580dd] | 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 net
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
| 34 | * Character string to integer map.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #ifndef __CHAR_MAP_H__
|
---|
| 38 | #define __CHAR_MAP_H__
|
---|
| 39 |
|
---|
| 40 | /** Invalid assigned value used also if an entry does not exist.
|
---|
| 41 | */
|
---|
[aadf01e] | 42 | #define CHAR_MAP_NULL (-1)
|
---|
[21580dd] | 43 |
|
---|
| 44 | /** Type definition of the character string to integer map.
|
---|
| 45 | * @see char_map
|
---|
| 46 | */
|
---|
| 47 | typedef struct char_map char_map_t;
|
---|
| 48 |
|
---|
| 49 | /** Type definition of the character string to integer map pointer.
|
---|
| 50 | * @see char_map
|
---|
| 51 | */
|
---|
| 52 | typedef char_map_t * char_map_ref;
|
---|
| 53 |
|
---|
| 54 | /** Character string to integer map item.
|
---|
| 55 | * This structure recursivelly contains itself as a character by character tree.
|
---|
| 56 | * The actually mapped character string consists o fall the parent characters and the actual one.
|
---|
| 57 | */
|
---|
| 58 | struct char_map{
|
---|
| 59 | /** Actually mapped character.
|
---|
| 60 | */
|
---|
[aadf01e] | 61 | char c;
|
---|
[21580dd] | 62 | /** Stored integral value.
|
---|
| 63 | */
|
---|
[aadf01e] | 64 | int value;
|
---|
[21580dd] | 65 | /** Next character array size.
|
---|
| 66 | */
|
---|
[aadf01e] | 67 | int size;
|
---|
[21580dd] | 68 | /** First free position in the next character array.
|
---|
| 69 | */
|
---|
[aadf01e] | 70 | int next;
|
---|
[21580dd] | 71 | /** Next character array.
|
---|
| 72 | */
|
---|
[aadf01e] | 73 | char_map_ref * items;
|
---|
[21580dd] | 74 | /** Consistency check magic value.
|
---|
| 75 | */
|
---|
[aadf01e] | 76 | int magic;
|
---|
[21580dd] | 77 | };
|
---|
| 78 |
|
---|
| 79 | /** Adds the value with the key to the map.
|
---|
| 80 | * @param[in,out] map The character string to integer map.
|
---|
| 81 | * @param[in] 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.
|
---|
| 82 | * @param[in] 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.
|
---|
| 83 | * @param[in] value The integral value to be stored for the key character string.
|
---|
| 84 | * @returns EOK on success.
|
---|
| 85 | * @returns EINVAL if the map is not valid.
|
---|
| 86 | * @returns EINVAL if the identifier parameter is NULL.
|
---|
| 87 | * @returns EINVAL if the length parameter zero (0) and the identifier parameter is an empty character string (the first character is the terminating zero ('\\0') character.
|
---|
| 88 | * @returns EEXIST if the key character string is already used.
|
---|
| 89 | * @returns Other error codes as defined for the char_map_add_item() function.
|
---|
| 90 | */
|
---|
[aadf01e] | 91 | int char_map_add(char_map_ref map, const char * identifier, size_t length, const int value);
|
---|
[21580dd] | 92 |
|
---|
| 93 | /** Clears and destroys the map.
|
---|
| 94 | * @param[in,out] map The character string to integer map.
|
---|
| 95 | */
|
---|
[aadf01e] | 96 | void char_map_destroy(char_map_ref map);
|
---|
[21580dd] | 97 |
|
---|
| 98 | /** Excludes the value assigned to the key from the map.
|
---|
| 99 | * The entry is cleared from the map.
|
---|
| 100 | * @param[in,out] map The character string to integer map.
|
---|
| 101 | * @param[in] 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.
|
---|
| 102 | * @param[in] 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.
|
---|
| 103 | * @returns The integral value assigned to the key character string.
|
---|
| 104 | * @returns CHAR_MAP_NULL if the key is not assigned a value.
|
---|
| 105 | */
|
---|
[aadf01e] | 106 | int char_map_exclude(char_map_ref map, const char * identifier, size_t length);
|
---|
[21580dd] | 107 |
|
---|
| 108 | /** Returns the value assigned to the key from the map.
|
---|
| 109 | * @param[in] map The character string to integer map.
|
---|
| 110 | * @param[in] 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.
|
---|
| 111 | * @param[in] 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.
|
---|
| 112 | * @returns The integral value assigned to the key character string.
|
---|
| 113 | * @returns CHAR_MAP_NULL if the key is not assigned a value.
|
---|
| 114 | */
|
---|
[aadf01e] | 115 | int char_map_find(const char_map_ref map, const char * identifier, size_t length);
|
---|
[21580dd] | 116 |
|
---|
| 117 | /** Initializes the map.
|
---|
| 118 | * @param[in,out] map The character string to integer map.
|
---|
| 119 | * @returns EOK on success.
|
---|
| 120 | * @returns EINVAL if the map parameter is NULL.
|
---|
| 121 | * @returns ENOMEM if there is not enough memory left.
|
---|
| 122 | */
|
---|
[aadf01e] | 123 | int char_map_initialize(char_map_ref map);
|
---|
[21580dd] | 124 |
|
---|
| 125 | /** Adds or updates the value with the key to the map.
|
---|
| 126 | * @param[in,out] map The character string to integer map.
|
---|
| 127 | * @param[in] 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.
|
---|
| 128 | * @param[in] 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.
|
---|
| 129 | * @param[in] value The integral value to be stored for the key character string.
|
---|
| 130 | * @returns EOK on success.
|
---|
| 131 | * @returns EINVAL if the map is not valid.
|
---|
| 132 | * @returns EINVAL if the identifier parameter is NULL.
|
---|
| 133 | * @returns EINVAL if the length parameter zero (0) and the identifier parameter is an empty character string (the first character is the terminating zero ('\\0) character.
|
---|
| 134 | * @returns EEXIST if the key character string is already used.
|
---|
| 135 | * @returns Other error codes as defined for the char_map_add_item() function.
|
---|
| 136 | */
|
---|
[aadf01e] | 137 | int char_map_update(char_map_ref map, const char * identifier, size_t length, const int value);
|
---|
[21580dd] | 138 |
|
---|
| 139 | #endif
|
---|
| 140 |
|
---|
| 141 | /** @}
|
---|
| 142 | */
|
---|