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