wcstod.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "shgetc.h"
  2. #include "floatscan.h"
  3. #include "stdio_impl.h"
  4. #include <wctype.h>
  5. /* This read function heavily cheats. It knows:
  6. * (1) len will always be 1
  7. * (2) non-ascii characters don't matter */
  8. static size_t do_read(FILE *f, unsigned char *buf, size_t len)
  9. {
  10. size_t i;
  11. const wchar_t *wcs = f->cookie;
  12. if (!wcs[0]) wcs=L"@";
  13. for (i=0; i<f->buf_size && wcs[i]; i++)
  14. f->buf[i] = wcs[i] < 128 ? wcs[i] : '@';
  15. f->rpos = f->buf;
  16. f->rend = f->buf + i;
  17. f->cookie = (void *)(wcs+i);
  18. if (i && len) {
  19. *buf = *f->rpos++;
  20. return 1;
  21. }
  22. return 0;
  23. }
  24. static long double wcstox(const wchar_t *s, wchar_t **p, int prec)
  25. {
  26. wchar_t *t = (wchar_t *)s;
  27. unsigned char buf[64];
  28. FILE f = {0};
  29. f.flags = 0;
  30. f.rpos = f.rend = 0;
  31. f.buf = buf + 4;
  32. f.buf_size = sizeof buf - 4;
  33. f.lock = -1;
  34. f.read = do_read;
  35. while (iswspace(*t)) t++;
  36. f.cookie = (void *)t;
  37. shlim(&f, 0);
  38. long double y = __floatscan(&f, prec, 1);
  39. if (p) {
  40. size_t cnt = shcnt(&f);
  41. *p = cnt ? t + cnt : (wchar_t *)s;
  42. }
  43. return y;
  44. }
  45. float wcstof(const wchar_t *restrict s, wchar_t **restrict p)
  46. {
  47. return wcstox(s, p, 0);
  48. }
  49. double wcstod(const wchar_t *restrict s, wchar_t **restrict p)
  50. {
  51. return wcstox(s, p, 1);
  52. }
  53. long double wcstold(const wchar_t *restrict s, wchar_t **restrict p)
  54. {
  55. return wcstox(s, p, 2);
  56. }