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

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