diff --git a/c-programming/experiments/floatscan-experiment.c b/c-programming/experiments/floatscan-experiment.c index adb6f3b..3e65af7 100644 --- a/c-programming/experiments/floatscan-experiment.c +++ b/c-programming/experiments/floatscan-experiment.c @@ -18,7 +18,12 @@ #include /* wrapping code --- beginning */ -#define shgetc(f) fgetc(f) +const char* fgetc_ptr = NULL; + +int __shgetc(void); +long double floatscan(const char* s, int prec); + +#define shgetc(f) __shgetc() #define shlim(...) #define shunget(...) @@ -531,10 +536,28 @@ long double __floatscan(FILE *f, int prec, int pok) } /* the original musl libc code --- end */ +/* wrapping code --- beginning */ +int __shgetc(void) { + return *fgetc_ptr++; +} + +long double floatscan(const char* s, int prec) { + fgetc_ptr = s; + return __floatscan(NULL, prec, 1); +} +/* wrapping code --- end */ + /* test code --- beginning */ -int main(void) { - printf("LD_B1B_DIG = %d\n", LD_B1B_DIG); - printf("%La\n", __floatscan(stdin, LONG_DOUBLE_TYPE, 1)); +int main(int argc, char** argv) { + char* value = argc > 1 ? argv[1] : "+314.1e-2"; + const int prec = LD_B1B_DIG; + + printf("Selected precision index = %d\n", prec); + long double result = floatscan(value, LONG_DOUBLE_TYPE); + printf("Input : %s\n", value); + printf("Decimal floating-point format: %Lf\n", result); + printf("Double floating-point format : %Lg\n", result); + printf("Hexadecimal form : %La\n", result); return 0; } /* test code --- end */