From 8b37476352c83bd21886d1c45395ec956d49881b Mon Sep 17 00:00:00 2001 From: Carsten Haitzler Date: Sat, 1 Mar 2008 17:37:39 +0000 Subject: [PATCH] add file SVN revision: 33887 --- legacy/eet/src/lib/eet_dictionary.c | 293 ++++++++++++++++++++++++++++ 1 file changed, 293 insertions(+) create mode 100644 legacy/eet/src/lib/eet_dictionary.c diff --git a/legacy/eet/src/lib/eet_dictionary.c b/legacy/eet/src/lib/eet_dictionary.c new file mode 100644 index 0000000000..0e05c6f282 --- /dev/null +++ b/legacy/eet/src/lib/eet_dictionary.c @@ -0,0 +1,293 @@ +#include + +#include "Eet.h" +#include "Eet_private.h" + +Eet_Dictionary * +eet_dictionary_add(void) +{ + Eet_Dictionary *new; + + new = calloc(1, sizeof (Eet_Dictionary)); + if (!new) + return NULL; + + new->count = 0; + new->total = 0; + + memset(new->hash, -1, sizeof (int) * 256); + + return new; +} + +void +eet_dictionary_free(Eet_Dictionary *ed) +{ + if (ed) + { + int i; + + for (i = 0; i < ed->count; ++i) + if (ed->all[i].str) + free(ed->all[i].str); + if (ed->all) free(ed->all); + free(ed); + } +} + +static int +_eet_dictionary_lookup(Eet_Dictionary *ed, const char *string, int hash) +{ + int prev = -1; + int current; + + current = ed->hash[hash]; + + while (current != -1) + { + if (ed->all[current].str) + { + if (strcmp(ed->all[current].str, string) >= 0) + break ; + } + if (ed->all[current].mmap) + { + if (strcmp(ed->all[current].mmap, string) >= 0) + break ; + } + + prev = current; + current = ed->all[current].next; + } + + if (current == -1) + return prev; + + return current; +} + +int +eet_dictionary_string_add(Eet_Dictionary *ed, const char *string) +{ + Eet_String *current; + char *str; + int hash; + int index; + int len; + + if (!ed) + return -1; + + hash = _eet_hash_gen(string, 8); + + index = _eet_dictionary_lookup(ed, string, hash); + + if (index != -1) + { + if (ed->all[index].str) + { + if (strcmp(ed->all[index].str, string) == 0) + return index; + } + if (ed->all[index].mmap) + { + if (strcmp(ed->all[index].mmap, string) == 0) + return index; + } + } + + if (ed->total == ed->count) + { + Eet_String *new; + int total; + + total = ed->total + 8; + + new = realloc(ed->all, sizeof (Eet_String) * total); + if (new == NULL) + return -1; + + ed->all = new; + ed->total = total; + } + + len = strlen(string) + 1; + str = strdup(string); + if (str == NULL) + return -1; + + current = ed->all + ed->count; + + current->flags.converted = 0; + current->flags.is_float = 0; + + current->hash = hash; + + current->str = str; + current->len = len; + current->mmap = NULL; + + if (index == -1) + { + current->next = ed->hash[hash]; + current->prev = -1; + ed->hash[hash] = ed->count; + } + else + { + current->next = index; + current->prev = ed->all[index].prev; + + if (current->next != -1) + ed->all[current->next].prev = ed->count; + if (current->prev != -1) + ed->all[current->prev].next = ed->count; + else + ed->hash[hash] = ed->count; + } + + return ed->count++; +} + +int +eet_dictionary_string_get_size(const Eet_Dictionary *ed, int index) +{ + if (!ed) + return 0; + if (index < 0) + return 0; + if (index < ed->count) + return ed->all[index].len; + return 0; +} + +const char * +eet_dictionary_string_get_char(const Eet_Dictionary *ed, int index) +{ + if (!ed) + return NULL; + if (index < 0) + return NULL; + if (index < ed->count) + { + if (ed->all[index].mmap) + return ed->all[index].mmap; + return ed->all[index].str; + } + return NULL; +} + +static inline int +_eet_dictionary_string_get_me_cache(const char *s, int len, int *mantisse, int *exponent) +{ + if ((len == 6) && (s[0] == '0') && (s[1] == 'x') && (s[3] == 'p')) + { + *mantisse = (s[2] >= 'a') ? (s[2] - 'a' + 10) : (s[2] - '0'); + *exponent = (s[5] - '0'); + + return -1; + } + return 0; +} + +static inline int +_eet_dictionary_string_get_float_cache(const char *s, int len, float *result) +{ + int mantisse; + int exponent; + + if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent)) + { + if (s[4] == '+') *result = (float) (mantisse << exponent); + else *result = (float) mantisse / (float) (1 << exponent); + + return -1; + } + return 0; +} + +static inline int +_eet_dictionary_string_get_double_cache(const char *s, int len, double *result) +{ + int mantisse; + int exponent; + + if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent)) + { + if (s[4] == '+') *result = (double) (mantisse << exponent); + else *result = (double) mantisse / (float) (1 << exponent); + + return -1; + } + return 0; +} + +int +eet_dictionary_string_get_float(const Eet_Dictionary *ed, int index, float *result) +{ + if (!result) return 0; + if (!ed) return 0; + if (index < 0) return 0; + if (index < ed->count) + { + if (!(ed->all[index].flags.converted + && ed->all[index].flags.is_float)) + { + const char *str; + + str = ed->all[index].str ? ed->all[index].str : ed->all[index].mmap; + + if (!_eet_dictionary_string_get_float_cache(str, ed->all[index].len, &ed->all[index].convert.f)) + { + long long mantisse = 0; + long exponent = 0; + + if (!_eet_string_to_double_convert(str, &mantisse, &exponent)) + return 0; + + ed->all[index].convert.f = ldexpf((float) mantisse, exponent); + } + + ed->all[index].flags.is_float = 1; + } + + *result = ed->all[index].convert.f; + return -1; + } + return 0; +} + +int +eet_dictionary_string_get_double(const Eet_Dictionary *ed, int index, double *result) +{ + if (!result) return 0; + if (!ed) return 0; + if (index < 0) return 0; + if (index < ed->count) + { + if (!(ed->all[index].flags.converted + && !ed->all[index].flags.is_float)) + { + const char *str; + + str = ed->all[index].str ? ed->all[index].str : ed->all[index].mmap; + + if (!_eet_dictionary_string_get_double_cache(str, ed->all[index].len, &ed->all[index].convert.d)) + { + long long mantisse = 0; + long exponent = 0; + + if (!_eet_string_to_double_convert(str, &mantisse, &exponent)) + return 0; + + ed->all[index].convert.d = ldexp((double) mantisse, exponent); + } + + ed->all[index].flags.is_float = 0; + } + + *result = ed->all[index].convert.d; + return -1; + } + return 0; +}