3 #endif /* ifdef HAVE_CONFIG_H */
12 #include "Eet_private.h"
15 eet_dictionary_add(void)
19 new = eet_dictionary_calloc(1);
23 memset(new->hash, -1, sizeof (int) * 256);
24 eina_lock_new(&new->mutex);
30 eet_dictionary_free(Eet_Dictionary *ed)
36 eina_lock_free(&ed->mutex);
38 for (i = 0; i < ed->count; ++i)
39 if (ed->all[i].allocated)
40 eina_stringshare_del(ed->all[i].str);
45 if (ed->converts) eina_hash_free(ed->converts);
47 eet_dictionary_mp_free(ed);
51 _eet_dictionary_lookup(Eet_Dictionary *ed,
56 Eina_Bool found = EINA_FALSE;
60 current = ed->hash[hash];
64 if (ed->all[current].len == len)
66 if (ed->all[current].str &&
67 ((ed->all[current].str == string) ||
68 (!strcmp(ed->all[current].str, string))))
76 current = ed->all[current].next;
79 if ((current == -1) && found)
86 eet_dictionary_string_add(Eet_Dictionary *ed,
99 hash = _eet_hash_gen(string, 8);
100 len = strlen(string) + 1;
102 eina_lock_take(&ed->mutex);
104 idx = _eet_dictionary_lookup(ed, string, len, hash);
108 if (ed->all[idx].str &&
109 ((ed->all[idx].str == string) ||
110 (!strcmp(ed->all[idx].str, string))))
112 eina_lock_release(&ed->mutex);
117 if (ed->total == ed->count)
122 total = ed->total + 8;
124 new = realloc(ed->all, total * sizeof(Eet_String));
125 if (!new) goto on_error;
131 str = eina_stringshare_add(string);
132 if (!str) goto on_error;
134 current = ed->all + ed->count;
136 current->allocated = EINA_TRUE;
138 current->hash = hash;
145 current->next = ed->hash[hash];
147 ed->hash[hash] = ed->count;
152 current->prev = ed->all[idx].prev;
154 if (current->next != -1)
155 ed->all[current->next].prev = ed->count;
157 if (current->prev != -1)
158 ed->all[current->prev].next = ed->count;
160 ed->hash[hash] = ed->count;
165 eina_lock_release(&ed->mutex);
169 eina_lock_release(&ed->mutex);
174 eet_dictionary_string_get_size(const Eet_Dictionary *ed,
181 if (idx < 0) goto done;
183 eina_lock_take((Eina_Lock*) &ed->mutex);
186 length = ed->all[idx].len;
188 eina_lock_release((Eina_Lock*) &ed->mutex);
195 eet_dictionary_count(const Eet_Dictionary *ed)
201 eet_dictionary_string_get_hash(const Eet_Dictionary *ed,
208 if (idx < 0) goto done;
210 eina_lock_take((Eina_Lock*) &ed->mutex);
213 hash = ed->all[idx].hash;
215 eina_lock_release((Eina_Lock*) &ed->mutex);
222 eet_dictionary_string_get_char(const Eet_Dictionary *ed,
225 const char *s = NULL;
229 if (idx < 0) goto done;
231 eina_lock_take((Eina_Lock*) &ed->mutex);
236 /* Windows file system could change the mmaped file when replacing a file. So we need to copy all string in memory to avoid bugs. */
237 if (!ed->all[idx].allocated)
239 ed->all[idx].str = eina_stringshare_add(ed->all[idx].str);
240 ed->all[idx].allocated = EINA_TRUE;
242 #endif /* ifdef _WIN32 */
243 s = ed->all[idx].str;
246 eina_lock_release((Eina_Lock*) &ed->mutex);
252 static inline Eina_Bool
253 _eet_dictionary_string_get_me_cache(const char *s,
258 if ((len == 6) && (s[0] == '0') && (s[1] == 'x') && (s[3] == 'p'))
260 *mantisse = (s[2] >= 'a') ? (s[2] - 'a' + 10) : (s[2] - '0');
261 *exponent = (s[5] - '0');
269 static inline Eina_Bool
270 _eet_dictionary_string_get_float_cache(const char *s,
277 if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent))
280 *result = (float)(mantisse << exponent);
282 *result = (float)mantisse / (float)(1 << exponent);
290 static inline Eina_Bool
291 _eet_dictionary_string_get_double_cache(const char *s,
298 if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent))
301 *result = (double)(mantisse << exponent);
303 *result = (double)mantisse / (float)(1 << exponent);
311 static inline Eina_Bool
312 _eet_dictionary_test(const Eet_Dictionary *ed,
316 Eina_Bool limit = EINA_FALSE;
318 if (!result) goto done;
322 if (idx < 0) goto done;
324 eina_lock_take((Eina_Lock*) &ed->mutex);
326 if (!(idx < ed->count)) goto unlock_done;
331 eina_lock_release((Eina_Lock*) &ed->mutex);
338 eet_dictionary_convert_get(const Eet_Dictionary *ed,
344 eina_lock_take((Eina_Lock*) &ed->mutex);
346 *str = ed->all[idx].str;
350 ((Eet_Dictionary *)ed)->converts = eina_hash_int32_new(free);
355 result = eina_hash_find(ed->converts, &idx);
356 if (result) goto done;
359 result = calloc(1, sizeof (Eet_Convert));
361 eina_hash_add(ed->converts, &idx, result);
364 eina_lock_release((Eina_Lock*) &ed->mutex);
370 eet_dictionary_string_get_float(const Eet_Dictionary *ed,
374 Eet_Convert *convert;
377 if (!_eet_dictionary_test(ed, idx, result))
380 convert = eet_dictionary_convert_get(ed, idx, &str);
381 if (!convert) return EINA_FALSE;
383 if (!(convert->type & EET_D_FLOAT))
385 eina_lock_take((Eina_Lock*) &ed->mutex);
386 if (!_eet_dictionary_string_get_float_cache(str, ed->all[idx].len,
389 long long mantisse = 0;
392 if (eina_convert_atod(str, ed->all[idx].len, &mantisse,
393 &exponent) == EINA_FALSE)
395 eina_lock_release((Eina_Lock*) &ed->mutex);
399 convert->f = ldexpf((float)mantisse, exponent);
401 eina_lock_release((Eina_Lock*) &ed->mutex);
403 convert->type |= EET_D_FLOAT;
406 *result = convert->f;
411 eet_dictionary_string_get_double(const Eet_Dictionary *ed,
415 Eet_Convert *convert;
418 if (!_eet_dictionary_test(ed, idx, result))
421 convert = eet_dictionary_convert_get(ed, idx, &str);
422 if (!convert) return EINA_FALSE;
424 if (!(convert->type & EET_D_DOUBLE))
426 eina_lock_take((Eina_Lock*) &ed->mutex);
428 if (!_eet_dictionary_string_get_double_cache(str, ed->all[idx].len,
431 long long mantisse = 0;
434 if (eina_convert_atod(str, ed->all[idx].len, &mantisse,
435 &exponent) == EINA_FALSE)
437 eina_lock_release((Eina_Lock*) &ed->mutex);
441 convert->d = ldexp((double)mantisse, exponent);
443 eina_lock_release((Eina_Lock*) &ed->mutex);
445 convert->type |= EET_D_DOUBLE;
448 *result = convert->d;
453 eet_dictionary_string_get_fp(const Eet_Dictionary *ed,
457 Eet_Convert *convert;
460 if (!_eet_dictionary_test(ed, idx, result))
463 convert = eet_dictionary_convert_get(ed, idx, &str);
464 if (!convert) return EINA_FALSE;
466 if (!(convert->type & EET_D_FIXED_POINT))
470 eina_lock_take((Eina_Lock*) &ed->mutex);
471 if (!eina_convert_atofp(str, ed->all[idx].len, &fp))
473 eina_lock_release((Eina_Lock*) &ed->mutex);
476 eina_lock_release((Eina_Lock*) &ed->mutex);
479 convert->type |= EET_D_FIXED_POINT;
482 *result = convert->fp;
487 eet_dictionary_string_check(Eet_Dictionary *ed,
493 if ((!ed) || (!string))
496 eina_lock_take(&ed->mutex);
498 if ((ed->start <= string) && (string < ed->end))
503 for (i = 0; i < ed->count; ++i)
504 if ((ed->all[i].allocated) && ed->all[i].str == string)
511 eina_lock_release(&ed->mutex);