home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_08 / 2n08054a < prev    next >
Text File  |  1991-06-10  |  8KB  |  173 lines

  1. /*==============================start==================================*/
  2. /*  aprintf():  printf()-like function that provides attribute control;*/
  3. /*  useful for both mono and color monitors in textmode; provides for  */
  4. /*  visibility, underscore, reverse video, blinking, intensity and     */
  5. /*  color control; requires DEVICE=ANSI.SYS in CONFIG.SYS; suitable    */
  6. /*  for use with any Microsoft (MSC) or Borland (BC) C compiler,       */
  7. /*  and for any memory model (T,S,M,C,L,H); software adapted from the  */
  8. /*  book ENCYCLOPEDIA C, by R.Radcliffe (Sybex/1991), Pg 951-952.      */
  9. /*=====================================================================*/
  10. #include <stdarg.h>                 /* va_list, va_start()             */
  11. #include <stdio.h>                  /* printf(), scanf(), vsprintf()   */
  12. #include <string.h>                 /* strcat(), strcpy(), strlen()    */
  13. /*-------------------------custom prototypes---------------------------*/
  14. int aprintf(unsigned char, const char *, ...);
  15.  
  16. /*---------helpful defined items/ perhaps create a header file---------*/
  17. #define BLINKON         0x80u
  18.  
  19. #define BACK_BLACK      0x00u
  20. #define BACK_BLUE       0x10u
  21. #define BACK_GREEN      0x20u
  22. #define BACK_CYAN       0x30u
  23. #define BACK_RED        0x40u
  24. #define BACK_MAGENTA    0x50u
  25. #define BACK_YELLOW     0x60u
  26. #define BACK_WHITE      0x70u
  27.  
  28. #define BOLDON          0x08u
  29.  
  30. #define FORE_BLACK      0x00u
  31. #define FORE_BLUE       0x01u
  32. #define FORE_GREEN      0x02u
  33. #define FORE_CYAN       0x03u
  34. #define FORE_RED        0x04u
  35. #define FORE_MAGENTA    0x05u
  36. #define FORE_YELLOW     0x06u
  37. #define FORE_WHITE      0x07u
  38.  
  39. #define INVISIBLE       0x00u
  40. #define UNDERLINE       0x01u
  41. #define NORMAL          0x07u
  42. #define REVERSE         0x70u
  43.  
  44. #if (0)                             /* begin comments by preprocessor */
  45. /*------------------------attribute byte details----------------------*/
  46. To create the attribute byte, bit-wise OR together a desired effect
  47. from each of the following categories: (1) Blinking Control,
  48. (2) Background Color, (3) Intensity Control and (4) Foreground Color
  49.  
  50. For Example:
  51.               (1)         (2)        (3)         (4)
  52. attribute = BLINKON  | BACK_BLACK | BOLDON  | FORE_WHITE
  53. attribute =   0x80   |    0x00    |  0x08   |   0x07
  54. attribute ==> 0x8F
  55.  
  56. ---------------------------------------------
  57. Attribute Byte Bit Settings
  58. ---------------------------------------------
  59. bit-7   Blink Control           (0=OFF, 1=ON)
  60. bit-6   Background Red-gun      (0=OFF, 1=ON)
  61. bit-5   Background Green-gun    (0=OFF, 1=ON)
  62. bit-4   Background Blue-gun     (0=OFF, 1=ON)
  63.  
  64. bit-3   Intensity Control       (0=LOW, 1=HI)
  65. bit-2   Foreground Red-gun      (0=OFF, 1=ON)
  66. bit-1   Foreground Green-gun    (0=OFF, 1=ON)
  67. bit-0   Foreground Blue-gun     (0=OFF, 1=ON)
  68.  
  69. ----------------------------------------------------
  70. RGB (Red:Green:Blue)    Bit--Settings    SGR     SGR
  71. Red:Green:Blue  Color   Bits  Decimal   Back    Fore
  72. ----------------------------------------------------
  73. OFF:  OFF: OFF  BLACK   [000]   0       "40"   "30"
  74. OFF:  OFF: ON   BLUE    [001]   1       "44"   "34"
  75. OFF:  ON : OFF  GREEN   [010]   2       "42"   "32"
  76. OFF:  ON : ON   CYAN    [011]   3       "46"   "36"
  77. ON:   OFF: OFF  RED     [100]   4       "41"   "31"
  78. ON:   OFF: ON   MAGENTA [101]   5       "45"   "35"
  79. ON:   ON : OFF  YELLOW  [110]   6       "43"   "33"
  80. ON:   ON : ON   WHITE   [111]   7       "47"   "37"
  81.  
  82. ---------------------------------------------
  83. Typical Attribute Byte Settings
  84. ---------------------------------------------
  85. 0x00    non-display (invisible)
  86. 0x01    underline (monochrome only)
  87. 0x07    normal display (white on black)
  88. 0x70    reverse video (black on white)
  89. 0x87    blinking normal display
  90. 0xF0    blinking normal reverse video display
  91. ---------------------------------------------
  92. #endif                              /* end preprocessor comments       */
  93.  
  94. /*-------------------------function aprintf()--------------------------*/
  95. int aprintf( unsigned char attr, const char *format, ... )
  96. {
  97. int size;                           /* string length                   */
  98. int ret;                            /* aprintf() return count          */
  99. va_list ptr;                        /* va_start() address              */
  100. char buffer[0x80];                  /* workspace for attr + string     */
  101.  
  102. /* ansi.sys SGR (Set Graphics Rendition) Control Sequences             */
  103. /* bold/ underscore/ blink/ reverse/ invisible                         */
  104. static const char *control[] =
  105.     { "1;", "4;", "5;", "7;", "8;" };
  106. /* see SGR Back Table above                                            */
  107. static const char *back[] =
  108.     { "40;", "44;", "42;", "46;", "41;", "45;", "43;", "47;" };
  109. /* see SGR Fore Table above                                            */
  110. static const char *fore[] =
  111.     { "30;", "34;", "32;", "36;", "31;", "35;", "33;", "37;" };
  112.  
  113. /* start graphics rendition prologue                                   */
  114. strcpy(buffer,"\x1b[?7l\x1b[0;");   /* no line wrap; reset attr        */
  115.  
  116. /* test/set blinking attribute bit                                     */
  117. if (attr >> 7) strcat(buffer, control[2]);
  118.  
  119. /* test/set background color attribute bits                            */
  120. strcat(buffer, back[(unsigned char)(attr << 1) >> 5]);
  121.  
  122. /* test/set intensity attribute bit                                    */
  123. if ((unsigned char)(attr << 4) >> 7) strcat(buffer, control[0]);
  124.  
  125. /* test/set foreground color attribute bits                            */
  126. strcat(buffer, fore[(unsigned char)(attr << 5) >> 5]);
  127.  
  128. /* test special attribute bit cases                                    */
  129. /* --> invisible criteria                                              */
  130. if ( ((unsigned char)(attr << 1) >> 5) == (BACK_BLACK >> 4) &&
  131.      ((unsigned char)(attr << 5) >> 5) == FORE_BLACK )
  132.      strcat(buffer, control[4]);
  133.  
  134. /* --> underline criteria                                              */
  135. if ( ((unsigned char)(attr << 5) >> 5) == UNDERLINE )
  136.      strcat(buffer, control[1]);
  137.  
  138. /* --> reverse video criteria                                          */
  139. if ( ((unsigned char)(attr << 1) >> 5) == (BACK_WHITE >> 4) &&
  140.      ((unsigned char)(attr << 5) >> 5) == FORE_BLACK )
  141.      strcat(buffer, control[3]);
  142.  
  143. size = strlen(buffer);              /* overwrite very last semi-colon  */
  144. strcpy(&buffer[size-1], "m");       /* end graphics rendition prologue */
  145. size = strlen(buffer);
  146.  
  147. va_start(ptr, format);              /* format variable to buffer       */
  148. ret = vsprintf(&buffer[size], format, ptr);
  149. strcat(buffer, "\x1b[0m\x1b[K");    /* reset attr; blank to EOL        */
  150. printf(buffer);                     /* display attr control & text     */
  151. fflush(stdout);                     /* flush output buffer             */
  152. return (ret);                       /* count of characters displayed   */
  153. }                                   /* end function aprintf()          */
  154.  
  155. /*----------------------embedded test driver main()--------------------*/
  156. #if !defined(NDEBUG)                /* conditional compilation         */
  157. int main( void)                     /* embedded test driver main()     */
  158. {
  159. int retval;                         /* aprintf() count of characters   */
  160. unsigned char attr;                 /* attribute control byte          */
  161. while (1) {
  162.     aprintf(NORMAL|BOLDON,"\nEnter Attribute as 0xhh (0x00 to END): ");
  163.     fflush(stdin);                  /* clear the input buffer          */
  164.     scanf("%x", &attr);             /* get attribute from user         */
  165.     if (!attr) break;               /* test to end                     */
  166.     retval = aprintf(attr, "%d %f %s", 33, -100.25F, "Sample String!");
  167.     aprintf(NORMAL, "\nCharacters written = %d", retval);
  168.     }
  169. aprintf(INVISIBLE, "\nYou will not see this message\n");
  170.  }                                  /* end function main()             */
  171. #endif
  172. /*===============================stop==================================*/
  173.