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.

169 lines
4.5 KiB

  1. /***
  2. *swprintf.c - print formatted to string
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines swprintf() and _snwprintf() - print formatted data to string
  8. *
  9. *Revision History:
  10. * 05-16-92 KRS Created from sprintf.c
  11. * 02-18-93 SRW Make FILE a local and remove lock usage.
  12. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  13. * 02-07-94 CFW POSIXify.
  14. * 09-05-94 SKS Change "#ifdef" inside comments to "*ifdef" to avoid
  15. * problems with CRTL source release process.
  16. * 02-06-94 CFW assert -> _ASSERTE.
  17. * 01-06-99 GJF Changes for 64-bit size_t.
  18. * 03-16-00 GB Added _scwprintf()
  19. *
  20. *******************************************************************************/
  21. #ifndef _POSIX_
  22. #include <cruntime.h>
  23. #include <stdio.h>
  24. #include <wchar.h>
  25. #include <dbgint.h>
  26. #include <stdarg.h>
  27. #include <internal.h>
  28. #include <limits.h>
  29. #include <mtdll.h>
  30. #define MAXSTR INT_MAX
  31. /***
  32. *ifndef _COUNT_
  33. *int swprintf(string, format, ...) - print formatted data to string
  34. *else
  35. *int _snwprintf(string, cnt, format, ...) - print formatted data to string
  36. *endif
  37. *
  38. *Purpose:
  39. * Prints formatted data to the using the format string to
  40. * format data and getting as many arguments as called for
  41. * Sets up a FILE so file i/o operations can be used, make
  42. * string look like a huge buffer to it, but _flsbuf will
  43. * refuse to flush it if it fills up. Appends '\0' to make
  44. * it a true string. _output does the real work here
  45. *
  46. * Allocate the 'fake' _iob[] entry statically instead of on
  47. * the stack so that other routines can assume that _iob[]
  48. * entries are in are in DGROUP and, thus, are near.
  49. *
  50. *ifdef _COUNT_
  51. * The _snwprintf() flavor takes a count argument that is
  52. * the max number of wide characters that should be written to the
  53. * user's buffer.
  54. *endif
  55. *
  56. * Multi-thread: (1) Since there is no stream, this routine must
  57. * never try to get the stream lock (i.e., there is no stream
  58. * lock either). (2) Also, since there is only one statically
  59. * allocated 'fake' iob, we must lock/unlock to prevent collisions.
  60. *
  61. *Entry:
  62. * wchar_t *string - pointer to place to put output
  63. *ifdef _COUNT_
  64. * size_t count - max number of wide characters to put in buffer
  65. *endif
  66. * wchar_t *format - format string to control data format/number
  67. * of arguments followed by list of arguments, number and type
  68. * controlled by format string
  69. *
  70. *Exit:
  71. * returns number of wide characters printed
  72. *
  73. *Exceptions:
  74. *
  75. *******************************************************************************/
  76. #ifndef _COUNT_
  77. int __cdecl swprintf (
  78. wchar_t *string,
  79. const wchar_t *format,
  80. ...
  81. )
  82. #else
  83. int __cdecl _snwprintf (
  84. wchar_t *string,
  85. size_t count,
  86. const wchar_t *format,
  87. ...
  88. )
  89. #endif
  90. {
  91. FILE str;
  92. REG1 FILE *outfile = &str;
  93. va_list arglist;
  94. REG2 int retval;
  95. va_start(arglist, format);
  96. _ASSERTE(string != NULL);
  97. _ASSERTE(format != NULL);
  98. outfile->_flag = _IOWRT|_IOSTRG;
  99. outfile->_ptr = outfile->_base = (char *) string;
  100. #ifndef _COUNT_
  101. outfile->_cnt = MAXSTR;
  102. #else
  103. outfile->_cnt = (int)(count*sizeof(wchar_t));
  104. #endif
  105. retval = _woutput(outfile,format,arglist);
  106. _putc_lk('\0',outfile); /* no-lock version */
  107. _putc_lk('\0',outfile); /* 2nd null byte for wchar_t version */
  108. return(retval);
  109. }
  110. /***
  111. * _scwprintf() - counts the number of character needed to print the formatted
  112. * data
  113. *
  114. *Purpose:
  115. * Counts the number of characters in the fotmatted data.
  116. *
  117. *Entry:
  118. * wchar_t *format - format string to control data format/number
  119. * of arguments followed by list of arguments, number and type
  120. * controlled by format string
  121. *
  122. *Exit:
  123. * returns number of characters needed to print formatted data.
  124. *
  125. *Exceptions:
  126. *
  127. *******************************************************************************/
  128. #ifndef _COUNT_
  129. int __cdecl _scwprintf (
  130. const wchar_t *format,
  131. ...
  132. )
  133. {
  134. FILE str;
  135. REG1 FILE *outfile = &str;
  136. va_list arglist;
  137. REG2 int retval;
  138. va_start(arglist, format);
  139. _ASSERTE(format != NULL);
  140. outfile->_cnt = MAXSTR;
  141. outfile->_flag = _IOWRT|_IOSTRG;
  142. outfile->_ptr = outfile->_base = NULL;
  143. retval = _woutput(outfile,format,arglist);
  144. return(retval);
  145. }
  146. #endif
  147. #endif /* _POSIX_ */