home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / cdactual / demobin / share / program / C / ANSISYS.ZIP / ANSISYS.C
Encoding:
C/C++ Source or Header  |  1988-06-28  |  12.0 KB  |  279 lines

  1. /** ansisys.c
  2.  
  3. *
  4. *   ANSISYS.C
  5. *   (C) Copyright 1985 Don F. Ridgway
  6. *   All Rights Reserved.
  7. *   This program may be copied for
  8. *   personal, nonprofit use only.
  9. *   
  10. *   Written, compiled and tested in Microsoft C
  11. *   version 2.03, and Lattice C version 2.15 and 3.00, under
  12. *   PC-DOS 2.1 and 3.1 on a Compaq and an IBM AT, both with 640K bytes
  13. *   of RAM and an 8087, using the Turbo Pascal 3.0 screen editor.
  14. *
  15. *   Please refer to the MS-DOS/PC-DOS Reference Manual and the
  16. *   ANSI.SYS device driver for the original commands and control 
  17. *   sequences that are here made into C macros.
  18. *   
  19. *   Refer to the IBM Technical Reference Manual or to the 
  20. *   appendix of the BASIC version 2 Reference for the ASCII
  21. *   character codes and the extended keyboard function codes.
  22. *
  23. *   Remember that C is case-sensitive, so be sure and
  24. *   reference the following macros with CAPITAL LETTERS.
  25. *
  26. *   Modified 6/25/88 Bruce K. Butler to reverse x,y for y,x (row,col) usage.
  27. *   and added some more defines.
  28. **/
  29.  
  30. #define DOUBLELINE  205
  31. #define SINGLELINE  196
  32. #define DARK        176
  33. #define MEDIUM      177
  34. #define LIGHT       178
  35. #define WHITE       219
  36. #define BLANK       255
  37. #define SUNSHINE    15
  38. #define MUSIC       14
  39. #define ASTERISKS   42
  40. #define HAPPYFACE1  1
  41. #define HAPPYFACE2  2
  42. #define HEARTS      3
  43. #define DIAMONDS    4
  44. #define CLUBS       5
  45. #define SPADES      6
  46. #define BEEPS       7
  47.  
  48.     /* 800-Mz tone for 1/4 second */
  49. #define BEEP                            printf("\007")
  50.  
  51.     /* clears the screen and positions cursor at top left corner */
  52. #define CLEARSCREEN                     printf("\033[2J")
  53. #define CLS                             CLEARSCREEN
  54.  
  55.     /* positions cursor at y = row, x = column */
  56. #define CURSPOS(y,x)                    printf("\033[%u;%uH",(y),(x))
  57. #define XY(y,x)                         CURSPOS(y,x)
  58.  
  59.     /* erases to end of line, including cursor position */
  60.     /* NOTE: error in DOS documentation has 'K' lowercase */
  61. #define EOL                             printf("\033[K")
  62.  
  63.     /* positions cursor at y=row,x=column then erase to end of line */
  64. #define XYEOL(y,x)                      printf("033[%u;%uH\033[K",(y),(x))
  65.  
  66.     /* requests cursor position, device driver answers row,col--declare int */
  67. #define XYWHERE      printf("\033[6n");scanf("%*1c%2d%*1c%2d%*2c",&row,&col)
  68.  
  69.     /* cursor up or down x-number of lines */
  70. #define CURSUP(x)                       printf("\033[%uA",(x))
  71. #define CURSDWN(x)                      printf("\033[%uB",(x))
  72.  
  73.     /* cursor forward (right) or backward (left) y-number of spaces */
  74. #define CURSFWD(y)                      printf("\033[%uC",(y))
  75. #define CURSBCK(y)                      printf("\033[%uD",(y))
  76.  
  77.     /* cursor position is saved for later recall via recallcurs */
  78. #define SAVCURS                         printf("\033[s")
  79. #define RECALLCURS                      printf("\033[u")
  80.  
  81.     /* positions cursor at y=row,x=col and print char z (using ASCII code) */
  82. #define CPR(y,x,z)                      printf("\033[%u;%uH%c",(y),(x),(z))
  83. #define XYCHAR(y,x,z)                   CPR((y),(x),(z))
  84.  
  85.     /* on row y, center (and printf) the string (in double quotes) */
  86. #define XCTRPRINTF(y,x,str)             printf("\033[%u;%uH%s",(y),((80-(strlen(str)-1))/2),str)
  87.  
  88.     /* at position y,x printf the string str (in double quotes) */
  89. #define CURSPOSPRTF(y,x,str)            printf("\033[%u;%uH%s",(y),(x),str)
  90. #defineXYPRINTF(y,x,str)                CURSPOSPRTF(y,x,str)
  91.  
  92. #ifdef PC
  93.     /* extended keyboard read, reads function keys, arrow keys, etc. */
  94.     /* NOTE: bdos() doesn't work this way in Microsoft C3.0 and 4.0 */
  95. #define XKREAD(x)                   x=0;x=bdos(1);if(bdos(11))x=bdos(8)+128
  96.  
  97.     /* same as XKREAD(), except this one echoes the input on the screen */
  98. #define XKREADE(x)                  x=0;x=bdos(1);if(bdos(11))x=bdos(1)+128
  99.  
  100.     /* if F10 key was pressed, break out of loop */
  101. #define CHKBRK                      if (key==196) break
  102. #endif
  103.  
  104.     /* set screen graphics mode                                            */
  105.     /* 0=40x25 monochrome, 1=40x25 color, 2=80x25 mono, 3=80x25 color,     */
  106.     /* 4=320x200 color, 5=320x200 mono, 6=640x200 mono, 7=enable word wrap */
  107. #define SETSCREEN(a)                printf("\033[=%uh",a)
  108.  
  109.     /* reset screen graphics mode                                          */
  110.     /* the attributes are same as SETSCREEN(a) except 7=disables word wrap */
  111. #define RESETSCREEN(a)              printf("\033[=%ul",a)
  112.  
  113. /* set screen display attributes and colors = (a,b,c) any order:       */
  114. /* 0=default, 1=high intensity, 4=underline, 5=blinking,               */
  115. /* 7=inverse, 8=invisible (black-on-black), 30=foreground black        */
  116. /* 31=fore red, 32=fore green, 33=fore yellow, 34=fore blue, 35=fore magenta */
  117. /* 36=fore cyan, 37=fore white, 40=background black, 41=back red, 42=back green */
  118. /* 43=back yellow, 44=back white, 40=back magenta, 46=back cyan, 47=back white */
  119. #define SETDISPLAY(a,b,c)           printf("\033[%u;%u;%um",a,b,c);
  120.  
  121.     /* set highlight (high intensity) on */
  122. #define HLON                        SETDISPLAY(0,0,1)
  123.  
  124.     /* set blinking on */
  125. #define BLON                        SETDISPLAY(0,0,5)
  126.  
  127.     /* set high intensity, blink (and all other display attributes) to off */
  128. #define HLOFF                       SETDISPLAY(0,0,0)
  129. #define BLOFF                       HLOFF
  130.  
  131.     /* at position y=row, x=col read inverse prompt for input cc */
  132. #define PROMPT(y,x,cc)   SETDISPLAY(0,0,7);printf("\033[%u;%uH:,(y),(x)); \
  133.                         cc = getchar();SETDISPLAY(0,0,0)
  134.  
  135.     /* at position y,x read highlighted prompt for input z */
  136. #define XKPROMPT(y,x,z)  HLON;XY((y),(x));printf("\b");XKREAD(z);HLOFF 
  137.  
  138.     /* a rectangle determined by upper left-hand corner coordinates, */
  139.     /* row1=a, col1=b, and lower right-hand corner coordinates, */
  140.     /* row2=c, col2=d, is filled with extended graphics character */
  141.     /* ASCII decimal code e, and the border is ASCII decimal code f */
  142. #define WINDOW(a,b,c,d,e,f)      DRAW(a,b,c,d,f);FILL(a+1,b+2,c-1,d-2,e)    
  143.  
  144.     /* same as WINDOW(a,b,c,d,e,f) except use this one to overwrite other */
  145.     /* drawings because this one fills empty spaces with blanks */
  146. #define WINDOW2(a,b,c,d,e,f)     DRAW(a,b,c,d,f);DRAW(a+1,b+1,c-1,d-1,255); \
  147.                                     FILL(a+1,b+2,c-1,d-2,e)
  148.  
  149. /* ----------------------------------------------------------------------- */
  150. /** DRAW(row1,col1,row2,col2,icon)                                         */
  151. /*                                                                         */
  152. /*  can be rectangle, vertical line, horizontal line or point!             */
  153. /*                                                                         */
  154. /*  row1,col1=upper left-hand corner of border                             */
  155. /*  row2,col2=lower right-hand corner                                      */
  156. /*  icon=ASCII decimal number of character you want border made of         */
  157. /*                                                                         */
  158. /*  (NOTE: Error-trapping is up to you in calling program,                 */
  159. /*        e.g.,[0<=row,=24],[0<=col<=80],graphics mode,                    */
  160. /*        etc.)                                                            */
  161. /*                                                                         */
  162. /*  Dbl Lines=205;Sngl Line=196;Dark=176;Medium=177;Light=178              */
  163. /*  White=219;Blank=255;Sunshine=15;Music notes=14;Asterisks=42            */
  164. /*  Happy Face=1,2;Hearts=3;Diamonds=4;clubs=5;Spades=6;Beeps=7            */
  165. /*                                                                         */
  166. /* ----------------------------------------------------------------------- */
  167. /**/
  168.  
  169. DRAW(row1,col1,fow2,col2,icon)
  170. int row1,col1,row2,col2,icon;
  171. {
  172.   int hlen,hlen2,vlen,r,c,hzl,vtl,ulc,llc,urc,lrc;
  173.  
  174.   hlen=hlen2=col2-col1;
  175.   vlen=row2-row1;
  176.   if (hlen < 0 || vlen < 0) BEEP;
  177.  
  178.   if (hlen <= 0 && vlen <= 0)   /* then it's a point or a corner */
  179.   {
  180.     CPR(row1,col1,icon);
  181.     return(0);
  182.   }
  183.  
  184.   if (vlen <= 0)                /* then it's a horizontal line */
  185.   {
  186.     CURSPOS(row1,col1);
  187.     while(hlen--)
  188.       printf("%c",icon);
  189.     return(0);
  190.   }
  191.   
  192.   switch (icon)
  193.   {
  194.     case 196:       /* for single line border */
  195.     case 218:
  196.         hzl=196;vtl=179;ulc=218;llc=192;urc=191;lrc=217;
  197.         break;
  198.     case 201:       /* for double line border */
  199.     case 205:
  200.         hzl=205;vtl=186;ulc=201;llc=200;urc=187;lrc=190;
  201.         break;
  202.     case 213:       /* for double top, single side */
  203.         hzl=205;vtl=179;ulc=213;llc=212;urc=184;lrc=188;
  204.         break;
  205.     default:        /* for same char all around */
  206.         hzl=vtl=ulc=llc=urc=lrc=icon;
  207.   }
  208.  
  209.   if (hlen <= 0)    /* it's a vertical line -- use vtl from above */
  210.   {
  211.     CURSPOS(fow1,col1);
  212.     for (r=1;r<=hlen;r++)
  213.       CPR(r,col1,vtl);
  214.     return(0);
  215.   }
  216.  
  217.   CURSPOS(row1,col1);  /* if it's fallen through this far, it's a rectangle */
  218.   while(hlen--)        /* print horizontal icon top row, left to right */
  219.     printf("%c",hzl);
  220.   CPR(row1,col2,urc);  /* print upper right-hand corner */
  221.   for (r=row1+1;r<row2;r++) /* print vertical right-hand column, top to bot */
  222.     CPR(r,col2,vtl);
  223.   CPR(row2,col2,lrc);  /* printf lower right-hand corner */
  224.   CURSPOS(row2,col2-1);
  225.   while(hlen2--)       /* print horizontal bottom row, right to left */
  226.     printf("%c\b\b",hzl);   /* one forward, two back (NOTE: this is slow) */
  227.   CPR(row2,col1,llc);  /* print lower left-hand corner */
  228.   for (r=row2-1;r>row1;r--) /* print vertical left-hand column, bottom to top */
  229.     CPR(r,col1,vtl);
  230.   CPR(row1,col1,ulc);  /* print upper left-hand corner to complete object */
  231.   return(0);
  232. }                      /* end DRAW() function */
  233.  
  234. /* ------------------------------------------------------------------------ */
  235. /** FILL(row1,col1,row2,col2,icon)                                          */
  236. /*                                                                          */
  237. /* can be "window,"vertical line, horizontal line or point!                 */
  238. /*                                                                          */
  239. /* row1,col1=upper left-hand corner of area to be filled                    */
  240. /* row2,col2=lower right-hand corner                                        */
  241. /* icon=ASCII decimal number of character you want area filled with         */
  242. /*                                                                          */
  243. /*   (NOTE: Error-trapping is up to you in calling program,                 */
  244. /*         e.g., [0<=row<=24],[0<=col<=80],graphics mode,                   */
  245. /*         etc.)                                                            */
  246. /*                                                                          */
  247. /* Dbl Lines=205;Sngl Line=196;Dark=176;Medium=177;Light=178                */
  248. /* White=219;blank=255;Sunshine=15;Music notes=14;Asterisks=42              */
  249. /* Happy Face=1,2;Hearts=3;Diamonds=4;Clubs=5;Spades=6;Beeps=7              */
  250. /* ------------------------------------------------------------------------ */
  251. /**/
  252. FILL(row1,col1,row2,col2,icon)
  253. int row1,col1,row2,col2,icon;
  254. {
  255.   int hlen,hlen2,vlen,r,c;
  256.  
  257.   hlen=hlen2=col2-col1;
  258.   vlen=row2-row1;
  259.   if (hlen<0 || vlen<0) BEEP;
  260.  
  261.   for (r=row1;r<row2;r++)
  262.   {
  263.     hlen=hlen2+1;
  264.     CURSPOS(r,col1);
  265.     {
  266.       while(hlen--)
  267.         printf("%c",icon);
  268.     }
  269.   }
  270.   return(0);
  271. }               /* end FILL() function */
  272.                                */
  273. /*                                                                         */
  274. /*  can be rectangle, vertical line, horizontal line or point!             */
  275. /*                                                                         */
  276. /*  row1,col1=upper left-hand corner of border                             */
  277. /*  row2,col2=lower right-hand corner                                      */
  278. /*  icon=ASCII decimal number of character you want border made of         */
  279. /*