scanf.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2000-2002 Opsycon AB (www.opsycon.se)
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. All advertising materials mentioning features or use of this software
  13. * must display the following acknowledgement:
  14. * This product includes software developed by Opsycon AB.
  15. * 4. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  19. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. */
  31. #include <stdio.h>
  32. #include <stdarg.h>
  33. #include <string.h>
  34. #include <ctype.h>
  35. #define MAXLN 512
  36. static const char* determine_base(const char* p, int& base)
  37. {
  38. if (p[0] == '0') {
  39. switch (p[1]) {
  40. case 'x':
  41. base = 16;
  42. break;
  43. case 't':
  44. case 'n':
  45. base = 10;
  46. break;
  47. case 'o':
  48. base = 8;
  49. break;
  50. default:
  51. base = 10;
  52. return p;
  53. }
  54. return p + 2;
  55. }
  56. base = 10;
  57. return p;
  58. }
  59. static int _atob(unsigned long* vp, const char* p, int base)
  60. {
  61. unsigned long value, v1, v2;
  62. char *q, tmp[20];
  63. int digit;
  64. if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
  65. base = 16;
  66. p += 2;
  67. }
  68. if (base == 16 && (q = strchr(p, '.')) != 0) {
  69. if (q - p > (int)sizeof(tmp) - 1)
  70. return 0;
  71. strncpy(tmp, p, q - p);
  72. tmp[q - p] = '\0';
  73. if (!_atob(&v1, tmp, 16))
  74. return 0;
  75. ++q;
  76. if (strchr(q, '.'))
  77. return 0;
  78. if (!_atob(&v2, q, 16))
  79. return 0;
  80. *vp = (v1 << 16) + v2;
  81. return 1;
  82. }
  83. value = *vp = 0;
  84. for (; *p; p++) {
  85. if (*p >= '0' && *p <= '9')
  86. digit = *p - '0';
  87. else if (*p >= 'a' && *p <= 'f')
  88. digit = *p - 'a' + 10;
  89. else if (*p >= 'A' && *p <= 'F')
  90. digit = *p - 'A' + 10;
  91. else
  92. return 0;
  93. if (digit >= base)
  94. return 0;
  95. value *= base;
  96. value += digit;
  97. }
  98. *vp = value;
  99. return 1;
  100. }
  101. int atob(unsigned int* vp, const char* p, int base)
  102. {
  103. unsigned long v;
  104. if (base == 0)
  105. p = determine_base(p, base);
  106. if (_atob(&v, p, base)) {
  107. *vp = v;
  108. return 1;
  109. }
  110. return 0;
  111. }
  112. static int vfscanf(FILE*, const char*, va_list);
  113. static int vsscanf(const char*, const char*, va_list);
  114. #define ISSPACE " \t\n\r\f\v"
  115. int scanf(const char* fmt, ...)
  116. {
  117. int count;
  118. va_list ap;
  119. va_start(ap, fmt);
  120. count = vfscanf(stdin, fmt, ap);
  121. va_end(ap);
  122. return count;
  123. }
  124. int fscanf(FILE *fp, const char* fmt, ...)
  125. {
  126. va_list ap;
  127. va_start(ap, fmt);
  128. int count = vfscanf(fp, fmt, ap);
  129. va_end(ap);
  130. return count;
  131. }
  132. int sscanf(const char *buf, const char* fmt, ...)
  133. {
  134. va_list ap;
  135. va_start(ap, fmt);
  136. int count = vsscanf(buf, fmt, ap);
  137. va_end(ap);
  138. return count;
  139. }
  140. static int vfscanf(FILE *fp, const char* fmt, va_list ap)
  141. {
  142. char buf[MAXLN + 1];
  143. if (!fgets(buf, MAXLN, fp))
  144. return -1;
  145. int count = vsscanf(buf, fmt, ap);
  146. return count;
  147. }
  148. static int vsscanf(const char *buf, const char *s, va_list ap)
  149. {
  150. int base = 10;
  151. char *t;
  152. char tmp[MAXLN];
  153. bool noassign = false;
  154. int count = 0;
  155. int width = 0;
  156. while (*s && *buf) {
  157. while (isspace (*s))
  158. s++;
  159. if (*s == '%') {
  160. s++;
  161. for (; *s; s++) {
  162. if (strchr ("dibouxcsefg%", *s))
  163. break;
  164. if (*s == '*')
  165. noassign = true;
  166. else if (*s >= '1' && *s <= '9') {
  167. const char* tc;
  168. for (tc = s; isdigit(*s); s++);
  169. strncpy (tmp, tc, s - tc);
  170. tmp[s - tc] = '\0';
  171. atob ((uint32_t*)&width, tmp, 10);
  172. s--;
  173. }
  174. }
  175. if (*s == 's') {
  176. while (isspace(*buf))
  177. buf++;
  178. if (!width)
  179. width = strcspn(buf, ISSPACE);
  180. if (!noassign) {
  181. strncpy(t = va_arg(ap, char*), buf, width);
  182. t[width] = '\0';
  183. }
  184. buf += width;
  185. } else if (*s == 'c') {
  186. if (!width)
  187. width = 1;
  188. if (!noassign) {
  189. strncpy(t = va_arg(ap, char*), buf, width);
  190. t[width] = '\0';
  191. }
  192. buf += width;
  193. } else if (strchr("dobxu", *s)) {
  194. while (isspace(*buf))
  195. buf++;
  196. if (*s == 'd' || *s == 'u')
  197. base = 10;
  198. else if (*s == 'x')
  199. base = 16;
  200. else if (*s == 'o')
  201. base = 8;
  202. else if (*s == 'b')
  203. base = 2;
  204. if (!width) {
  205. if (isspace(*(s + 1)) || *(s + 1) == 0)
  206. width = strcspn(buf, ISSPACE);
  207. else
  208. width = strchr(buf, *(s + 1)) - buf;
  209. }
  210. strncpy(tmp, buf, width);
  211. tmp[width] = '\0';
  212. buf += width;
  213. if (!noassign)
  214. atob(va_arg(ap, uint32_t*), tmp, base);
  215. }
  216. if (!noassign)
  217. ++count;
  218. width = 0;
  219. noassign = false;
  220. ++s;
  221. } else {
  222. while (isspace(*buf))
  223. buf++;
  224. if (*s != *buf)
  225. break;
  226. else {
  227. ++s;
  228. ++buf;
  229. }
  230. }
  231. }
  232. return count;
  233. }