3 #endif /* ifdef HAVE_CONFIG_H */
12 #include "Eet_private.h"
15 eet_dictionary_add(void)
19 new = calloc(1, sizeof (Eet_Dictionary));
23 memset(new->hash, -1, sizeof (int) * 256);
26 } /* eet_dictionary_add */
29 eet_dictionary_free(Eet_Dictionary *ed)
35 for (i = 0; i < ed->count; ++i)
36 if (ed->all[i].allocated)
37 free(ed->all[i].u.str);
42 if (ed->converts) eina_hash_free(ed->converts);
46 } /* eet_dictionary_free */
49 _eet_dictionary_lookup(Eet_Dictionary *ed,
54 Eina_Bool found = EINA_FALSE;
58 current = ed->hash[hash];
62 if (ed->all[current].len == len)
64 if (ed->all[current].allocated)
66 if (strcmp(ed->all[current].u.str, string) == 0)
72 else if (ed->all[current].u.mmap
73 && strcmp(ed->all[current].u.mmap, string) == 0)
81 current = ed->all[current].next;
84 if (current == -1 && found)
88 } /* _eet_dictionary_lookup */
91 eet_dictionary_string_add(Eet_Dictionary *ed,
103 hash = _eet_hash_gen(string, 8);
104 len = strlen(string) + 1;
106 idx = _eet_dictionary_lookup(ed, string, len, hash);
110 if (ed->all[idx].allocated)
112 if (strcmp(ed->all[idx].u.str, string) == 0)
115 else if (ed->all[idx].u.mmap && strcmp(ed->all[idx].u.mmap, string) == 0)
119 if (ed->total == ed->count)
124 total = ed->total + 8;
126 new = realloc(ed->all, sizeof (Eet_String) * total);
134 str = strdup(string);
138 current = ed->all + ed->count;
140 current->allocated = EINA_TRUE;
142 current->hash = hash;
144 current->u.str = str;
149 current->next = ed->hash[hash];
151 ed->hash[hash] = ed->count;
156 current->prev = ed->all[idx].prev;
158 if (current->next != -1)
159 ed->all[current->next].prev = ed->count;
161 if (current->prev != -1)
162 ed->all[current->prev].next = ed->count;
164 ed->hash[hash] = ed->count;
168 } /* eet_dictionary_string_add */
171 eet_dictionary_string_get_size(const Eet_Dictionary *ed,
181 return ed->all[idx].len;
184 } /* eet_dictionary_string_get_size */
187 eet_dictionary_string_get_hash(const Eet_Dictionary *ed,
197 return ed->all[idx].hash;
200 } /* eet_dictionary_string_get_hash */
203 eet_dictionary_string_get_char(const Eet_Dictionary *ed,
215 /* Windows file system could change the mmaped file when replacing a file. So we need to copy all string in memory to avoid bugs. */
216 if (!ed->all[idx].allocated)
218 ed->all[idx].u.str = strdup(ed->all[idx].u.mmap);
219 ed->all[idx].allocated = EINA_TRUE;
222 #else /* ifdef _WIN32 */
223 if (!(ed->all[idx].allocated) && ed->all[idx].u.mmap)
224 return ed->all[idx].u.mmap;
226 #endif /* ifdef _WIN32 */
227 return ed->all[idx].u.str;
231 } /* eet_dictionary_string_get_char */
233 static inline Eina_Bool
234 _eet_dictionary_string_get_me_cache(const char *s,
239 if ((len == 6) && (s[0] == '0') && (s[1] == 'x') && (s[3] == 'p'))
241 *mantisse = (s[2] >= 'a') ? (s[2] - 'a' + 10) : (s[2] - '0');
242 *exponent = (s[5] - '0');
248 } /* _eet_dictionary_string_get_me_cache */
250 static inline Eina_Bool
251 _eet_dictionary_string_get_float_cache(const char *s,
258 if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent))
261 *result = (float)(mantisse << exponent);
263 *result = (float)mantisse / (float)(1 << exponent);
269 } /* _eet_dictionary_string_get_float_cache */
271 static inline Eina_Bool
272 _eet_dictionary_string_get_double_cache(const char *s,
279 if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent))
282 *result = (double)(mantisse << exponent);
284 *result = (double)mantisse / (float)(1 << exponent);
290 } /* _eet_dictionary_string_get_double_cache */
292 static inline Eina_Bool
293 _eet_dictionary_test(const Eet_Dictionary *ed,
306 if (!(idx < ed->count))
310 } /* _eet_dictionary_test */
313 eet_dictionary_convert_get(const Eet_Dictionary *ed,
319 *str = ed->all[idx].allocated ? ed->all[idx].u.str : ed->all[idx].u.mmap;
323 ((Eet_Dictionary*)ed)->converts = eina_hash_int32_new(free);
328 result = eina_hash_find(ed->converts, &idx);
329 if (result) return result;
332 result = calloc(1, sizeof (Eet_Convert));
334 eina_hash_add(ed->converts, &idx, result);
339 eet_dictionary_string_get_float(const Eet_Dictionary *ed,
343 Eet_Convert *convert;
346 if (!_eet_dictionary_test(ed, idx, result))
349 convert = eet_dictionary_convert_get(ed, idx, &str);
350 if (!convert) return EINA_FALSE;
352 if (!(convert->type & EET_D_FLOAT))
354 if (!_eet_dictionary_string_get_float_cache(str, ed->all[idx].len,
357 long long mantisse = 0;
360 if (eina_convert_atod(str, ed->all[idx].len, &mantisse,
361 &exponent) == EINA_FALSE)
364 convert->f = ldexpf((float)mantisse, exponent);
367 convert->type |= EET_D_FLOAT;
370 *result = convert->f;
372 } /* eet_dictionary_string_get_float */
375 eet_dictionary_string_get_double(const Eet_Dictionary *ed,
379 Eet_Convert *convert;
382 if (!_eet_dictionary_test(ed, idx, result))
385 convert = eet_dictionary_convert_get(ed, idx, &str);
386 if (!convert) return EINA_FALSE;
388 if (!(convert->type & EET_D_DOUBLE))
390 if (!_eet_dictionary_string_get_double_cache(str, ed->all[idx].len,
393 long long mantisse = 0;
396 if (eina_convert_atod(str, ed->all[idx].len, &mantisse,
397 &exponent) == EINA_FALSE)
400 convert->d = ldexp((double)mantisse, exponent);
403 convert->type |= EET_D_DOUBLE;
406 *result = convert->d;
408 } /* eet_dictionary_string_get_double */
411 eet_dictionary_string_get_fp(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_FIXED_POINT))
428 if (!eina_convert_atofp(str, ed->all[idx].len, &fp))
432 convert->type |= EET_D_FIXED_POINT;
435 *result = convert->fp;
437 } /* eet_dictionary_string_get_fp */
440 eet_dictionary_string_check(Eet_Dictionary *ed,
445 if ((!ed) || (!string))
448 if ((ed->start <= string) && (string < ed->end))
451 for (i = 0; i < ed->count; ++i)
452 if ((ed->all[i].allocated) && ed->all[i].u.str == string)
456 } /* eet_dictionary_string_check */