vfscanf.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * pANS stdio -- vfscanf
  3. */
  4. #include "iolib.h"
  5. #include <ctype.h>
  6. static int icvt_f(FILE *f, va_list *args, int store, int width, int type);
  7. static int icvt_x(FILE *f, va_list *args, int store, int width, int type);
  8. static int icvt_sq(FILE *f, va_list *args, int store, int width, int type);
  9. static int icvt_c(FILE *f, va_list *args, int store, int width, int type);
  10. static int icvt_d(FILE *f, va_list *args, int store, int width, int type);
  11. static int icvt_i(FILE *f, va_list *args, int store, int width, int type);
  12. static int icvt_n(FILE *f, va_list *args, int store, int width, int type);
  13. static int icvt_o(FILE *f, va_list *args, int store, int width, int type);
  14. static int icvt_p(FILE *f, va_list *args, int store, int width, int type);
  15. static int icvt_s(FILE *f, va_list *args, int store, int width, int type);
  16. static int icvt_u(FILE *f, va_list *args, int store, int width, int type);
  17. static int (*icvt[])(FILE *, va_list *, int, int, int)={
  18. 0, 0, 0, 0, 0, 0, 0, 0, /* ^@ ^A ^B ^C ^D ^E ^F ^G */
  19. 0, 0, 0, 0, 0, 0, 0, 0, /* ^H ^I ^J ^K ^L ^M ^N ^O */
  20. 0, 0, 0, 0, 0, 0, 0, 0, /* ^P ^Q ^R ^S ^T ^U ^V ^W */
  21. 0, 0, 0, 0, 0, 0, 0, 0, /* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
  22. 0, 0, 0, 0, 0, 0, 0, 0, /* sp ! " # $ % & ' */
  23. 0, 0, 0, 0, 0, 0, 0, 0, /* ( ) * + , - . / */
  24. 0, 0, 0, 0, 0, 0, 0, 0, /* 0 1 2 3 4 5 6 7 */
  25. 0, 0, 0, 0, 0, 0, 0, 0, /* 8 9 : ; < = > ? */
  26. 0, 0, 0, 0, 0, icvt_f, 0, icvt_f, /* @ A B C D E F G */
  27. 0, 0, 0, 0, 0, 0, 0, 0, /* H I J K L M N O */
  28. 0, 0, 0, 0, 0, 0, 0, 0, /* P Q R S T U V W */
  29. icvt_x, 0, 0, icvt_sq,0, 0, 0, 0, /* X Y Z [ \ ] ^ _ */
  30. 0, 0, 0, icvt_c, icvt_d, icvt_f, icvt_f, icvt_f, /* ` a b c d e f g */
  31. 0, icvt_i, 0, 0, 0, 0, icvt_n, icvt_o, /* h i j k l m n o */
  32. icvt_p, 0, 0, icvt_s, 0, icvt_u, 0, 0, /* p q r s t u v w */
  33. icvt_x, 0, 0, 0, 0, 0, 0, 0, /* x y z { | } ~ ^? */
  34. 0, 0, 0, 0, 0, 0, 0, 0,
  35. 0, 0, 0, 0, 0, 0, 0, 0,
  36. 0, 0, 0, 0, 0, 0, 0, 0,
  37. 0, 0, 0, 0, 0, 0, 0, 0,
  38. 0, 0, 0, 0, 0, 0, 0, 0,
  39. 0, 0, 0, 0, 0, 0, 0, 0,
  40. 0, 0, 0, 0, 0, 0, 0, 0,
  41. 0, 0, 0, 0, 0, 0, 0, 0,
  42. 0, 0, 0, 0, 0, 0, 0, 0,
  43. 0, 0, 0, 0, 0, 0, 0, 0,
  44. 0, 0, 0, 0, 0, 0, 0, 0,
  45. 0, 0, 0, 0, 0, 0, 0, 0,
  46. 0, 0, 0, 0, 0, 0, 0, 0,
  47. 0, 0, 0, 0, 0, 0, 0, 0,
  48. 0, 0, 0, 0, 0, 0, 0, 0,
  49. 0, 0, 0, 0, 0, 0, 0, 0,
  50. };
  51. #define ngetc(f) (nread++, getc(f))
  52. #define nungetc(c, f) (--nread, ungetc((c), f))
  53. #define wgetc(c, f, out) if(width--==0) goto out; (c)=ngetc(f)
  54. #define wungetc(c, f) (++width, nungetc(c, f))
  55. static int nread, ncvt;
  56. static const char *fmtp;
  57. int vfscanf(FILE *f, const char *s, va_list args){
  58. int c, width, type, store;
  59. nread=0;
  60. ncvt=0;
  61. fmtp=s;
  62. for(;*fmtp;fmtp++) switch(*fmtp){
  63. default:
  64. if(isspace(*fmtp)){
  65. do
  66. c=ngetc(f);
  67. while(isspace(c));
  68. if(c==EOF) return ncvt?ncvt:EOF;
  69. nungetc(c, f);
  70. break;
  71. }
  72. NonSpecial:
  73. c=ngetc(f);
  74. if(c==EOF) return ncvt?ncvt:EOF;
  75. if(c!=*fmtp){
  76. nungetc(c, f);
  77. return ncvt;
  78. }
  79. break;
  80. case '%':
  81. fmtp++;
  82. if(*fmtp!='*') store=1;
  83. else{
  84. store=0;
  85. fmtp++;
  86. }
  87. if('0'<=*fmtp && *fmtp<='9'){
  88. width=0;
  89. while('0'<=*fmtp && *fmtp<='9') width=width*10 + *fmtp++ - '0';
  90. }
  91. else
  92. width=-1;
  93. type=*fmtp=='h' || *fmtp=='l' || *fmtp=='L'?*fmtp++:'n';
  94. if(!icvt[*fmtp]) goto NonSpecial;
  95. if(!(*icvt[*fmtp])(f, &args, store, width, type))
  96. return ncvt?ncvt:EOF;
  97. if(*fmtp=='\0') break;
  98. if(store) ncvt++;
  99. }
  100. return ncvt;
  101. }
  102. static int icvt_n(FILE *f, va_list *args, int store, int width, int type){
  103. #pragma ref f
  104. #pragma ref width
  105. if(store){
  106. --ncvt; /* this assignment doesn't count! */
  107. switch(type){
  108. case 'h': *va_arg(*args, short *)=nread; break;
  109. case 'n': *va_arg(*args, int *)=nread; break;
  110. case 'l':
  111. case 'L': *va_arg(*args, long *)=nread; break;
  112. }
  113. }
  114. return 1;
  115. }
  116. #define SIGNED 1
  117. #define UNSIGNED 2
  118. #define POINTER 3
  119. /*
  120. * Generic fixed-point conversion
  121. * f is the input FILE *;
  122. * args is the va_list * into which to store the number;
  123. * store is a flag to enable storing;
  124. * width is the maximum field width;
  125. * type is 'h' 'l' or 'L', the scanf type modifier;
  126. * unsgned is SIGNED, UNSIGNED or POINTER, giving part of the type to store in;
  127. * base is the number base -- if 0, C number syntax is used.
  128. */
  129. static int icvt_fixed(FILE *f, va_list *args,
  130. int store, int width, int type, int unsgned, int base){
  131. unsigned long int num=0;
  132. int sign=1, ndig=0, dig;
  133. int c;
  134. do
  135. c=ngetc(f);
  136. while(isspace(c));
  137. if(width--==0){
  138. nungetc(c, f);
  139. goto Done;
  140. }
  141. if(c=='+'){
  142. wgetc(c, f, Done);
  143. }
  144. else if(c=='-'){
  145. sign=-1;
  146. wgetc(c, f, Done);
  147. }
  148. switch(base){
  149. case 0:
  150. if(c=='0'){
  151. wgetc(c, f, Done);
  152. if(c=='x' || c=='X'){
  153. wgetc(c, f, Done);
  154. base=16;
  155. }
  156. else{
  157. ndig=1;
  158. base=8;
  159. }
  160. }
  161. else
  162. base=10;
  163. break;
  164. case 16:
  165. if(c=='0'){
  166. wgetc(c, f, Done);
  167. if(c=='x' || c=='X'){
  168. wgetc(c, f, Done);
  169. }
  170. else ndig=1;
  171. }
  172. break;
  173. }
  174. while('0'<=c && c<='9' || 'a'<=c && c<='f' || 'A'<=c && c<='F'){
  175. dig='0'<=c && c<='9'?c-'0':'a'<=c && c<='f'?c-'a'+10:c-'A'+10;
  176. if(dig>=base) break;
  177. ndig++;
  178. num=num*base+dig;
  179. wgetc(c, f, Done);
  180. }
  181. nungetc(c, f);
  182. Done:
  183. if(ndig==0) return 0;
  184. if(store){
  185. switch(unsgned){
  186. case SIGNED:
  187. switch(type){
  188. case 'h': *va_arg(*args, short *)=num*sign; break;
  189. case 'n': *va_arg(*args, int *)=num*sign; break;
  190. case 'l':
  191. case 'L': *va_arg(*args, long *)=num*sign; break;
  192. }
  193. break;
  194. case UNSIGNED:
  195. switch(type){
  196. case 'h': *va_arg(*args, unsigned short *)=num*sign; break;
  197. case 'n': *va_arg(*args, unsigned int *)=num*sign; break;
  198. case 'l':
  199. case 'L': *va_arg(*args, unsigned long *)=num*sign; break;
  200. }
  201. break;
  202. case POINTER:
  203. *va_arg(*args, void **)=(void *)(num*sign); break;
  204. }
  205. }
  206. return 1;
  207. }
  208. static int icvt_d(FILE *f, va_list *args, int store, int width, int type){
  209. return icvt_fixed(f, args, store, width, type, SIGNED, 10);
  210. }
  211. static int icvt_x(FILE *f, va_list *args, int store, int width, int type){
  212. return icvt_fixed(f, args, store, width, type, UNSIGNED, 16);
  213. }
  214. static int icvt_o(FILE *f, va_list *args, int store, int width, int type){
  215. return icvt_fixed(f, args, store, width, type, UNSIGNED, 8);
  216. }
  217. static int icvt_i(FILE *f, va_list *args, int store, int width, int type){
  218. return icvt_fixed(f, args, store, width, type, SIGNED, 0);
  219. }
  220. static int icvt_u(FILE *f, va_list *args, int store, int width, int type){
  221. return icvt_fixed(f, args, store, width, type, UNSIGNED, 10);
  222. }
  223. static int icvt_p(FILE *f, va_list *args, int store, int width, int type){
  224. return icvt_fixed(f, args, store, width, type, POINTER, 16);
  225. }
  226. #define NBUF 509
  227. static int icvt_f(FILE *f, va_list *args, int store, int width, int type){
  228. char buf[NBUF+1];
  229. char *s=buf;
  230. int c, ndig=0, ndpt=0, nexp=1;
  231. if(width<0 || NBUF<width) width=NBUF; /* bug -- no limit specified in ansi */
  232. do
  233. c=ngetc(f);
  234. while(isspace(c));
  235. if(width--==0){
  236. nungetc(c, f);
  237. goto Done;
  238. }
  239. if(c=='+' || c=='-'){
  240. *s++=c;
  241. wgetc(c, f, Done);
  242. }
  243. while('0'<=c && c<='9' || ndpt==0 && c=='.'){
  244. if(c=='.') ndpt++;
  245. else ndig++;
  246. *s++=c;
  247. wgetc(c, f, Done);
  248. }
  249. if(c=='e' || c=='E'){
  250. *s++=c;
  251. nexp=0;
  252. wgetc(c, f, Done);
  253. if(c=='+' || c=='-'){
  254. *s++=c;
  255. wgetc(c, f, Done);
  256. }
  257. while('0'<=c && c<='9'){
  258. *s++=c;
  259. nexp++;
  260. wgetc(c, f, Done);
  261. }
  262. }
  263. nungetc(c, f);
  264. Done:
  265. if(ndig==0 || nexp==0) return 0;
  266. *s='\0';
  267. if(store) switch(type){
  268. case 'h':
  269. case 'n': *va_arg(*args, float *)=atof(buf); break;
  270. case 'L': /* bug -- should store in a long double */
  271. case 'l': *va_arg(*args, double *)=atof(buf); break;
  272. }
  273. return 1;
  274. }
  275. static int icvt_s(FILE *f, va_list *args, int store, int width, int type){
  276. #pragma ref type
  277. int c, nn;
  278. register char *s;
  279. if(store) s=va_arg(*args, char *);
  280. do
  281. c=ngetc(f);
  282. while(isspace(c));
  283. if(width--==0){
  284. nungetc(c, f);
  285. goto Done;
  286. }
  287. nn=0;
  288. while(!isspace(c)){
  289. if(c==EOF){
  290. if(nn==0) return 0;
  291. else goto Done;
  292. }
  293. nn++;
  294. if(store) *s++=c;
  295. wgetc(c, f, Done);
  296. }
  297. nungetc(c, f);
  298. Done:
  299. if(store) *s='\0';
  300. return 1;
  301. }
  302. static int icvt_c(FILE *f, va_list *args, int store, int width, int type){
  303. #pragma ref type
  304. int c;
  305. register char *s;
  306. if(store) s=va_arg(*args, char *);
  307. if(width<0) width=1;
  308. for(;;){
  309. wgetc(c, f, Done);
  310. if(c==EOF) return 0;
  311. if(store) *s++=c;
  312. }
  313. Done:
  314. return 1;
  315. }
  316. static int match(int c, const char *pat){
  317. int ok=1;
  318. if(*pat=='^'){
  319. ok=!ok;
  320. pat++;
  321. }
  322. while(pat!=fmtp){
  323. if(pat+2<fmtp && pat[1]=='-'){
  324. if(pat[0]<=c && c<=pat[2]
  325. || pat[2]<=c && c<=pat[0])
  326. return ok;
  327. pat+=2;
  328. }
  329. else if(c==*pat) return ok;
  330. pat++;
  331. }
  332. return !ok;
  333. }
  334. static int icvt_sq(FILE *f, va_list *args, int store, int width, int type){
  335. #pragma ref type
  336. int c, nn;
  337. register char *s;
  338. register const char *pat;
  339. pat=++fmtp;
  340. if(*fmtp=='^') fmtp++;
  341. if(*fmtp!='\0') fmtp++;
  342. while(*fmtp!='\0' && *fmtp!=']') fmtp++;
  343. if(store) s=va_arg(*args, char *);
  344. nn=0;
  345. for(;;){
  346. wgetc(c, f, Done);
  347. if(c==EOF){
  348. if(nn==0) return 0;
  349. else goto Done;
  350. }
  351. if(!match(c, pat)) break;
  352. if(store) *s++=c;
  353. nn++;
  354. }
  355. nungetc(c, f);
  356. Done:
  357. if(store) *s='\0';
  358. return 1;
  359. }