move around - flatter.
[framework/uifw/eet.git] / src / tests / eet_data_suite.c
1 #include <string.h>
2 #include <stdio.h>
3
4 #include "eet_suite.h"
5
6 static char*
7 _eet_str_direct_alloc(const char *str)
8 {
9    return (char*) str;
10 }
11
12 static void
13 _eet_str_direct_free(const char *str)
14 {
15 }
16
17 /* Internal list stuff. */
18 struct _Eet_List
19 {
20    Eet_List *next;
21    const void *data;
22 };
23 Eet_List*
24 eet_list_prepend(Eet_List *list, const void *data)
25 {
26    Eet_List *new;
27
28    new = malloc(sizeof (Eet_List));
29    if (!new) return list;
30
31    new->next = list;
32    new->data = data;
33
34    return new;
35 }
36 Eet_List*
37 eet_list_next(Eet_List *list)
38 {
39    if (!list) return NULL;
40
41    return list->next;
42 }
43 void*
44 eet_list_data(Eet_List *list)
45 {
46    if (!list) return NULL;
47
48    return (void*) list->data;
49 }
50 void
51 eet_list_free(Eet_List *list)
52 {
53    while (list)
54      {
55         Eet_List *current = list;
56
57         list = list->next;
58         free(current);
59      }
60 }
61
62 /* Internal hash stuff */
63 struct _Eet_Hash
64 {
65    Eet_List *bucket[256];
66 };
67 typedef struct _Eet_Hash_Item Eet_Hash_Item;
68 struct _Eet_Hash_Item
69 {
70    const void *data;
71    char *key;
72 };
73 static inline int
74 _eet_hash_gen(const char *key)
75 {
76    unsigned int hash_num = 5381;
77    const unsigned char *ptr;
78
79    if (!key) return 0;
80    for (ptr = (unsigned char *)key; *ptr; ptr++)
81      hash_num = (hash_num * 33) ^ *ptr;
82
83    hash_num &= 0xff;
84    return (int)hash_num;
85 }
86 void
87 eet_hash_foreach(const Eet_Hash *hash, int (*func) (const Eet_Hash *hash, const char *key, void *data, void *fdata), const void *fdata)
88 {
89    int i;
90
91    if (!hash) return ;
92
93    for (i = 0; i < 256; ++i)
94      {
95         Eet_List *over;
96
97         for (over = hash->bucket[i]; over; over = eet_list_next(over))
98           {
99              Eet_Hash_Item *item = eet_list_data(over);
100
101              if (!func(hash, item->key, (void*) item->data, (void*) fdata)) return ;
102           }
103      }
104 }
105 Eet_Hash*
106 eet_hash_add(Eet_Hash *hash, const char *key, const void *data)
107 {
108    Eet_Hash_Item *item;
109    Eet_List *find;
110    int index;
111
112    if (!hash) hash = calloc(1, sizeof (Eet_Hash));
113    if (!hash) return NULL;
114
115    item = malloc(sizeof (Eet_Hash_Item) + strlen(key) + 1);
116    if (!item) return hash;
117
118    item->data = data;
119    item->key = (char*)(item + 1);
120    strcpy(item->key, key);
121
122    hash->bucket[_eet_hash_gen(key)] = eet_list_prepend(hash->bucket[_eet_hash_gen(key)], item);
123
124    return hash;
125 }
126 void
127 eet_hash_free(Eet_Hash *hash)
128 {
129    int i;
130
131    if (!hash) return ;
132
133    for (i = 0; i < 256; ++i)
134      {
135         Eet_List *over;
136
137         for (over = hash->bucket[i]; over; over = eet_list_next(over))
138           free(eet_list_data(over));
139         eet_list_free(hash->bucket[i]);
140      }
141
142    free(hash);
143 }
144
145 void
146 eet_test_setup_eddc(Eet_Data_Descriptor_Class *eddc)
147 {
148    eddc->version = EET_DATA_DESCRIPTOR_CLASS_VERSION;
149    eddc->func.mem_alloc = NULL;
150    eddc->func.mem_free = NULL;
151    eddc->func.str_alloc = NULL;
152    eddc->func.str_free = NULL;
153    eddc->func.list_next = eet_list_next;
154    eddc->func.list_append = eet_list_prepend;
155    eddc->func.list_data = eet_list_data;
156    eddc->func.list_free = eet_list_free;
157    eddc->func.hash_foreach = eet_hash_foreach;
158    eddc->func.hash_add = eet_hash_add;
159    eddc->func.hash_free = eet_hash_free;
160    eddc->func.str_direct_alloc = _eet_str_direct_alloc;
161    eddc->func.str_direct_free = _eet_str_direct_free;
162 }
163