Subversion Repositories HelenOS

Rev

Rev 4192 | Rev 4743 | 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
3912 mejdrech 34
 *  Character string to integer map implementation.
35
 *  @see char_map.h
3666 mejdrech 36
 */
37
 
38
#include <errno.h>
39
#include <malloc.h>
4192 mejdrech 40
#include <mem.h>
3666 mejdrech 41
#include <unistd.h>
42
 
43
#include "char_map.h"
44
 
3912 mejdrech 45
/** Internal magic value for a&nbsp;consistency check.
3846 mejdrech 46
 */
3666 mejdrech 47
#define CHAR_MAP_MAGIC_VALUE    0x12345611
48
 
3846 mejdrech 49
/** Adds the value with the key to the map.
50
 *  Creates new nodes to map the key.
51
 *  @param map The character string to integer map. Input/output parameter.
3912 mejdrech 52
 *  @param 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. Input parameter.
53
 *  @param 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. Input parameter.
3846 mejdrech 54
 *  @param value The integral value to be stored for the key character string. Input parameter.
55
 *  @returns EOK on success.
3912 mejdrech 56
 *  @returns ENOMEM if there is not enough memory left.
3846 mejdrech 57
 *  @returns EEXIST if the key character string is already used.
58
 */
59
int char_map_add_item( char_map_ref map, const char * identifier, size_t length, const int value );
3666 mejdrech 60
 
3846 mejdrech 61
/** Returns the node assigned to the key from the map.
62
 *  @param map The character string to integer map. Input parameter.
3912 mejdrech 63
 *  @param 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. Input parameter.
64
 *  @param 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. Input parameter.
3846 mejdrech 65
 *  @returns The node holding the integral value assigned to the key character string.
66
 *  @returns NULL if the key is not assigned a&nbsp;node.
67
 */
68
char_map_ref    char_map_find_node( const char_map_ref map, const char * identifier, const size_t length );
69
 
70
/** Checks if the map is valid.
71
 *  @param map The character string to integer map. Input parameter.
72
 *  @returns TRUE if the map is valid.
73
 *  @returns FALSE otherwise.
74
 */
75
int char_map_is_valid( const char_map_ref map );
76
 
77
int char_map_add( char_map_ref map, const char * identifier, size_t length, const int value ){
78
    if( char_map_is_valid( map ) && ( identifier ) && (( length ) || ( * identifier ))){
3666 mejdrech 79
        int index;
80
 
81
        for( index = 0; index < map->next; ++ index ){
82
            if( map->items[ index ]->c == * identifier ){
83
                ++ identifier;
3846 mejdrech 84
                if( length ) -- length;
85
                if( length || ( * identifier )){
86
                    return char_map_add( map->items[ index ], identifier, length, value );
3666 mejdrech 87
                }else{
88
                    if( map->items[ index ]->value != CHAR_MAP_NULL ) return EEXISTS;
89
                    map->items[ index ]->value = value;
90
                    return EOK;
91
                }
92
            }
93
        }
3846 mejdrech 94
        return char_map_add_item( map, identifier, length, value );
3666 mejdrech 95
    }
96
    return EINVAL;
97
}
98
 
3846 mejdrech 99
int char_map_add_item( char_map_ref map, const char * identifier, size_t length, const int value ){
3666 mejdrech 100
    if( map->next == ( map->size - 1 )){
101
        char_map_ref    * tmp;
102
 
4197 mejdrech 103
        tmp = ( char_map_ref * ) realloc( map->items, sizeof( char_map_ref ) * 2 * map->size );
3666 mejdrech 104
        if( ! tmp ) return ENOMEM;
105
        map->size *= 2;
106
        map->items = tmp;
107
    }
108
    map->items[ map->next ] = ( char_map_ref ) malloc( sizeof( char_map_t ));
109
    if( ! map->items[ map->next ] ) return ENOMEM;
4197 mejdrech 110
    if( char_map_initialize( map->items[ map->next ] ) != EOK ){
111
        free( map->items[ map->next ] );
112
        map->items[ map->next ] = NULL;
113
        return ENOMEM;
114
    }
3666 mejdrech 115
    map->items[ map->next ]->c = * identifier;
116
    ++ identifier;
3846 mejdrech 117
    if( length ) -- length;
118
    ++ map->next;
119
    if( length || ( * identifier )){
120
        map->items[ map->next - 1 ]->value = CHAR_MAP_NULL;
121
        return char_map_add_item( map->items[ map->next - 1 ], identifier, length, value );
3666 mejdrech 122
    }else{
3846 mejdrech 123
        map->items[ map->next - 1 ]->value = value;
3666 mejdrech 124
    }
125
    return EOK;
126
}
127
 
