modules/rpsl/class.c

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

FUNCTIONS

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

   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 /* hash mapping class name to entry in class_tab[] */
  37 static GHashTable *class_hash;
  38 
  39 static guint
  40 my_g_str_hash (gconstpointer v)
     /* [<][>][^][v][top][bottom][index][help] */
  41 {
  42     gchar *s;
  43     guint hash;
  44 
  45     s = g_strdup(v);
  46     g_strdown(s);
  47     hash = g_str_hash(s);
  48     g_free(s);
  49     return hash;
  50 }
  51 
  52 static gint 
  53 my_g_strcasecmp (gconstpointer a, gconstpointer b)
     /* [<][>][^][v][top][bottom][index][help] */
  54 {
  55     return (g_strcasecmp(a, b) == 0);
  56 }
  57 
  58 static void
  59 class_init()
     /* [<][>][^][v][top][bottom][index][help] */
  60 {
  61     int i, j;
  62     char *attribute_name;
  63     const attribute_t *attr_info;
  64     class_attr_t *class_attr_info;
  65 
  66     class_hash = g_hash_table_new(my_g_str_hash, my_g_strcasecmp);
  67     for (i=0; i<CLASS_TAB_LEN; i++) {
  68         class_tab[i].attr_hash = g_hash_table_new(my_g_str_hash, 
  69                                                   my_g_strcasecmp);
  70         for (j=0; j<class_tab[i].num_attr; j++) {
  71             attr_info = attribute_lookup_by_offset(class_tab[i].attr[j].offset);
  72             attribute_name = attr_info->name;
  73             class_attr_info = &(class_tab[i].attr[j]);
  74             g_hash_table_insert(class_tab[i].attr_hash,  
  75                                 attribute_name, 
  76                                 class_attr_info);
  77         }
  78         g_hash_table_insert(class_hash, class_tab[i].name, &class_tab[i]);
  79     }
  80 }
  81 
  82 static pthread_once_t init_control = PTHREAD_ONCE_INIT;
  83 
  84 const class_t *
  85 class_lookup (const char *name)
     /* [<][>][^][v][top][bottom][index][help] */
  86 {
  87     pthread_once(&init_control, class_init);
  88     return g_hash_table_lookup(class_hash, name);
  89 }
  90 
  91 const class_attr_t *
  92 class_attr_lookup (const class_t *class, const char *attr_name)
     /* [<][>][^][v][top][bottom][index][help] */
  93 {
  94     pthread_once(&init_control, class_init);
  95     return g_hash_table_lookup(class->attr_hash, attr_name);
  96 }
  97 
  98 

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