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

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