home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / graphics / wprintf.c < prev   
Encoding:
C/C++ Source or Header  |  1988-10-28  |  2.2 KB  |  79 lines

  1. /* WPRINTF.C shows how to use vprintf functions to write new versions
  2.  * of printf. Functions illustrated include:
  3.  *      vsprintf        vprintf         vfprintf
  4.  *
  5.  * The vsprintf function is used in the example. The other variations
  6.  * can be used similarly.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <graph.h>
  11. #include <string.h>
  12. #include <stdarg.h>
  13. #include <malloc.h>
  14.  
  15. int wprintf( short row, short col, short clr, long bclr, char *fmt, ... );
  16.  
  17. main()
  18. {
  19.     short fgd = 0;
  20.     long  bgd = 0L;
  21.  
  22.     _clearscreen( _GCLEARSCREEN );
  23.     _outtext( "Color text example:\n\n" );
  24.  
  25.     /* Loop through 8 background colors. */
  26.     for( bgd = 0L; bgd < 8; bgd++ )
  27.     {
  28.         wprintf( (int)bgd + 3, 1, 7, bgd, "Back: %d Fore:", bgd );
  29.  
  30.         /* Loop through 16 foreground colors. */
  31.         for( fgd = 0; fgd < 16; fgd++ )
  32.             wprintf( -1, -1, fgd, -1L, " %2d ", fgd );
  33.     }
  34. }
  35.  
  36. /* Full-screen window version of printf that takes row, column, textcolor,
  37.  * and background color as its first arguments, followed by normal printf
  38.  * format strings (except that \t is not handled). You can specify -1 for
  39.  * any of the first arguments to use the current value. The function returns
  40.  * the number of characters printed, or a negative number for errors.
  41.  */
  42. int wprintf( short row, short col, short clr, long bclr, char *fmt, ... )
  43. {
  44.     struct  rccoord tmppos;
  45.     short   ret, size;
  46.     va_list marker;
  47.     char    *buffer;
  48.  
  49.     /* It's probably safe to use a buffer 512 bytes long or five times
  50.      * longer than the format string.
  51.      */
  52.     size = strlen( fmt );
  53.     size = (size > 512) ? 512 : size * 5;
  54.     if( (buffer = (char *)malloc( size )) == NULL )
  55.         return -1;
  56.  
  57.     /* Set text position. */
  58.     tmppos = _gettextposition();
  59.     if( row < 1 )
  60.         row = tmppos.row;
  61.     if( col < 1 )
  62.         col = tmppos.col;
  63.     _settextposition( row, col );
  64.  
  65.     /* Set foreground and background colors. */
  66.     if( clr >= 0 )
  67.         _settextcolor( clr );
  68.     if( bclr >= 0 )
  69.         _setbkcolor( bclr );
  70.  
  71.     /* Write text to a string and output the string. */
  72.     va_start( marker, fmt );
  73.     ret = vsprintf( buffer, fmt, marker );
  74.     va_end( marker );
  75.     _outtext( buffer );
  76.     free( buffer );
  77.     return ret;
  78. }
  79.