/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following functions.
- my_g_str_hash
- my_g_strcasecmp
- attribute_init
- attribute_lookup
- attribute_lookup_by_offset
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 "attribute.h"
28 #include "attribute_tab.h"
29
30 #define ATTRIBUTE_TAB_LEN (sizeof(attribute_tab)/sizeof(attribute_tab[0]))
31
32 /* hash mapping attribute name to TRUE (only present if ambiguous) */
33 static GHashTable *ambiguous_names;
34
35 /* hash mapping attribute name to entry in attribute_tab[] */
36 static GHashTable *name_to_attribute;
37
38 static guint
39 my_g_str_hash (gconstpointer v)
/* [<][>][^][v][top][bottom][index][help] */
40 {
41 gchar *s;
42 guint hash;
43
44 s = g_strdup(v);
45 g_strdown(s);
46 hash = g_str_hash(s);
47 g_free(s);
48 return hash;
49 }
50
51 static gint
52 my_g_strcasecmp (gconstpointer a, gconstpointer b)
/* [<][>][^][v][top][bottom][index][help] */
53 {
54 return (g_strcasecmp(a, b) == 0);
55 }
56
57 static void
58 attribute_init()
/* [<][>][^][v][top][bottom][index][help] */
59 {
60 int i;
61 gchar *name;
62 attribute_t *attr;
63 static gboolean true = TRUE;
64
65 name_to_attribute = g_hash_table_new(my_g_str_hash, my_g_strcasecmp);
66 ambiguous_names = g_hash_table_new(my_g_str_hash, my_g_strcasecmp);
67 for (i=0; i<ATTRIBUTE_TAB_LEN; i++) {
68 name = attribute_tab[i].name;
69 /* don't do anything if we've already discovered this name is
70 ambiguous (i.e. appears more than once in the table) */
71 if (g_hash_table_lookup(ambiguous_names, name) == NULL) {
72 /* otherwise, see if it is already in the table */
73 attr = g_hash_table_lookup(name_to_attribute, name);
74 if (attr == NULL) {
75 /* if it has not appeared, add it */
76 g_hash_table_insert(name_to_attribute, name, &attribute_tab[i]);
77 } else {
78 /* if it has appeared, mark this as ambiguous */
79 g_hash_table_insert(ambiguous_names, name, &true);
80 }
81 }
82 }
83 }
84
85 static pthread_once_t init_control = PTHREAD_ONCE_INIT;
86
87 /* returns attribute information if found and unambiguous, else NULL */
88 const attribute_t *
89 attribute_lookup (const char *name, gboolean *is_ambiguous)
/* [<][>][^][v][top][bottom][index][help] */
90 {
91 pthread_once(&init_control, attribute_init);
92
93 if (g_hash_table_lookup(ambiguous_names, name)) {
94 *is_ambiguous = TRUE;
95 } else {
96 *is_ambiguous = FALSE;
97 }
98 return g_hash_table_lookup(name_to_attribute, name);
99 }
100
101 const attribute_t *
102 attribute_lookup_by_offset (int offset)
/* [<][>][^][v][top][bottom][index][help] */
103 {
104 pthread_once(&init_control, attribute_init);
105
106 if ((offset < 0) || (offset >= ATTRIBUTE_TAB_LEN)) {
107 return NULL;
108 } else {
109 return &attribute_tab[offset];
110 }
111 }
112