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

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