128
void char_map_destroy( char_map_ref map ){
129
    if( char_map_is_valid( map )){
130
        int index;
131
 
132
        map->magic = 0;
133
        for( index = 0; index < map->next; ++ index ){
134
            char_map_destroy( map->items[ index ] );
135
        }
136
        free( map->items );
4197 mejdrech 137
        map->items = NULL;
3666 mejdrech 138
    }
139
}
140
 
3846 mejdrech 141
int char_map_exclude( char_map_ref map, const char * identifier, size_t length ){
3666 mejdrech 142
    char_map_ref    node;
143
 
3846 mejdrech 144
    node = char_map_find_node( map, identifier, length );
3666 mejdrech 145
    if( node ){
146
        int value;
147
 
148
        value = node->value;
149
        node->value = CHAR_MAP_NULL;
150
        return value;
151
    }
152
    return CHAR_MAP_NULL;
153
}
154
 
3846 mejdrech 155
int char_map_find( const char_map_ref map, const char * identifier, size_t length ){
3666 mejdrech 156
    char_map_ref    node;
157
 
3846 mejdrech 158
    node = char_map_find_node( map, identifier, length );
3666 mejdrech 159
    return node ? node->value : CHAR_MAP_NULL;
160
}
161
 
3846 mejdrech 162
char_map_ref char_map_find_node( const char_map_ref map, const char * identifier, size_t length ){
3666 mejdrech 163
    if( ! char_map_is_valid( map )) return NULL;
3846 mejdrech 164
    if( length ) -- length;
165
    if( length || ( * identifier )){
3666 mejdrech 166
        int index;
167
 
168
        for( index = 0; index < map->next; ++ index ){
169
            if( map->items[ index ]->c == * identifier ){
170
                ++ identifier;
3846 mejdrech 171
                return char_map_find_node( map->items[ index ], identifier, length );
3666 mejdrech 172
            }
173
        }
174
        return NULL;
175
    }
176
    return map;
177
}
178
 
3846 mejdrech 179
int char_map_get_value( const char_map_ref map ){
3666 mejdrech 180
    return char_map_is_valid( map ) ? map->value : CHAR_MAP_NULL;
181
}
182
 
183
int char_map_initialize( char_map_ref map ){
184
    if( ! map ) return EINVAL;
4197 mejdrech 185
    map->c = '\0';
3666 mejdrech 186
    map->value = CHAR_MAP_NULL;
187
    map->size = 2;
188
    map->next = 0;
189
    map->items = malloc( sizeof( char_map_ref ) * map->size );
4197 mejdrech 190
    if( ! map->items ){
191
        map->magic = 0;
192
        return ENOMEM;
193
    }
3666 mejdrech 194
    map->items[ map->next ] = NULL;
195
    map->magic = CHAR_MAP_MAGIC_VALUE;
196
    return EOK;
197
}
198
 
3846 mejdrech 199
int char_map_is_valid( const char_map_ref map ){
3666 mejdrech 200
    return map && ( map->magic == CHAR_MAP_MAGIC_VALUE );
201
}
202
 
3846 mejdrech 203
int char_map_update( char_map_ref map, const char * identifier, const size_t length, const int value ){
3666 mejdrech 204
    char_map_ref    node;
205
 
206
//  if( ! char_map_is_valid( map )) return EINVAL;
3846 mejdrech 207
    node = char_map_find_node( map, identifier, length );
3666 mejdrech 208
    if( node ){
209
        node->value = value;
210
        return EOK;
211
    }else{
3846 mejdrech 212
        return char_map_add( map, identifier, length, value );
3666 mejdrech 213
    }
214
}
215
 
216
/** @}
217
 */