Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

57 lines
1.0 KiB

  1. #include "scan.h"
  2. #ifdef __GNUC__
  3. static inline int isdigit(int c) { return (c>='0' && c<='9'); }
  4. #else
  5. #include <ctype.h>
  6. #endif
  7. size_t scan_double(const char *in, double *dest) {
  8. double d=0;
  9. register const char *c=in;
  10. char neg=0;
  11. switch (*c) {
  12. case '-': neg=1; /* fall through */
  13. case '+': c++; break;
  14. default: break;
  15. }
  16. while (isdigit(*c)) {
  17. d=d*10+(*c-'0');
  18. ++c;
  19. }
  20. if (*c=='.') {
  21. double factor=.1;
  22. while (isdigit(*++c)) {
  23. d=d+(factor*(*c-'0'));
  24. factor/=10;
  25. }
  26. }
  27. if ((*c|32)=='e') {
  28. int exp=0;
  29. char neg=0;
  30. if (c[1]<'0') {
  31. switch (*c) {
  32. case '-': neg=1; /* fall through */
  33. case '+': c++; break;
  34. default:
  35. d=0;
  36. c=in;
  37. goto done;
  38. }
  39. }
  40. while (isdigit(*++c))
  41. exp=exp*10+(*c-'0');
  42. if (neg)
  43. while (exp) { /* XXX: this introduces rounding errors */
  44. d/=10; --exp;
  45. }
  46. else
  47. while (exp) { /* XXX: this introduces rounding errors */
  48. d*=10; --exp;
  49. }
  50. }
  51. done:
  52. *dest=(neg?-d:d);
  53. return (size_t)(c-in);
  54. }