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 || strcmp(ed->all[current].str, string) == 0))
75 current = ed->all[current].next;
78 if (current == -1 && found)
85 eet_dictionary_string_add(Eet_Dictionary *ed,
97 hash = _eet_hash_gen(string, 8);
98 len = strlen(string) + 1;
100 eina_lock_take(&ed->mutex);
102 idx = _eet_dictionary_lookup(ed, string, len, hash);
106 if (ed->all[idx].str && (ed->all[idx].str == string || strcmp(ed->all[idx].str, string) == 0))
108 eina_lock_release(&ed->mutex);
113 if (ed->total == ed->count)
118 total = ed->total + 8;
120 new = realloc(ed->all, total * sizeof(Eet_String));
121 if (!new) goto on_error;
127 str = eina_stringshare_add(string);
128 if (!str) goto on_error;
130 current = ed->all + ed->count;
132 current->allocated = EINA_TRUE;
134 current->hash = hash;
141 current->next = ed->hash[hash];
143 ed->hash[hash] = ed->count;
148 current->prev = ed->all[idx].prev;
150 if (current->next != -1)
151 ed->all[current->next].prev = ed->count;
153 if (current->prev != -1)
154 ed->all[current->prev].next = ed->count;
156 ed->hash[hash] = ed->count;
159 eina_lock_release(&ed->mutex);
163 eina_lock_release(&ed->mutex);
168 eet_dictionary_string_get_size(const Eet_Dictionary *ed,
175 if (idx < 0) goto done;
177 eina_lock_take((Eina_Lock*) &ed->mutex);
180 length = ed->all[idx].len;
182 eina_lock_release((Eina_Lock*) &ed->mutex);
189 eet_dictionary_count(const Eet_Dictionary *ed)
195 eet_dictionary_string_get_hash(const Eet_Dictionary *ed,
202 if (idx < 0) goto done;
204 eina_lock_take((Eina_Lock*) &ed->mutex);
207 hash = ed->all[idx].hash;
209 eina_lock_release((Eina_Lock*) &ed->mutex);
216 eet_dictionary_string_get_char(const Eet_Dictionary *ed,
219 const char *s = NULL;
223 if (idx < 0) goto done;
225 eina_lock_take((Eina_Lock*) &ed->mutex);
230 /* Windows file system could change the mmaped file when replacing a file. So we need to copy all string in memory to avoid bugs. */
231 if (!ed->all[idx].allocated)
233 ed->all[idx].str = eina_stringshare_add(ed->all[idx].str);
234 ed->all[idx].allocated = EINA_TRUE;
236 #endif /* ifdef _WIN32 */
237 s = ed->all[idx].str;
240 eina_lock_release((Eina_Lock*) &ed->mutex);
246 static inline Eina_Bool
247 _eet_dictionary_string_get_me_cache(const char *s,
252 if ((len == 6) && (s[0] == '0') && (s[1] == 'x') && (s[3] == 'p'))
254 *mantisse = (s[2] >= 'a') ? (s[2] - 'a' + 10) : (s[2] - '0');
255 *exponent = (s[5] - '0');
263 static inline Eina_Bool
264 _eet_dictionary_string_get_float_cache(const char *s,
271 if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent))
274 *result = (float)(mantisse << exponent);
276 *result = (float)mantisse / (float)(1 << exponent);
284 static inline Eina_Bool
285 _eet_dictionary_string_get_double_cache(const char *s,
292 if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent))
295 *result = (double)(mantisse << exponent);
297 *result = (double)mantisse / (float)(1 << exponent);
305 static inline Eina_Bool
306 _eet_dictionary_test(const Eet_Dictionary *ed,
310 Eina_Bool limit = EINA_FALSE;
312 if (!result) goto done;
316 if (idx < 0) goto done;
318 eina_lock_take((Eina_Lock*) &ed->mutex);
320 if (!(idx < ed->count)) goto unlock_done;
325 eina_lock_release((Eina_Lock*) &ed->mutex);
332 eet_dictionary_convert_get(const Eet_Dictionary *ed,
338 eina_lock_take((Eina_Lock*) &ed->mutex);
340 *str = ed->all[idx].str;
344 ((Eet_Dictionary *)ed)->converts = eina_hash_int32_new(free);
349 result = eina_hash_find(ed->converts, &idx);
350 if (result) goto done;
353 result = calloc(1, sizeof (Eet_Convert));
355 eina_hash_add(ed->converts, &idx, result);
358 eina_lock_release((Eina_Lock*) &ed->mutex);
364 eet_dictionary_string_get_float(const Eet_Dictionary *ed,
368 Eet_Convert *convert;
371 if (!_eet_dictionary_test(ed, idx, result))
374 convert = eet_dictionary_convert_get(ed, idx, &str);
375 if (!convert) return EINA_FALSE;
377 if (!(convert->type & EET_D_FLOAT))
379 eina_lock_take((Eina_Lock*) &ed->mutex);
380 if (!_eet_dictionary_string_get_float_cache(str, ed->all[idx].len,
383 long long mantisse = 0;
386 if (eina_convert_atod(str, ed->all[idx].len, &mantisse,
387 &exponent) == EINA_FALSE)
389 eina_lock_release((Eina_Lock*) &ed->mutex);
393 convert->f = ldexpf((float)mantisse, exponent);
395 eina_lock_release((Eina_Lock*) &ed->mutex);
397 convert->type |= EET_D_FLOAT;
400 *result = convert->f;
405 eet_dictionary_string_get_double(const Eet_Dictionary *ed,
409 Eet_Convert *convert;
412 if (!_eet_dictionary_test(ed, idx, result))
415 convert = eet_dictionary_convert_get(ed, idx, &str);
416 if (!convert) return EINA_FALSE;
418 if (!(convert->type & EET_D_DOUBLE))
420 eina_lock_take((Eina_Lock*) &ed->mutex);
422 if (!_eet_dictionary_string_get_double_cache(str, ed->all[idx].len,
425 long long mantisse = 0;
428 if (eina_convert_atod(str, ed->all[idx].len, &mantisse,
429 &exponent) == EINA_FALSE)
431 eina_lock_release((Eina_Lock*) &ed->mutex);
435 convert->d = ldexp((double)mantisse, exponent);
437 eina_lock_release((Eina_Lock*) &ed->mutex);
439 convert->type |= EET_D_DOUBLE;
442 *result = convert->d;
447 eet_dictionary_string_get_fp(const Eet_Dictionary *ed,
451 Eet_Convert *convert;
454 if (!_eet_dictionary_test(ed, idx, result))
457 convert = eet_dictionary_convert_get(ed, idx, &str);
458 if (!convert) return EINA_FALSE;
460 if (!(convert->type & EET_D_FIXED_POINT))
464 eina_lock_take((Eina_Lock*) &ed->mutex);
465 if (!eina_convert_atofp(str, ed->all[idx].len, &fp))
467 eina_lock_release((Eina_Lock*) &ed->mutex);
470 eina_lock_release((Eina_Lock*) &ed->mutex);
473 convert->type |= EET_D_FIXED_POINT;
476 *result = convert->fp;
481 eet_dictionary_string_check(Eet_Dictionary *ed,
487 if ((!ed) || (!string))
490 eina_lock_take(&ed->mutex);
492 if ((ed->start <= string) && (string < ed->end))
497 for (i = 0; i < ed->count; ++i)
498 if ((ed->all[i].allocated) && ed->all[i].str == string)
505 eina_lock_release(&ed->mutex);