| 1 |
#include <src/ll_strtod.h> |
| 2 |
|
| 3 |
#include <locale.h> |
| 4 |
#include <ctype.h> |
| 5 |
#include <string.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include <errno.h> |
| 9 |
|
| 10 |
|
| 11 |
double LL_strtod_parts_to_float(char *sign, char *beforept, |
| 12 |
char *afterpt, char *exponent) |
| 13 |
{ |
| 14 |
char *fail_pos; |
| 15 |
struct lconv *locale_data; |
| 16 |
const char *decimal_point; |
| 17 |
int decimal_point_len; |
| 18 |
double x; |
| 19 |
char *last; |
| 20 |
char *expo = exponent; |
| 21 |
int buf_size; |
| 22 |
char *s; |
| 23 |
|
| 24 |
if (*expo == '\0') { |
| 25 |
expo = "0"; |
| 26 |
} |
| 27 |
|
| 28 |
locale_data = localeconv(); |
| 29 |
decimal_point = locale_data->decimal_point; |
| 30 |
decimal_point_len = strlen(decimal_point); |
| 31 |
|
| 32 |
buf_size = strlen(sign) + |
| 33 |
strlen(beforept) + |
| 34 |
decimal_point_len + |
| 35 |
strlen(afterpt) + |
| 36 |
1 + |
| 37 |
strlen(expo) + |
| 38 |
1 ; |
| 39 |
|
| 40 |
s = (char*)malloc(buf_size); |
| 41 |
|
| 42 |
strcpy(s, sign); |
| 43 |
strcat(s, beforept); |
| 44 |
strcat(s, decimal_point); |
| 45 |
strcat(s, afterpt); |
| 46 |
strcat(s, "e"); |
| 47 |
strcat(s, expo); |
| 48 |
|
| 49 |
last = s + (buf_size-1); |
| 50 |
x = strtod(s, &fail_pos); |
| 51 |
errno = 0; |
| 52 |
if (fail_pos > last) |
| 53 |
fail_pos = last; |
| 54 |
if (fail_pos == s || *fail_pos != '\0' || fail_pos != last) { |
| 55 |
free(s); |
| 56 |
errno = 42; |
| 57 |
return -1.0; |
| 58 |
} |
| 59 |
if (x == 0.0) { |
| 60 |
x = strtod(s, NULL); |
| 61 |
errno = 0; |
| 62 |
} |
| 63 |
free(s); |
| 64 |
return x; |
| 65 |
} |
| 66 |
|
| 67 |
static char buffer[120]; |
| 68 |
static const int buflen = 120; |
| 69 |
|
| 70 |
#ifdef _MSC_VER |
| 71 |
#define snprintf _snprintf |
| 72 |
#endif |
| 73 |
|
| 74 |
char* LL_strtod_formatd(double x, char code, int precision) |
| 75 |
{ |
| 76 |
int res; |
| 77 |
const char* fmt; |
| 78 |
if (code == 'e') fmt = "%.*e"; |
| 79 |
else if (code == 'f') fmt = "%.*f"; |
| 80 |
else if (code == 'g') fmt = "%.*g"; |
| 81 |
else { |
| 82 |
strcpy(buffer, "??.?"); |
| 83 |
return buffer; |
| 84 |
} |
| 85 |
res = snprintf(buffer, buflen, fmt, precision, x); |
| 86 |
if (res <= 0 || res >= buflen) { |
| 87 |
strcpy(buffer, "??.?"); |
| 88 |
} else { |
| 89 |
struct lconv *locale_data; |
| 90 |
const char *decimal_point; |
| 91 |
int decimal_point_len; |
| 92 |
char *p; |
| 93 |
|
| 94 |
locale_data = localeconv(); |
| 95 |
decimal_point = locale_data->decimal_point; |
| 96 |
decimal_point_len = strlen(decimal_point); |
| 97 |
|
| 98 |
if (decimal_point[0] != '.' || |
| 99 |
decimal_point[1] != 0) |
| 100 |
{ |
| 101 |
p = buffer; |
| 102 |
|
| 103 |
if (*p == '+' || *p == '-') |
| 104 |
p++; |
| 105 |
|
| 106 |
while (isdigit((unsigned char)*p)) |
| 107 |
p++; |
| 108 |
|
| 109 |
if (strncmp(p, decimal_point, decimal_point_len) == 0) |
| 110 |
{ |
| 111 |
*p = '.'; |
| 112 |
p++; |
| 113 |
if (decimal_point_len > 1) { |
| 114 |
int rest_len; |
| 115 |
rest_len = strlen(p + (decimal_point_len - 1)); |
| 116 |
memmove(p, p + (decimal_point_len - 1), |
| 117 |
rest_len); |
| 118 |
p[rest_len] = 0; |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
return buffer; |
| 126 |
} |