home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / vswprint.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  110 lines

  1. /***
  2. *vswprint.c - print formatted data into a string from var arg list
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines vswprintf() and _vsnwprintf() - print formatted output to
  8. *       a string, get the data from an argument ptr instead of explicit
  9. *       arguments.
  10. *
  11. *******************************************************************************/
  12.  
  13.  
  14. #include <cruntime.h>
  15. #include <stdio.h>
  16. #include <wchar.h>
  17. #include <dbgint.h>
  18. #include <stdarg.h>
  19. #include <internal.h>
  20. #include <limits.h>
  21. #include <mtdll.h>
  22.  
  23. #define MAXSTR INT_MAX
  24.  
  25.  
  26. /***
  27. *ifndef _COUNT_
  28. *int vswprintf(string, format, ap) - print formatted data to string from arg ptr
  29. *else
  30. *int _vsnwprintf(string, format, ap) - print formatted data to string from arg ptr
  31. *endif
  32. *
  33. *Purpose:
  34. *       Prints formatted data, but to a string and gets data from an argument
  35. *       pointer.
  36. *       Sets up a FILE so file i/o operations can be used, make string look
  37. *       like a huge buffer to it, but _flsbuf will refuse to flush it if it
  38. *       fills up. Appends '\0' to make it a true string.
  39. *
  40. *       Allocate the 'fake' _iob[] entryit statically instead of on
  41. *       the stack so that other routines can assume that _iob[] entries are in
  42. *       are in DGROUP and, thus, are near.
  43. *
  44. *ifdef _COUNT_
  45. *       The _vsnwprintf() flavor takes a count argument that is
  46. *       the max number of bytes that should be written to the
  47. *       user's buffer.
  48. *endif
  49. *
  50. *       Multi-thread: (1) Since there is no stream, this routine must never try
  51. *       to get the stream lock (i.e., there is no stream lock either).  (2)
  52. *       Also, since there is only one staticly allocated 'fake' iob, we must
  53. *       lock/unlock to prevent collisions.
  54. *
  55. *Entry:
  56. *       wchar_t *string - place to put destination string
  57. *ifdef _COUNT_
  58. *       size_t count - max number of bytes to put in buffer
  59. *endif
  60. *       wchar_t *format - format string, describes format of data
  61. *       va_list ap - varargs argument pointer
  62. *
  63. *Exit:
  64. *       returns number of wide characters in string
  65. *
  66. *Exceptions:
  67. *
  68. *******************************************************************************/
  69.  
  70. #ifndef _COUNT_
  71.  
  72. int __cdecl vswprintf (
  73.         wchar_t *string,
  74.         const wchar_t *format,
  75.         va_list ap
  76.         )
  77. #else  /* _COUNT_ */
  78.  
  79. int __cdecl _vsnwprintf (
  80.         wchar_t *string,
  81.         size_t count,
  82.         const wchar_t *format,
  83.         va_list ap
  84.         )
  85. #endif  /* _COUNT_ */
  86.  
  87. {
  88.         FILE str;
  89.         REG1 FILE *outfile = &str;
  90.         REG2 int retval;
  91.  
  92.         _ASSERTE(string != NULL);
  93.         _ASSERTE(format != NULL);
  94.  
  95.         outfile->_flag = _IOWRT|_IOSTRG;
  96.         outfile->_ptr = outfile->_base = (char *) string;
  97. #ifndef _COUNT_
  98.         outfile->_cnt = MAXSTR;
  99. #else  /* _COUNT_ */
  100.         outfile->_cnt = count*sizeof(wchar_t);
  101. #endif  /* _COUNT_ */
  102.  
  103.         retval = _woutput(outfile,format,ap );
  104.         _putc_lk('\0',outfile);     /* no-lock version */
  105.         _putc_lk('\0',outfile);     /* 2nd byte for wide char version */
  106.  
  107.         return(retval);
  108. }
  109.  
  110.