Subversion Repositories HelenOS

Rev

Rev 4733 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4733 Rev 4756
1
/*
1
/*
2
 * Copyright (c) 2009 Lukas Mejdrech
2
 * Copyright (c) 2009 Lukas Mejdrech
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup net
29
/** @addtogroup net
30
 *  @{
30
 *  @{
31
 */
31
 */
32
 
32
 
33
/** @file
33
/** @file
34
 *  Character string to generic type map.
34
 *  Character string to generic type map.
35
 */
35
 */
36
 
36
 
37
#ifndef __GENERIC_CHAR_MAP_H__
37
#ifndef __GENERIC_CHAR_MAP_H__
38
#define __GENERIC_CHAR_MAP_H__
38
#define __GENERIC_CHAR_MAP_H__
39
 
39
 
40
#include <errno.h>
40
#include <errno.h>
41
#include <unistd.h>
41
#include <unistd.h>
42
 
42
 
43
#include "../err.h"
43
#include "../err.h"
44
 
44
 
45
#include "char_map.h"
45
#include "char_map.h"
46
#include "generic_field.h"
46
#include "generic_field.h"
47
 
47
 
48
/** Internal magic value for a&nbsp;map consistency check.
48
/** Internal magic value for a&nbsp;map consistency check.
49
 */
49
 */
50
#define GENERIC_CHAR_MAP_MAGIC_VALUE    0x12345622
50
#define GENERIC_CHAR_MAP_MAGIC_VALUE    0x12345622
51
 
51
 
52
/** Character string to generic type map declaration.
52
/** Character string to generic type map declaration.
53
 *  @param name Name of the map. Input parameter.
53
 *  @param[in] name Name of the map.
54
 *  @param type Inner object type. Input parameter
54
 *  @param[in] type Inner object type.
55
 */
55
 */
56
#define GENERIC_CHAR_MAP_DECLARE( name, type )                                  \
56
#define GENERIC_CHAR_MAP_DECLARE( name, type )                                  \
57
                                                                                \
57
                                                                                \
58
GENERIC_FIELD_DECLARE( name##_items, type )                                     \
58
GENERIC_FIELD_DECLARE( name##_items, type )                                     \
59
                                                                                \
59
                                                                                \
60
typedef struct name     name##_t;                                               \
60
typedef struct name     name##_t;                                               \
61
typedef name##_t *      name##_ref;                                             \
61
typedef name##_t *      name##_ref;                                             \
62
                                                                                \
62
                                                                                \
63
struct  name{                                                                   \
63
struct  name{                                                                   \
64
    char_map_t      names;                                                      \
64
    char_map_t      names;                                                      \
65
    name##_items_t  values;                                                     \
65
    name##_items_t  values;                                                     \
66
    int             magic;                                                      \
66
    int             magic;                                                      \
67
};                                                                              \
67
};                                                                              \
68
                                                                                \
68
                                                                                \
69
int name##_add( name##_ref map, const char * name, const size_t length, type * value ); \
69
int name##_add( name##_ref map, const char * name, const size_t length, type * value ); \
70
int name##_count( name##_ref map );                                             \
70
int name##_count( name##_ref map );                                             \
71
void    name##_destroy( name##_ref map );                                       \
71
void    name##_destroy( name##_ref map );                                       \
72
void    name##_exclude( name##_ref map, const char * name, const size_t length );   \
72
void    name##_exclude( name##_ref map, const char * name, const size_t length );   \
73
type *  name##_find( name##_ref map, const char * name, const size_t length );  \
73
type *  name##_find( name##_ref map, const char * name, const size_t length );  \
74
int name##_initialize( name##_ref map );                                        \
74
int name##_initialize( name##_ref map );                                        \
75
int name##_is_valid( name##_ref map );
75
int name##_is_valid( name##_ref map );
76
 
76
 
77
/** Character string to generic type map implementation.
77
/** Character string to generic type map implementation.
78
 *  Should follow declaration with the same parameters.
78
 *  Should follow declaration with the same parameters.
79
 *  @param name Name of the map. Input parameter.
79
 *  @param[in] name Name of the map.
80
 *  @param type Inner object type. Input parameter
80
 *  @param[in] type Inner object type.
81
 */
81
 */
82
#define GENERIC_CHAR_MAP_IMPLEMENT( name, type )                                \
82
#define GENERIC_CHAR_MAP_IMPLEMENT( name, type )                                \
83
                                                                                \
83
                                                                                \
84
GENERIC_FIELD_IMPLEMENT( name##_items, type )                                   \
84
GENERIC_FIELD_IMPLEMENT( name##_items, type )                                   \
85
                                                                                \
85
                                                                                \
86
int name##_add( name##_ref map, const char * name, const size_t length, type * value ){ \
86
int name##_add( name##_ref map, const char * name, const size_t length, type * value ){ \
87
    ERROR_DECLARE;                                                              \
87
    ERROR_DECLARE;                                                              \
88
                                                                                \
88
                                                                                \
89
    int index;                                                                  \
89
    int index;                                                                  \
90
                                                                                \
90
                                                                                \
91
    if( ! name##_is_valid( map )) return EINVAL;                                \
91
    if( ! name##_is_valid( map )) return EINVAL;                                \
92
    index = name##_items_add( & map->values, value );                           \
92
    index = name##_items_add( & map->values, value );                           \
93
    if( index < 0 ) return index;                                               \
93
    if( index < 0 ) return index;                                               \
94
    if( ERROR_OCCURRED( char_map_add( & map->names, name, length, index ))){        \
94
    if( ERROR_OCCURRED( char_map_add( & map->names, name, length, index ))){        \
95
        name##_items_exclude_index( & map->values, index );                     \
95
        name##_items_exclude_index( & map->values, index );                     \
96
        return ERROR_CODE;                                                      \
96
        return ERROR_CODE;                                                      \
97
    }                                                                           \
97
    }                                                                           \
98
    return EOK;                                                                 \
98
    return EOK;                                                                 \
99
}                                                                               \
99
}                                                                               \
100
                                                                                \
100
                                                                                \
101
int name##_count( name##_ref map ){                                             \
101
int name##_count( name##_ref map ){                                             \
102
    return name##_is_valid( map ) ? name##_items_count( & map->values ) : -1;   \
102
    return name##_is_valid( map ) ? name##_items_count( & map->values ) : -1;   \
103
}                                                                               \
103
}                                                                               \
104
                                                                                \
104
                                                                                \
105
void name##_destroy( name##_ref map ){                                          \
105
void name##_destroy( name##_ref map ){                                          \
106
    if( name##_is_valid( map )){                                                \
106
    if( name##_is_valid( map )){                                                \
107
        char_map_destroy( & map->names );                                       \
107
        char_map_destroy( & map->names );                                       \
108
        name##_items_destroy( & map->values );                                  \
108
        name##_items_destroy( & map->values );                                  \
109
    }                                                                           \
109
    }                                                                           \
110
}                                                                               \
110
}                                                                               \
111
                                                                                \
