Subversion Repositories HelenOS

Rev

Rev 3666 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3666 Rev 3846
Line 25... Line 25...
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
 */
34
 */
35
 
35
 
Line 42... Line 42...
42
#include "err.h"
42
#include "err.h"
43
#include "generic_field.h"
43
#include "generic_field.h"
44
 
44
 
45
#define GENERIC_CHAR_MAP_MAGIC_VALUE    0x12345622
45
#define GENERIC_CHAR_MAP_MAGIC_VALUE    0x12345622
46
 
46
 
47
#define GENERIC_CHAR_MAP_DECLARE( name, type )                  \
47
#define GENERIC_CHAR_MAP_DECLARE( name, type )                                  \
48
                                        \
48
                                                                                \
49
GENERIC_FIELD_DECLARE( name##_items, type )                 \
49
GENERIC_FIELD_DECLARE( name##_items, type )                                     \
50
                                        \
50
                                                                                \
51
typedef struct name     name##_t;                   \
51
typedef struct name     name##_t;                                               \
52
typedef name##_t *      name##_ref;                 \
52
typedef name##_t *      name##_ref;                                             \
53
                                        \
53
                                                                                \
54
struct  name{                                   \
54
struct  name{                                                                   \
55
    char_map_t  names;                          \
55
    char_map_t      names;                                                      \
56
    name##_items_t  values;                         \
56
    name##_items_t  values;                                                     \
57
    int magic;                              \
57
    int             magic;                                                      \
58
};                                      \
58
};                                                                              \
59
                                        \
59
                                                                                \
60
int name##_add( name##_ref map, const char * name, type * value );      \
60
int name##_add( name##_ref map, const char * name, const size_t length, type * value ); \
61
int name##_count( name##_ref map );                     \
61
int name##_count( name##_ref map );                                             \
62
void    name##_destroy( name##_ref map );                   \
62
void    name##_destroy( name##_ref map );                                       \
63
void    name##_exclude( name##_ref map, const char * name );            \
63
void    name##_exclude( name##_ref map, const char * name, const size_t length );   \
64
type *  name##_find( name##_ref map, const char * name );           \
64
type *  name##_find( name##_ref map, const char * name, const size_t length );  \
65
type *  name##_get_index( name##_ref map, int index );              \
65
type *  name##_get_index( name##_ref map, int index );                          \
66
int name##_initialize( name##_ref map );                    \
66
int name##_initialize( name##_ref map );                                        \
67
int name##_is_valid( name##_ref map );
67
int name##_is_valid( name##_ref map );
68
 
68
 
69
 
-
 
70
#define GENERIC_CHAR_MAP_IMPLEMENT( name, type )                \
69
#define GENERIC_CHAR_MAP_IMPLEMENT( name, type )                                \
71
                                        \
70
                                                                                \
72
GENERIC_FIELD_IMPLEMENT( name##_items, type )                   \
71
GENERIC_FIELD_IMPLEMENT( name##_items, type )                                   \
73
                                        \
72
                                                                                \
74
int name##_add( name##_ref map, const char * name, type * value ){      \
73
int name##_add( name##_ref map, const char * name, const size_t length, type * value ){ \
75
    ERROR_DECLARE;                              \
74
    ERROR_DECLARE;                                                              \
-
 
75
                                                                                \
76
    int index;                              \
76
    int index;                                                                  \
77
                                        \
77
                                                                                \
78
    if( ! name##_is_valid( map )) return EINVAL;                \
78
    if( ! name##_is_valid( map )) return EINVAL;                                \
79
    index = name##_items_add( & map->values, value );           \
79
    index = name##_items_add( & map->values, value );                           \
80
    if( index < 0 ) return index;                       \
80
    if( index < 0 ) return index;                                               \
81
    if( ERROR_OCCURED( char_map_add( & map->names, name, index ))){     \
81
    if( ERROR_OCCURED( char_map_add( & map->names, name, length, index ))){     \
82
        name##_items_exclude_index( & map->values, index );     \
82
        name##_items_exclude_index( & map->values, index );                     \
83
        return ERROR_CODE;                      \
83
        return ERROR_CODE;                                                      \
84
    }                                   \
84
    }                                                                           \
85
    return EOK;                             \
85
    return EOK;                                                                 \
86
}                                       \
86
}                                                                               \
87
                                        \
87
                                                                                \
88
int name##_count( name##_ref map ){                     \
88
int name##_count( name##_ref map ){                                             \
89
    return name##_is_valid( map ) ? name##_items_count( & map->values ) : -1;   \
89
    return name##_is_valid( map ) ? name##_items_count( & map->values ) : -1;   \
90
}                                       \
90
}                                                                               \
91
                                        \
91
                                                                                \
92
void name##_destroy( name##_ref map ){                      \
92
void name##_destroy( name##_ref map ){                                          \
93
    if( name##_is_valid( map )){                        \
93
    if( name##_is_valid( map )){                                                \
94
        char_map_destroy( & map->names );               \
94
        char_map_destroy( & map->names );                                       \
95
        name##_items_destroy( & map->values );              \
95
        name##_items_destroy( & map->values );                                  \
96
    }                                   \
96
    }                                                                           \
97
}                                       \
97
}                                                                               \
98
                                        \
