Similar to strtod(), but must be passed the current locale's decimal point * character. Guaranteed to be called at the start of any valid number in a string */
| 104 | /* Similar to strtod(), but must be passed the current locale's decimal point |
| 105 | * character. Guaranteed to be called at the start of any valid number in a string */ |
| 106 | double fpconv_strtod(const char *nptr, char **endptr) |
| 107 | { |
| 108 | char localbuf[FPCONV_G_FMT_BUFSIZE]; |
| 109 | char *buf, *endbuf, *dp; |
| 110 | int buflen; |
| 111 | double value; |
| 112 | |
| 113 | /* System strtod() is fine when decimal point is '.' */ |
| 114 | if (locale_decimal_point == '.') |
| 115 | return strtod(nptr, endptr); |
| 116 | |
| 117 | buflen = strtod_buffer_size(nptr); |
| 118 | if (!buflen) { |
| 119 | /* No valid characters found, standard strtod() return */ |
| 120 | *endptr = (char *)nptr; |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /* Duplicate number into buffer */ |
| 125 | if (buflen >= FPCONV_G_FMT_BUFSIZE) { |
| 126 | /* Handle unusually large numbers */ |
| 127 | buf = malloc(buflen + 1); |
| 128 | if (!buf) { |
| 129 | fprintf(stderr, "Out of memory"); |
| 130 | abort(); |
| 131 | } |
| 132 | } else { |
| 133 | /* This is the common case.. */ |
| 134 | buf = localbuf; |
| 135 | } |
| 136 | memcpy(buf, nptr, buflen); |
| 137 | buf[buflen] = 0; |
| 138 | |
| 139 | /* Update decimal point character if found */ |
| 140 | dp = strchr(buf, '.'); |
| 141 | if (dp) |
| 142 | *dp = locale_decimal_point; |
| 143 | |
| 144 | value = strtod(buf, &endbuf); |
| 145 | *endptr = (char *)&nptr[endbuf - buf]; |
| 146 | if (buflen >= FPCONV_G_FMT_BUFSIZE) |
| 147 | free(buf); |
| 148 | |
| 149 | return value; |
| 150 | } |
| 151 | |
| 152 | /* "fmt" must point to a buffer of at least 6 characters */ |
| 153 | static void set_number_format(char *fmt, int precision) |
no test coverage detected