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