diff options
Diffstat (limited to 'tests/suite/ecore/src/lib/eina_convert.c')
-rw-r--r-- | tests/suite/ecore/src/lib/eina_convert.c | 595 |
1 files changed, 284 insertions, 311 deletions
diff --git a/tests/suite/ecore/src/lib/eina_convert.c b/tests/suite/ecore/src/lib/eina_convert.c index 0d75469ca0..d949723c28 100644 --- a/tests/suite/ecore/src/lib/eina_convert.c +++ b/tests/suite/ecore/src/lib/eina_convert.c @@ -17,7 +17,7 @@ */ #ifdef HAVE_CONFIG_H -# include "config.h" +#include "config.h" #endif #include <math.h> @@ -26,7 +26,7 @@ #include <stdio.h> #ifdef HAVE_EVIL -# include <Evil.h> +#include <Evil.h> #endif #include "eina_config.h" @@ -46,9 +46,11 @@ * @cond LOCAL */ -static const char look_up_table[] = {'0', '1', '2', '3', '4', - '5', '6', '7', '8', '9', - 'a', 'b', 'c', 'd', 'e', 'f'}; +static const char look_up_table[] = { '0', '1', '2', '3', '4', + '5', '6', '7', '8', '9', + 'a', 'b', 'c', 'd', 'e', 'f' +}; + static int _eina_convert_log_dom = -1; #ifdef ERR @@ -65,15 +67,14 @@ static int _eina_convert_log_dom = -1; static inline void reverse(char s[], int length) { - int i, j; - char c; - - for (i = 0, j = length - 1; i < j; i++, j--) - { - c = s[i]; - s[i] = s[j]; - s[j] = c; - } + int i, j; + char c; + + for (i = 0, j = length - 1; i < j; i++, j--) { + c = s[i]; + s[i] = s[j]; + s[j] = c; + } } /** @@ -93,11 +94,11 @@ EAPI Eina_Error EINA_ERROR_CONVERT_0X_NOT_FOUND = 0; EAPI Eina_Error EINA_ERROR_CONVERT_OUTRUN_STRING_LENGTH = 0; static const char EINA_ERROR_CONVERT_0X_NOT_FOUND_STR[] = - "Error during string conversion to float, First '0x' was not found."; + "Error during string conversion to float, First '0x' was not found."; static const char EINA_ERROR_CONVERT_P_NOT_FOUND_STR[] = - "Error during string conversion to float, First 'p' was not found."; + "Error during string conversion to float, First 'p' was not found."; static const char EINA_ERROR_CONVERT_OUTRUN_STRING_LENGTH_STR[] = - "Error outrun string limit during conversion string conversion to float."; + "Error outrun string limit during conversion string conversion to float."; /** * @endcond @@ -119,24 +120,22 @@ static const char EINA_ERROR_CONVERT_OUTRUN_STRING_LENGTH_STR[] = * * @see eina_init() */ -Eina_Bool -eina_convert_init(void) +Eina_Bool eina_convert_init(void) { - _eina_convert_log_dom = eina_log_domain_register("eina_convert", - EINA_LOG_COLOR_DEFAULT); - if (_eina_convert_log_dom < 0) - { - EINA_LOG_ERR("Could not register log domain: eina_convert"); - return EINA_FALSE; - } - + _eina_convert_log_dom = eina_log_domain_register("eina_convert", + EINA_LOG_COLOR_DEFAULT); + if (_eina_convert_log_dom < 0) { + EINA_LOG_ERR + ("Could not register log domain: eina_convert"); + return EINA_FALSE; + } #define EEMR(n) n = eina_error_msg_static_register(n ## _STR) - EEMR(EINA_ERROR_CONVERT_0X_NOT_FOUND); - EEMR(EINA_ERROR_CONVERT_P_NOT_FOUND); - EEMR(EINA_ERROR_CONVERT_OUTRUN_STRING_LENGTH); + EEMR(EINA_ERROR_CONVERT_0X_NOT_FOUND); + EEMR(EINA_ERROR_CONVERT_P_NOT_FOUND); + EEMR(EINA_ERROR_CONVERT_OUTRUN_STRING_LENGTH); #undef EEMR - return EINA_TRUE; + return EINA_TRUE; } /** @@ -150,12 +149,11 @@ eina_convert_init(void) * * @see eina_shutdown() */ -Eina_Bool -eina_convert_shutdown(void) +Eina_Bool eina_convert_shutdown(void) { - eina_log_domain_unregister(_eina_convert_log_dom); - _eina_convert_log_dom = -1; - return EINA_TRUE; + eina_log_domain_unregister(_eina_convert_log_dom); + _eina_convert_log_dom = -1; + return EINA_TRUE; } /*============================================================================* @@ -305,30 +303,28 @@ eina_convert_shutdown(void) * The returned value is the length of the string, including the nul * terminated character. */ -EAPI int -eina_convert_itoa(int n, char *s) +EAPI int eina_convert_itoa(int n, char *s) { - int i = 0; - int r = 0; + int i = 0; + int r = 0; - EINA_SAFETY_ON_NULL_RETURN_VAL(s, 0); + EINA_SAFETY_ON_NULL_RETURN_VAL(s, 0); - if (n < 0) - { - n = -n; - *s++ = '-'; - r = 1; - } + if (n < 0) { + n = -n; + *s++ = '-'; + r = 1; + } - do { - s[i++] = n % 10 + '0'; - } while ((n /= 10) > 0); + do { + s[i++] = n % 10 + '0'; + } while ((n /= 10) > 0); - s[i] = '\0'; + s[i] = '\0'; - reverse(s, i); + reverse(s, i); - return i + r; + return i + r; } /** @@ -347,23 +343,22 @@ eina_convert_itoa(int n, char *s) * The returned value is the length of the string, including the nul * terminated character. */ -EAPI int -eina_convert_xtoa(unsigned int n, char *s) +EAPI int eina_convert_xtoa(unsigned int n, char *s) { - int i; + int i; - EINA_SAFETY_ON_NULL_RETURN_VAL(s, 0); + EINA_SAFETY_ON_NULL_RETURN_VAL(s, 0); - i = 0; - do { - s[i++] = look_up_table[n & 0xF]; - } while ((n >>= 4) > 0); + i = 0; + do { + s[i++] = look_up_table[n & 0xF]; + } while ((n >>= 4) > 0); - s[i] = '\0'; + s[i] = '\0'; - reverse(s, i); + reverse(s, i); - return i; + return i; } /** @@ -411,99 +406,96 @@ eina_convert_xtoa(unsigned int n, char *s) EAPI Eina_Bool eina_convert_atod(const char *src, int length, long long *m, long *e) { - const char *str = src; - long long mantisse; - long exponent; - int nbr_decimals = 0; - int sign = 1; - - EINA_SAFETY_ON_NULL_RETURN_VAL(src, EINA_FALSE); - EINA_SAFETY_ON_NULL_RETURN_VAL(m, EINA_FALSE); - EINA_SAFETY_ON_NULL_RETURN_VAL(e, EINA_FALSE); - - if (length <= 0) - goto on_length_error; - - /* Compute the mantisse. */ - if (*str == '-') - { - sign = -1; - str++; - length--; - } - - if (length <= 2) - goto on_length_error; - - if (strncmp(str, "0x", 2)) - { - eina_error_set(EINA_ERROR_CONVERT_0X_NOT_FOUND); - DBG("'0x' not found in '%s'", src); - return EINA_FALSE; - } - - str += 2; - length -= 2; - - mantisse = HEXA_TO_INT(*str); - - str++; - length--; if (length <= 0) - goto on_length_error; - - if (*str == '.') - for (str++, length--; - length > 0 && *str != 'p'; - ++str, --length, ++nbr_decimals) - { - mantisse <<= 4; - mantisse += HEXA_TO_INT(*str); - } - - if (sign < 0) - mantisse = -mantisse; - - /* Compute the exponent. */ - if (*str != 'p') - { - eina_error_set(EINA_ERROR_CONVERT_P_NOT_FOUND); - DBG("'p' not found in '%s'", src); - return EINA_FALSE; - } - - sign = +1; - - str++; - length--; if (length <= 0) - goto on_length_error; - - if (strchr("-+", *str)) - { - sign = (*str == '-') ? -1 : +1; - - str++; length--; - } - - for (exponent = 0; length > 0 && *str != '\0'; ++str, --length) - { - exponent *= 10; - exponent += *str - '0'; - } - - if (length < 0) - goto on_length_error; - - if (sign < 0) - exponent = -exponent; - - *m = mantisse; - *e = exponent - (nbr_decimals << 2); - - return EINA_TRUE; - -on_length_error: - eina_error_set(EINA_ERROR_CONVERT_OUTRUN_STRING_LENGTH); - return EINA_FALSE; + const char *str = src; + long long mantisse; + long exponent; + int nbr_decimals = 0; + int sign = 1; + + EINA_SAFETY_ON_NULL_RETURN_VAL(src, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(m, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(e, EINA_FALSE); + + if (length <= 0) + goto on_length_error; + + /* Compute the mantisse. */ + if (*str == '-') { + sign = -1; + str++; + length--; + } + + if (length <= 2) + goto on_length_error; + + if (strncmp(str, "0x", 2)) { + eina_error_set(EINA_ERROR_CONVERT_0X_NOT_FOUND); + DBG("'0x' not found in '%s'", src); + return EINA_FALSE; + } + + str += 2; + length -= 2; + + mantisse = HEXA_TO_INT(*str); + + str++; + length--; + if (length <= 0) + goto on_length_error; + + if (*str == '.') + for (str++, length--; + length > 0 && *str != 'p'; + ++str, --length, ++nbr_decimals) { + mantisse <<= 4; + mantisse += HEXA_TO_INT(*str); + } + + if (sign < 0) + mantisse = -mantisse; + + /* Compute the exponent. */ + if (*str != 'p') { + eina_error_set(EINA_ERROR_CONVERT_P_NOT_FOUND); + DBG("'p' not found in '%s'", src); + return EINA_FALSE; + } + + sign = +1; + + str++; + length--; + if (length <= 0) + goto on_length_error; + + if (strchr("-+", *str)) { + sign = (*str == '-') ? -1 : +1; + + str++; + length--; + } + + for (exponent = 0; length > 0 && *str != '\0'; ++str, --length) { + exponent *= 10; + exponent += *str - '0'; + } + + if (length < 0) + goto on_length_error; + + if (sign < 0) + exponent = -exponent; + + *m = mantisse; + *e = exponent - (nbr_decimals << 2); + + return EINA_TRUE; + + on_length_error: + eina_error_set(EINA_ERROR_CONVERT_OUTRUN_STRING_LENGTH); + return EINA_FALSE; } /** @@ -528,67 +520,59 @@ on_length_error: * The returned value is the length of the string, including the nul * character. */ -EAPI int -eina_convert_dtoa(double d, char *des) +EAPI int eina_convert_dtoa(double d, char *des) { - int length = 0; - int p; - int i; - - EINA_SAFETY_ON_NULL_RETURN_VAL(des, EINA_FALSE); - - if (d < 0.0) - { - *(des++) = '-'; - d = -d; - length++; - } - - d = frexp(d, &p); - - if (p) - { - d *= 2; - p -= 1; - } - - *(des++) = '0'; - *(des++) = 'x'; - *(des++) = look_up_table[(size_t)d]; - *(des++) = '.'; - length += 4; - - for (i = 0; i < 16; i++, length++) - { - d -= floor(d); - d *= 16; - *(des++) = look_up_table[(size_t)d]; - } - - while (*(des - 1) == '0') - { - des--; - length--; - } - - if (*(des - 1) == '.') - { - des--; - length--; - } - - *(des++) = 'p'; - if (p < 0) - { - *(des++) = '-'; - p = -p; - } - else - *(des++) = '+'; - - length += 2; - - return length + eina_convert_itoa(p, des); + int length = 0; + int p; + int i; + + EINA_SAFETY_ON_NULL_RETURN_VAL(des, EINA_FALSE); + + if (d < 0.0) { + *(des++) = '-'; + d = -d; + length++; + } + + d = frexp(d, &p); + + if (p) { + d *= 2; + p -= 1; + } + + *(des++) = '0'; + *(des++) = 'x'; + *(des++) = look_up_table[(size_t) d]; + *(des++) = '.'; + length += 4; + + for (i = 0; i < 16; i++, length++) { + d -= floor(d); + d *= 16; + *(des++) = look_up_table[(size_t) d]; + } + + while (*(des - 1) == '0') { + des--; + length--; + } + + if (*(des - 1) == '.') { + des--; + length--; + } + + *(des++) = 'p'; + if (p < 0) { + *(des++) = '-'; + p = -p; + } else + *(des++) = '+'; + + length += 2; + + return length + eina_convert_itoa(p, des); } /** @@ -618,88 +602,77 @@ eina_convert_dtoa(double d, char *des) * implements the frexp() function for fixed point numbers and does * some optimisations. */ -EAPI int -eina_convert_fptoa(Eina_F32p32 fp, char *des) +EAPI int eina_convert_fptoa(Eina_F32p32 fp, char *des) { - int length = 0; - int p = 0; - int i; - - EINA_SAFETY_ON_NULL_RETURN_VAL(des, EINA_FALSE); - - if (fp == 0) - { - memcpy(des, "0x0p+0", 7); - return 7; - } - - if (fp < 0) - { - *(des++) = '-'; - fp = -fp; - length++; - } - - /* fp >= 1 */ - if (fp >= 0x0000000100000000LL) - while (fp >= 0x0000000100000000LL) - { - p++; - /* fp /= 2 */ - fp >>= 1; - } /* fp < 0.5 */ - else if (fp < 0x80000000) - while (fp < 0x80000000) - { - p--; - /* fp *= 2 */ - fp <<= 1; - } - - if (p) - { - p--; - /* fp *= 2 */ - fp <<= 1; - } - - *(des++) = '0'; - *(des++) = 'x'; - *(des++) = look_up_table[fp >> 32]; - *(des++) = '.'; - length += 4; - - for (i = 0; i < 16; i++, length++) - { - fp &= 0x00000000ffffffffLL; - fp <<= 4; /* fp *= 16 */ - *(des++) = look_up_table[fp >> 32]; - } - - while (*(des - 1) == '0') - { - des--; - length--; - } - - if (*(des - 1) == '.') - { - des--; - length--; - } - - *(des++) = 'p'; - if (p < 0) - { - *(des++) = '-'; - p = -p; - } - else - *(des++) = '+'; - - length += 2; - - return length + eina_convert_itoa(p, des); + int length = 0; + int p = 0; + int i; + + EINA_SAFETY_ON_NULL_RETURN_VAL(des, EINA_FALSE); + + if (fp == 0) { + memcpy(des, "0x0p+0", 7); + return 7; + } + + if (fp < 0) { + *(des++) = '-'; + fp = -fp; + length++; + } + + /* fp >= 1 */ + if (fp >= 0x0000000100000000LL) + while (fp >= 0x0000000100000000LL) { + p++; + /* fp /= 2 */ + fp >>= 1; + } /* fp < 0.5 */ + else if (fp < 0x80000000) + while (fp < 0x80000000) { + p--; + /* fp *= 2 */ + fp <<= 1; + } + + if (p) { + p--; + /* fp *= 2 */ + fp <<= 1; + } + + *(des++) = '0'; + *(des++) = 'x'; + *(des++) = look_up_table[fp >> 32]; + *(des++) = '.'; + length += 4; + + for (i = 0; i < 16; i++, length++) { + fp &= 0x00000000ffffffffLL; + fp <<= 4; /* fp *= 16 */ + *(des++) = look_up_table[fp >> 32]; + } + + while (*(des - 1) == '0') { + des--; + length--; + } + + if (*(des - 1) == '.') { + des--; + length--; + } + + *(des++) = 'p'; + if (p < 0) { + *(des++) = '-'; + p = -p; + } else + *(des++) = '+'; + + length += 2; + + return length + eina_convert_itoa(p, des); } /** @@ -747,25 +720,25 @@ eina_convert_fptoa(Eina_F32p32 fp, char *des) * shift to compute the fixed point number. */ EAPI Eina_Bool -eina_convert_atofp(const char *src, int length, Eina_F32p32 *fp) +eina_convert_atofp(const char *src, int length, Eina_F32p32 * fp) { - long long m; - long e; + long long m; + long e; - if (!eina_convert_atod(src, length, &m, &e)) - return EINA_FALSE; + if (!eina_convert_atod(src, length, &m, &e)) + return EINA_FALSE; - if (!fp) - return EINA_TRUE; + if (!fp) + return EINA_TRUE; - e += 32; + e += 32; - if (e > 0) - *fp = m << e; - else - *fp = m >> -e; + if (e > 0) + *fp = m << e; + else + *fp = m >> -e; - return EINA_TRUE; + return EINA_TRUE; } /** |