111
                                                                                \
112
void name##_exclude( name##_ref map, const char * name, const size_t length ){  \
112
void name##_exclude( name##_ref map, const char * name, const size_t length ){  \
113
    if( name##_is_valid( map )){                                                \
113
    if( name##_is_valid( map )){                                                \
114
        int index;                                                              \
114
        int index;                                                              \
115
                                                                                \
115
                                                                                \
116
        index = char_map_exclude( & map->names, name, length );                 \
116
        index = char_map_exclude( & map->names, name, length );                 \
117
        if( index != CHAR_MAP_NULL ){                                           \
117
        if( index != CHAR_MAP_NULL ){                                           \
118
            name##_items_exclude_index( & map->values, index );                 \
118
            name##_items_exclude_index( & map->values, index );                 \
119
        }                                                                       \
119
        }                                                                       \
120
    }                                                                           \
120
    }                                                                           \
121
}                                                                               \
121
}                                                                               \
122
                                                                                \
122
                                                                                \
123
type * name##_find( name##_ref map, const char * name, const size_t length ){   \
123
type * name##_find( name##_ref map, const char * name, const size_t length ){   \
124
    if( name##_is_valid( map )){                                                \
124
    if( name##_is_valid( map )){                                                \
125
        int index;                                                              \
125
        int index;                                                              \
126
                                                                                \
126
                                                                                \
127
        index = char_map_find( & map->names, name, length );                    \
127
        index = char_map_find( & map->names, name, length );                    \
128
        if( index != CHAR_MAP_NULL ){                                           \
128
        if( index != CHAR_MAP_NULL ){                                           \
129
            return name##_items_get_index( & map->values, index );              \
129
            return name##_items_get_index( & map->values, index );              \
130
        }                                                                       \
130
        }                                                                       \
131
    }                                                                           \
131
    }                                                                           \
132
    return NULL;                                                                \
132
    return NULL;                                                                \
133
}                                                                               \
133
}                                                                               \
134
                                                                                \
134
                                                                                \
135
int name##_initialize( name##_ref map ){                                        \
135
int name##_initialize( name##_ref map ){                                        \
136
    ERROR_DECLARE;                                                              \
136
    ERROR_DECLARE;                                                              \
137
                                                                                \
137
                                                                                \
138
    if( ! map ) return EINVAL;                                                  \
138
    if( ! map ) return EINVAL;                                                  \
139
    ERROR_PROPAGATE( char_map_initialize( & map->names ));                      \
139
    ERROR_PROPAGATE( char_map_initialize( & map->names ));                      \
140
    if( ERROR_OCCURRED( name##_items_initialize( & map->values ))){             \
140
    if( ERROR_OCCURRED( name##_items_initialize( & map->values ))){             \
141
        char_map_destroy( & map->names );                                       \
141
        char_map_destroy( & map->names );                                       \
142
        return ERROR_CODE;                                                      \
142
        return ERROR_CODE;                                                      \
143
    }                                                                           \
143
    }                                                                           \
144
    map->magic = GENERIC_CHAR_MAP_MAGIC_VALUE;                                  \
144
    map->magic = GENERIC_CHAR_MAP_MAGIC_VALUE;                                  \
145
    return EOK;                                                                 \
145
    return EOK;                                                                 \
146
}                                                                               \
146
}                                                                               \
147
                                                                                \
147
                                                                                \
148
int name##_is_valid( name##_ref map ){                                          \
148
int name##_is_valid( name##_ref map ){                                          \
149
    return map && ( map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE );               \
149
    return map && ( map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE );               \
150
}
150
}
151
 
151
 
152
#endif
152
#endif
153
 
153
 
154
/** @}
154
/** @}
155
 */
155
 */
156
 
156