8 /* I use the ex_data stuff to manage the identifiers for the obj_name_types
9 * that applications may define. I only really use the free function field.
11 static LHASH
*names_lh
=NULL
;
12 static int names_type_num
=OBJ_NAME_TYPE_NUM
;
13 static STACK
*names_cmp
=NULL
;
14 static STACK
*names_hash
=NULL
;
15 static STACK
*names_free
=NULL
;
17 static unsigned long obj_name_hash(OBJ_NAME
*a
);
18 static int obj_name_cmp(OBJ_NAME
*a
,OBJ_NAME
*b
);
22 if (names_lh
!= NULL
) return(1);
24 names_lh
=lh_new(obj_name_hash
,obj_name_cmp
);
26 return(names_lh
!= NULL
);
29 int OBJ_NAME_new_index(hash_func
,cmp_func
,free_func
)
30 unsigned long (*hash_func
)();
37 if (names_free
== NULL
)
40 names_hash
=sk_new_null();
41 names_cmp
=sk_new_null();
42 names_free
=sk_new_null();
45 if ((names_free
== NULL
) || (names_hash
== NULL
) || (names_cmp
== NULL
))
52 for (i
=sk_num(names_free
); i
<names_type_num
; i
++)
55 sk_push(names_hash
,(char *)strcmp
);
56 sk_push(names_cmp
,(char *)lh_strhash
);
57 sk_push(names_free
,NULL
);
60 if (hash_func
!= NULL
)
61 sk_value(names_hash
,ret
)=(char *)hash_func
;
63 sk_value(names_cmp
,ret
)= (char *)cmp_func
;
64 if (free_func
!= NULL
)
65 sk_value(names_free
,ret
)=(char *)free_func
;
69 static int obj_name_cmp(a
,b
)
79 if ((names_cmp
!= NULL
) && (sk_num(names_cmp
) > a
->type
))
81 cmp
=(int (*)())sk_value(names_cmp
,a
->type
);
82 ret
=cmp(a
->name
,b
->name
);
85 ret
=strcmp(a
->name
,b
->name
);
90 static unsigned long obj_name_hash(a
)
94 unsigned long (*hash
)();
96 if ((names_hash
!= NULL
) && (sk_num(names_hash
) > a
->type
))
98 hash
=(unsigned long (*)())sk_value(names_hash
,a
->type
);
103 ret
=lh_strhash(a
->name
);
109 char *OBJ_NAME_get(name
,type
)
116 if (name
== NULL
) return(NULL
);
117 if ((names_lh
== NULL
) && !OBJ_NAME_init()) return(NULL
);
119 alias
=type
&OBJ_NAME_ALIAS
;
120 type
&= ~OBJ_NAME_ALIAS
;
127 ret
=(OBJ_NAME
*)lh_retrieve(names_lh
,(char *)&on
);
128 if (ret
== NULL
) return(NULL
);
129 if ((ret
->alias
) && !alias
)
131 if (++num
> 10) return(NULL
);
141 int OBJ_NAME_add(name
,type
,data
)
150 if ((names_lh
== NULL
) && !OBJ_NAME_init()) return(0);
152 alias
=type
&OBJ_NAME_ALIAS
;
153 type
&= ~OBJ_NAME_ALIAS
;
155 onp
=(OBJ_NAME
*)Malloc(sizeof(OBJ_NAME
));
167 ret
=(OBJ_NAME
*)lh_insert(names_lh
,(char *)onp
);
171 if ((names_free
!= NULL
) && (sk_num(names_free
) > ret
->type
))
173 f
=(void (*)())sk_value(names_free
,ret
->type
);
174 f(ret
->name
,ret
->type
,ret
->data
);
180 if (lh_error(names_lh
))
189 int OBJ_NAME_remove(name
,type
)
196 if (names_lh
== NULL
) return(0);
198 type
&= ~OBJ_NAME_ALIAS
;
201 ret
=(OBJ_NAME
*)lh_delete(names_lh
,(char *)&on
);
205 if ((names_free
!= NULL
) && (sk_num(names_free
) > type
))
207 f
=(void (*)())sk_value(names_free
,type
);
208 f(ret
->name
,ret
->type
,ret
->data
);
217 static int free_type
;
219 static void names_lh_free(onp
,type
)
222 if ((free_type
< 0) || (free_type
== onp
->type
))
224 OBJ_NAME_remove(onp
->name
,onp
->type
);
228 void OBJ_NAME_cleanup(type
)
231 unsigned long down_load
;
233 if (names_lh
== NULL
) return;
236 down_load
=names_lh
->down_load
;
237 names_lh
->down_load
=0;
239 lh_doall(names_lh
,names_lh_free
);
252 names_lh
->down_load
=down_load
;