wcstod.c 1.3 KB

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