2008-08-21 07:20:41 -07:00
|
|
|
/* EINA - EFL data type library
|
|
|
|
* Copyright (C) 2008 Cedric BAIL, Vincent Torri
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library;
|
|
|
|
* if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-09-03 17:53:19 -07:00
|
|
|
#include <stdio.h>
|
2008-08-21 07:20:41 -07:00
|
|
|
|
2009-08-28 05:03:34 -07:00
|
|
|
#include "eina_config.h"
|
2009-09-03 18:03:27 -07:00
|
|
|
#include "eina_private.h"
|
|
|
|
#include "eina_log.h"
|
2009-09-06 15:21:56 -07:00
|
|
|
|
|
|
|
/* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
|
2008-12-26 10:31:14 -08:00
|
|
|
#include "eina_safety_checks.h"
|
2009-08-28 05:03:34 -07:00
|
|
|
#include "eina_convert.h"
|
2009-09-23 09:47:08 -07:00
|
|
|
#include "eina_f32p32.h"
|
2008-08-21 07:20:41 -07:00
|
|
|
|
2008-08-28 00:46:42 -07:00
|
|
|
/*============================================================================*
|
|
|
|
* Local *
|
|
|
|
*============================================================================*/
|
|
|
|
|
2008-09-17 11:55:54 -07:00
|
|
|
/**
|
|
|
|
* @cond LOCAL
|
|
|
|
*/
|
|
|
|
|
2008-08-21 07:20:41 -07:00
|
|
|
static const char look_up_table[] = {'0', '1', '2', '3', '4',
|
|
|
|
'5', '6', '7', '8', '9',
|
|
|
|
'a', 'b', 'c', 'd', 'e', 'f'};
|
2009-09-03 18:03:27 -07:00
|
|
|
static int _eina_convert_log_dom = -1;
|
|
|
|
|
2009-09-06 21:23:05 -07:00
|
|
|
#ifdef ERR
|
|
|
|
#undef ERR
|
|
|
|
#endif
|
2009-09-03 18:03:27 -07:00
|
|
|
#define ERR(...) EINA_LOG_DOM_ERR(_eina_convert_log_dom, __VA_ARGS__)
|
2009-09-06 21:23:05 -07:00
|
|
|
|
|
|
|
#ifdef DBG
|
|
|
|
#undef DBG
|
|
|
|
#endif
|
2009-09-03 18:03:27 -07:00
|
|
|
#define DBG(...) EINA_LOG_DOM_DBG(_eina_convert_log_dom, __VA_ARGS__)
|
2008-08-21 07:20:41 -07:00
|
|
|
|
|
|
|
#define HEXA_TO_INT(Hexa) (Hexa >= 'a') ? Hexa - 'a' + 10 : Hexa - '0'
|
|
|
|
|
|
|
|
static inline void reverse(char s[], int length)
|
|
|
|
{
|
2008-12-29 00:53:01 -08:00
|
|
|
int i, j;
|
|
|
|
char c;
|
2008-08-21 07:20:41 -07:00
|
|
|
|
|
|
|
for (i = 0, j = length - 1; i < j; i++, j--)
|
|
|
|
{
|
|
|
|
c = s[i];
|
|
|
|
s[i] = s[j];
|
|
|
|
s[j] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-23 09:47:08 -07:00
|
|
|
static inline Eina_F32p32 eina_f32p32_mul2(Eina_F32p32 fp)
|
|
|
|
{
|
|
|
|
int64_t low;
|
|
|
|
int64_t high;
|
|
|
|
|
|
|
|
low = (fp & 0x00000000ffffffffLL) << 1;
|
|
|
|
high = (fp >> 32) << 33;
|
|
|
|
|
|
|
|
return low + high;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Eina_F32p32 eina_f32p32_mul16(Eina_F32p32 fp)
|
|
|
|
{
|
|
|
|
int64_t low;
|
|
|
|
int64_t high;
|
|
|
|
|
|
|
|
low = (fp & 0x00000000ffffffffLL) << 4;
|
|
|
|
high = (fp >> 32) << 36;
|
|
|
|
|
|
|
|
return low + high;
|
|
|
|
}
|
|
|
|
|
2008-09-17 11:55:54 -07:00
|
|
|
/**
|
|
|
|
* @endcond
|
|
|
|
*/
|
|
|
|
|
2008-08-28 00:46:42 -07:00
|
|
|
/*============================================================================*
|
|
|
|
* Global *
|
|
|
|
*============================================================================*/
|
|
|
|
|
|
|
|
/*============================================================================*
|
|
|
|
* API *
|
|
|
|
*============================================================================*/
|
|
|
|
|
2008-09-17 11:55:54 -07:00
|
|
|
/**
|
|
|
|
* @cond LOCAL
|
|
|
|
*/
|
|
|
|
|
2008-08-28 00:46:42 -07:00
|
|
|
EAPI Eina_Error EINA_ERROR_CONVERT_P_NOT_FOUND = 0;
|
|
|
|
EAPI Eina_Error EINA_ERROR_CONVERT_0X_NOT_FOUND = 0;
|
|
|
|
EAPI Eina_Error EINA_ERROR_CONVERT_OUTRUN_STRING_LENGTH = 0;
|
|
|
|
|
2009-09-06 20:32:22 -07:00
|
|
|
static const char EINA_ERROR_CONVERT_0X_NOT_FOUND_STR[] = "Error during string convertion to float, First '0x' was not found.";
|
|
|
|
static const char EINA_ERROR_CONVERT_P_NOT_FOUND_STR[] = "Error during string convertion to float, First 'p' was not found.";
|
|
|
|
static const char EINA_ERROR_CONVERT_OUTRUN_STRING_LENGTH_STR[] = "Error outrun string limit during convertion string convertion to float.";
|
|
|
|
|
2008-09-17 11:55:54 -07:00
|
|
|
/**
|
|
|
|
* @endcond
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2008-09-18 22:22:43 -07:00
|
|
|
* @addtogroup Eina_Convert_Group Convert
|
2008-09-17 11:55:54 -07:00
|
|
|
*
|
|
|
|
* These functions allow you to convert integer or real numbers to
|
|
|
|
* string or conversely.
|
|
|
|
*
|
2009-09-06 15:21:56 -07:00
|
|
|
* To use these functions, you have to call eina_init()
|
|
|
|
* first, and eina_shutdown() when eina is not used anymore.
|
2008-09-23 08:57:57 -07:00
|
|
|
*
|
|
|
|
* @section Eina_Convert_From_Integer_To_Sring Conversion from integer to string
|
|
|
|
*
|
|
|
|
* To convert an integer to a string in the decimal base,
|
|
|
|
* eina_convert_itoa() should be used. If the hexadecimal base is
|
|
|
|
* wanted, eina_convert_xtoa() should be used. They all need a bufffer
|
|
|
|
* sufficiently large to store all the cyphers.
|
|
|
|
*
|
|
|
|
* Here is an exemple of use:
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* #include <stdlib.h>
|
|
|
|
* #include <stdio.h>
|
|
|
|
*
|
|
|
|
* #include <eina_convert.h>
|
|
|
|
*
|
|
|
|
* int main(void)
|
|
|
|
* {
|
|
|
|
* char *tmp[128];
|
|
|
|
*
|
2009-09-06 15:21:56 -07:00
|
|
|
* if (!eina_init())
|
2008-09-23 08:57:57 -07:00
|
|
|
* {
|
2009-09-06 15:21:56 -07:00
|
|
|
* printf ("Error during the initialization of eina.\n");
|
2008-09-23 08:57:57 -07:00
|
|
|
* return EXIT_FAILURE;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* eina_convert_itoa(45, tmp);
|
|
|
|
* printf("value: %s\n", tmp);
|
|
|
|
|
|
|
|
* eina_convert_xtoa(0xA1, tmp);
|
|
|
|
* printf("value: %s\n", tmp);
|
|
|
|
*
|
2009-09-06 15:21:56 -07:00
|
|
|
* eina_shutdown();
|
2008-09-23 08:57:57 -07:00
|
|
|
*
|
|
|
|
* return EXIT_SUCCESS;
|
|
|
|
* }
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* Compile this code with the following commant:
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* gcc -Wall -o test_eina_convert test_eina.c `pkg-config --cflags --libs eina`
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
* The alphabetical cyphers are in lower case.
|
|
|
|
*
|
|
|
|
* @section Eina_Convert_Double Conversion double / string
|
|
|
|
*
|
|
|
|
* To convert a double to a string, eina_convert_dtoa() should be
|
|
|
|
* used. Like with the integer functions, a buffer must be used. The
|
|
|
|
* resulting string ghas the following format (which is the result
|
|
|
|
* obtained with snprintf() and the @%a modifier):
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* [-]0xh.hhhhhp[+-]e
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* To convert a string to a double, eina_convert_atod() should be
|
|
|
|
* used. The format of the string must be as above. Then, the double
|
|
|
|
* has the following mantiss and exponent:
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* mantiss : [-]hhhhhh
|
|
|
|
* exponent : 2^([+-]e - 4 * n)
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* with n being number of cypers after the point in the string
|
|
|
|
* format. To obtain the double number from the mantiss and exponent,
|
|
|
|
* use ldexp().
|
|
|
|
*
|
|
|
|
* Here is an exemple of use:
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* #include <stdlib.h>
|
|
|
|
* #include <stdio.h>
|
|
|
|
*
|
|
|
|
* #include <eina_convert.h>
|
|
|
|
*
|
|
|
|
* int main(void)
|
|
|
|
* {
|
|
|
|
* char *tmp[128];
|
|
|
|
* long long int m = 0;
|
|
|
|
* long int e = 0;
|
|
|
|
* doule r;
|
|
|
|
*
|
2009-09-06 15:21:56 -07:00
|
|
|
* if (!eina_init())
|
2008-09-23 08:57:57 -07:00
|
|
|
* {
|
2009-09-06 15:21:56 -07:00
|
|
|
* printf ("Error during the initialization of eina.\n");
|
2008-09-23 08:57:57 -07:00
|
|
|
* return EXIT_FAILURE;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* eina_convert_dtoa(40.56, tmp);
|
|
|
|
* printf("value: %s\n", tmp);
|
|
|
|
|
|
|
|
* eina_convert_atod(tmp, 128, &m, &e);
|
|
|
|
* r = ldexp((double)m, e);
|
|
|
|
* printf("value: %s\n", tmp);
|
|
|
|
*
|
2009-09-06 15:21:56 -07:00
|
|
|
* eina_shutdown();
|
2008-09-23 08:57:57 -07:00
|
|
|
*
|
|
|
|
* return EXIT_SUCCESS;
|
|
|
|
* }
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* Compile this code with the same command as above.
|
|
|
|
*
|
2008-09-17 11:55:54 -07:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2009-09-06 15:21:56 -07:00
|
|
|
* @internal
|
|
|
|
* @brief Initialize the convert module.
|
|
|
|
*
|
|
|
|
* @return #EINA_TRUE on success, #EINA_FALSE on failure.
|
2008-09-17 11:55:54 -07:00
|
|
|
*
|
2009-09-06 15:21:56 -07:00
|
|
|
* This function sets up the convert module of Eina. It is called by
|
|
|
|
* eina_init().
|
2008-09-17 11:55:54 -07:00
|
|
|
*
|
|
|
|
* This function sets up the error module of Eina and registers the
|
|
|
|
* errors #EINA_ERROR_CONVERT_0X_NOT_FOUND,
|
|
|
|
* #EINA_ERROR_CONVERT_P_NOT_FOUND and
|
2009-09-06 15:21:56 -07:00
|
|
|
* #EINA_ERROR_CONVERT_OUTRUN_STRING_LENGTH.
|
|
|
|
*
|
|
|
|
* @see eina_init()
|
2008-09-17 11:55:54 -07:00
|
|
|
*/
|
2009-09-06 15:21:56 -07:00
|
|
|
Eina_Bool
|
2008-09-17 11:55:54 -07:00
|
|
|
eina_convert_init(void)
|
|
|
|
{
|
2009-09-03 18:03:27 -07:00
|
|
|
_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");
|
2009-09-06 15:21:56 -07:00
|
|
|
return EINA_FALSE;
|
2009-09-03 18:03:27 -07:00
|
|
|
}
|
|
|
|
|
2009-09-06 20:32:22 -07:00
|
|
|
#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);
|
|
|
|
#undef EEMR
|
2009-09-03 17:53:19 -07:00
|
|
|
|
2009-09-06 15:21:56 -07:00
|
|
|
return EINA_TRUE;
|
2008-09-17 11:55:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-09-06 15:21:56 -07:00
|
|
|
* @internal
|
|
|
|
* @brief Shut down the convert module.
|
|
|
|
*
|
|
|
|
* @return #EINA_TRUE on success, #EINA_FALSE on failure.
|
2008-09-17 11:55:54 -07:00
|
|
|
*
|
2009-09-06 15:21:56 -07:00
|
|
|
* This function shuts down the convert module set up by
|
|
|
|
* eina_convert_init(). It is called by eina_shutdown().
|
2008-09-17 11:55:54 -07:00
|
|
|
*
|
2009-09-06 15:21:56 -07:00
|
|
|
* @see eina_shutdown()
|
2008-09-17 11:55:54 -07:00
|
|
|
*/
|
2009-09-06 15:21:56 -07:00
|
|
|
Eina_Bool
|
2008-09-17 11:55:54 -07:00
|
|
|
eina_convert_shutdown(void)
|
|
|
|
{
|
2009-09-03 18:03:27 -07:00
|
|
|
eina_log_domain_unregister(_eina_convert_log_dom);
|
2009-09-06 15:21:56 -07:00
|
|
|
_eina_convert_log_dom = -1;
|
|
|
|
return EINA_TRUE;
|
2008-09-17 11:55:54 -07:00
|
|
|
}
|
|
|
|
|
2008-08-21 07:20:41 -07:00
|
|
|
/*
|
|
|
|
* Come from the second edition of The C Programming Language ("K&R2") on page 64
|
|
|
|
*/
|
2008-09-17 11:55:54 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Convert an integer number to a string in decimal base.
|
|
|
|
*
|
|
|
|
* @param n The integer to convert.
|
|
|
|
* @param s The buffer to store the converted integer.
|
|
|
|
* @return The length of the string, including the nul terminated
|
|
|
|
* character.
|
|
|
|
*
|
|
|
|
* This function converts @p n to a nul terminated string. The
|
|
|
|
* converted string is in decimal base. As no check is done, @p s must
|
|
|
|
* be a buffer that is sufficiently large to store the integer.
|
|
|
|
*
|
2009-09-25 01:44:04 -07:00
|
|
|
* The returned value is the length of the string, including the nul
|
2008-09-17 11:55:54 -07:00
|
|
|
* terminated character.
|
|
|
|
*/
|
2008-08-21 07:20:41 -07:00
|
|
|
EAPI int
|
|
|
|
eina_convert_itoa(int n, char *s)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
int r = 0;
|
|
|
|
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(s, 0);
|
|
|
|
|
2008-08-21 07:20:41 -07:00
|
|
|
if (n < 0)
|
|
|
|
{
|
|
|
|
n = -n;
|
|
|
|
*s++ = '-';
|
|
|
|
r = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
s[i++] = n % 10 + '0';
|
|
|
|
} while ((n /= 10) > 0);
|
|
|
|
|
|
|
|
s[i] = '\0';
|
|
|
|
|
|
|
|
reverse(s, i);
|
|
|
|
|
|
|
|
return i + r;
|
|
|
|
}
|
|
|
|
|
2008-09-17 11:55:54 -07:00
|
|
|
/**
|
|
|
|
* @brief Convert an integer number to a string in hexadecimal base.
|
|
|
|
*
|
|
|
|
* @param n The integer to convert.
|
|
|
|
* @param s The buffer to store the converted integer.
|
|
|
|
* @return The length of the string, including the nul terminated
|
|
|
|
* character.
|
|
|
|
*
|
|
|
|
* This function converts @p n to a nul terminated string. The
|
2008-09-23 08:57:57 -07:00
|
|
|
* converted string is in hexadecimal base and the alphabetical
|
|
|
|
* cyphers are in lower case. As no check is done, @p s must be a
|
|
|
|
* buffer that is sufficiently large to store the integer.
|
2008-09-17 11:55:54 -07:00
|
|
|
*
|
2009-09-25 01:44:04 -07:00
|
|
|
* The returned value is the length of the string, including the nul
|
2008-09-17 11:55:54 -07:00
|
|
|
* terminated character.
|
|
|
|
*/
|
2008-08-21 07:20:41 -07:00
|
|
|
EAPI int
|
|
|
|
eina_convert_xtoa(unsigned int n, char *s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(s, 0);
|
|
|
|
|
2008-08-21 07:20:41 -07:00
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
s[i++] = look_up_table[n & 0xF];
|
|
|
|
} while ((n >>= 4) > 0);
|
|
|
|
|
|
|
|
s[i] = '\0';
|
|
|
|
|
|
|
|
reverse(s, i);
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2008-09-17 11:55:54 -07:00
|
|
|
/**
|
|
|
|
* @brief Convert a string to a double
|
|
|
|
*
|
|
|
|
* @param src The string to convert.
|
|
|
|
* @param length The length of the string.
|
|
|
|
* @param m The mantisse.
|
|
|
|
* @param e The exponent.
|
|
|
|
* @return #EINA_TRUE on success, #EINA_FALSE otherwise.
|
|
|
|
*
|
|
|
|
* This function converts the string @p s of length @p length that
|
|
|
|
* represent a double in hexadecimal base to a double. It is used to
|
|
|
|
* replace the use of snprintf() with the \%a modifier, which is
|
|
|
|
* missing on some platform (like Windows (tm) or OpenBSD).
|
|
|
|
*
|
|
|
|
* The string must have the following format:
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* [-]0xh.hhhhhp[+-]e
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* where the h are the hexadecimal cyphers of the mantiss and e the
|
|
|
|
* exponent (a decimal number). If n is the number of cypers after the
|
|
|
|
* point, the returned mantiss and exponents are:
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* mantiss : [-]hhhhhh
|
|
|
|
* exponent : 2^([+-]e - 4 * n)
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* The mantiss and exponent are stored in the buffers pointed
|
|
|
|
* respectively by @p m and @p e.
|
|
|
|
*
|
|
|
|
* If the string is invalid, the error is set to:
|
|
|
|
*
|
|
|
|
* @li #EINA_ERROR_CONVERT_0X_NOT_FOUND if no 0x is found,
|
|
|
|
* @li #EINA_ERROR_CONVERT_P_NOT_FOUND if no p is found,
|
|
|
|
* @li #EINA_ERROR_CONVERT_OUTRUN_STRING_LENGTH if @p length is not
|
|
|
|
* correct.
|
|
|
|
*
|
|
|
|
* In those cases, #EINA_FALSE is returned, otherwise #EINA_TRUE is
|
|
|
|
* returned.
|
|
|
|
*/
|
2008-08-21 07:20:41 -07:00
|
|
|
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;
|
|
|
|
|
2008-12-26 10:31:14 -08:00
|
|
|
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);
|
|
|
|
|
2008-08-21 07:20:41 -07:00
|
|
|
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);
|
2009-09-03 18:03:27 -07:00
|
|
|
DBG("'0x' not found in '%s'", src);
|
2008-08-21 07:20:41 -07:00
|
|
|
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);
|
2009-09-03 18:03:27 -07:00
|
|
|
DBG("'p' not found in '%s'", src);
|
2008-08-21 07:20:41 -07:00
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
2008-10-20 06:21:52 -07:00
|
|
|
if (length < 0) goto on_length_error;
|
2008-08-21 07:20:41 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-09-17 11:55:54 -07:00
|
|
|
/**
|
|
|
|
* @brief Convert a double to a string
|
|
|
|
*
|
|
|
|
* @param d The double to convert.
|
|
|
|
* @param des The destination buffer to store the converted double.
|
|
|
|
* @return #EINA_TRUE on success, #EINA_FALSE otherwise.
|
|
|
|
*
|
|
|
|
* This function converts the double @p d to a string. The string is
|
|
|
|
* stored in the buffer pointed by @p des and must be sufficiently
|
2009-09-25 01:44:04 -07:00
|
|
|
* large to contain the converted double. The returned string is nul
|
2008-09-17 11:55:54 -07:00
|
|
|
* terminated and has the following format:
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* [-]0xh.hhhhhp[+-]e
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* where the h are the hexadecimal cyphers of the mantiss and e the
|
|
|
|
* exponent (a decimal number).
|
|
|
|
*
|
|
|
|
* The returned value is the length of the string, including the nul
|
|
|
|
* character.
|
|
|
|
*/
|
2008-08-21 07:20:41 -07:00
|
|
|
EAPI int
|
|
|
|
eina_convert_dtoa(double d, char *des)
|
|
|
|
{
|
|
|
|
int length = 0;
|
|
|
|
int p;
|
|
|
|
int i;
|
|
|
|
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(des, EINA_FALSE);
|
|
|
|
|
2008-08-21 07:20:41 -07:00
|
|
|
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);
|
|
|
|
}
|
2008-09-17 11:55:54 -07:00
|
|
|
|
2009-09-25 01:44:04 -07:00
|
|
|
/**
|
|
|
|
* @brief Convert a 32.32 fixed point number to a string
|
|
|
|
*
|
|
|
|
* @param fp The fixed point number to convert.
|
|
|
|
* @param des The destination buffer to store the converted fixed point number.
|
|
|
|
* @return #EINA_TRUE on success, #EINA_FALSE otherwise.
|
|
|
|
*
|
|
|
|
* This function converts the 32.32 fixed point number @fp to a
|
|
|
|
* string. The string is stored in the buffer pointed by @p des and
|
|
|
|
* must be sufficiently large to contain the converted fixed point
|
|
|
|
* number. The returned string is terminated and has the following
|
|
|
|
* format:
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* [-]0xh.hhhhhp[+-]e
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* where the h are the hexadecimal cyphers of the mantiss and e the
|
|
|
|
* exponent (a decimal number).
|
|
|
|
*
|
|
|
|
* The returned value is the length of the string, including the nul
|
|
|
|
* character.
|
|
|
|
*
|
|
|
|
* @note The code is the same than eina_convert_dtoa() except that it
|
|
|
|
* implements the frexp() function for fixed point numbers and does
|
|
|
|
* some optimisations.
|
|
|
|
*/
|
2009-09-23 09:47:08 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-09-17 11:55:54 -07:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|