Subversion Repositories HelenOS

Rev

Rev 3846 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3666 mejdrech 1
/*
2
 * Copyright (c) 2008 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
 */
35
 
36
#include <errno.h>
37
#include <malloc.h>
38
#include <unistd.h>
39
#include <string.h>
40
 
41
#include "char_map.h"
42
 
43
#define CHAR_MAP_MAGIC_VALUE    0x12345611
44
 
45
int char_map_add_item( char_map_ref map, const char * identifier, const int value );
46
char_map_ref    char_map_find_node( char_map_ref map, const char * identifier );
47
int char_map_is_valid( char_map_ref map );
48
 
49
int char_map_add( char_map_ref map, const char * identifier, const int value ){
50
    if( char_map_is_valid( map ) && ( * identifier )){
51
        int index;
52
 
53
        for( index = 0; index < map->next; ++ index ){
54
            if( map->items[ index ]->c == * identifier ){
55
                ++ identifier;
56
                if( * identifier ){
57
                    return char_map_add( map->items[ index ], identifier, value );
58
                }else{
59
                    if( map->items[ index ]->value != CHAR_MAP_NULL ) return EEXISTS;
60
                    map->items[ index ]->value = value;
61
                    return EOK;
62
                }
63
            }
64
        }
65
        return char_map_add_item( map, identifier, value );
66
    }
67
    return EINVAL;
68
}
69
 
70
int char_map_add_item( char_map_ref map, const char * identifier, const int value ){
71
    if( map->next == ( map->size - 1 )){
72
        char_map_ref    * tmp;
73
 
74
        tmp = ( char_map_ref * ) malloc( sizeof( char_map_ref ) * 2 * map->size );
75
        if( ! tmp ) return ENOMEM;
76
        map->size *= 2;
77
        memcpy( tmp, map->items, sizeof( char_map_ref ) * map->next );
78
        free( map->items );
79
        map->items = tmp;
80
    }
81
    map->items[ map->next ] = ( char_map_ref ) malloc( sizeof( char_map_t ));
82
    if( ! map->items[ map->next ] ) return ENOMEM;
83
    char_map_initialize( map->items[ map->next ] );
84
    map->items[ map->next ]->c = * identifier;
85
    ++ identifier;
86
    if( * identifier ){
87
        map->items[ map->next ]->value = CHAR_MAP_NULL;
88
        char_map_add_item( map->items[ map->next ], identifier, value );
89
    }else{
90
        map->items[ map->next ]->value = value;
91
    }
92
    ++ map->next;
93
    return EOK;
94
}
95
 
96
void char_map_destroy( char_map_ref map ){
97
    if( char_map_is_valid( map )){
98
        int index;
99
 
100
        map->magic = 0;
101
        for( index = 0; index < map->next; ++ index ){
102
            char_map_destroy( map->items[ index ] );
103
        }
104
        free( map->items );
105
    }
106
}
107
 
108
int char_map_exclude( char_map_ref map, const char * identifier ){
109
    char_map_ref    node;
110
 
111
    node = char_map_find_node( map, identifier );
112
    if( node ){
113
        int value;
114
 
115
        value = node->value;
116
        node->value = CHAR_MAP_NULL;
117
        return value;
118
    }
119
    return CHAR_MAP_NULL;
120
}
121
 
122
int char_map_find( char_map_ref map, const char * identifier ){
123
    char_map_ref    node;
124
 
125
    node = char_map_find_node( map, identifier );
126
    return node ? node->value : CHAR_MAP_NULL;
127
}
128
 
129
char_map_ref char_map_find_node( char_map_ref map, const char * identifier ){
130
    if( ! char_map_is_valid( map )) return NULL;
131
    if( * identifier ){
132
        int index;
133
 
134
        for( index = 0; index < map->next; ++ index ){
135
            if( map->items[ index ]->c == * identifier ){
136
                ++ identifier;
137
                return char_map_find_node( map->items[ index ], identifier );
138
            }
139
        }
140
        return NULL;
141
    }
142
    return map;
143
}
144
 
145
int char_map_get_value( char_map_ref map ){
146
    return char_map_is_valid( map ) ? map->value : CHAR_MAP_NULL;
147
}
148
 
149
int char_map_initialize( char_map_ref map ){
150
    if( ! map ) return EINVAL;
151
    map->c = 0;
152
    map->value = CHAR_MAP_NULL;
153
    map->size = 2;
154
    map->next = 0;
155
    map->items = malloc( sizeof( char_map_ref ) * map->size );
156
    if( ! map->items ) return ENOMEM;
157
    map->items[ map->next ] = NULL;
158
    map->magic = CHAR_MAP_MAGIC_VALUE;
159
    return EOK;
160
}
161
 
162
int char_map_is_valid( char_map_ref map ){
163
    return map && ( map->magic == CHAR_MAP_MAGIC_VALUE );
164
}
165
 
166
int char_map_update( char_map_ref map, const char * identifier, const int value ){
167
    char_map_ref    node;
168
 
169
//  if( ! char_map_is_valid( map )) return EINVAL;
170
    node = char_map_find_node( map, identifier );
171
    if( node ){
172
        node->value = value;
173
        return EOK;
174
    }else{
175
        return char_map_add( map, identifier, value );
176
    }
177
}
178
 
179
/** @}
180
 */