modules/rpsl/class.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following functions.
  1. my_g_str_hash
  2. my_g_strcasecmp
  3. class_init
  4. get_class_names
  5. class_lookup
  6. class_attr_lookup
  7. attr_hash

   1 /******************
   2   Copyright (c) 2002                                        RIPE NCC
   3 
   4   All Rights Reserved
   5 
   6   Permission to use, copy, modify, and distribute this software and its
   7   documentation for any purpose and without fee is hereby granted,
   8   provided that the above copyright notice appear in all copies and that
   9   both that copyright notice and this permission notice appear in
  10   supporting documentation, and that the name of the author not be
  11   used in advertising or publicity pertaining to distribution of the
  12   software without specific, written prior permission.
  13 
  14   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  15   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
  16   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  17   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  18   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  19   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  20   ***************************************/
  21 
  22 #include <stdio.h>
  23 
  24 #include <glib.h>
  25 #include <string.h>
  26 #include <pthread.h>
  27 #include "class.h"
  28 #include "class_tab.h"
  29 #include "attribute.h"
  30 
  31 /* defined in attribute_tab.h */
  32 /*extern attribute_t attribute_tab[];*/
  33 
  34 #define CLASS_TAB_LEN (sizeof(class_tab)/sizeof(class_tab[0]))
  35 
  36 /* array with only lists of class names */
  37 static char** class_names;
  38 
  39 /* hash mapping class name to entry in class_tab[] */
  40 static GHashTable *class_hash;
  41 
  42 static guint
  43 my_g_str_hash (gconstpointer v)
     /* [<][>][^][v][top][bottom][index][help] */
  44 {
  45     gchar *s;
  46     guint hash;
  47 
  48     s = g_strdup(v);
  49     g_strdown(s);
  50     hash = g_str_hash(s);
  51     g_free(s);
  52     return hash;
  53 }
  54 
  55 static gint 
  56 my_g_strcasecmp (gconstpointer a, gconstpointer b)
     /* [<][>][^][v][top][bottom][index][help] */
  57 {
  58     return (g_strcasecmp(a, b) == 0);
  59 }
  60 
  61 static void
  62 class_init()
     /* [<][>][^][v][top][bottom][index][help] */
  63 {
  64     int i, j;
  65     char *attribute_name;
  66     const attribute_t *attr_info;
  67     class_attr_t *class_attr_info;
  68 
  69     /* initialize table of class names */
  70     class_names = g_new(char*, CLASS_TAB_LEN+1);
  71     for (i=0; i<CLASS_TAB_LEN; i++) {
  72         class_names[i] = g_strdup(class_tab[i].name);
  73     }
  74     class_names[i] = NULL;
  75 
  76     /* initialize hash of class names to class information */
  77     class_hash = g_hash_table_new(my_g_str_hash, my_g_strcasecmp);
  78     for (i=0; i<CLASS_TAB_LEN; i++) {
  79         class_tab[i].attr_hash = g_hash_table_new(my_g_str_hash, 
  80                                                   my_g_strcasecmp);
  81         for (j=0; j<class_tab[i].num_attr; j++) {
  82             attr_info = attribute_lookup_by_offset(class_tab[i].attr[j].offset);
  83             attribute_name = attr_info->name;
  84             class_attr_info = &(class_tab[i].attr[j]);
  85             g_hash_table_insert(class_tab[i].attr_hash,  
  86                                 attribute_name, 
  87                                 class_attr_info);
  88         }
  89         g_hash_table_insert(class_hash, class_tab[i].name, &class_tab[i]);
  90     }
  91 }
  92 
  93 static pthread_once_t init_control = PTHREAD_ONCE_INIT;
  94 
  95 const char* const *
  96 get_class_names ()
     /* [<][>][^][v][top][bottom][index][help] */
  97 {
  98     pthread_once(&init_control, class_init);
  99     return (const char* const*)class_names;
 100 }
 101 
 102 const class_t *
 103 class_lookup (const char *name)
     /* [<][>][^][v][top][bottom][index][help] */
 104 {
 105     pthread_once(&init_control, class_init);
 106     return g_hash_table_lookup(class_hash, name);
 107 }
 108 
 109 const class_attr_t *
 110 class_attr_lookup (const class_t *class, const char *attr_name)
     /* [<][>][^][v][top][bottom][index][help] */
 111 {
 112     pthread_once(&init_control, class_init);
 113     return g_hash_table_lookup(class->attr_hash, attr_name);
     /* [<][>][^][v][top][bottom][index][help] */
 114 }
 115 
 116 

/* [<][>][^][v][top][bottom][index][help] */