X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=functions%2Fstdlib%2Fstrtox.c;h=db739529f7e33b90068712b467b1465de12a4fab;hp=cca971138d5fb6ed339b569f843157e5e70632f3;hb=1d9d92ba957a0b8307c9a65c35867fde68e6533b;hpb=34893ecc2200dc7017c36a54cb6c5f4c2378b5ec diff --git a/functions/stdlib/strtox.c b/functions/stdlib/strtox.c index cca9711..db73952 100644 --- a/functions/stdlib/strtox.c +++ b/functions/stdlib/strtox.c @@ -1,9 +1,9 @@ -// ---------------------------------------------------------------------------- -// $Id$ -// ---------------------------------------------------------------------------- -// Public Domain C Library - http://pdclib.sourceforge.net -// This code is Public Domain. Use, modify, and redistribute at will. -// ---------------------------------------------------------------------------- +/* ---------------------------------------------------------------------------- + * $Id$ + * ---------------------------------------------------------------------------- + * Public Domain C Library - http://pdclib.sourceforge.net + * This code is Public Domain. Use, modify, and redistribute at will. + * --------------------------------------------------------------------------*/ double atof( const char * s ) { /* TODO */ }; int atoi( const char * s ) { /* TODO */ }; @@ -19,3 +19,131 @@ unsigned long long strtoull( const char * restrict s, char * * restrict endptr, long strtol( const char * restrict s, char * * restrict endptr, int base ) { /* TODO */ }; unsigned long strtoul( const char * restrict s, char * * restrict endptr, int base) { /* TODO */ }; + +/* PDPC code - unreviewed, verbatim +double atof(const char *nptr) +{ + return (strtod(nptr, (char **)NULL)); +} + +double strtod(const char *nptr, char **endptr) +{ + double x = 0.0; + + while (1) + { + if (isdigit(*nptr)) + { + x = x * 10 + (*nptr - '0'); + } + else + { + if (endptr != NULL) + { + *endptr = (char *)nptr; + } + break; + } + nptr++; + } + return (x); +} + +int atoi(const char *nptr) +{ + return ((int)strtol(nptr, (char **)NULL, 10)); +} + +long int atol(const char *nptr) +{ + return (strtol(nptr, (char **)NULL, 10)); +} + +long int strtol(const char *nptr, char **endptr, int base) +{ + long x = 0; + int undecided = 0; + + if (base == 0) + { + undecided = 1; + } + while (1) + { + if (isdigit(*nptr)) + { + if (base == 0) + { + if (*nptr == '0') + { + base = 8; + } + else + { + base = 10; + undecided = 0; + } + } + x = x * base + (*nptr - '0'); + nptr++; + } + else if (isalpha(*nptr)) + { + if ((*nptr == 'X') || (*nptr == 'x')) + { + if ((base == 0) || ((base == 8) && undecided)) + { + base = 16; + undecided = 0; + } + else + { + break; + } + } + else + { + x = x * base + (toupper((unsigned char)*nptr) - 'A') + 10; + nptr++; + } + } + else + { + break; + } + } + if (endptr != NULL) + { + *endptr = (char *)nptr; + } + return (x); +} + +unsigned long int strtoul(const char *nptr, char **endptr, int base) +{ + unsigned long x = 0; + + while (1) + { + if (isdigit(*nptr)) + { + x = x * base + (*nptr - '0'); + nptr++; + } + else if (isalpha(*nptr) && (base > 10)) + { + x = x * base + (toupper((unsigned char)*nptr) - 'A') + 10; + nptr++; + } + else + { + break; + } + } + if (endptr != NULL) + { + *endptr = (char *)nptr; + } + return (x); +} +*/