uscanf_p.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. *
  6. * Copyright (C) 1998-2016, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. *******************************************************************************
  10. *
  11. * File uscnnf_p.c
  12. *
  13. * Modification History:
  14. *
  15. * Date Name Description
  16. * 12/02/98 stephen Creation.
  17. * 03/13/99 stephen Modified for new C API.
  18. *******************************************************************************
  19. */
  20. #include "unicode/utypes.h"
  21. #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION
  22. #include "unicode/uchar.h"
  23. #include "unicode/ustring.h"
  24. #include "unicode/unum.h"
  25. #include "unicode/udat.h"
  26. #include "unicode/uset.h"
  27. #include "uscanf.h"
  28. #include "ufmt_cmn.h"
  29. #include "ufile.h"
  30. #include "locbund.h"
  31. #include "cmemory.h"
  32. #include "ustr_cnv.h"
  33. /* flag characters for u_scanf */
  34. #define FLAG_ASTERISK 0x002A
  35. #define FLAG_PAREN 0x0028
  36. #define ISFLAG(s) (s) == FLAG_ASTERISK || \
  37. (s) == FLAG_PAREN
  38. /* special characters for u_scanf */
  39. #define SPEC_DOLLARSIGN 0x0024
  40. /* unicode digits */
  41. #define DIGIT_ZERO 0x0030
  42. #define DIGIT_ONE 0x0031
  43. #define DIGIT_TWO 0x0032
  44. #define DIGIT_THREE 0x0033
  45. #define DIGIT_FOUR 0x0034
  46. #define DIGIT_FIVE 0x0035
  47. #define DIGIT_SIX 0x0036
  48. #define DIGIT_SEVEN 0x0037
  49. #define DIGIT_EIGHT 0x0038
  50. #define DIGIT_NINE 0x0039
  51. #define ISDIGIT(s) (s) == DIGIT_ZERO || \
  52. (s) == DIGIT_ONE || \
  53. (s) == DIGIT_TWO || \
  54. (s) == DIGIT_THREE || \
  55. (s) == DIGIT_FOUR || \
  56. (s) == DIGIT_FIVE || \
  57. (s) == DIGIT_SIX || \
  58. (s) == DIGIT_SEVEN || \
  59. (s) == DIGIT_EIGHT || \
  60. (s) == DIGIT_NINE
  61. /* u_scanf modifiers */
  62. #define MOD_H 0x0068
  63. #define MOD_LOWERL 0x006C
  64. #define MOD_L 0x004C
  65. #define ISMOD(s) (s) == MOD_H || \
  66. (s) == MOD_LOWERL || \
  67. (s) == MOD_L
  68. /**
  69. * Struct encapsulating a single uscanf format specification.
  70. */
  71. typedef struct u_scanf_spec_info {
  72. int32_t fWidth; /* Width */
  73. char16_t fSpec; /* Format specification */
  74. char16_t fPadChar; /* Padding character */
  75. UBool fSkipArg; /* true if arg should be skipped */
  76. UBool fIsLongDouble; /* L flag */
  77. UBool fIsShort; /* h flag */
  78. UBool fIsLong; /* l flag */
  79. UBool fIsLongLong; /* ll flag */
  80. UBool fIsString; /* true if this is a NUL-terminated string. */
  81. } u_scanf_spec_info;
  82. /**
  83. * Struct encapsulating a single u_scanf format specification.
  84. */
  85. typedef struct u_scanf_spec {
  86. u_scanf_spec_info fInfo; /* Information on this spec */
  87. int32_t fArgPos; /* Position of data in arg list */
  88. } u_scanf_spec;
  89. /**
  90. * Parse a single u_scanf format specifier in Unicode.
  91. * @param fmt A pointer to a '%' character in a u_scanf format specification.
  92. * @param spec A pointer to a <TT>u_scanf_spec</TT> to receive the parsed
  93. * format specifier.
  94. * @return The number of characters contained in this specifier.
  95. */
  96. static int32_t
  97. u_scanf_parse_spec (const char16_t *fmt,
  98. u_scanf_spec *spec)
  99. {
  100. const char16_t *s = fmt;
  101. const char16_t *backup;
  102. u_scanf_spec_info *info = &(spec->fInfo);
  103. /* initialize spec to default values */
  104. spec->fArgPos = -1;
  105. info->fWidth = -1;
  106. info->fSpec = 0x0000;
  107. info->fPadChar = 0x0020;
  108. info->fSkipArg = false;
  109. info->fIsLongDouble = false;
  110. info->fIsShort = false;
  111. info->fIsLong = false;
  112. info->fIsLongLong = false;
  113. info->fIsString = true;
  114. /* skip over the initial '%' */
  115. s++;
  116. /* Check for positional argument */
  117. if(ISDIGIT(*s)) {
  118. /* Save the current position */
  119. backup = s;
  120. /* handle positional parameters */
  121. if(ISDIGIT(*s)) {
  122. spec->fArgPos = (int) (*s++ - DIGIT_ZERO);
  123. while(ISDIGIT(*s)) {
  124. spec->fArgPos *= 10;
  125. spec->fArgPos += (int) (*s++ - DIGIT_ZERO);
  126. }
  127. }
  128. /* if there is no '$', don't read anything */
  129. if(*s != SPEC_DOLLARSIGN) {
  130. spec->fArgPos = -1;
  131. s = backup;
  132. }
  133. /* munge the '$' */
  134. else
  135. s++;
  136. }
  137. /* Get any format flags */
  138. while(ISFLAG(*s)) {
  139. switch(*s++) {
  140. /* skip argument */
  141. case FLAG_ASTERISK:
  142. info->fSkipArg = true;
  143. break;
  144. /* pad character specified */
  145. case FLAG_PAREN:
  146. /* first four characters are hex values for pad char */
  147. info->fPadChar = (char16_t)ufmt_digitvalue(*s++);
  148. info->fPadChar = (char16_t)((info->fPadChar * 16) + ufmt_digitvalue(*s++));
  149. info->fPadChar = (char16_t)((info->fPadChar * 16) + ufmt_digitvalue(*s++));
  150. info->fPadChar = (char16_t)((info->fPadChar * 16) + ufmt_digitvalue(*s++));
  151. /* final character is ignored */
  152. s++;
  153. break;
  154. }
  155. }
  156. /* Get the width */
  157. if(ISDIGIT(*s)){
  158. info->fWidth = (int) (*s++ - DIGIT_ZERO);
  159. while(ISDIGIT(*s)) {
  160. info->fWidth *= 10;
  161. info->fWidth += (int) (*s++ - DIGIT_ZERO);
  162. }
  163. }
  164. /* Get any modifiers */
  165. if(ISMOD(*s)) {
  166. switch(*s++) {
  167. /* short */
  168. case MOD_H:
  169. info->fIsShort = true;
  170. break;
  171. /* long or long long */
  172. case MOD_LOWERL:
  173. if(*s == MOD_LOWERL) {
  174. info->fIsLongLong = true;
  175. /* skip over the next 'l' */
  176. s++;
  177. }
  178. else
  179. info->fIsLong = true;
  180. break;
  181. /* long double */
  182. case MOD_L:
  183. info->fIsLongDouble = true;
  184. break;
  185. }
  186. }
  187. /* finally, get the specifier letter */
  188. info->fSpec = *s++;
  189. /* return # of characters in this specifier */
  190. return (int32_t)(s - fmt);
  191. }
  192. #define UP_PERCENT 0x0025
  193. /* ANSI style formatting */
  194. /* Use US-ASCII characters only for formatting */
  195. /* % */
  196. #define UFMT_SIMPLE_PERCENT {ufmt_simple_percent, u_scanf_simple_percent_handler}
  197. /* s */
  198. #define UFMT_STRING {ufmt_string, u_scanf_string_handler}
  199. /* c */
  200. #define UFMT_CHAR {ufmt_string, u_scanf_char_handler}
  201. /* d, i */
  202. #define UFMT_INT {ufmt_int, u_scanf_integer_handler}
  203. /* u */
  204. #define UFMT_UINT {ufmt_int, u_scanf_uinteger_handler}
  205. /* o */
  206. #define UFMT_OCTAL {ufmt_int, u_scanf_octal_handler}
  207. /* x, X */
  208. #define UFMT_HEX {ufmt_int, u_scanf_hex_handler}
  209. /* f */
  210. #define UFMT_DOUBLE {ufmt_double, u_scanf_double_handler}
  211. /* e, E */
  212. #define UFMT_SCIENTIFIC {ufmt_double, u_scanf_scientific_handler}
  213. /* g, G */
  214. #define UFMT_SCIDBL {ufmt_double, u_scanf_scidbl_handler}
  215. /* n */
  216. #define UFMT_COUNT {ufmt_count, u_scanf_count_handler}
  217. /* [ */
  218. #define UFMT_SCANSET {ufmt_string, u_scanf_scanset_handler}
  219. /* non-ANSI extensions */
  220. /* Use US-ASCII characters only for formatting */
  221. /* p */
  222. #define UFMT_POINTER {ufmt_pointer, u_scanf_pointer_handler}
  223. /* V */
  224. #define UFMT_SPELLOUT {ufmt_double, u_scanf_spellout_handler}
  225. /* P */
  226. #define UFMT_PERCENT {ufmt_double, u_scanf_percent_handler}
  227. /* C K is old format */
  228. #define UFMT_UCHAR {ufmt_uchar, u_scanf_uchar_handler}
  229. /* S U is old format */
  230. #define UFMT_USTRING {ufmt_ustring, u_scanf_ustring_handler}
  231. #define UFMT_EMPTY {ufmt_empty, nullptr}
  232. /**
  233. * A u_scanf handler function.
  234. * A u_scanf handler is responsible for handling a single u_scanf
  235. * format specification, for example 'd' or 's'.
  236. * @param stream The UFILE to which to write output.
  237. * @param info A pointer to a <TT>u_scanf_spec_info</TT> struct containing
  238. * information on the format specification.
  239. * @param args A pointer to the argument data
  240. * @param fmt A pointer to the first character in the format string
  241. * following the spec.
  242. * @param fmtConsumed On output, set to the number of characters consumed
  243. * in <TT>fmt</TT>. Do nothing, if the argument isn't variable width.
  244. * @param argConverted The number of arguments converted and assigned, or -1 if an
  245. * error occurred.
  246. * @return The number of code points consumed during reading.
  247. */
  248. typedef int32_t (*u_scanf_handler) (UFILE *stream,
  249. u_scanf_spec_info *info,
  250. ufmt_args *args,
  251. const char16_t *fmt,
  252. int32_t *fmtConsumed,
  253. int32_t *argConverted);
  254. typedef struct u_scanf_info {
  255. ufmt_type_info info;
  256. u_scanf_handler handler;
  257. } u_scanf_info;
  258. #define USCANF_NUM_FMT_HANDLERS 108
  259. #define USCANF_SYMBOL_BUFFER_SIZE 8
  260. /* We do not use handlers for 0-0x1f */
  261. #define USCANF_BASE_FMT_HANDLERS 0x20
  262. static int32_t
  263. u_scanf_skip_leading_ws(UFILE *input,
  264. char16_t pad)
  265. {
  266. char16_t c;
  267. int32_t count = 0;
  268. UBool isNotEOF;
  269. /* skip all leading ws in the input */
  270. while( ((isNotEOF = ufile_getch(input, &c))==(UBool)true) && (c == pad || u_isWhitespace(c)) )
  271. {
  272. count++;
  273. }
  274. /* put the final character back on the input */
  275. if(isNotEOF)
  276. u_fungetc(c, input);
  277. return count;
  278. }
  279. /* TODO: Is always skipping the prefix symbol as a positive sign a good idea in all locales? */
  280. static int32_t
  281. u_scanf_skip_leading_positive_sign(UFILE *input,
  282. UNumberFormat *format,
  283. UErrorCode *status)
  284. {
  285. char16_t c;
  286. int32_t count = 0;
  287. UBool isNotEOF;
  288. char16_t plusSymbol[USCANF_SYMBOL_BUFFER_SIZE];
  289. int32_t symbolLen;
  290. UErrorCode localStatus = U_ZERO_ERROR;
  291. if (U_SUCCESS(*status)) {
  292. symbolLen = unum_getSymbol(format,
  293. UNUM_PLUS_SIGN_SYMBOL,
  294. plusSymbol,
  295. UPRV_LENGTHOF(plusSymbol),
  296. &localStatus);
  297. if (U_SUCCESS(localStatus)) {
  298. /* skip all leading ws in the input */
  299. while( ((isNotEOF = ufile_getch(input, &c))==(UBool)true) && (count < symbolLen && c == plusSymbol[count]) )
  300. {
  301. count++;
  302. }
  303. /* put the final character back on the input */
  304. if(isNotEOF) {
  305. u_fungetc(c, input);
  306. }
  307. }
  308. }
  309. return count;
  310. }
  311. static int32_t
  312. u_scanf_simple_percent_handler(UFILE *input,
  313. u_scanf_spec_info *info,
  314. ufmt_args *args,
  315. const char16_t *fmt,
  316. int32_t *fmtConsumed,
  317. int32_t *argConverted)
  318. {
  319. (void)info;
  320. (void)args;
  321. (void)fmt;
  322. (void)fmtConsumed;
  323. /* make sure the next character in the input is a percent */
  324. *argConverted = 0;
  325. if(u_fgetc(input) != 0x0025) {
  326. *argConverted = -1;
  327. }
  328. return 1;
  329. }
  330. static int32_t
  331. u_scanf_count_handler(UFILE *input,
  332. u_scanf_spec_info *info,
  333. ufmt_args *args,
  334. const char16_t *fmt,
  335. int32_t *fmtConsumed,
  336. int32_t *argConverted)
  337. {
  338. (void)input;
  339. (void)fmt;
  340. (void)fmtConsumed;
  341. /* in the special case of count, the u_scanf_spec_info's width */
  342. /* will contain the # of items converted thus far */
  343. if (!info->fSkipArg) {
  344. if (info->fIsShort)
  345. *(int16_t*)(args[0].ptrValue) = (int16_t)(UINT16_MAX & info->fWidth);
  346. else if (info->fIsLongLong)
  347. *(int64_t*)(args[0].ptrValue) = info->fWidth;
  348. else
  349. *(int32_t*)(args[0].ptrValue) = (int32_t)(UINT32_MAX & info->fWidth);
  350. }
  351. *argConverted = 0;
  352. /* we converted 0 args */
  353. return 0;
  354. }
  355. static int32_t
  356. u_scanf_double_handler(UFILE *input,
  357. u_scanf_spec_info *info,
  358. ufmt_args *args,
  359. const char16_t *fmt,
  360. int32_t *fmtConsumed,
  361. int32_t *argConverted)
  362. {
  363. (void)fmt;
  364. (void)fmtConsumed;
  365. int32_t len;
  366. double num;
  367. UNumberFormat *format;
  368. int32_t parsePos = 0;
  369. int32_t skipped;
  370. UErrorCode status = U_ZERO_ERROR;
  371. /* skip all ws in the input */
  372. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  373. /* fill the input's internal buffer */
  374. ufile_fill_uchar_buffer(input);
  375. /* determine the size of the input's buffer */
  376. len = (int32_t)(input->str.fLimit - input->str.fPos);
  377. /* truncate to the width, if specified */
  378. if(info->fWidth != -1)
  379. len = ufmt_min(len, info->fWidth);
  380. /* get the formatter */
  381. format = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_DECIMAL);
  382. /* handle error */
  383. if(format == 0)
  384. return 0;
  385. /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */
  386. skipped += u_scanf_skip_leading_positive_sign(input, format, &status);
  387. /* parse the number */
  388. num = unum_parseDouble(format, input->str.fPos, len, &parsePos, &status);
  389. if (!info->fSkipArg) {
  390. if (info->fIsLong)
  391. *(double*)(args[0].ptrValue) = num;
  392. else if (info->fIsLongDouble)
  393. *(long double*)(args[0].ptrValue) = num;
  394. else
  395. *(float*)(args[0].ptrValue) = (float)num;
  396. }
  397. /* mask off any necessary bits */
  398. /* if(! info->fIsLong_double)
  399. num &= DBL_MAX;*/
  400. /* update the input's position to reflect consumed data */
  401. input->str.fPos += parsePos;
  402. /* we converted 1 arg */
  403. *argConverted = !info->fSkipArg;
  404. return parsePos + skipped;
  405. }
  406. #define UPRINTF_SYMBOL_BUFFER_SIZE 8
  407. static int32_t
  408. u_scanf_scientific_handler(UFILE *input,
  409. u_scanf_spec_info *info,
  410. ufmt_args *args,
  411. const char16_t *fmt,
  412. int32_t *fmtConsumed,
  413. int32_t *argConverted)
  414. {
  415. (void)fmt;
  416. (void)fmtConsumed;
  417. int32_t len;
  418. double num;
  419. UNumberFormat *format;
  420. int32_t parsePos = 0;
  421. int32_t skipped;
  422. UErrorCode status = U_ZERO_ERROR;
  423. char16_t srcExpBuf[UPRINTF_SYMBOL_BUFFER_SIZE];
  424. int32_t srcLen, expLen;
  425. char16_t expBuf[UPRINTF_SYMBOL_BUFFER_SIZE];
  426. /* skip all ws in the input */
  427. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  428. /* fill the input's internal buffer */
  429. ufile_fill_uchar_buffer(input);
  430. /* determine the size of the input's buffer */
  431. len = (int32_t)(input->str.fLimit - input->str.fPos);
  432. /* truncate to the width, if specified */
  433. if(info->fWidth != -1)
  434. len = ufmt_min(len, info->fWidth);
  435. /* get the formatter */
  436. format = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_SCIENTIFIC);
  437. /* handle error */
  438. if(format == 0)
  439. return 0;
  440. /* set the appropriate flags on the formatter */
  441. srcLen = unum_getSymbol(format,
  442. UNUM_EXPONENTIAL_SYMBOL,
  443. srcExpBuf,
  444. sizeof(srcExpBuf),
  445. &status);
  446. /* Upper/lower case the e */
  447. if (info->fSpec == (char16_t)0x65 /* e */) {
  448. expLen = u_strToLower(expBuf, (int32_t)sizeof(expBuf),
  449. srcExpBuf, srcLen,
  450. input->str.fBundle.fLocale,
  451. &status);
  452. }
  453. else {
  454. expLen = u_strToUpper(expBuf, (int32_t)sizeof(expBuf),
  455. srcExpBuf, srcLen,
  456. input->str.fBundle.fLocale,
  457. &status);
  458. }
  459. unum_setSymbol(format,
  460. UNUM_EXPONENTIAL_SYMBOL,
  461. expBuf,
  462. expLen,
  463. &status);
  464. /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */
  465. skipped += u_scanf_skip_leading_positive_sign(input, format, &status);
  466. /* parse the number */
  467. num = unum_parseDouble(format, input->str.fPos, len, &parsePos, &status);
  468. if (!info->fSkipArg) {
  469. if (info->fIsLong)
  470. *(double*)(args[0].ptrValue) = num;
  471. else if (info->fIsLongDouble)
  472. *(long double*)(args[0].ptrValue) = num;
  473. else
  474. *(float*)(args[0].ptrValue) = (float)num;
  475. }
  476. /* mask off any necessary bits */
  477. /* if(! info->fIsLong_double)
  478. num &= DBL_MAX;*/
  479. /* update the input's position to reflect consumed data */
  480. input->str.fPos += parsePos;
  481. /* we converted 1 arg */
  482. *argConverted = !info->fSkipArg;
  483. return parsePos + skipped;
  484. }
  485. static int32_t
  486. u_scanf_scidbl_handler(UFILE *input,
  487. u_scanf_spec_info *info,
  488. ufmt_args *args,
  489. const char16_t *fmt,
  490. int32_t *fmtConsumed,
  491. int32_t *argConverted)
  492. {
  493. (void)fmt;
  494. (void)fmtConsumed;
  495. int32_t len;
  496. double num;
  497. UNumberFormat *scientificFormat, *genericFormat;
  498. /*int32_t scientificResult, genericResult;*/
  499. double scientificResult, genericResult;
  500. int32_t scientificParsePos = 0, genericParsePos = 0, parsePos = 0;
  501. int32_t skipped;
  502. UErrorCode scientificStatus = U_ZERO_ERROR;
  503. UErrorCode genericStatus = U_ZERO_ERROR;
  504. /* since we can't determine by scanning the characters whether */
  505. /* a number was formatted in the 'f' or 'g' styles, parse the */
  506. /* string with both formatters, and assume whichever one */
  507. /* parsed the most is the correct formatter to use */
  508. /* skip all ws in the input */
  509. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  510. /* fill the input's internal buffer */
  511. ufile_fill_uchar_buffer(input);
  512. /* determine the size of the input's buffer */
  513. len = (int32_t)(input->str.fLimit - input->str.fPos);
  514. /* truncate to the width, if specified */
  515. if(info->fWidth != -1)
  516. len = ufmt_min(len, info->fWidth);
  517. /* get the formatters */
  518. scientificFormat = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_SCIENTIFIC);
  519. genericFormat = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_DECIMAL);
  520. /* handle error */
  521. if(scientificFormat == 0 || genericFormat == 0)
  522. return 0;
  523. /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */
  524. skipped += u_scanf_skip_leading_positive_sign(input, genericFormat, &genericStatus);
  525. /* parse the number using each format*/
  526. scientificResult = unum_parseDouble(scientificFormat, input->str.fPos, len,
  527. &scientificParsePos, &scientificStatus);
  528. genericResult = unum_parseDouble(genericFormat, input->str.fPos, len,
  529. &genericParsePos, &genericStatus);
  530. /* determine which parse made it farther */
  531. if(scientificParsePos > genericParsePos) {
  532. /* stash the result in num */
  533. num = scientificResult;
  534. /* update the input's position to reflect consumed data */
  535. parsePos += scientificParsePos;
  536. }
  537. else {
  538. /* stash the result in num */
  539. num = genericResult;
  540. /* update the input's position to reflect consumed data */
  541. parsePos += genericParsePos;
  542. }
  543. input->str.fPos += parsePos;
  544. if (!info->fSkipArg) {
  545. if (info->fIsLong)
  546. *(double*)(args[0].ptrValue) = num;
  547. else if (info->fIsLongDouble)
  548. *(long double*)(args[0].ptrValue) = num;
  549. else
  550. *(float*)(args[0].ptrValue) = (float)num;
  551. }
  552. /* mask off any necessary bits */
  553. /* if(! info->fIsLong_double)
  554. num &= DBL_MAX;*/
  555. /* we converted 1 arg */
  556. *argConverted = !info->fSkipArg;
  557. return parsePos + skipped;
  558. }
  559. static int32_t
  560. u_scanf_integer_handler(UFILE *input,
  561. u_scanf_spec_info *info,
  562. ufmt_args *args,
  563. const char16_t *fmt,
  564. int32_t *fmtConsumed,
  565. int32_t *argConverted)
  566. {
  567. (void)fmt;
  568. (void)fmtConsumed;
  569. int32_t len;
  570. void *num = (void*) (args[0].ptrValue);
  571. UNumberFormat *format, *localFormat;
  572. int32_t parsePos = 0;
  573. int32_t skipped;
  574. int32_t parseIntOnly = 0;
  575. UErrorCode status = U_ZERO_ERROR;
  576. int64_t result;
  577. /* skip all ws in the input */
  578. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  579. /* fill the input's internal buffer */
  580. ufile_fill_uchar_buffer(input);
  581. /* determine the size of the input's buffer */
  582. len = (int32_t)(input->str.fLimit - input->str.fPos);
  583. /* truncate to the width, if specified */
  584. if(info->fWidth != -1)
  585. len = ufmt_min(len, info->fWidth);
  586. /* get the formatter */
  587. format = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_DECIMAL);
  588. /* handle error */
  589. if(format == 0)
  590. return 0;
  591. /* for integer types, do not attempt to parse fractions */
  592. localFormat = unum_clone(format, &status);
  593. if(U_FAILURE(status))
  594. return 0;
  595. if(info->fSpec == 'd' || info->fSpec == 'i' || info->fSpec == 'u')
  596. parseIntOnly = 1;
  597. unum_setAttribute(localFormat, UNUM_PARSE_INT_ONLY, parseIntOnly);
  598. /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */
  599. skipped += u_scanf_skip_leading_positive_sign(input, localFormat, &status);
  600. /* parse the number */
  601. result = unum_parseInt64(localFormat, input->str.fPos, len, &parsePos, &status);
  602. /* mask off any necessary bits */
  603. if (!info->fSkipArg) {
  604. if (info->fIsShort)
  605. *(int16_t*)num = (int16_t)(UINT16_MAX & result);
  606. else if (info->fIsLongLong)
  607. *(int64_t*)num = result;
  608. else
  609. *(int32_t*)num = (int32_t)(UINT32_MAX & result);
  610. }
  611. /* update the input's position to reflect consumed data */
  612. input->str.fPos += parsePos;
  613. /* cleanup cloned formatter */
  614. unum_close(localFormat);
  615. /* we converted 1 arg */
  616. *argConverted = !info->fSkipArg;
  617. return parsePos + skipped;
  618. }
  619. static int32_t
  620. u_scanf_uinteger_handler(UFILE *input,
  621. u_scanf_spec_info *info,
  622. ufmt_args *args,
  623. const char16_t *fmt,
  624. int32_t *fmtConsumed,
  625. int32_t *argConverted)
  626. {
  627. /* TODO Fix this when Numberformat handles uint64_t */
  628. return u_scanf_integer_handler(input, info, args, fmt, fmtConsumed, argConverted);
  629. }
  630. static int32_t
  631. u_scanf_percent_handler(UFILE *input,
  632. u_scanf_spec_info *info,
  633. ufmt_args *args,
  634. const char16_t *fmt,
  635. int32_t *fmtConsumed,
  636. int32_t *argConverted)
  637. {
  638. (void)fmt;
  639. (void)fmtConsumed;
  640. int32_t len;
  641. double num;
  642. UNumberFormat *format;
  643. int32_t parsePos = 0;
  644. UErrorCode status = U_ZERO_ERROR;
  645. /* skip all ws in the input */
  646. u_scanf_skip_leading_ws(input, info->fPadChar);
  647. /* fill the input's internal buffer */
  648. ufile_fill_uchar_buffer(input);
  649. /* determine the size of the input's buffer */
  650. len = (int32_t)(input->str.fLimit - input->str.fPos);
  651. /* truncate to the width, if specified */
  652. if(info->fWidth != -1)
  653. len = ufmt_min(len, info->fWidth);
  654. /* get the formatter */
  655. format = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_PERCENT);
  656. /* handle error */
  657. if(format == 0)
  658. return 0;
  659. /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */
  660. u_scanf_skip_leading_positive_sign(input, format, &status);
  661. /* parse the number */
  662. num = unum_parseDouble(format, input->str.fPos, len, &parsePos, &status);
  663. if (!info->fSkipArg) {
  664. *(double*)(args[0].ptrValue) = num;
  665. }
  666. /* mask off any necessary bits */
  667. /* if(! info->fIsLong_double)
  668. num &= DBL_MAX;*/
  669. /* update the input's position to reflect consumed data */
  670. input->str.fPos += parsePos;
  671. /* we converted 1 arg */
  672. *argConverted = !info->fSkipArg;
  673. return parsePos;
  674. }
  675. static int32_t
  676. u_scanf_string_handler(UFILE *input,
  677. u_scanf_spec_info *info,
  678. ufmt_args *args,
  679. const char16_t *fmt,
  680. int32_t *fmtConsumed,
  681. int32_t *argConverted)
  682. {
  683. (void)fmt;
  684. (void)fmtConsumed;
  685. const char16_t *source;
  686. UConverter *conv;
  687. char *arg = (char*)(args[0].ptrValue);
  688. char *alias = arg;
  689. char *limit;
  690. UErrorCode status = U_ZERO_ERROR;
  691. int32_t count;
  692. int32_t skipped = 0;
  693. char16_t c;
  694. UBool isNotEOF = false;
  695. /* skip all ws in the input */
  696. if (info->fIsString) {
  697. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  698. }
  699. /* get the string one character at a time, truncating to the width */
  700. count = 0;
  701. /* open the default converter */
  702. conv = u_getDefaultConverter(&status);
  703. if(U_FAILURE(status))
  704. return -1;
  705. while( (info->fWidth == -1 || count < info->fWidth)
  706. && ((isNotEOF = ufile_getch(input, &c))==(UBool)true)
  707. && (!info->fIsString || (c != info->fPadChar && !u_isWhitespace(c))))
  708. {
  709. if (!info->fSkipArg) {
  710. /* put the character from the input onto the target */
  711. source = &c;
  712. /* Since we do this one character at a time, do it this way. */
  713. if (info->fWidth > 0) {
  714. limit = alias + info->fWidth - count;
  715. }
  716. else {
  717. limit = alias + ucnv_getMaxCharSize(conv);
  718. }
  719. /* convert the character to the default codepage */
  720. ucnv_fromUnicode(conv, &alias, limit, &source, source + 1,
  721. nullptr, true, &status);
  722. if(U_FAILURE(status)) {
  723. /* clean up */
  724. u_releaseDefaultConverter(conv);
  725. return -1;
  726. }
  727. }
  728. /* increment the count */
  729. ++count;
  730. }
  731. /* put the final character we read back on the input */
  732. if (!info->fSkipArg) {
  733. if ((info->fWidth == -1 || count < info->fWidth) && isNotEOF)
  734. u_fungetc(c, input);
  735. /* add the terminator */
  736. if (info->fIsString) {
  737. *alias = 0x00;
  738. }
  739. }
  740. /* clean up */
  741. u_releaseDefaultConverter(conv);
  742. /* we converted 1 arg */
  743. *argConverted = !info->fSkipArg;
  744. return count + skipped;
  745. }
  746. static int32_t
  747. u_scanf_char_handler(UFILE *input,
  748. u_scanf_spec_info *info,
  749. ufmt_args *args,
  750. const char16_t *fmt,
  751. int32_t *fmtConsumed,
  752. int32_t *argConverted)
  753. {
  754. if (info->fWidth < 0) {
  755. info->fWidth = 1;
  756. }
  757. info->fIsString = false;
  758. return u_scanf_string_handler(input, info, args, fmt, fmtConsumed, argConverted);
  759. }
  760. static int32_t
  761. u_scanf_ustring_handler(UFILE *input,
  762. u_scanf_spec_info *info,
  763. ufmt_args *args,
  764. const char16_t *fmt,
  765. int32_t *fmtConsumed,
  766. int32_t *argConverted)
  767. {
  768. (void)fmt;
  769. (void)fmtConsumed;
  770. char16_t *arg = (char16_t*)(args[0].ptrValue);
  771. char16_t *alias = arg;
  772. int32_t count;
  773. int32_t skipped = 0;
  774. char16_t c;
  775. UBool isNotEOF = false;
  776. /* skip all ws in the input */
  777. if (info->fIsString) {
  778. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  779. }
  780. /* get the string one character at a time, truncating to the width */
  781. count = 0;
  782. while( (info->fWidth == -1 || count < info->fWidth)
  783. && ((isNotEOF = ufile_getch(input, &c))==(UBool)true)
  784. && (!info->fIsString || (c != info->fPadChar && !u_isWhitespace(c))))
  785. {
  786. /* put the character from the input onto the target */
  787. if (!info->fSkipArg) {
  788. *alias++ = c;
  789. }
  790. /* increment the count */
  791. ++count;
  792. }
  793. /* put the final character we read back on the input */
  794. if (!info->fSkipArg) {
  795. if((info->fWidth == -1 || count < info->fWidth) && isNotEOF) {
  796. u_fungetc(c, input);
  797. }
  798. /* add the terminator */
  799. if (info->fIsString) {
  800. *alias = 0x0000;
  801. }
  802. }
  803. /* we converted 1 arg */
  804. *argConverted = !info->fSkipArg;
  805. return count + skipped;
  806. }
  807. static int32_t
  808. u_scanf_uchar_handler(UFILE *input,
  809. u_scanf_spec_info *info,
  810. ufmt_args *args,
  811. const char16_t *fmt,
  812. int32_t *fmtConsumed,
  813. int32_t *argConverted)
  814. {
  815. if (info->fWidth < 0) {
  816. info->fWidth = 1;
  817. }
  818. info->fIsString = false;
  819. return u_scanf_ustring_handler(input, info, args, fmt, fmtConsumed, argConverted);
  820. }
  821. static int32_t
  822. u_scanf_spellout_handler(UFILE *input,
  823. u_scanf_spec_info *info,
  824. ufmt_args *args,
  825. const char16_t *fmt,
  826. int32_t *fmtConsumed,
  827. int32_t *argConverted)
  828. {
  829. (void)fmt;
  830. (void)fmtConsumed;
  831. int32_t len;
  832. double num;
  833. UNumberFormat *format;
  834. int32_t parsePos = 0;
  835. int32_t skipped;
  836. UErrorCode status = U_ZERO_ERROR;
  837. /* skip all ws in the input */
  838. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  839. /* fill the input's internal buffer */
  840. ufile_fill_uchar_buffer(input);
  841. /* determine the size of the input's buffer */
  842. len = (int32_t)(input->str.fLimit - input->str.fPos);
  843. /* truncate to the width, if specified */
  844. if(info->fWidth != -1)
  845. len = ufmt_min(len, info->fWidth);
  846. /* get the formatter */
  847. format = u_locbund_getNumberFormat(&input->str.fBundle, UNUM_SPELLOUT);
  848. /* handle error */
  849. if(format == 0)
  850. return 0;
  851. /* Skip the positive prefix. ICU normally can't handle this due to strict parsing. */
  852. /* This is not applicable to RBNF. */
  853. /*skipped += u_scanf_skip_leading_positive_sign(input, format, &status);*/
  854. /* parse the number */
  855. num = unum_parseDouble(format, input->str.fPos, len, &parsePos, &status);
  856. if (!info->fSkipArg) {
  857. *(double*)(args[0].ptrValue) = num;
  858. }
  859. /* mask off any necessary bits */
  860. /* if(! info->fIsLong_double)
  861. num &= DBL_MAX;*/
  862. /* update the input's position to reflect consumed data */
  863. input->str.fPos += parsePos;
  864. /* we converted 1 arg */
  865. *argConverted = !info->fSkipArg;
  866. return parsePos + skipped;
  867. }
  868. static int32_t
  869. u_scanf_hex_handler(UFILE *input,
  870. u_scanf_spec_info *info,
  871. ufmt_args *args,
  872. const char16_t *fmt,
  873. int32_t *fmtConsumed,
  874. int32_t *argConverted)
  875. {
  876. (void)fmt;
  877. (void)fmtConsumed;
  878. int32_t len;
  879. int32_t skipped;
  880. void *num = (void*) (args[0].ptrValue);
  881. int64_t result;
  882. /* skip all ws in the input */
  883. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  884. /* fill the input's internal buffer */
  885. ufile_fill_uchar_buffer(input);
  886. /* determine the size of the input's buffer */
  887. len = (int32_t)(input->str.fLimit - input->str.fPos);
  888. /* truncate to the width, if specified */
  889. if(info->fWidth != -1)
  890. len = ufmt_min(len, info->fWidth);
  891. /* check for alternate form */
  892. if( *(input->str.fPos) == 0x0030 &&
  893. (*(input->str.fPos + 1) == 0x0078 || *(input->str.fPos + 1) == 0x0058) ) {
  894. /* skip the '0' and 'x' or 'X' if present */
  895. input->str.fPos += 2;
  896. len -= 2;
  897. }
  898. /* parse the number */
  899. result = ufmt_uto64(input->str.fPos, &len, 16);
  900. /* update the input's position to reflect consumed data */
  901. input->str.fPos += len;
  902. /* mask off any necessary bits */
  903. if (!info->fSkipArg) {
  904. if (info->fIsShort)
  905. *(int16_t*)num = (int16_t)(UINT16_MAX & result);
  906. else if (info->fIsLongLong)
  907. *(int64_t*)num = result;
  908. else
  909. *(int32_t*)num = (int32_t)(UINT32_MAX & result);
  910. }
  911. /* we converted 1 arg */
  912. *argConverted = !info->fSkipArg;
  913. return len + skipped;
  914. }
  915. static int32_t
  916. u_scanf_octal_handler(UFILE *input,
  917. u_scanf_spec_info *info,
  918. ufmt_args *args,
  919. const char16_t *fmt,
  920. int32_t *fmtConsumed,
  921. int32_t *argConverted)
  922. {
  923. (void)fmt;
  924. (void)fmtConsumed;
  925. int32_t len;
  926. int32_t skipped;
  927. void *num = (void*) (args[0].ptrValue);
  928. int64_t result;
  929. /* skip all ws in the input */
  930. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  931. /* fill the input's internal buffer */
  932. ufile_fill_uchar_buffer(input);
  933. /* determine the size of the input's buffer */
  934. len = (int32_t)(input->str.fLimit - input->str.fPos);
  935. /* truncate to the width, if specified */
  936. if(info->fWidth != -1)
  937. len = ufmt_min(len, info->fWidth);
  938. /* parse the number */
  939. result = ufmt_uto64(input->str.fPos, &len, 8);
  940. /* update the input's position to reflect consumed data */
  941. input->str.fPos += len;
  942. /* mask off any necessary bits */
  943. if (!info->fSkipArg) {
  944. if (info->fIsShort)
  945. *(int16_t*)num = (int16_t)(UINT16_MAX & result);
  946. else if (info->fIsLongLong)
  947. *(int64_t*)num = result;
  948. else
  949. *(int32_t*)num = (int32_t)(UINT32_MAX & result);
  950. }
  951. /* we converted 1 arg */
  952. *argConverted = !info->fSkipArg;
  953. return len + skipped;
  954. }
  955. static int32_t
  956. u_scanf_pointer_handler(UFILE *input,
  957. u_scanf_spec_info *info,
  958. ufmt_args *args,
  959. const char16_t *fmt,
  960. int32_t *fmtConsumed,
  961. int32_t *argConverted)
  962. {
  963. (void)fmt;
  964. (void)fmtConsumed;
  965. int32_t len;
  966. int32_t skipped;
  967. void *result;
  968. void **p = (void**)(args[0].ptrValue);
  969. /* skip all ws in the input */
  970. skipped = u_scanf_skip_leading_ws(input, info->fPadChar);
  971. /* fill the input's internal buffer */
  972. ufile_fill_uchar_buffer(input);
  973. /* determine the size of the input's buffer */
  974. len = (int32_t)(input->str.fLimit - input->str.fPos);
  975. /* truncate to the width, if specified */
  976. if(info->fWidth != -1) {
  977. len = ufmt_min(len, info->fWidth);
  978. }
  979. /* Make sure that we don't consume too much */
  980. if (len > (int32_t)(sizeof(void*)*2)) {
  981. len = (int32_t)(sizeof(void*)*2);
  982. }
  983. /* parse the pointer - assign to temporary value */
  984. result = ufmt_utop(input->str.fPos, &len);
  985. if (!info->fSkipArg) {
  986. *p = result;
  987. }
  988. /* update the input's position to reflect consumed data */
  989. input->str.fPos += len;
  990. /* we converted 1 arg */
  991. *argConverted = !info->fSkipArg;
  992. return len + skipped;
  993. }
  994. static int32_t
  995. u_scanf_scanset_handler(UFILE *input,
  996. u_scanf_spec_info *info,
  997. ufmt_args *args,
  998. const char16_t *fmt,
  999. int32_t *fmtConsumed,
  1000. int32_t *argConverted)
  1001. {
  1002. USet *scanset;
  1003. UErrorCode status = U_ZERO_ERROR;
  1004. int32_t chLeft = INT32_MAX;
  1005. UChar32 c;
  1006. char16_t *alias = (char16_t*) (args[0].ptrValue);
  1007. UBool isNotEOF = false;
  1008. UBool readCharacter = false;
  1009. /* Create an empty set */
  1010. scanset = uset_open(0, -1);
  1011. /* Back up one to get the [ */
  1012. fmt--;
  1013. /* truncate to the width, if specified and alias the target */
  1014. if(info->fWidth >= 0) {
  1015. chLeft = info->fWidth;
  1016. }
  1017. /* parse the scanset from the fmt string */
  1018. *fmtConsumed = uset_applyPattern(scanset, fmt, -1, 0, &status);
  1019. /* verify that the parse was successful */
  1020. if (U_SUCCESS(status)) {
  1021. c=0;
  1022. /* grab characters one at a time and make sure they are in the scanset */
  1023. while(chLeft > 0) {
  1024. if ( ((isNotEOF = ufile_getch32(input, &c))==(UBool)true) && uset_contains(scanset, c) ) {
  1025. readCharacter = true;
  1026. if (!info->fSkipArg) {
  1027. int32_t idx = 0;
  1028. UBool isError = false;
  1029. U16_APPEND(alias, idx, chLeft, c, isError);
  1030. if (isError) {
  1031. break;
  1032. }
  1033. alias += idx;
  1034. }
  1035. chLeft -= (1 + U_IS_SUPPLEMENTARY(c));
  1036. }
  1037. else {
  1038. /* if the character's not in the scanset, break out */
  1039. break;
  1040. }
  1041. }
  1042. /* put the final character we read back on the input */
  1043. if(isNotEOF && chLeft > 0) {
  1044. u_fungetc(c, input);
  1045. }
  1046. }
  1047. uset_close(scanset);
  1048. /* if we didn't match at least 1 character, fail */
  1049. if(!readCharacter)
  1050. return -1;
  1051. /* otherwise, add the terminator */
  1052. else if (!info->fSkipArg) {
  1053. *alias = 0x00;
  1054. }
  1055. /* we converted 1 arg */
  1056. *argConverted = !info->fSkipArg;
  1057. return (info->fWidth >= 0 ? info->fWidth : INT32_MAX) - chLeft;
  1058. }
  1059. /* Use US-ASCII characters only for formatting. Most codepages have
  1060. characters 20-7F from Unicode. Using any other codepage specific
  1061. characters will make it very difficult to format the string on
  1062. non-Unicode machines */
  1063. static const u_scanf_info g_u_scanf_infos[USCANF_NUM_FMT_HANDLERS] = {
  1064. /* 0x20 */
  1065. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1066. UFMT_EMPTY, UFMT_SIMPLE_PERCENT,UFMT_EMPTY, UFMT_EMPTY,
  1067. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1068. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1069. /* 0x30 */
  1070. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1071. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1072. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1073. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1074. /* 0x40 */
  1075. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_UCHAR,
  1076. UFMT_EMPTY, UFMT_SCIENTIFIC, UFMT_EMPTY, UFMT_SCIDBL,
  1077. #ifdef U_USE_OBSOLETE_IO_FORMATTING
  1078. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_UCHAR/*deprecated*/,
  1079. #else
  1080. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1081. #endif
  1082. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1083. /* 0x50 */
  1084. UFMT_PERCENT, UFMT_EMPTY, UFMT_EMPTY, UFMT_USTRING,
  1085. #ifdef U_USE_OBSOLETE_IO_FORMATTING
  1086. UFMT_EMPTY, UFMT_USTRING/*deprecated*/,UFMT_SPELLOUT, UFMT_EMPTY,
  1087. #else
  1088. UFMT_EMPTY, UFMT_EMPTY, UFMT_SPELLOUT, UFMT_EMPTY,
  1089. #endif
  1090. UFMT_HEX, UFMT_EMPTY, UFMT_EMPTY, UFMT_SCANSET,
  1091. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1092. /* 0x60 */
  1093. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_CHAR,
  1094. UFMT_INT, UFMT_SCIENTIFIC, UFMT_DOUBLE, UFMT_SCIDBL,
  1095. UFMT_EMPTY, UFMT_INT, UFMT_EMPTY, UFMT_EMPTY,
  1096. UFMT_EMPTY, UFMT_EMPTY, UFMT_COUNT, UFMT_OCTAL,
  1097. /* 0x70 */
  1098. UFMT_POINTER, UFMT_EMPTY, UFMT_EMPTY, UFMT_STRING,
  1099. UFMT_EMPTY, UFMT_UINT, UFMT_EMPTY, UFMT_EMPTY,
  1100. UFMT_HEX, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1101. UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
  1102. };
  1103. U_CFUNC int32_t
  1104. u_scanf_parse(UFILE *f,
  1105. const char16_t *patternSpecification,
  1106. va_list ap)
  1107. {
  1108. const char16_t *alias;
  1109. int32_t count, converted, argConsumed, cpConsumed;
  1110. uint16_t handlerNum;
  1111. ufmt_args args;
  1112. u_scanf_spec spec;
  1113. ufmt_type_info info;
  1114. u_scanf_handler handler;
  1115. /* alias the pattern */
  1116. alias = patternSpecification;
  1117. /* haven't converted anything yet */
  1118. argConsumed = 0;
  1119. converted = 0;
  1120. cpConsumed = 0;
  1121. /* iterate through the pattern */
  1122. for(;;) {
  1123. /* match any characters up to the next '%' */
  1124. while(*alias != UP_PERCENT && *alias != 0x0000 && u_fgetc(f) == *alias) {
  1125. alias++;
  1126. }
  1127. /* if we aren't at a '%', or if we're at end of string, break*/
  1128. if(*alias != UP_PERCENT || *alias == 0x0000)
  1129. break;
  1130. /* parse the specifier */
  1131. count = u_scanf_parse_spec(alias, &spec);
  1132. /* update the pointer in pattern */
  1133. alias += count;
  1134. handlerNum = (uint16_t)(spec.fInfo.fSpec - USCANF_BASE_FMT_HANDLERS);
  1135. if (handlerNum < USCANF_NUM_FMT_HANDLERS) {
  1136. /* skip the argument, if necessary */
  1137. /* query the info function for argument information */
  1138. info = g_u_scanf_infos[ handlerNum ].info;
  1139. if (info != ufmt_count && u_feof(f)) {
  1140. break;
  1141. }
  1142. else if(spec.fInfo.fSkipArg) {
  1143. args.ptrValue = nullptr;
  1144. }
  1145. else {
  1146. switch(info) {
  1147. case ufmt_count:
  1148. /* set the spec's width to the # of items converted */
  1149. spec.fInfo.fWidth = cpConsumed;
  1150. U_FALLTHROUGH;
  1151. case ufmt_char:
  1152. case ufmt_uchar:
  1153. case ufmt_int:
  1154. case ufmt_string:
  1155. case ufmt_ustring:
  1156. case ufmt_pointer:
  1157. case ufmt_float:
  1158. case ufmt_double:
  1159. args.ptrValue = va_arg(ap, void*);
  1160. break;
  1161. default:
  1162. /* else args is ignored */
  1163. args.ptrValue = nullptr;
  1164. break;
  1165. }
  1166. }
  1167. /* call the handler function */
  1168. handler = g_u_scanf_infos[ handlerNum ].handler;
  1169. if(handler != 0) {
  1170. /* reset count to 1 so that += for alias works. */
  1171. count = 1;
  1172. cpConsumed += (*handler)(f, &spec.fInfo, &args, alias, &count, &argConsumed);
  1173. /* if the handler encountered an error condition, break */
  1174. if(argConsumed < 0) {
  1175. converted = -1;
  1176. break;
  1177. }
  1178. /* add to the # of items converted */
  1179. converted += argConsumed;
  1180. /* update the pointer in pattern */
  1181. alias += count-1;
  1182. }
  1183. /* else do nothing */
  1184. }
  1185. /* else do nothing */
  1186. /* just ignore unknown tags */
  1187. }
  1188. /* return # of items converted */
  1189. return converted;
  1190. }
  1191. #endif /* #if !UCONFIG_NO_FORMATTING */