Subversion Repositories HelenOS

Rev

Rev 3666 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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