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
|
|
|
|
2017-09-22 03:06:10 -07:00
|
|
|
#ifdef _WIN32
|
2009-09-30 10:29:30 -07:00
|
|
|
# include <Evil.h>
|
|
|
|
#endif
|
|
|
|
|
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-29 05:04:50 -07:00
|
|
|
#include "eina_fp.h"
|
2008-08-21 07:20:41 -07:00
|
|
|
|
2008-08-28 00:46:42 -07:00
|
|
|
/*============================================================================*
|
2010-07-27 19:37:05 -07:00
|
|
|
* Local *
|
|
|
|
*============================================================================*/
|
2008-08-28 00:46:42 -07:00
|
|
|
|
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',
|
2010-07-27 19:37:05 -07:00
|
|
|
'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--)
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
c = s[i];
|
|
|
|
s[i] = s[j];
|
|
|
|
s[j] = c;
|
2008-08-21 07:20:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-17 11:55:54 -07:00
|
|
|
/**
|
|
|
|
* @endcond
|
|
|
|
*/
|
|
|
|
|
2008-08-28 00:46:42 -07:00
|
|
|
/*============================================================================*
|
2010-07-27 19:37:05 -07:00
|
|
|
* Global *
|
|
|
|
*============================================================================*/
|
2008-08-28 00:46:42 -07:00
|
|
|
|
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;
|
|
|
|
|
2008-09-17 11:55:54 -07:00
|
|
|
/**
|
|
|
|
* @endcond
|
|
|
|
*/
|
|
|
|
|
2009-12-27 00:45:30 -08:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
* @brief Initialize the convert module.
|
|
|
|
*
|
|
|
|
* @return #EINA_TRUE on success, #EINA_FALSE on failure.
|
|
|
|
*
|
|
|
|
* This function sets up the convert module of Eina. It is called by
|
|
|
|
* eina_init().
|
|
|
|
*
|
|
|
|
* @see eina_init()
|
|
|
|
*/
|
|
|
|
Eina_Bool
|
|
|
|
eina_convert_init(void)
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
_eina_convert_log_dom = eina_log_domain_register("eina_convert",
|
|
|
|
EINA_LOG_COLOR_DEFAULT);
|
2009-12-27 00:45:30 -08:00
|
|
|
if (_eina_convert_log_dom < 0)
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
EINA_LOG_ERR("Could not register log domain: eina_convert");
|
|
|
|
return EINA_FALSE;
|
2009-12-27 00:45:30 -08:00
|
|
|
}
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
* @brief Shut down the convert module.
|
|
|
|
*
|
|
|
|
* @return #EINA_TRUE on success, #EINA_FALSE on failure.
|
|
|
|
*
|
|
|
|
* This function shuts down the convert module set up by
|
|
|
|
* eina_convert_init(). It is called by eina_shutdown().
|
|
|
|
*
|
|
|
|
* @see eina_shutdown()
|
|
|
|
*/
|
|
|
|
Eina_Bool
|
|
|
|
eina_convert_shutdown(void)
|
|
|
|
{
|
|
|
|
eina_log_domain_unregister(_eina_convert_log_dom);
|
|
|
|
_eina_convert_log_dom = -1;
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*============================================================================*
|
2010-07-27 19:37:05 -07:00
|
|
|
* API *
|
|
|
|
*============================================================================*/
|
2009-12-27 00:45:30 -08: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
|
|
|
|
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)
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
*s++ = '-';
|
|
|
|
r = 1;
|
2008-08-21 07:20:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2014-03-06 18:58:36 -08:00
|
|
|
s[i++] = abs(n % 10) + '0';
|
|
|
|
} while (n /= 10);
|
2008-08-21 07:20:41 -07:00
|
|
|
|
|
|
|
s[i] = '\0';
|
|
|
|
|
|
|
|
reverse(s, i);
|
|
|
|
|
|
|
|
return i + r;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2010-07-27 19:37:05 -07:00
|
|
|
s[i++] = look_up_table[n & 0xF];
|
2012-09-06 19:39:25 -07:00
|
|
|
} while ((n >>= 4) > 0);
|
2008-08-21 07:20:41 -07:00
|
|
|
|
|
|
|
s[i] = '\0';
|
|
|
|
|
|
|
|
reverse(s, i);
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI Eina_Bool
|
|
|
|
eina_convert_atod(const char *src, int length, long long *m, long *e)
|
|
|
|
{
|
|
|
|
const char *str = src;
|
2010-07-27 19:37:05 -07:00
|
|
|
long long mantisse;
|
|
|
|
long exponent;
|
|
|
|
int nbr_decimals = 0;
|
|
|
|
int sign = 1;
|
2008-08-21 07:20:41 -07:00
|
|
|
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(src, EINA_FALSE);
|
2010-07-27 19:37:05 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(m, EINA_FALSE);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(e, EINA_FALSE);
|
2008-12-26 10:31:14 -08:00
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
if (length <= 0)
|
2012-09-06 19:39:25 -07:00
|
|
|
goto on_length_error;
|
2008-08-21 07:20:41 -07:00
|
|
|
|
|
|
|
/* Compute the mantisse. */
|
|
|
|
if (*str == '-')
|
|
|
|
{
|
|
|
|
sign = -1;
|
|
|
|
str++;
|
2010-07-27 19:37:05 -07:00
|
|
|
length--;
|
2008-08-21 07:20:41 -07:00
|
|
|
}
|
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
if (length <= 2)
|
2012-09-06 19:39:25 -07:00
|
|
|
goto on_length_error;
|
2008-08-21 07:20:41 -07:00
|
|
|
|
|
|
|
if (strncmp(str, "0x", 2))
|
|
|
|
{
|
2010-07-27 19:37:05 -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++;
|
2010-07-27 19:37:05 -07:00
|
|
|
length--; if (length <= 0)
|
|
|
|
goto on_length_error;
|
2008-08-21 07:20:41 -07:00
|
|
|
|
|
|
|
if (*str == '.')
|
2012-09-06 19:39:25 -07:00
|
|
|
for (str++, length--;
|
|
|
|
length > 0 && *str != 'p';
|
|
|
|
++str, --length, ++nbr_decimals)
|
|
|
|
{
|
|
|
|
mantisse <<= 4;
|
|
|
|
mantisse += HEXA_TO_INT(*str);
|
|
|
|
}
|
2010-07-27 19:37:05 -07:00
|
|
|
|
|
|
|
if (sign < 0)
|
2012-09-06 19:39:25 -07:00
|
|
|
mantisse = -mantisse;
|
2008-08-21 07:20:41 -07:00
|
|
|
|
|
|
|
/* Compute the exponent. */
|
|
|
|
if (*str != 'p')
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
DBG("'p' not found in '%s'", src);
|
2008-08-21 07:20:41 -07:00
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
2010-07-27 19:37:05 -07:00
|
|
|
|
2008-08-21 07:20:41 -07:00
|
|
|
sign = +1;
|
|
|
|
|
|
|
|
str++;
|
2010-07-27 19:37:05 -07:00
|
|
|
length--; if (length <= 0)
|
|
|
|
goto on_length_error;
|
2008-08-21 07:20:41 -07:00
|
|
|
|
|
|
|
if (strchr("-+", *str))
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
sign = (*str == '-') ? -1 : +1;
|
2008-08-21 07:20:41 -07:00
|
|
|
|
|
|
|
str++; length--;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (exponent = 0; length > 0 && *str != '\0'; ++str, --length)
|
|
|
|
{
|
|
|
|
exponent *= 10;
|
|
|
|
exponent += *str - '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sign < 0)
|
2012-09-06 19:39:25 -07:00
|
|
|
exponent = -exponent;
|
2008-08-21 07:20:41 -07:00
|
|
|
|
|
|
|
*m = mantisse;
|
|
|
|
*e = exponent - (nbr_decimals << 2);
|
|
|
|
|
|
|
|
return EINA_TRUE;
|
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
on_length_error:
|
2008-08-21 07:20:41 -07:00
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI int
|
|
|
|
eina_convert_dtoa(double d, char *des)
|
|
|
|
{
|
|
|
|
int length = 0;
|
|
|
|
int p;
|
|
|
|
int i;
|
|
|
|
|
2015-12-28 22:45:35 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(des, 0);
|
|
|
|
EINA_SAFETY_ON_FALSE_RETURN_VAL(!isnan(d) && !isinf(d), 0);
|
2008-12-26 10:31:14 -08:00
|
|
|
|
2008-08-21 07:20:41 -07:00
|
|
|
if (d < 0.0)
|
|
|
|
{
|
|
|
|
*(des++) = '-';
|
|
|
|
d = -d;
|
2010-07-27 19:37:05 -07:00
|
|
|
length++;
|
2008-08-21 07:20:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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')
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
des--;
|
|
|
|
length--;
|
2008-08-21 07:20:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*(des - 1) == '.')
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
des--;
|
|
|
|
length--;
|
2008-08-21 07:20:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
*(des++) = 'p';
|
|
|
|
if (p < 0)
|
|
|
|
{
|
|
|
|
*(des++) = '-';
|
|
|
|
p = -p;
|
|
|
|
}
|
|
|
|
else
|
2012-09-06 19:39:25 -07:00
|
|
|
*(des++) = '+';
|
2010-07-27 19:37:05 -07:00
|
|
|
|
2008-08-21 07:20:41 -07:00
|
|
|
length += 2;
|
|
|
|
|
|
|
|
return length + eina_convert_itoa(p, des);
|
|
|
|
}
|
2008-09-17 11:55:54 -07:00
|
|
|
|
2009-09-23 09:47:08 -07:00
|
|
|
EAPI int
|
|
|
|
eina_convert_fptoa(Eina_F32p32 fp, char *des)
|
|
|
|
{
|
|
|
|
int length = 0;
|
2009-09-30 10:29:30 -07:00
|
|
|
int p = 0;
|
2009-09-23 09:47:08 -07:00
|
|
|
int i;
|
|
|
|
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(des, EINA_FALSE);
|
|
|
|
|
|
|
|
if (fp == 0)
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
memcpy(des, "0x0p+0", 7);
|
|
|
|
return 7;
|
2009-09-23 09:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fp < 0)
|
|
|
|
{
|
|
|
|
*(des++) = '-';
|
|
|
|
fp = -fp;
|
2010-07-27 19:37:05 -07:00
|
|
|
length++;
|
2009-09-23 09:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* fp >= 1 */
|
|
|
|
if (fp >= 0x0000000100000000LL)
|
2012-09-06 19:39:25 -07:00
|
|
|
while (fp >= 0x0000000100000000LL)
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
/* fp /= 2 */
|
|
|
|
fp >>= 1;
|
|
|
|
} /* fp < 0.5 */
|
2009-09-23 09:47:08 -07:00
|
|
|
else if (fp < 0x80000000)
|
2012-09-06 19:39:25 -07:00
|
|
|
while (fp < 0x80000000)
|
|
|
|
{
|
|
|
|
p--;
|
|
|
|
/* fp *= 2 */
|
|
|
|
fp <<= 1;
|
|
|
|
}
|
2009-09-23 09:47:08 -07:00
|
|
|
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
p--;
|
2010-07-27 19:37:05 -07:00
|
|
|
/* fp *= 2 */
|
2009-09-23 09:47:08 -07:00
|
|
|
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')
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
des--;
|
|
|
|
length--;
|
2009-09-23 09:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*(des - 1) == '.')
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
des--;
|
|
|
|
length--;
|
2009-09-23 09:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
*(des++) = 'p';
|
|
|
|
if (p < 0)
|
|
|
|
{
|
|
|
|
*(des++) = '-';
|
|
|
|
p = -p;
|
|
|
|
}
|
|
|
|
else
|
2012-09-06 19:39:25 -07:00
|
|
|
*(des++) = '+';
|
2010-07-27 19:37:05 -07:00
|
|
|
|
2009-09-23 09:47:08 -07:00
|
|
|
length += 2;
|
|
|
|
|
|
|
|
return length + eina_convert_itoa(p, des);
|
|
|
|
}
|
|
|
|
|
2009-09-25 03:03:19 -07:00
|
|
|
EAPI Eina_Bool
|
|
|
|
eina_convert_atofp(const char *src, int length, Eina_F32p32 *fp)
|
|
|
|
{
|
|
|
|
long long m;
|
|
|
|
long e;
|
|
|
|
|
2009-09-25 05:02:14 -07:00
|
|
|
if (!eina_convert_atod(src, length, &m, &e))
|
2012-09-06 19:39:25 -07:00
|
|
|
return EINA_FALSE;
|
2009-09-25 03:03:19 -07:00
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
if (!fp)
|
2012-09-06 19:39:25 -07:00
|
|
|
return EINA_FALSE;
|
2009-09-25 05:02:14 -07:00
|
|
|
|
2009-09-25 04:23:37 -07:00
|
|
|
e += 32;
|
2009-09-25 03:03:19 -07:00
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
if (e > 0)
|
2012-09-06 19:39:25 -07:00
|
|
|
*fp = m << e;
|
2010-07-27 19:37:05 -07:00
|
|
|
else
|
2012-09-06 19:39:25 -07:00
|
|
|
*fp = m >> -e;
|
2009-09-25 03:03:19 -07:00
|
|
|
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-09-17 11:55:54 -07:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|