vfscanf.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <mes/lib.h>
  21. #include <ctype.h>
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. int
  25. vfscanf (FILE * stream, char const *template, va_list ap)
  26. {
  27. char p = fgetc (stream);
  28. char const *t = template;
  29. int count = 0;
  30. while (*t && p != EOF)
  31. if (*t != '%')
  32. {
  33. t++;
  34. p = fgetc (stream);
  35. }
  36. else
  37. {
  38. t++;
  39. char c = *t;
  40. if (c == 'l')
  41. c = *++t;
  42. switch (c)
  43. {
  44. case '%':
  45. {
  46. p = fgetc (stream);
  47. break;
  48. }
  49. case 'c':
  50. {
  51. char *c = va_arg (ap, char *);
  52. *c = p;
  53. p = fgetc (stream);
  54. count++;
  55. break;
  56. }
  57. case 'd':
  58. case 'i':
  59. case 'u':
  60. {
  61. int *d = va_arg (ap, int *);
  62. char buf[20];
  63. char *q = buf;
  64. if (p == '+' || p == '-')
  65. {
  66. *q++ = p;
  67. p = fgetc (stream);
  68. }
  69. while (isdigit (p))
  70. {
  71. *q++ = p;
  72. p = fgetc (stream);
  73. }
  74. ungetc (p, stream);
  75. *q = 0;
  76. q = buf;
  77. *d = abtol (&q, 10);
  78. count++;
  79. break;
  80. }
  81. case 'e':
  82. case 'f':
  83. case 'g':
  84. case 'E':
  85. case 'G':
  86. {
  87. float *f = va_arg (ap, float *);
  88. char buf[20];
  89. char *q = buf;
  90. if (p == '+' || p == '-')
  91. {
  92. *q++ = p;
  93. p = fgetc (stream);
  94. }
  95. while (isdigit (p))
  96. {
  97. *q++ = p;
  98. p = fgetc (stream);
  99. }
  100. ungetc (p, stream);
  101. *q = 0;
  102. q = buf;
  103. *f = strtod (q, &q);
  104. count++;
  105. break;
  106. }
  107. default:
  108. {
  109. eputs ("vsscanf: not supported: %:");
  110. eputc (c);
  111. eputs ("\n");
  112. t++;
  113. p = fgetc (stream);
  114. }
  115. }
  116. t++;
  117. }
  118. va_end (ap);
  119. return count;
  120. }