home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 5.ddi / MWHC.005 / 00 < prev    next >
Encoding:
Text File  |  1993-01-15  |  7.4 KB  |  274 lines

  1. // Program SHOWARGS: Display arguments in random colors.
  2. // ** WARNING ** This program works only on MS-DOS, and
  3. //               does not work with a monochrome monitor.
  4. #include <dos.h>
  5. #include <graph.h>
  6. #include <iostream.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. enum Boolean { false, true };
  11. enum Toggle  { off,   on   };
  12. enum Color   { black,    blue,       green,     cyan,
  13.                red,      magenta,    yellow,    white,
  14.                br_black, br_blue,    br_green,  br_cyan,
  15.                br_red,   br_magenta, br_yellow, br_white
  16.              };
  17. class Char_Att {
  18.    // 8-bit color variable allows:
  19.    //    7 6 5 4 3 2 1 0
  20.    //    + BBBBB ! FFFFF
  21.    // where:
  22.    //    F = foreground color
  23.    //    B = background color
  24.    //    + = blinking
  25.    //    ! = foreground bright
  26.    char color;
  27.  
  28.    void set_bit(Toggle T, int bit) {
  29.       if (T != off) color |= bit; else color ^= ~bit;
  30.       }
  31.  
  32. public:
  33.    char get_color(void) { return color; }
  34.    void set_blink(Toggle blink) {
  35.       set_bit(blink,128);
  36.       }
  37.  
  38.    void set_bright(Toggle bright) {
  39.       set_bit(bright,8);
  40.       }
  41.  
  42.    void set_color(char C) {
  43.       color = C;
  44.       }
  45.  
  46.    void set_color(Color fg, Color bg) {
  47.       if (fg <= br_white && bg <= white) {
  48.          // Leave bright/blink bits alone:
  49.          color &= 0x77;
  50.          color ^= (char) bg << 4 + fg;
  51.          }
  52.       }
  53.  
  54.    // Constructors allow specifying defaults:
  55.    Char_Att(Color  fg = white,
  56.             Color  bg = black,
  57.             Toggle bl = off) {
  58.       set_color(fg, bg);
  59.       set_blink(bl);
  60.       }
  61.    };
  62. // Line inherits Char_Att and uses it to store
  63. // the character attributes of the whole Line.
  64.  
  65. class Line : public Char_Att {
  66.    int XScreen, YScreen, length;
  67.    Toggle visible;
  68.    char *line_buffer;
  69.    Boolean print(void);  // Returns false on fail.
  70.    char blank;
  71.  
  72. public:
  73.    Line();
  74.    Line(char *initial, int X = 0, int Y = 0,
  75.         Color fg = white, Color bg = black,
  76.         int sz = 12, Toggle bl = off);
  77.    ~Line() { delete line_buffer; }
  78.    Line &operator =(char *string);
  79.    Line &operator =(Line &line);
  80.    void display(Toggle Tog, char blank = '*',
  81.                 int bg = _BLACK);
  82.    void move(int new_X, int new_Y);
  83.    void center(int new_Y);
  84.    };
  85. Line::Line() { // Default constructor for Line.
  86.    XScreen = YScreen = length = 0;
  87.    blank = '*';
  88.    visible = off;
  89.    return;
  90.    }
  91.  
  92. Line::Line(char *initial, int X, int Y,
  93.            Color fg, Color bg, int sz, Toggle bl) {
  94.    XScreen = X;  YScreen = Y;
  95.    length = 0;
  96.    visible = off;
  97.    blank = '*';
  98.  
  99.    set_color(fg, bg);
  100.    set_blink(bl);
  101.  
  102.    // Invokes Line::operator = to do the rest.
  103.    // *this refers to a particular class object.
  104.    *this = initial;
  105.    }
  106.  
  107. Line &Line::operator =(char *char_value) {
  108.    // If line_buffer is already allocated . . .
  109.    if (length) {
  110.       // delete it and reset length.
  111.       delete line_buffer;
  112.       length = 0;
  113.       }
  114.    length = strlen(char_value);
  115.    line_buffer = new char[length];
  116.    strcpy(line_buffer,char_value);
  117.    return *this;
  118.    }
  119.  
  120. Line &Line::operator =(Line &line) {
  121.    // Copy all attributes but the buffer, and set
  122.    // visible to off; assign one Line to another.
  123.    char *buf = line_buffer;
  124.    *this = line;
  125.    visible = off;
  126.    line_buffer = buf;  // Restore buffer.
  127.    memcpy(line_buffer,line.line_buffer,length);
  128.    return *this;
  129.    }
  130. Line::Line(char *initial, int X, int Y,
  131.            Color fg, Color bg, int sz, Toggle bl);
  132. void Line::display(Toggle Tog, char Blank, int bg) {
  133.    if (Tog == visible) return;
  134.    visible = Tog;
  135.    if (Tog == off) {
  136.       blank = Blank;
  137.       _setbkcolor(_BLACK);
  138.       }
  139.    else
  140.       _setbkcolor(bg);
  141.    if (!print()) {
  142.       exit(-1);
  143.       }
  144.    }
  145.  
  146. void Line::move(int new_X, int new_Y) {
  147.    XScreen = new_X;
  148.    YScreen = new_Y;
  149.    display(visible);
  150.    }
  151.  
  152. // Center Line and put at line new_Y.
  153. // Assumes screen width of 80 characters.
  154. void Line::center(int new_Y) {
  155.    move((80-length)/2,new_Y);
  156.    }
  157.  
  158. // The following print routine is specific to MS-DOS.
  159.  
  160. Boolean Line::print(void) {
  161.    union _REGS  myregs;
  162.  
  163.    myregs.h.bh = 0;          // Set display page to 0.
  164.    myregs.h.ah = 2;          // Set cursor position.
  165.    myregs.h.dh = YScreen;    // Set X and Y coordinates.
  166.    myregs.h.dl = XScreen;
  167.    // Issue DOS interrupt:
  168.    int86(0x10, &myregs, &myregs);
  169.  
  170.    myregs.h.ah = 14;       // Print char with attribute.
  171.    myregs.h.bh = 0;        // Always use display page 0.
  172.    myregs.h.bl = get_color();  // Color attribute.
  173.  
  174.    // Print line or blanks:
  175.    char *end = line_buffer+length;
  176.    for (char *char_ptr = line_buffer;
  177.         char_ptr < end; char_ptr++) {
  178.       myregs.l.al = visible ? *char_ptr : blank;
  179.       // Issue DOS interrupt:
  180.       int86(0x10,&myregs,&myregs);
  181.       }
  182.    return true;
  183.    }
  184.  
  185. // This program displays each argument from the command
  186. // line on a separate line, in graphics mode.
  187. // Run this program and give it several arguments.
  188.  
  189. void main(int argc, char **argv) {
  190.  
  191.    // Read in lines from command line:
  192.    //  one line per argument.
  193.  
  194.    Line *lines = new Line[argc];
  195.    struct _videoconfig videoinfo;
  196.    struct _videoconfig *videoptr =&videoinfo;
  197.    int modeok = 0;
  198.    int j = 0;
  199.    int firstbk = _BRIGHTWHITE;
  200.    int maxbk   = _BRIGHTWHITE;
  201.    int fg_color = 0;
  202.  
  203.    for (int i = 1; i <= argc; i++) {
  204.       // Refer to lines[i-1]:
  205.       Line &L = lines[i-1];
  206.       // Invokes user-defined operator = :
  207.       L = argv[i];
  208.       // Separate each arg by two lines:
  209.       L.move(20,i*2);
  210.       // Set the foreground to a color:
  211.       // Set the foreground to a color
  212.       //  (skip potentially look-alike colors):
  213.       if (fg_color == 5) fg_color++;
  214.       L.set_color(br_blue + fg_color++);
  215.       // Every other line blinks:
  216.       if (i%2) L.set_bright(on);
  217.       else L.set_blink(on);
  218.       }
  219.  
  220.    _setvideomode(_HRES16COLOR);
  221.    videoptr = _getvideoconfig(videoptr);
  222.    switch(videoptr->adapter) {
  223.       case _MDPA:
  224.          modeok  = _setvideomode(_TEXTMONO);
  225.          firstbk = _BLACK;
  226.          break;
  227.       case _MCGA:
  228.       case _CGA:
  229.          firstbk = _CYAN;
  230.          modeok  = _setvideomode(_MRES4COLOR);
  231.          break;
  232.       case _EGA:
  233.          modeok =  _setvideomode(_ERESCOLOR);
  234.          break;
  235.       case _VGA:
  236.          modeok =  _setvideomode(_VRES16COLOR);
  237.          break;
  238.       case _HGC:
  239.          modeok =  _setvideomode(_HERCMONO);
  240.          break;
  241.       default:
  242.          break;
  243.       }
  244.  
  245.    if (modeok == 0) {
  246.       _setvideomode(_DEFAULTMODE);
  247.       cerr << "*********************************\n"
  248.            << "**  Could not set video mode.  **\n"
  249.            << "**    This program will not    **\n"
  250.            << "**   work on most monochrone   **\n"
  251.            << "**          monitors.          **\n"
  252.            << "*********************************\n";
  253.       return;
  254.       }
  255.    maxbk = firstbk;
  256.    // Invoke constructor for Line that takes
  257.    //  a character string:
  258.    Line L("A display of your arguments:");
  259.    L.center(1); L.set_color(br_red);
  260.    L.set_blink(on);
  261.    L.display(on);
  262.  
  263.    // Now display the arguments:
  264.    for (i = 0; i < argc-1; i++) {
  265.       if (firstbk < 0) firstbk = maxbk;
  266.       lines[i].display(on, ' ', firstbk--);
  267.       for (j=0; j <1_000_000; j++);
  268.       }
  269.  
  270.    // Pause to show final screen...
  271.    for(i = 0; i < 3_000_000; i++);
  272.    _setvideomode(_DEFAULTMODE);
  273.    }
  274.