Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

805 lines
22 KiB

  1. /***
  2. *output.c - printf style output to a struct w4io
  3. *
  4. * Copyright (c) 1989-1991, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * This file contains the code that does all the work for the
  8. * printf family of functions. It should not be called directly, only
  9. * by the *printf functions. We don't make any assumtions about the
  10. * sizes of ints, longs, shorts, or long doubles, but if types do overlap, we
  11. * also try to be efficient. We do assume that pointers are the same size
  12. * as either ints or longs.
  13. *
  14. *Revision History:
  15. * 06-01-89 PHG Module created
  16. * 08-28-89 JCR Added cast to get rid of warning (no object changes)
  17. * 02-15-90 GJF Fixed copyright
  18. * 10-03-90 WHB Defined LOCAL(x) to "static x" for local procedures
  19. *
  20. *******************************************************************************/
  21. #if DBG == 1
  22. #include <limits.h>
  23. #include <string.h>
  24. #include <stdarg.h>
  25. #include "wchar.h"
  26. #include "w4io.h"
  27. /* this macro defines a function which is private and as fast as possible: */
  28. /* for example, in C 6.0, it might be static _fastcall <type>. */
  29. #define LOCAL(x) static x // 100390--WHB
  30. #define NOFLOATS // Win 4 doesn't need floating point
  31. /* int/long/short/pointer sizes */
  32. /* the following should be set depending on the sizes of various types */
  33. // FLAT or LARGE model is assumed
  34. #ifdef FLAT
  35. # define LONG_IS_INT 1 /* 1 means long is same size as int */
  36. # define SHORT_IS_INT 0 /* 1 means short is same size as int */
  37. # define PTR_IS_INT 1 /* 1 means ptr is same size as int */
  38. # define PTR_IS_LONG 0 /* 1 means ptr is same size as long */
  39. #else // LARGE model
  40. # define LONG_IS_INT 0 /* 1 means long is same size as int */
  41. # define SHORT_IS_INT 1 /* 1 means short is same size as int */
  42. # define PTR_IS_INT 0 /* 1 means ptr is same size as int */
  43. # define PTR_IS_LONG 1 /* 1 means ptr is same size as long */
  44. #endif
  45. #define LONGDOUBLE_IS_DOUBLE 0 /* 1 means long double is same as double */
  46. #if LONG_IS_INT
  47. #define get_long_arg(x) (long)get_int_arg(x)
  48. #endif
  49. #if defined(_WIN64)
  50. #define get_ptr_arg(x) (void *)get_int64_arg(x)
  51. #elif PTR_IS_INT
  52. #define get_ptr_arg(x) (void *)get_int_arg(x)
  53. #elif PTR_IS_LONG
  54. #define get_ptr_arg(x) (void *)get_long_arg(x)
  55. #else
  56. #error Size of pointer must be same as size of int or long
  57. #endif
  58. #ifndef NOFLOATS
  59. /* These are "fake" double and long doubles to fool the compiler,
  60. so we don't drag in floating point. */
  61. typedef struct {
  62. char x[sizeof(double)];
  63. } DOUBLE;
  64. typedef struct {
  65. char x[sizeof(long double)];
  66. } LONGDOUBLE;
  67. #endif
  68. /* CONSTANTS */
  69. //#define BUFFERSIZE CVTBUFSIZE /* buffer size for maximum double conv */
  70. #define BUFFERSIZE 20
  71. /* flag definitions */
  72. #define FL_SIGN 0x0001 /* put plus or minus in front */
  73. #define FL_SIGNSP 0x0002 /* put space or minus in front */
  74. #define FL_LEFT 0x0004 /* left justify */
  75. #define FL_LEADZERO 0x0008 /* pad with leading zeros */
  76. #define FL_LONG 0x0010 /* long value given */
  77. #define FL_SHORT 0x0020 /* short value given */
  78. #define FL_SIGNED 0x0040 /* signed data given */
  79. #define FL_ALTERNATE 0x0080 /* alternate form requested */
  80. #define FL_NEGATIVE 0x0100 /* value is negative */
  81. #define FL_FORCEOCTAL 0x0200 /* force leading '0' for octals */
  82. #define FL_LONGDOUBLE 0x0400 /* long double value given */
  83. #define FL_WIDE 0x0800 /* wide character/string given */
  84. /* state definitions */
  85. enum STATE {
  86. ST_NORMAL, /* normal state; outputting literal chars */
  87. ST_PERCENT, /* just read '%' */
  88. ST_FLAG, /* just read flag character */
  89. ST_WIDTH, /* just read width specifier */
  90. ST_DOT, /* just read '.' */
  91. ST_PRECIS, /* just read precision specifier */
  92. ST_SIZE, /* just read size specifier */
  93. ST_TYPE /* just read type specifier */
  94. };
  95. #define NUMSTATES (ST_TYPE + 1)
  96. /* character type values */
  97. enum CHARTYPE {
  98. CH_OTHER, /* character with no special meaning */
  99. CH_PERCENT, /* '%' */
  100. CH_DOT, /* '.' */
  101. CH_STAR, /* '*' */
  102. CH_ZERO, /* '0' */
  103. CH_DIGIT, /* '1'..'9' */
  104. CH_FLAG, /* ' ', '+', '-', '#' */
  105. CH_SIZE, /* 'h', 'l', 'L', 'N', 'F' */
  106. CH_TYPE /* type specifying character */
  107. };
  108. /* static data (read only, since we are re-entrant) */
  109. char *nullstring = "(null)"; /* string to print on null ptr */
  110. /* The state table. This table is actually two tables combined into one. */
  111. /* The lower nybble of each byte gives the character class of any */
  112. /* character; while the uper nybble of the byte gives the next state */
  113. /* to enter. See the macros below the table for details. */
  114. /* */
  115. /* The table is generated by maketab.c -- use the maketab program to make */
  116. /* changes. */
  117. static char lookuptable[] = {
  118. 0x06, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00,
  119. 0x10, 0x00, 0x03, 0x06, 0x00, 0x06, 0x02, 0x10,
  120. 0x04, 0x45, 0x45, 0x45, 0x05, 0x05, 0x05, 0x05,
  121. 0x05, 0x35, 0x30, 0x00, 0x50, 0x00, 0x00, 0x00,
  122. 0x00, 0x20, 0x28, 0x38, 0x50, 0x58, 0x07, 0x08,
  123. 0x00, 0x30, 0x30, 0x30, 0x57, 0x50, 0x07, 0x00,
  124. 0x00, 0x20, 0x20, 0x08, 0x00, 0x00, 0x00, 0x00,
  125. 0x08, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x00,
  126. 0x00, 0x70, 0x70, 0x78, 0x78, 0x78, 0x78, 0x08,
  127. 0x07, 0x08, 0x00, 0x00, 0x07, 0x00, 0x08, 0x08,
  128. 0x08, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00, 0x07,
  129. 0x08
  130. };
  131. #define find_char_class(c) \
  132. ((c) < ' ' || (c) > 'x' ? \
  133. CH_OTHER \
  134. : \
  135. lookuptable[(c)-' '] & 0xF)
  136. #define find_next_state(class, state) \
  137. (lookuptable[(class) * NUMSTATES + (state)] >> 4)
  138. #if !LONG_IS_INT
  139. LOCAL(long) get_long_arg(va_list *pargptr);
  140. #endif
  141. LOCAL(int) get_int_arg(va_list *pargptr);
  142. LOCAL(void) writestring(char *string,
  143. int len,
  144. struct w4io *f,
  145. int *pcchwritten,
  146. int fwide);
  147. #ifndef NOFLOATS
  148. /* extern float convert routines */
  149. typedef int (* PFI)();
  150. extern PFI _cfltcvt_tab[5];
  151. #define _cfltcvt(a,b,c,d,e) (*_cfltcvt_tab[0])(a,b,c,d,e)
  152. #define _cropzeros(a) (*_cfltcvt_tab[1])(a)
  153. #define _fassign(a,b,c) (*_cfltcvt_tab[2])(a,b,c)
  154. #define _forcdecpt(a) (*_cfltcvt_tab[3])(a)
  155. #define _positive(a) (*_cfltcvt_tab[4])(a)
  156. #define _cldcvt(a,b,c,d,e) (*_cfltcvt_tab[5])(a,b,c,d,e)
  157. #endif
  158. /***
  159. *int w4iooutput(f, format, argptr)
  160. *
  161. *Purpose:
  162. * Output performs printf style output onto a stream. It is called by
  163. * printf/fprintf/sprintf/vprintf/vfprintf/vsprintf to so the dirty
  164. * work. In multi-thread situations, w4iooutput assumes that the given
  165. * stream is already locked.
  166. *
  167. * Algorithm:
  168. * The format string is parsed by using a finite state automaton
  169. * based on the current state and the current character read from
  170. * the format string. Thus, looping is on a per-character basis,
  171. * not a per conversion specifier basis. Once the format specififying
  172. * character is read, output is performed.
  173. *
  174. *Entry:
  175. * struct w4io *f - stream for output
  176. * char *format - printf style format string
  177. * va_list argptr - pointer to list of subsidiary arguments
  178. *
  179. *Exit:
  180. * Returns the number of characters written, or -1 if an output error
  181. * occurs.
  182. *
  183. *Exceptions:
  184. *
  185. *******************************************************************************/
  186. int _cdecl w4iooutput(struct w4io *f, const char *format, va_list argptr)
  187. {
  188. int hexadd; /* offset to add to number to get 'a'..'f' */
  189. char ch; /* character just read */
  190. wchar_t wc; /* wide character temp */
  191. wchar_t *pwc; /* wide character temp pointer */
  192. int flags; /* flag word -- see #defines above for flag values */
  193. enum STATE state; /* current state */
  194. enum CHARTYPE chclass; /* class of current character */
  195. int radix; /* current conversion radix */
  196. int charsout; /* characters currently written so far, -1 = IO error */
  197. int fldwidth; /* selected field with -- 0 means default */
  198. int fwide;
  199. int precision; /* selected precision -- -1 means default */
  200. char prefix[2]; /* numeric prefix -- up to two characters */
  201. int prefixlen; /* length of prefix -- 0 means no prefix */
  202. int capexp; /* non-zero = 'E' exponent signifiet, zero = 'e' */
  203. int no_output; /* non-zero = prodcue no output for this specifier */
  204. char *text; /* pointer text to be printed, not zero terminated */
  205. int textlen; /* length of the text to be printed */
  206. char buffer[BUFFERSIZE]; /* buffer for conversions */
  207. charsout = 0; /* no characters written yet */
  208. state = ST_NORMAL; /* starting state */
  209. /* main loop -- loop while format character exist and no I/O errors */
  210. while ((ch = *format++) != '\0' && charsout >= 0) {
  211. chclass = find_char_class(ch); /* find character class */
  212. state = find_next_state(chclass, state); /* find next state */
  213. /* execute code for each state */
  214. switch (state) {
  215. case ST_NORMAL:
  216. /* normal state -- just write character */
  217. f->writechar(ch, 1, f, &charsout);
  218. break;
  219. case ST_PERCENT:
  220. /* set default value of conversion parameters */
  221. prefixlen = fldwidth = no_output = capexp = 0;
  222. flags = 0;
  223. precision = -1;
  224. fwide = 0;
  225. break;
  226. case ST_FLAG:
  227. /* set flag based on which flag character */
  228. switch (ch) {
  229. case '-':
  230. flags |= FL_LEFT; /* '-' => left justify */
  231. break;
  232. case '+':
  233. flags |= FL_SIGN; /* '+' => force sign indicator */
  234. break;
  235. case ' ':
  236. flags |= FL_SIGNSP; /* ' ' => force sign or space */
  237. break;
  238. case '#':
  239. flags |= FL_ALTERNATE; /* '#' => alternate form */
  240. break;
  241. case '0':
  242. flags |= FL_LEADZERO; /* '0' => pad with leading zeros */
  243. break;
  244. }
  245. break;
  246. case ST_WIDTH:
  247. /* update width value */
  248. if (ch == '*') {
  249. /* get width from arg list */
  250. fldwidth = get_int_arg(&argptr);
  251. if (fldwidth < 0) {
  252. /* ANSI says neg fld width means '-' flag and pos width */
  253. flags |= FL_LEFT;
  254. fldwidth = -fldwidth;
  255. }
  256. }
  257. else {
  258. /* add digit to current field width */
  259. fldwidth = fldwidth * 10 + (ch - '0');
  260. }
  261. break;
  262. case ST_DOT:
  263. /* zero the precision, since dot with no number means 0
  264. not default, according to ANSI */
  265. precision = 0;
  266. break;
  267. case ST_PRECIS:
  268. /* update precison value */
  269. if (ch == '*') {
  270. /* get precision from arg list */
  271. precision = get_int_arg(&argptr);
  272. if (precision < 0)
  273. precision = -1; /* neg precision means default */
  274. }
  275. else {
  276. /* add digit to current precision */
  277. precision = precision * 10 + (ch - '0');
  278. }
  279. break;
  280. case ST_SIZE:
  281. /* just read a size specifier, set the flags based on it */
  282. switch (ch) {
  283. #if !LONG_IS_INT
  284. case 'l':
  285. flags |= FL_LONG; /* 'l' => long int */
  286. break;
  287. #endif
  288. #if !LONGDOUBLE_IS_DOUBLE
  289. case 'L':
  290. flags |= FL_LONGDOUBLE; /* 'L' => long double */
  291. break;
  292. #endif
  293. #if !SHORT_IS_INT
  294. case 'h':
  295. flags |= FL_SHORT; /* 'h' => short int */
  296. break;
  297. #endif
  298. case 'w':
  299. flags |= FL_WIDE; /* 'w' => wide character */
  300. break;
  301. }
  302. break;
  303. case ST_TYPE:
  304. /* we have finally read the actual type character, so we */
  305. /* now format and "print" the output. We use a big switch */
  306. /* statement that sets 'text' to point to the text that should */
  307. /* be printed, and 'textlen' to the length of this text. */
  308. /* Common code later on takes care of justifying it and */
  309. /* other miscellaneous chores. Note that cases share code, */
  310. /* in particular, all integer formatting is doen in one place. */
  311. /* Look at those funky goto statements! */
  312. switch (ch) {
  313. case 'c': {
  314. /* print a single character specified by int argument */
  315. wc = (wchar_t) get_int_arg(&argptr); /* get char to print */
  316. * (wchar_t *) buffer = wc;
  317. text = buffer;
  318. textlen = 1; /* print just a single character */
  319. }
  320. break;
  321. case 'S': {
  322. /* print a Counted String
  323. int i;
  324. char *p; /* temps */
  325. struct string {
  326. short Length;
  327. short MaximumLength;
  328. char *Buffer;
  329. } *pstr;
  330. pstr = get_ptr_arg(&argptr);
  331. if (pstr == NULL || pstr->Buffer == NULL) {
  332. /* null ptr passed, use special string */
  333. text = nullstring;
  334. textlen = strlen(text);
  335. flags &= ~FL_WIDE;
  336. } else {
  337. text = pstr->Buffer;
  338. textlen = pstr->Length;
  339. }
  340. }
  341. break;
  342. case 's': {
  343. /* print a string -- */
  344. /* ANSI rules on how much of string to print: */
  345. /* all if precision is default, */
  346. /* min(precision, length) if precision given. */
  347. /* prints '(null)' if a null string is passed */
  348. int i;
  349. char *p; /* temps */
  350. text = get_ptr_arg(&argptr);
  351. if (text == NULL) {
  352. /* null ptr passed, use special string */
  353. text = nullstring;
  354. flags &= ~FL_WIDE;
  355. }
  356. /* At this point it is tempting to use strlen(), but */
  357. /* if a precision is specified, we're not allowed to */
  358. /* scan past there, because there might be no null */
  359. /* at all. Thus, we must do our own scan. */
  360. i = (precision == -1) ? INT_MAX : precision;
  361. /* scan for null upto i characters */
  362. if (flags & FL_WIDE) {
  363. pwc = (wchar_t *) text;
  364. while (i-- && (wc = *pwc) && (wc & 0x00ff)) {
  365. ++pwc;
  366. if (wc & 0xff00) { // if high byte set,
  367. break; // error will be indicated
  368. }
  369. }
  370. textlen = pwc - (wchar_t *) text; /* length of string */
  371. } else {
  372. p = text;
  373. while (i-- && *p) {
  374. ++p;
  375. }
  376. textlen = p - text; /* length of the string */
  377. }
  378. }
  379. break;
  380. case 'n': {
  381. /* write count of characters seen so far into */
  382. /* short/int/long thru ptr read from args */
  383. void *p; /* temp */
  384. p = get_ptr_arg(&argptr);
  385. /* store chars out into short/long/int depending on flags */
  386. #if !LONG_IS_INT
  387. if (flags & FL_LONG)
  388. *(long *)p = charsout;
  389. else
  390. #endif
  391. #if !SHORT_IS_INT
  392. if (flags & FL_SHORT)
  393. *(short *)p = (short) charsout;
  394. else
  395. #endif
  396. *(int *)p = charsout;
  397. no_output = 1; /* force no output */
  398. }
  399. break;
  400. #ifndef NOFLOATS
  401. case 'E':
  402. case 'G':
  403. capexp = 1; /* capitalize exponent */
  404. ch += 'a' - 'A'; /* convert format char to lower */
  405. /* DROP THROUGH */
  406. case 'e':
  407. case 'f':
  408. case 'g': {
  409. /* floating point conversion -- we call cfltcvt routines */
  410. /* to do the work for us. */
  411. flags |= FL_SIGNED; /* floating point is signed conversion */
  412. text = buffer; /* put result in buffer */
  413. flags &= ~FL_WIDE; /* 8 bit string */
  414. /* compute the precision value */
  415. if (precision < 0)
  416. precision = 6; /* default precision: 6 */
  417. else if (precision == 0 && ch == 'g')
  418. precision = 1; /* ANSI specified */
  419. #if !LONGDOUBLE_IS_DOUBLE
  420. /* do the conversion */
  421. if (flags & FL_LONGDOUBLE) {
  422. _cldcvt(argptr, text, ch, precision, capexp);
  423. va_arg(argptr, LONGDOUBLE);
  424. }
  425. else
  426. #endif
  427. {
  428. _cfltcvt(argptr, text, ch, precision, capexp);
  429. va_arg(argptr, DOUBLE);
  430. }
  431. /* '#' and precision == 0 means force a decimal point */
  432. if ((flags & FL_ALTERNATE) && precision == 0)
  433. _forcdecpt(text);
  434. /* 'g' format means crop zero unless '#' given */
  435. if (ch == 'g' && !(flags & FL_ALTERNATE))
  436. _cropzeros(text);
  437. /* check if result was negative, save '-' for later */
  438. /* and point to positive part (this is for '0' padding) */
  439. if (*text == '-') {
  440. flags |= FL_NEGATIVE;
  441. ++text;
  442. }
  443. textlen = strlen(text); /* compute length of text */
  444. }
  445. break;
  446. #endif // NOFLOATS
  447. case 'd':
  448. case 'i':
  449. /* signed decimal output */
  450. flags |= FL_SIGNED;
  451. radix = 10;
  452. goto COMMON_INT;
  453. case 'u':
  454. radix = 10;
  455. goto COMMON_INT;
  456. case 'p':
  457. /* write a pointer -- this is like an integer or long */
  458. /* except we force precision to pad with zeros and */
  459. /* output in big hex. */
  460. precision = 2 * sizeof(void *); /* number of hex digits needed */
  461. #if !PTR_IS_INT
  462. flags |= FL_LONG; /* assume we're converting a long */
  463. #endif
  464. /* DROP THROUGH to hex formatting */
  465. case 'C':
  466. case 'X':
  467. /* unsigned upper hex output */
  468. hexadd = 'A' - '9' - 1; /* set hexadd for uppercase hex */
  469. goto COMMON_HEX;
  470. case 'x':
  471. /* unsigned lower hex output */
  472. hexadd = 'a' - '9' - 1; /* set hexadd for lowercase hex */
  473. /* DROP THROUGH TO COMMON_HEX */
  474. COMMON_HEX:
  475. radix = 16;
  476. if (flags & FL_ALTERNATE) {
  477. /* alternate form means '0x' prefix */
  478. prefix[0] = '0';
  479. prefix[1] = (char)('x' - 'a' + '9' + 1 + hexadd); /* 'x' or 'X' */
  480. prefixlen = 2;
  481. }
  482. goto COMMON_INT;
  483. case 'o':
  484. /* unsigned octal output */
  485. radix = 8;
  486. if (flags & FL_ALTERNATE) {
  487. /* alternate form means force a leading 0 */
  488. flags |= FL_FORCEOCTAL;
  489. }
  490. /* DROP THROUGH to COMMON_INT */
  491. COMMON_INT: {
  492. /* This is the general integer formatting routine. */
  493. /* Basically, we get an argument, make it positive */
  494. /* if necessary, and convert it according to the */
  495. /* correct radix, setting text and textlen */
  496. /* appropriately. */
  497. unsigned long number; /* number to convert */
  498. int digit; /* ascii value of digit */
  499. long l; /* temp long value */
  500. /* 1. read argument into l, sign extend as needed */
  501. #if !LONG_IS_INT
  502. if (flags & FL_LONG)
  503. l = get_long_arg(&argptr);
  504. else
  505. #endif
  506. #if !SHORT_IS_INT
  507. if (flags & FL_SHORT) {
  508. if (flags & FL_SIGNED)
  509. l = (short) get_int_arg(&argptr); /* sign extend */
  510. else
  511. l = (unsigned short) get_int_arg(&argptr); /* zero-extend*/
  512. }
  513. else
  514. #endif
  515. {
  516. if (flags & FL_SIGNED)
  517. l = get_int_arg(&argptr); /* sign extend */
  518. else
  519. l = (unsigned int) get_int_arg(&argptr); /* zero-extend*/
  520. }
  521. /* 2. check for negative; copy into number */
  522. if ( (flags & FL_SIGNED) && l < 0) {
  523. number = -l;
  524. flags |= FL_NEGATIVE; /* remember negative sign */
  525. }
  526. else {
  527. number = l;
  528. }
  529. /* 3. check precision value for default; non-default */
  530. /* turns off 0 flag, according to ANSI. */
  531. if (precision < 0)
  532. precision = 1; /* default precision */
  533. else
  534. flags &= ~FL_LEADZERO;
  535. /* 4. Check if data is 0; if so, turn off hex prefix */
  536. if (number == 0)
  537. prefixlen = 0;
  538. /* 5. Convert data to ASCII -- note if precision is zero */
  539. /* and number is zero, we get no digits at all. */
  540. text = &buffer[BUFFERSIZE-1]; // last digit at end of buffer
  541. flags &= ~FL_WIDE; // 8 bit characters
  542. while (precision-- > 0 || number != 0) {
  543. digit = (int)(number % radix) + '0';
  544. number /= radix; /* reduce number */
  545. if (digit > '9') {
  546. /* a hex digit, make it a letter */
  547. digit += hexadd;
  548. }
  549. *text-- = (char)digit; /* store the digit */
  550. }
  551. textlen = (char *)&buffer[BUFFERSIZE-1] - text; /* compute length of number */
  552. ++text; /* text points to first digit now */
  553. /* 6. Force a leading zero if FORCEOCTAL flag set */
  554. if ((flags & FL_FORCEOCTAL) && (text[0] != '0' || textlen == 0)) {
  555. *--text = '0';
  556. ++textlen; /* add a zero */
  557. }
  558. }
  559. break;
  560. }
  561. /* At this point, we have done the specific conversion, and */
  562. /* 'text' points to text to print; 'textlen' is length. Now we */
  563. /* justify it, put on prefixes, leading zeros, and then */
  564. /* print it. */
  565. if (!no_output) {
  566. int padding; /* amount of padding, negative means zero */
  567. if (flags & FL_SIGNED) {
  568. if (flags & FL_NEGATIVE) {
  569. /* prefix is a '-' */
  570. prefix[0] = '-';
  571. prefixlen = 1;
  572. }
  573. else if (flags & FL_SIGN) {
  574. /* prefix is '+' */
  575. prefix[0] = '+';
  576. prefixlen = 1;
  577. }
  578. else if (flags & FL_SIGNSP) {
  579. /* prefix is ' ' */
  580. prefix[0] = ' ';
  581. prefixlen = 1;
  582. }
  583. }
  584. /* calculate amount of padding -- might be negative, */
  585. /* but this will just mean zero */
  586. padding = fldwidth - textlen - prefixlen;
  587. /* put out the padding, prefix, and text, in the correct order */
  588. if (!(flags & (FL_LEFT | FL_LEADZERO))) {
  589. /* pad on left with blanks */
  590. f->writechar(' ', padding, f, &charsout);
  591. }
  592. /* write prefix */
  593. writestring(prefix, prefixlen, f, &charsout, 0);
  594. if ((flags & FL_LEADZERO) && !(flags & FL_LEFT)) {
  595. /* write leading zeros */
  596. f->writechar('0', padding, f, &charsout);
  597. }
  598. /* write text */
  599. writestring(text, textlen, f, &charsout, flags & FL_WIDE);
  600. if (flags & FL_LEFT) {
  601. /* pad on right with blanks */
  602. f->writechar(' ', padding, f, &charsout);
  603. }
  604. /* we're done! */
  605. }
  606. break;
  607. }
  608. }
  609. return charsout; /* return value = number of characters written */
  610. }
  611. /***
  612. *int get_int_arg(va_list pargptr)
  613. *
  614. *Purpose:
  615. * Gets an int argument off the given argument list and updates *pargptr.
  616. *
  617. *Entry:
  618. * va_list pargptr - pointer to argument list; updated by function
  619. *
  620. *Exit:
  621. * Returns the integer argument read from the argument list.
  622. *
  623. *Exceptions:
  624. *
  625. *******************************************************************************/
  626. LOCAL(int) get_int_arg(va_list *pargptr)
  627. {
  628. return va_arg(*pargptr, int);
  629. }
  630. /***
  631. *long get_long_arg(va_list pargptr)
  632. *
  633. *Purpose:
  634. * Gets an long argument off the given argument list and updates pargptr.
  635. *
  636. *Entry:
  637. * va_list pargptr - pointer to argument list; updated by function
  638. *
  639. *Exit:
  640. * Returns the long argument read from the argument list.
  641. *
  642. *Exceptions:
  643. *
  644. *******************************************************************************/
  645. #if !LONG_IS_INT
  646. LOCAL(long) get_long_arg(va_list *pargptr)
  647. {
  648. return va_arg(*pargptr, long);
  649. }
  650. #endif
  651. /***
  652. *void writestring(char *string, int len, struct w4io *f, int *pcchwritten, int fwide)
  653. *
  654. *Purpose:
  655. * Writes a string of the given length to the given file. If no error occurs,
  656. * then *pcchwritten is incremented by len; otherwise, *pcchwritten is set
  657. * to -1. If len is negative, it is treated as zero.
  658. *
  659. *Entry:
  660. * char *string - string to write (NOT null-terminated)
  661. * int len - length of string
  662. * struct w4io *f - file to write to
  663. * int *pcchwritten - pointer to integer to update with total chars written
  664. * int fwide - wide character flag
  665. *
  666. *Exit:
  667. * No return value.
  668. *
  669. *Exceptions:
  670. *
  671. *******************************************************************************/
  672. LOCAL(void) writestring(
  673. char *string,
  674. int len,
  675. struct w4io *f,
  676. int *pcchwritten,
  677. int fwide)
  678. {
  679. wchar_t *pwc;
  680. //printf("string: str=%.*s, len=%d, cch=%d, f=%d\n", len, string, len, *pcchwritten, fwide);
  681. if (fwide) {
  682. pwc = (wchar_t *) string;
  683. while (len-- > 0) {
  684. if (*pwc & 0xff00) {
  685. f->writechar('^', 1, f, pcchwritten);
  686. }
  687. f->writechar((char) *pwc++, 1, f, pcchwritten);
  688. }
  689. } else {
  690. while (len-- > 0) {
  691. f->writechar(*string++, 1, f, pcchwritten);
  692. }
  693. }
  694. }
  695. #endif // DBG == 1