mescc: Mes C Library: Support GNU Awk: Add abtod, implementing sttrod.

* include/libmes.h (abtod): Declare.
* lib/mes/abtod.c: New file.
* lib/mes/abtol.c: Update.
* lib/stdlib/strtod.c: Use it to implement; move from stub/strtod.
* lib/tests/mes/90-abtod.c: Test it.
* lib/tests/mes/90-abtod.stdout: Baseline.
* build-aux/check-mescc.sh (tests): Run it.
This commit is contained in:
Jan Nieuwenhuizen 2019-01-03 11:51:44 +01:00
parent c093998151
commit c8a3d50705
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
9 changed files with 115 additions and 13 deletions

View File

@ -192,6 +192,7 @@ scaffold/tests/84-struct-field-list
scaffold/tests/85-sizeof
lib/tests/dirent/90-readdir
lib/tests/io/90-stat
lib/tests/mes/90-abtod
lib/tests/posix/90-execlp
lib/tests/posix/90-unsetenv
lib/tests/signal/90-signal

View File

@ -24,6 +24,7 @@
#include <libmes-mini.h>
int __mes_debug ();
double abtod (char const** p, int base);
long abtol (char const** p, int base);
char const* ntoab (long number, int base, int signed_p);
char const* itoa (int number);

View File

@ -83,7 +83,6 @@
#include <math/fabs.c>
// gcc
#include <stdio/freopen.c>
#include <stub/times.c>
#include <posix/sleep.c>
@ -122,6 +121,7 @@
// gawk
#include <ctype/isgraph.c>
// #include <mes/abtod.c> in libc+tcc for strtod,strtof (could be stubbed)
// tar
#include <posix/execlp.c>

View File

@ -1,6 +1,6 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
* Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of GNU Mes.
*
@ -61,6 +61,7 @@ int errno;
#include <ctype/isupper.c>
#include <ctype/tolower.c>
#include <ctype/toupper.c>
#include <mes/abtod.c> // implementation instead of stub for GNU gawk
#include <mes/search-path.c>
#include <posix/execvp.c>
#include <stdio/fclose.c>
@ -85,6 +86,7 @@ int errno;
#include <stdio/vsscanf.c>
#include <stdlib/calloc.c>
#include <stdlib/qsort.c>
#include <stdlib/strtod.c>
#include <stdlib/strtof.c>
#include <stdlib/strtol.c>
#include <stdlib/strtold.c>
@ -105,5 +107,4 @@ int errno;
#include <stub/ldexp.c>
#include <stub/mprotect.c>
#include <stub/localtime.c>
#include <stub/strtod.c>
#include <stub/sigemptyset.c>

53
lib/mes/abtod.c Normal file
View File

@ -0,0 +1,53 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of GNU Mes.
*
* GNU Mes is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* GNU Mes 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
double
abtod (char const **p, int base)
{
char const *s = *p;
double d = 0;
int sign_p = 0;
if (!base)
base = 10;
double dbase = base;
long i = abtol (&s, base);
long f = 0;
long e = 0;
if (*s == '.')
{
s++;
f = abtol (&s, base);
}
if (*s == 'e')
{
s++;
e = abtol (&s, base);
}
d = i + f / dbase;
if (e < 0)
while (e++)
d = d / dbase;
while (e--)
d = d * dbase;
*p = s;
return sign_p ? -d : d;
}

View File

@ -25,12 +25,16 @@ abtol (char const **p, int base)
{
char const *s = *p;
int i = 0;
int sign = 1;
int sign_p = 0;
if (!base)
base = 10;
while (isspace (*s))
s++;
if (*s && *s == '+')
s++;
if (*s && *s == '-')
{
sign = -1;
sign_p = 1;
s++;
}
while (isnumber (*s, base))
@ -41,5 +45,5 @@ abtol (char const **p, int base)
s++;
}
*p = s;
return i * sign;
return sign_p ? -i : i;
}

40
lib/stdlib/strtod.c Normal file
View File

@ -0,0 +1,40 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of GNU Mes.
*
* GNU Mes is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* GNU Mes 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
#include <stdlib.h>
double
strtod (char const *string, char **tailptr)
{
int base = 10;
if (!strncmp (string, "0x", 2))
{
string += 2;
base = 16;
}
if (tailptr)
{
*tailptr = string;
return abtod (tailptr, base);
}
char **p = &string;
return abtod (p, base);
}

View File

@ -1,6 +1,6 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
* Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of GNU Mes.
*
@ -21,12 +21,13 @@
#include <libmes.h>
#include <stdlib.h>
double
strtod (char const *string, char **tailptr)
int
main ()
{
static int stub = 0;
if (__mes_debug () && !stub)
eputs ("strtod stub\n");
stub = 1;
char *s = "1.2e3";
char *p = s;
double d = abtod (&p, 0);
printf ("%f\n", d);
return 0;
}

View File

@ -0,0 +1 @@
1200.000000