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;
164 eina_lock_release(&ed->mutex);
168 eina_lock_release(&ed->mutex);
173 eet_dictionary_string_get_size(const Eet_Dictionary *ed,
180 if (idx < 0) goto done;
182 eina_lock_take((Eina_Lock*) &ed->mutex);
185 length = ed->all[idx].len;
187 eina_lock_release((Eina_Lock*) &ed->mutex);
194 eet_dictionary_count(const Eet_Dictionary *ed)
200 eet_dictionary_string_get_hash(const Eet_Dictionary *ed,
207 if (idx < 0) goto done;
209 eina_lock_take((Eina_Lock*) &ed->mutex);
212 hash = ed->all[idx].hash;
214 eina_lock_release((Eina_Lock*) &ed->mutex);
221 eet_dictionary_string_get_char(const Eet_Dictionary *ed,
224 const char *s = NULL;
228 if (idx < 0) goto done;
230 eina_lock_take((Eina_Lock*) &ed->mutex);
235 /* Windows file system could change the mmaped file when replacing a file. So we need to copy all string in memory to avoid bugs. */
236 if (!ed->all[idx].allocated)
238 ed->all[idx].str = eina_stringshare_add(ed->all[idx].str);
239 ed->all[idx].allocated = EINA_TRUE;
241 #endif /* ifdef _WIN32 */
242 s = ed->all[idx].str;
245 eina_lock_release((Eina_Lock*) &ed->mutex);
251 static inline Eina_Bool
252 _eet_dictionary_string_get_me_cache(const char *s,
257 if ((len == 6) && (s[0] == '0') && (s[1] == 'x') && (s[3] == 'p'))
259 *mantisse = (s[2] >= 'a') ? (s[2] - 'a' + 10) : (s[2] - '0');
260 *exponent = (s[5] - '0');
268 static inline Eina_Bool
269 _eet_dictionary_string_get_float_cache(const char *s,
276 if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent))
279 *result = (float)(mantisse << exponent);
281 *result = (float)mantisse / (float)(1 << exponent);
289 static inline Eina_Bool
290 _eet_dictionary_string_get_double_cache(const char *s,
297 if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent))
300 *result = (double)(mantisse << exponent);
302 *result = (double)mantisse / (float)(1 << exponent);
310 static inline Eina_Bool
311 _eet_dictionary_test(const Eet_Dictionary *ed,
315 Eina_Bool limit = EINA_FALSE;
317 if (!result) goto done;
321 if (idx < 0) goto done;
323 eina_lock_take((Eina_Lock*) &ed->mutex);
325 if (!(idx < ed->count)) goto unlock_done;
330 eina_lock_release((Eina_Lock*) &ed->mutex);
337 eet_dictionary_convert_get(const Eet_Dictionary *ed,
343 eina_lock_take((Eina_Lock*) &ed->mutex);
345 *str = ed->all[idx].str;
349 ((Eet_Dictionary *)ed)->converts = eina_hash_int32_new(free);
354 result = eina_hash_find(ed->converts, &idx);
355 if (result) goto done;
358 result = calloc(1, sizeof (Eet_Convert));
360 eina_hash_add(ed->converts, &idx, result);
363 eina_lock_release((Eina_Lock*) &ed->mutex);
369 eet_dictionary_string_get_float(const Eet_Dictionary *ed,
373 Eet_Convert *convert;
376 if (!_eet_dictionary_test(ed, idx, result))
379 convert = eet_dictionary_convert_get(ed, idx, &str);
380 if (!convert) return EINA_FALSE;
382 if (!(convert->type & EET_D_FLOAT))
384 eina_lock_take((Eina_Lock*) &ed->mutex);
385 if (!_eet_dictionary_string_get_float_cache(str, ed->all[idx].len,
388 long long mantisse = 0;
391 if (eina_convert_atod(str, ed->all[idx].len, &mantisse,
392 &exponent) == EINA_FALSE)
394 eina_lock_release((Eina_Lock*) &ed->mutex);
398 convert->f = ldexpf((float)mantisse, exponent);
400 eina_lock_release((Eina_Lock*) &ed->mutex);
402 convert->type |= EET_D_FLOAT;
405 *result = convert->f;
410 eet_dictionary_string_get_double(const Eet_Dictionary *ed,
414 Eet_Convert *convert;
417 if (!_eet_dictionary_test(ed, idx, result))
420 convert = eet_dictionary_convert_get(ed, idx, &str);
421 if (!convert) return EINA_FALSE;
423 if (!(convert->type & EET_D_DOUBLE))
425 eina_lock_take((Eina_Lock*) &ed->mutex);
427 if (!_eet_dictionary_string_get_double_cache(str, ed->all[idx].len,
430 long long mantisse = 0;
433 if (eina_convert_atod(str, ed->all[idx].len, &mantisse,
434 &exponent) == EINA_FALSE)
436 eina_lock_release((Eina_Lock*) &ed->mutex);
440 convert->d = ldexp((double)mantisse, exponent);
442 eina_lock_release((Eina_Lock*) &ed->mutex);
444 convert->type |= EET_D_DOUBLE;
447 *result = convert->d;
452 eet_dictionary_string_get_fp(const Eet_Dictionary *ed,
456 Eet_Convert *convert;
459 if (!_eet_dictionary_test(ed, idx, result))
462 convert = eet_dictionary_convert_get(ed, idx, &str);
463 if (!convert) return EINA_FALSE;
465 if (!(convert->type & EET_D_FIXED_POINT))
469 eina_lock_take((Eina_Lock*) &ed->mutex);
470 if (!eina_convert_atofp(str, ed->all[idx].len, &fp))
472 eina_lock_release((Eina_Lock*) &ed->mutex);
475 eina_lock_release((Eina_Lock*) &ed->mutex);
478 convert->type |= EET_D_FIXED_POINT;
481 *result = convert->fp;
486 eet_dictionary_string_check(Eet_Dictionary *ed,
492 if ((!ed) || (!string))
495 eina_lock_take(&ed->mutex);
497 if ((ed->start <= string) && (string < ed->end))
502 for (i = 0; i < ed->count; ++i)
503 if ((ed->all[i].allocated) && ed->all[i].str == string)
510 eina_lock_release(&ed->mutex);