getf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Copyright (c) 1990, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * This code is partly derived from software contributed to Berkeley by
  8. * Chris Torek, but heavily rewritten for Nut/OS.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the copyright holders nor the names of
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  30. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  31. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  33. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. * For additional information see http://www.ethernut.de/
  37. *
  38. */
  39. /*
  40. * $Log$
  41. * Revision 1.7 2008/08/11 06:59:40 haraldkipp
  42. * BSD types replaced by stdint types (feature request #1282721).
  43. *
  44. * Revision 1.6 2008/07/09 14:20:25 haraldkipp
  45. * Added support for length modifier h. According to C99, F, G and X type
  46. * specifiers should work like f, g, and x.
  47. *
  48. * Revision 1.5 2006/05/15 15:31:11 freckle
  49. * Take care of first character after integer
  50. *
  51. * Revision 1.4 2006/05/05 15:43:07 freckle
  52. * Fixes for bugs #1477658 and #1477676
  53. *
  54. * Revision 1.3 2004/11/24 15:24:07 haraldkipp
  55. * Floating point configuration works again.
  56. *
  57. * Revision 1.2 2004/02/28 20:14:38 drsung
  58. * Merge from nut-3_4-release b/c of bugfixes.
  59. *
  60. * Revision 1.1.1.1.2.1 2004/02/28 18:47:34 drsung
  61. * Several bugfixes provided by Francois Rademeyer.
  62. * - "%%" didnt work
  63. * - integer parsing corrected
  64. * - support for "%u"
  65. *
  66. * Revision 1.1.1.1 2003/05/09 14:40:29 haraldkipp
  67. * Initial using 3.2.1
  68. *
  69. * Revision 1.1 2003/02/04 17:49:07 harald
  70. * *** empty log message ***
  71. *
  72. */
  73. #include <cfg/crt.h>
  74. #include "nut_io.h"
  75. #include <ctype.h>
  76. #include <limits.h>
  77. #include <stdlib.h>
  78. #include <string.h>
  79. /*!
  80. * \addtogroup xgCrtStdio
  81. */
  82. /*@{*/
  83. #define CF_LONG 0x01 /* 1: long or double */
  84. #define CF_SUPPRESS 0x02 /* suppress assignment */
  85. #define CF_SIGNOK 0x04 /* +/- is (still) legal */
  86. #define CF_NDIGITS 0x08 /* no digits detected */
  87. #define CF_PFXOK 0x10 /* 0x prefix is (still) legal */
  88. #define CF_NZDIGITS 0x20 /* no zero digits detected */
  89. #define CF_DPTOK 0x10 /* (float) decimal point is still legal */
  90. #define CF_EXPOK 0x20 /* (float) exponent (e+3, etc) still legal */
  91. /*
  92. * Conversion types.
  93. */
  94. #define CT_CHAR 0 /* %c conversion */
  95. #define CT_STRING 2 /* %s conversion */
  96. #define CT_INT 3 /* integer, i.e., strtoq or strtouq */
  97. #define CT_FLOAT 4 /* floating, i.e., strtod */
  98. /*!
  99. * \brief Read formatted data using a given input function.
  100. *
  101. * \param _getb Input function for reading data.
  102. * \param fd Descriptor of a previously opened file, device or
  103. * connected socket.
  104. * \param fmt Format string containing coversion specifications.
  105. * \param ap List of pointer arguments.
  106. *
  107. * \return The number of fields successfully converted and assigned.
  108. * The return value is EOF, if an error occurs or if the end
  109. * of the stream is reached before the first conversion.
  110. */
  111. int _getf(int _getb(int, void *, size_t), int fd, const char *fmt, va_list ap)
  112. {
  113. uint8_t cf; /* Character from format. */
  114. uint8_t ch; /* Character from input. */
  115. size_t width; /* Field width. */
  116. uint8_t flags; /* CF_ flags. */
  117. uint8_t ct; /* CT_ conversion type. */
  118. uint8_t base; /* Conversion base. */
  119. uint8_t ccnt = 0; /* Number of conversions. */
  120. uint8_t acnt = 0; /* Number of fields assigned. */
  121. uint8_t hcnt; /* Number of 'half' specifiers. */
  122. char buf[16]; /* Temporary buffer. */
  123. char *cp; /* Temporary pointer. */
  124. uint8_t ch_ready = 0; /* Character available from previous peek
  125. This is necessary as a hack to get around a missing ungetc */
  126. for (;;) {
  127. cf = *fmt++;
  128. if (cf == 0)
  129. return acnt;
  130. /*
  131. * Match whitespace.
  132. */
  133. if (isspace(cf)) {
  134. for (;;) {
  135. if (_getb(fd, &ch, 1) != 1)
  136. break;
  137. if (!isspace(ch)) {
  138. ch_ready = 1; /* character avail without read */
  139. break;
  140. }
  141. }
  142. continue;
  143. }
  144. /*
  145. * Match literals.
  146. */
  147. if (cf != '%') {
  148. if (!ch_ready && _getb(fd, &ch, 1) != 1)
  149. return ccnt ? acnt : EOF;
  150. if (ch != cf)
  151. return acnt;
  152. ch_ready = 0; /* character used now */
  153. continue;
  154. }
  155. cf = *fmt++;
  156. /*
  157. * Check for a '%' literal.
  158. */
  159. if (cf == '%') {
  160. if (!ch_ready && _getb(fd, &ch, 1) != 1)
  161. return ccnt ? acnt : EOF;
  162. if (ch != cf)
  163. return acnt;
  164. ch_ready = 0; /* character used now */
  165. continue;
  166. }
  167. /*
  168. * Collect modifiers.
  169. */
  170. width = 0;
  171. flags = 0;
  172. hcnt = 0;
  173. for (;;) {
  174. if (cf == '*')
  175. flags |= CF_SUPPRESS;
  176. else if (cf == 'l')
  177. flags |= CF_LONG;
  178. else if (cf == 'h')
  179. hcnt++;
  180. else if (cf >= '0' && cf <= '9')
  181. width = width * 10 + cf - '0';
  182. else
  183. break;
  184. cf = *fmt++;
  185. }
  186. /*
  187. * Determine the types.
  188. */
  189. base = 10;
  190. ct = CT_INT;
  191. switch (cf) {
  192. case '\0':
  193. return EOF;
  194. case 's':
  195. ct = CT_STRING;
  196. break;
  197. case 'c':
  198. ct = CT_CHAR;
  199. break;
  200. case 'i':
  201. base = 0;
  202. break;
  203. case 'u':
  204. base = 10;
  205. break;
  206. case 'o':
  207. base = 8;
  208. break;
  209. case 'x':
  210. case 'X':
  211. flags |= CF_PFXOK;
  212. base = 16;
  213. break;
  214. #ifdef STDIO_FLOATING_POINT
  215. case 'e':
  216. case 'f':
  217. case 'F':
  218. case 'g':
  219. case 'G':
  220. ct = CT_FLOAT;
  221. break;
  222. #endif
  223. }
  224. /*
  225. * Process characters.
  226. */
  227. if (ct == CT_CHAR) {
  228. if (width == 0)
  229. width = 1;
  230. if (flags & CF_SUPPRESS) {
  231. while (width > sizeof(buf)) {
  232. if (!ch_ready && _getb(fd, buf, sizeof(buf)) <= 0)
  233. return ccnt ? acnt : EOF;
  234. width -= sizeof(buf);
  235. }
  236. if (width)
  237. if (!ch_ready && _getb(fd, &buf, width) <= 0)
  238. return ccnt ? acnt : EOF;
  239. } else {
  240. if (!ch_ready && _getb(fd, (void *) va_arg(ap, char *), width) <= 0)
  241. return ccnt ? acnt : EOF;
  242. acnt++;
  243. }
  244. ch_ready = 0; /* character used now */
  245. ccnt++;
  246. continue;
  247. }
  248. /*
  249. * Skip whitespaces.
  250. */
  251. if (!ch_ready && _getb(fd, &ch, 1) != 1)
  252. return ccnt ? acnt : EOF;
  253. ch_ready = 0; /* no character ready anymore */
  254. while (isspace(ch)) {
  255. if (_getb(fd, &ch, 1) != 1)
  256. return ccnt ? acnt : EOF;
  257. }
  258. /*
  259. * Process string.
  260. */
  261. if (ct == CT_STRING) {
  262. if (width == 0)
  263. width = 0xFFFF;
  264. if (flags & CF_SUPPRESS) {
  265. while (!isspace(ch)) {
  266. if (--width == 0)
  267. break;
  268. if (_getb(fd, &ch, 1) != 1)
  269. break;
  270. }
  271. } else {
  272. cp = va_arg(ap, char *);
  273. while (!isspace(ch)) {
  274. *cp++ = ch;
  275. if (--width == 0)
  276. break;
  277. if (_getb(fd, &ch, 1) != 1)
  278. break;
  279. }
  280. *cp = 0;
  281. acnt++;
  282. }
  283. ccnt++;
  284. }
  285. /*
  286. * Process integer.
  287. */
  288. else if (ct == CT_INT) {
  289. if (width == 0 || width > sizeof(buf) - 1)
  290. width = sizeof(buf) - 1;
  291. flags |= CF_SIGNOK | CF_NDIGITS | CF_NZDIGITS;
  292. for (cp = buf; width; width--) {
  293. if (ch == '0') {
  294. if (base == 0) {
  295. base = 8;
  296. flags |= CF_PFXOK;
  297. }
  298. if (flags & CF_NZDIGITS)
  299. flags &= ~(CF_SIGNOK | CF_NZDIGITS | CF_NDIGITS);
  300. else
  301. flags &= ~(CF_SIGNOK | CF_PFXOK | CF_NDIGITS);
  302. } else if (ch >= '1' && ch <= '7') {
  303. if (base == 0)
  304. base = 10;
  305. flags &= ~(CF_SIGNOK | CF_PFXOK | CF_NDIGITS);
  306. } else if (ch == '8' || ch == '9') {
  307. if (base == 0)
  308. base = 10;
  309. else if (base <= 8)
  310. break;
  311. flags &= ~(CF_SIGNOK | CF_PFXOK | CF_NDIGITS);
  312. } else if ((ch >= 'A' && ch <= 'F')
  313. || (ch >= 'a' && ch <= 'f')) {
  314. if (base <= 10)
  315. break;
  316. flags &= ~(CF_SIGNOK | CF_PFXOK | CF_NDIGITS);
  317. } else if (ch == '-' || ch == '+') {
  318. if ((flags & CF_SIGNOK) == 0)
  319. break;
  320. flags &= ~CF_SIGNOK;
  321. } else if (ch == 'x' || ch == 'X') {
  322. if ((flags & CF_PFXOK) == 0)
  323. break;
  324. base = 16;
  325. flags &= ~CF_PFXOK;
  326. } else {
  327. ch_ready = 1; /* character avail without read */
  328. break;
  329. }
  330. *cp++ = ch;
  331. if (width > 1) {
  332. if (_getb(fd, &ch, 1) != 1)
  333. break;
  334. }
  335. }
  336. if (flags & CF_NDIGITS)
  337. return acnt;
  338. if ((flags & CF_SUPPRESS) == 0) {
  339. uint32_t res;
  340. *cp = 0;
  341. res = strtol(buf, 0, base);
  342. if (flags & CF_LONG)
  343. *va_arg(ap, long *) = res;
  344. else if (hcnt == 1)
  345. *va_arg(ap, short *) = res;
  346. else if (hcnt)
  347. *va_arg(ap, char *) = res;
  348. else
  349. *va_arg(ap, int *) = res;
  350. acnt++;
  351. }
  352. ccnt++;
  353. }
  354. #ifdef STDIO_FLOATING_POINT
  355. else if (ct == CT_FLOAT) {
  356. if (width == 0 || width > sizeof(buf) - 1)
  357. width = sizeof(buf) - 1;
  358. flags |= CF_SIGNOK | CF_NDIGITS | CF_DPTOK | CF_EXPOK;
  359. for (cp = buf; width; width--) {
  360. if (ch >= '0' && ch <= '9')
  361. flags &= ~(CF_SIGNOK | CF_NDIGITS);
  362. else if (ch == '+' || ch == '-') {
  363. if ((flags & CF_SIGNOK) == 0)
  364. break;
  365. flags &= ~CF_SIGNOK;
  366. } else if (ch == '.') {
  367. if ((flags & CF_DPTOK) == 0)
  368. break;
  369. flags &= ~(CF_SIGNOK | CF_DPTOK);
  370. } else if (ch == 'e' || ch == 'E') {
  371. if ((flags & (CF_NDIGITS | CF_EXPOK)) != CF_EXPOK)
  372. break;
  373. flags = (flags & ~(CF_EXPOK | CF_DPTOK)) | CF_SIGNOK | CF_NDIGITS;
  374. } else {
  375. ch_ready = 1; /* character avail without read */
  376. break;
  377. }
  378. *cp++ = ch;
  379. if (_getb(fd, &ch, 1) != 1)
  380. break;
  381. }
  382. if (flags & CF_NDIGITS) {
  383. if (flags & CF_EXPOK)
  384. return acnt;
  385. }
  386. if ((flags & CF_SUPPRESS) == 0) {
  387. double res;
  388. *cp = 0;
  389. res = strtod(buf, 0);
  390. *va_arg(ap, double *) = res;
  391. acnt++;
  392. }
  393. ccnt++;
  394. }
  395. #endif /* STDIO_FLOATING_POINT */
  396. }
  397. }
  398. /*@}*/