9bcc0c1206ed31a008e29727a87daa849e621e0b
[rrq/rrqmisc.git] / vector / Binding.c
1 #include <string.h>
2 #include <HashVector.h>
3 #include <Binding.h>
4
5 /**
6  * This callback function returns the hashcode of a Binding key, which
7  * is its name string.
8  */
9 static unsigned long Binding_hashcode(void *this,void *key) {
10     return HashVector_hashcode(
11         (unsigned char *) key, strlen( (char*) key ) );
12 }
13
14 /**
15  * This callback function determines whether a Binding item has a
16  * given key or not.
17  */
18 static int Binding_haskey(void *this,void *item,void *key) {
19     Binding *b = (Binding*) item;
20     char *name = (char *) key;
21     return strcmp( name, (char*) b->name ) == 0;
22 }
23
24 /**
25  * This callback function returns the key of a Binding itme, which is
26  * its name string.
27  */
28 static void *Binding_itemkey(void *this,void *item) {
29     return ((Binding*) item)->name;
30 }
31     
32
33 /**
34  * This callback function handles the "release" of a Binding key,
35  * which is to do nothing, since the name string memory is not managed
36  * here.
37  */
38 static void Binding_releasekey(void *this,void *key) {
39 }
40
41 /**
42  * This callback function writes a representation of a Binding item
43  * into a character buffer.
44  */
45 static int Binding_tostring(void *this,void *item,char *buffer,int limit) {
46     Binding *b = (Binding*) item;
47     return snprintf( buffer, limit, "{%s,%p}", b->name, b->value );
48 }
49
50 /**
51  * This is the "item type" for Binding items.
52  */
53 ItemKeyFun Bindingitem = {
54     .hashcode = Binding_hashcode,
55     .haskey = Binding_haskey,
56     .itemkey = Binding_itemkey,
57     .releasekey = Binding_releasekey,
58     .tostring = Binding_tostring
59 };
60