98
                                                                                \
99
void name##_exclude( name##_ref map, const char * name ){           \
99
void name##_exclude( name##_ref map, const char * name, const size_t length ){  \
100
    if( name##_is_valid( map )){                        \
100
    if( name##_is_valid( map )){                                                \
101
        int index;                          \
101
        int index;                                                              \
102
                                        \
102
                                                                                \
103
        index = char_map_exclude( & map->names, name );         \
103
        index = char_map_exclude( & map->names, name, length );                 \
104
        if( index != CHAR_MAP_NULL ){                   \
104
        if( index != CHAR_MAP_NULL ){                                           \
105
            name##_items_exclude_index( & map->values, index ); \
105
            name##_items_exclude_index( & map->values, index );                 \
106
        }                               \
106
        }                                                                       \
107
    }                                   \
107
    }                                                                           \
108
}                                       \
108
}                                                                               \
109
                                        \
109
                                                                                \
110
type * name##_find( name##_ref map, const char * name ){            \
110
type * name##_find( name##_ref map, const char * name, const size_t length ){   \
111
    if( name##_is_valid( map )){                        \
111
    if( name##_is_valid( map )){                                                \
112
        int index;                          \
112
        int index;                                                              \
113
                                        \
113
                                                                                \
114
        index = char_map_find( & map->names, name );            \
114
        index = char_map_find( & map->names, name, length );                    \
115
        if( index != CHAR_MAP_NULL ){                   \
115
        if( index != CHAR_MAP_NULL ){                                           \
116
            return name##_items_get_index( & map->values, index );  \
116
            return name##_items_get_index( & map->values, index );              \
117
        }                               \
117
        }                                                                       \
118
    }                                   \
118
    }                                                                           \
119
    return NULL;                                \
119
    return NULL;                                                                \
120
}                                       \
120
}                                                                               \
121
                                        \
121
                                                                                \
122
int name##_initialize( name##_ref map ){                    \
122
int name##_initialize( name##_ref map ){                                        \
123
    if( ! map ) return EINVAL;                      \
123
    if( ! map ) return EINVAL;                                                  \
124
    char_map_initialize( & map->names );                    \
124
    char_map_initialize( & map->names );                                        \
125
    name##_items_initialize( & map->values );               \
125
    name##_items_initialize( & map->values );                                   \
126
    map->magic = GENERIC_CHAR_MAP_MAGIC_VALUE;              \
126
    map->magic = GENERIC_CHAR_MAP_MAGIC_VALUE;                                  \
127
    return EOK;                             \
127
    return EOK;                                                                 \
128
}                                       \
128
}                                                                               \
129
                                        \
129
                                                                                \
130
int name##_is_valid( name##_ref map ){                      \
130
int name##_is_valid( name##_ref map ){                                          \
131
    return map && ( map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE );       \
131
    return map && ( map->magic == GENERIC_CHAR_MAP_MAGIC_VALUE );               \
132
}
132
}
133
 
133
 
134
#endif
134
#endif
135
 
135
 
136
/** @}
136
/** @}