scanf.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <ctype.h>
  32. #include <stdarg.h>
  33. #include <stdint.h>
  34. #include <stdio.h>
  35. #include <string.h>
  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. const char* q;
  63. char tmp[20];
  64. int digit;
  65. if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
  66. base = 16;
  67. p += 2;
  68. }
  69. if (base == 16 && (q = strchr(p, '.')) != 0) {
  70. if (q - p > (int)sizeof(tmp) - 1)
  71. return 0;
  72. strncpy(tmp, p, q - p);
  73. tmp[q - p] = '\0';
  74. if (!_atob(&v1, tmp, 16))
  75. return 0;
  76. ++q;
  77. if (strchr(q, '.'))
  78. return 0;
  79. if (!_atob(&v2, q, 16))
  80. return 0;
  81. *vp = (v1 << 16) + v2;
  82. return 1;
  83. }
  84. value = *vp = 0;
  85. for (; *p; p++) {
  86. if (*p >= '0' && *p <= '9')
  87. digit = *p - '0';
  88. else if (*p >= 'a' && *p <= 'f')
  89. digit = *p - 'a' + 10;
  90. else if (*p >= 'A' && *p <= 'F')
  91. digit = *p - 'A' + 10;
  92. else
  93. return 0;
  94. if (digit >= base)
  95. return 0;
  96. value *= base;
  97. value += digit;
  98. }
  99. *vp = value;
  100. return 1;
  101. }
  102. static int atob(unsigned int* vp, const char* p, int base)
  103. {
  104. unsigned long v;
  105. if (base == 0)
  106. p = determine_base(p, base);
  107. if (_atob(&v, p, base)) {
  108. *vp = v;
  109. return 1;
  110. }
  111. return 0;
  112. }
  113. #define ISSPACE " \t\n\r\f\v"
  114. int vsscanf(const char* buf, const char* s, va_list ap)
  115. {
  116. int base = 10;
  117. char* t;
  118. char tmp[BUFSIZ];
  119. bool noassign = false;
  120. int count = 0;
  121. int width = 0;
  122. while (*s && *buf) {
  123. while (isspace(*s))
  124. s++;
  125. if (*s == '%') {
  126. s++;
  127. for (; *s; s++) {
  128. if (strchr("dibouxcsefg%", *s))
  129. break;
  130. if (*s == '*')
  131. noassign = true;
  132. else if (*s >= '1' && *s <= '9') {
  133. const char* tc;
  134. for (tc = s; isdigit(*s); s++)
  135. ;
  136. strncpy(tmp, tc, s - tc);
  137. tmp[s - tc] = '\0';
  138. atob((uint32_t*)&width, tmp, 10);
  139. s--;
  140. }
  141. }
  142. if (*s == 's') {
  143. while (isspace(*buf))
  144. buf++;
  145. if (!width)
  146. width = strcspn(buf, ISSPACE);
  147. if (!noassign) {
  148. strncpy(t = va_arg(ap, char*), buf, width);
  149. t[width] = '\0';
  150. }
  151. buf += width;
  152. } else if (*s == 'c') {
  153. if (!width)
  154. width = 1;
  155. if (!noassign) {
  156. strncpy(t = va_arg(ap, char*), buf, width);
  157. t[width] = '\0';
  158. }
  159. buf += width;
  160. } else if (strchr("dobxu", *s)) {
  161. while (isspace(*buf))
  162. buf++;
  163. if (*s == 'd' || *s == 'u')
  164. base = 10;
  165. else if (*s == 'x')
  166. base = 16;
  167. else if (*s == 'o')
  168. base = 8;
  169. else if (*s == 'b')
  170. base = 2;
  171. if (!width) {
  172. if (isspace(*(s + 1)) || *(s + 1) == 0) {
  173. width = strcspn(buf, ISSPACE);
  174. } else {
  175. auto* p = strchr(buf, *(s + 1));
  176. if (p)
  177. width = p - buf;
  178. else {
  179. noassign = true;
  180. width = 0;
  181. }
  182. }
  183. }
  184. strncpy(tmp, buf, width);
  185. tmp[width] = '\0';
  186. buf += width;
  187. if (!noassign) {
  188. if (!atob(va_arg(ap, uint32_t*), tmp, base))
  189. noassign = true;
  190. }
  191. }
  192. if (!noassign)
  193. ++count;
  194. width = 0;
  195. noassign = false;
  196. ++s;
  197. } else {
  198. while (isspace(*buf))
  199. buf++;
  200. if (*s != *buf)
  201. break;
  202. else {
  203. ++s;
  204. ++buf;
  205. }
  206. }
  207. }
  208. return count;
  209. }