testing uncrustify on eet. this shall be standard efl formatting from
[framework/uifw/eet.git] / src / lib / eet_dictionary.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4
5 #ifdef HAVE_CONFIG_H
6 # include <config.h>
7 #endif
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <math.h>
12
13 #include <Eina.h>
14
15 #include "Eet.h"
16 #include "Eet_private.h"
17
18 Eet_Dictionary *
19 eet_dictionary_add(void)
20 {
21    Eet_Dictionary *new;
22
23    new = calloc(1, sizeof (Eet_Dictionary));
24    if (!new)
25       return NULL;
26
27    memset(new->hash, -1, sizeof (int) * 256);
28
29    return new;
30 }
31
32 void
33 eet_dictionary_free(Eet_Dictionary *ed)
34 {
35    if (ed)
36      {
37         int i;
38
39         for (i = 0; i < ed->count; ++i)
40            if (ed->all[i].str)
41               free(ed->all[i].str);
42
43         if (ed->all)
44               free(ed->all);
45
46               free(ed);
47      }
48 }
49
50 static int
51 _eet_dictionary_lookup(Eet_Dictionary *ed, const char *string, int hash)
52 {
53    int prev = -1;
54    int current;
55
56    current = ed->hash[hash];
57
58    while (current != -1)
59      {
60         if (ed->all[current].str)
61            if (strcmp(ed->all[current].str, string) >= 0)
62               break;
63
64         if (ed->all[current].mmap)
65            if (strcmp(ed->all[current].mmap, string) >= 0)
66               break;
67
68         prev = current;
69         current = ed->all[current].next;
70      }
71
72    if (current == -1)
73       return prev;
74
75    return current;
76 }
77
78 int
79 eet_dictionary_string_add(Eet_Dictionary *ed, const char *string)
80 {
81    Eet_String *current;
82    char *str;
83    int hash;
84    int idx;
85    int len;
86
87    if (!ed)
88       return -1;
89
90    hash = _eet_hash_gen(string, 8);
91
92    idx = _eet_dictionary_lookup(ed, string, hash);
93
94    if (idx != -1)
95      {
96         if (ed->all[idx].str)
97            if (strcmp(ed->all[idx].str, string) == 0)
98               return idx;
99
100         if (ed->all[idx].mmap)
101            if (strcmp(ed->all[idx].mmap, string) == 0)
102               return idx;
103
104      }
105
106    if (ed->total == ed->count)
107      {
108         Eet_String *new;
109         int total;
110
111         total = ed->total + 8;
112
113         new = realloc(ed->all, sizeof (Eet_String) * total);
114         if (new == NULL)
115            return -1;
116
117         ed->all = new;
118         ed->total = total;
119      }
120
121    len = strlen(string) + 1;
122    str = strdup(string);
123    if (str == NULL)
124       return -1;
125
126    current = ed->all + ed->count;
127
128    current->type = EET_D_NOT_CONVERTED;
129
130    current->hash = hash;
131
132    current->str = str;
133    current->len = len;
134    current->mmap = NULL;
135
136    if (idx == -1)
137      {
138         current->next = ed->hash[hash];
139         current->prev = -1;
140         ed->hash[hash] = ed->count;
141      }
142    else
143      {
144         current->next = idx;
145         current->prev = ed->all[idx].prev;
146
147         if (current->next != -1)
148            ed->all[current->next].prev = ed->count;
149
150         if (current->prev != -1)
151            ed->all[current->prev].next = ed->count;
152         else
153            ed->hash[hash] = ed->count;
154      }
155
156    return ed->count++;
157 }
158
159 int
160 eet_dictionary_string_get_size(const Eet_Dictionary *ed, int idx)
161 {
162    if (!ed)
163       return 0;
164
165    if (idx < 0)
166       return 0;
167
168    if (idx < ed->count)
169       return ed->all[idx].len;
170
171    return 0;
172 }
173
174 int
175 eet_dictionary_string_get_hash(const Eet_Dictionary *ed, int idx)
176 {
177    if (!ed)
178       return -1;
179
180    if (idx < 0)
181       return -1;
182
183    if (idx < ed->count)
184       return ed->all[idx].hash;
185
186    return -1;
187 }
188
189 const char *
190 eet_dictionary_string_get_char(const Eet_Dictionary *ed, int idx)
191 {
192    if (!ed)
193       return NULL;
194
195    if (idx < 0)
196       return NULL;
197
198    if (idx < ed->count)
199      {
200 #ifdef _WIN32
201         /* Windows file system could change the mmaped file when replacing a file. So we need to copy all string in memory to avoid bugs. */
202         if (ed->all[idx].str == NULL)
203           {
204              ed->all[idx].str = strdup(ed->all[idx].mmap);
205              ed->all[idx].mmap = NULL;
206           }
207
208 #else
209         if (ed->all[idx].mmap)
210            return ed->all[idx].mmap;
211
212 #endif
213         return ed->all[idx].str;
214      }
215
216    return NULL;
217 }
218
219 static inline Eina_Bool
220 _eet_dictionary_string_get_me_cache(const char *s,
221                                     int len,
222                                     int *mantisse,
223                                     int *exponent)
224 {
225    if ((len == 6) && (s[0] == '0') && (s[1] == 'x') && (s[3] == 'p'))
226      {
227         *mantisse = (s[2] >= 'a') ? (s[2] - 'a' + 10) : (s[2] - '0');
228         *exponent = (s[5] - '0');
229
230         return EINA_TRUE;
231      }
232
233    return EINA_FALSE;
234 }
235
236 static inline Eina_Bool
237 _eet_dictionary_string_get_float_cache(const char *s, int len, float *result)
238 {
239    int mantisse;
240    int exponent;
241
242    if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent))
243      {
244         if (s[4] == '+')
245            *result = (float)(mantisse << exponent);
246         else
247            *result = (float)mantisse / (float)(1 << exponent);
248
249         return EINA_TRUE;
250      }
251
252    return EINA_FALSE;
253 }
254
255 static inline Eina_Bool
256 _eet_dictionary_string_get_double_cache(const char *s, int len, double *result)
257 {
258    int mantisse;
259    int exponent;
260
261    if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent))
262      {
263         if (s[4] == '+')
264            *result = (double)(mantisse << exponent);
265         else
266            *result = (double)mantisse / (float)(1 << exponent);
267
268         return EINA_TRUE;
269      }
270
271    return EINA_FALSE;
272 }
273
274 static inline Eina_Bool
275 _eet_dictionary_test(const Eet_Dictionary *ed, int idx, void *result)
276 {
277    if (!result)
278       return EINA_FALSE;
279
280    if (!ed)
281       return EINA_FALSE;
282
283    if (idx < 0)
284       return EINA_FALSE;
285
286    if (!(idx < ed->count))
287       return EINA_FALSE;
288
289    return EINA_TRUE;
290 }
291
292 Eina_Bool
293 eet_dictionary_string_get_float(const Eet_Dictionary *ed,
294                                 int idx,
295                                 float *result)
296 {
297    if (!_eet_dictionary_test(ed, idx, result))
298       return EINA_FALSE;
299
300    if (!(ed->all[idx].type & EET_D_FLOAT))
301      {
302         const char *str;
303
304         str = ed->all[idx].str ? ed->all[idx].str : ed->all[idx].mmap;
305
306         if (!_eet_dictionary_string_get_float_cache(str, ed->all[idx].len,
307                                                     &ed->all[idx].f))
308           {
309              long long mantisse = 0;
310              long exponent = 0;
311
312              if (eina_convert_atod(str, ed->all[idx].len, &mantisse,
313                                    &exponent) == EINA_FALSE)
314                 return EINA_FALSE;
315
316              ed->all[idx].f = ldexpf((float)mantisse, exponent);
317           }
318
319         ed->all[idx].type |= EET_D_FLOAT;
320      }
321
322    *result = ed->all[idx].f;
323    return EINA_TRUE;
324 }
325
326 Eina_Bool
327 eet_dictionary_string_get_double(const Eet_Dictionary *ed,
328                                  int idx,
329                                  double *result)
330 {
331    if (!_eet_dictionary_test(ed, idx, result))
332       return EINA_FALSE;
333
334    if (!(ed->all[idx].type & EET_D_DOUBLE))
335      {
336         const char *str;
337
338         str = ed->all[idx].str ? ed->all[idx].str : ed->all[idx].mmap;
339
340         if (!_eet_dictionary_string_get_double_cache(str, ed->all[idx].len,
341                                                      &ed->all[idx].d))
342           {
343              long long mantisse = 0;
344              long exponent = 0;
345
346              if (eina_convert_atod(str, ed->all[idx].len, &mantisse,
347                                    &exponent) == EINA_FALSE)
348                 return EINA_FALSE;
349
350              ed->all[idx].d = ldexp((double)mantisse, exponent);
351           }
352
353         ed->all[idx].type |= EET_D_DOUBLE;
354      }
355
356    *result = ed->all[idx].d;
357    return EINA_TRUE;
358 }
359
360 Eina_Bool
361 eet_dictionary_string_get_fp(const Eet_Dictionary *ed,
362                              int idx,
363                              Eina_F32p32 *result)
364 {
365    if (!_eet_dictionary_test(ed, idx, result))
366       return EINA_FALSE;
367
368    if (!(ed->all[idx].type & EET_D_FIXED_POINT))
369      {
370         const char *str;
371         Eina_F32p32 fp;
372
373         str = ed->all[idx].str ? ed->all[idx].str : ed->all[idx].mmap;
374
375         if (!eina_convert_atofp(str, ed->all[idx].len, &fp))
376            return EINA_FALSE;
377
378         ed->all[idx].fp = fp;
379         ed->all[idx].type |= EET_D_FIXED_POINT;
380      }
381
382    *result = ed->all[idx].fp;
383    return EINA_TRUE;
384 }
385
386 EAPI int
387 eet_dictionary_string_check(Eet_Dictionary *ed, const char *string)
388 {
389    int i;
390
391    if (ed == NULL
392        || string == NULL)
393       return 0;
394
395    if (ed->start <= string
396        && string < ed->end)
397       return 1;
398
399    for (i = 0; i < ed->count; ++i)
400       if (ed->all[i].str == string)
401          return 1;
402
403    return 0;
404 }