home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / ANSI_C / ANSISYS.C < prev   
Text File  |  1993-12-01  |  14KB  |  291 lines

  1. ========================
  2.  
  3.         ANSISYS.C
  4.  
  5. ========================
  6.  
  7.  
  8. /** ansisys.c
  9. *
  10. *   ANSISYS.C
  11. *   (C) Copyright 1985 Don F. Ridgway
  12. *   All rights reserved.
  13. *   This program may be copied for
  14. *   personal, non-profit use only.
  15. *
  16. *   This is an original and unique C programming
  17. *   language header/function file to #include with
  18. *   your C programs to give them "smart" cursor
  19. *   control and eye-catching "turtlegraphics"-
  20. *   type screen and graphics display qualities.
  21. *
  22. *   Programmed by:
  23. *   Don F. Ridgway
  24. *   Owner & Chief Programmer/Analyst
  25. *   A-1 IBM Programming & Training Service
  26. *   CUSTOM BUSINESS PROGRAMS
  27. *   119 Plantation Ct., Suite D
  28. *   Temple Terrace, FL  33617-3731
  29. *   Ph: (813) 985-3342  (10:00 am - 2:00 pm EST)
  30. *
  31. *   Written, compiled & tested in Microsoft C,
  32. *   ver. 2.03, and Lattice C, ver. 2.15, under
  33. *   PC-DOS 2.1 on a Compaq w/640Kb RAM & 8087, using
  34. *   the TURBO Pascal 3.0 screen editor.  (260+ lines.)
  35. *
  36. *   NOTE:  To utilize these macros you must have: (1) The
  37. *   ANSI.SYS file that came with your PC-DOS or MS-DOS 2.xx
  38. *   operating system on your boot disk and, (2) you must boot
  39. *   up with a CONFIG.SYS file on that boot disk which
  40. *   contains the statement: DEVICE = ANSI.SYS.  This loads
  41. *   the ANSI.SYS device driver into DOS at bootup time.  The
  42. *   operating system searches (for) CONFIG.SYS before it looks
  43. *   for an AUTOEXEC.BAT file.  Please refer to your DOS
  44. *   Reference Guide under "ANSI.SYS" and "COPY" for details.
  45. *
  46. *   (Simply, at A> prompt on a boot diskette, type:
  47. *                 COPY CON:CONFIG.SYS<cr>
  48. *                 DEVICE=ANSI.SYS<cr>
  49. *                 <F6><cr>
  50. *   and then reboot and you're ready to go!  Small bother
  51. *   for the brilliant performance gained in your C programs--
  52. *   just have these two files, ANSI.SYS and CONFIG.SYS, on
  53. *   your boot disk whenever you boot up.)
  54. *
  55. *   (The diskette these programs are sent to you on is a
  56. *   PC-DOS 2.1 boot disk so you may boot up with it and run
  57. *   ANSIDEMO.EXE.  Note the ANSI.SYS and CONFIG.SYS files.)
  58. *
  59. *   This custom C module/header file connects the C programming
  60. *   language to the MS-DOS/PC-DOS "ANSI.SYS" device driver
  61. *   used to implement extended screen and keyboard functions.
  62. *   Like any C program, each of the following macros can itself
  63. *   become a building block for a still larger one.  Note the
  64. *   evolution of WINDOW(row1,col1,row2,col2,fill,border) from
  65. *   DRAW(row1,col1,row2,col2,icon) and FILL(row1,col1,row2,col2,fill).
  66. *
  67. *   Please refer to the MS-DOS/PC-DOS Reference Manual and the
  68. *   ANSI.SYS Device Driver commands for the "original" commands
  69. *   and control sequences that are here made into C macros.
  70. *
  71. *   Refer to the IBM Technical Reference Manual or to the
  72. *   appendix of the BASIC Version 2 Reference for the ASCII
  73. *   Character Codes and the Extended Keyboard Function codes.
  74. *
  75. *   Run the ANSIDEMO.EXE for a superb demonstration of all these
  76. *   powerful macros and C programming tools in action.  The
  77. *   actual source code is included in ANSIDEMO.C, an excellent
  78. *   demonstration/introduction to the C programming language.
  79. *
  80. *   Simply #include "ansisys.c" this file in your programs
  81. *   to enable the following "smart" screen and cursor commands
  82. *   to really supercharge your C programs with professional
  83. *   features that are easier, safer and more portable than
  84. *   tacked-on assembly languge routines.
  85. *
  86. *   Remember that C is "case sensitive" so be sure and
  87. *   reference the following macros with CAPITAL LETTERS.
  88. *
  89. **/
  90.  
  91. #define BEEP                           printf("\007")
  92.         /* 800 Mz tone for 1/4 second -- same as PRINT CHR$(7) */
  93. #define CLEARSCREEN                    printf("\033[2J")
  94. #define CLS                            CLEARSCREEN
  95.         /* clears the screen and positions cursor at top left corner */
  96.         /* "\033" is Octal for "Escape" or ASCII Decimal 27  (CHR$(27)) */
  97.         /* "Escape-[" is the lead-in for the ANSI.SYS code routines */
  98. #define CURSPOS(x,y)                   printf("\033[%u;%uH",(x),(y))
  99. #define XY(x,y)                        CURSPOS(x,y)
  100.         /* positions cursor at x = row, y = column */
  101. #define EOL                            printf("\033[K")
  102.         /* erases to end of line, including cursor position */
  103.         /* NOTE:  error in DOS documentation has 'K' lower case */
  104. #define XYEOL(x,y)                     printf("\033[%u;%uH\033[K",(x),(y))
  105.         /* positions cursor at x,y then erases to end of line */
  106. #define XYWHERE        printf("\033[6n");scanf("%*1c%2d%*1c%2d%*2c",&row,&col)
  107.         /* requests cursor position, device driver answers row,col--declare int */
  108. #define CURSUP(x)                      printf("\033[%uA",(x))
  109. #define CURSDWN(x)                     printf("\033[%uB",(x))
  110.         /* cursor up or down x-many lines */
  111. #define CURSFWD(y)                     printf("\033[%uC",(y))
  112. #define CURSBCK(y)                     printf("\033[%uD",(y))
  113.         /* cursor forward (right) or backward (left) y-many spaces */
  114. #define SAVCURS                        printf("\033[s")
  115. #define RECALLCURS                     printf("\033[u")
  116.         /* cursor position is saved for later recall via RECALLCURS */
  117. #define CPR(x,y,z)                     printf("\033[%u;%uH%c",(x),(y),(z))
  118. #define XYCHAR(x,y,z)                  CPR(x,y,z)
  119.         /* position cursor at x,y and print char z (using ASCII code) */
  120. #define XCTRPRINTF(x,str)   printf("\033[%u;%uH%s",(x),((80-(strlen(str)-1))/2),str)
  121.         /* on row x, center (and printf) the string str (in double quotes) */
  122. #define CURSPOSPRTF(x,y,str)           printf("\033[%u;%uH%s",(x),(y),str)
  123. #define XYPRINTF(x,y,str)              CURSPOSPRTF(x,y,str)
  124.         /* at position x,y printf the string str (in double quotes) */
  125. #define XKREAD(x)                      x=0;x=bdos(1);if (bdos(11)) x=bdos(8)+128
  126.         /* extended code keyboard read, reads function keys, arrow keys, etc. */
  127. #define XKREADE(x)                     x=0;x=bdos(1);if (bdos(11)) x=bdos(1)+128
  128.         /* same as XKREAD(), except this one echoes the input on the screen */
  129. #define CHKBRK                          if (key==196) break
  130.         /* if F10 key was pressed, break out of loop */
  131. #define SETSCREEN(a)                   printf("\033[=%uh",a)
  132.      /* set screen graphics mode */
  133.      /* 0=40x25 monochrome,1=40x25 color,2=80x25 mono,3=80x25 color,        */
  134.      /* 4=320x200 color,5=320x200 mono,6=640x200 mono,7=enable word-wrap.   */
  135. #define RESETSCREEN(a)                 printf("\033[=%ul",a)
  136.      /* reset screen graphics mode */
  137.      /* the attributes are same as SETSCREEN(a) except 7=disables word-wrap */
  138. #define SETDISPLAY(a,b,c)              printf("\033[%u;%u;%um",a,b,c)
  139.      /* set screen display attributes and colors = (a,b,c) any order:       */
  140.      /* 0 = default, 1 = high intensity, 4 = underline,                     */
  141. /* 5=blinking,7=inverse,8=invisible (black-on-black),30=foreground black,   */
  142. /* 31=fore red,32=fore green,33=fore yellow,34=fore blue,35=fore magenta,   */
  143. /* 36=fore cyan,37=fore white,40=background black,41=back red,42=back green,*/
  144. /* 43=back yellow,44=back blue,45=back magenta,46=back cyan,47=back white.  */
  145. #define HLON                           SETDISPLAY(0,0,1)
  146.         /* set high light (high intensity) on */
  147. #define BLON                           SETDISPLAY(0,0,5)
  148.         /* set blinking on */
  149. #define HLOFF                          SETDISPLAY(0,0,0)
  150. #define BLOFF                          HLOFF
  151.         /* set high intensity, blink (and all other display attributes) to off */
  152. #define PROMPT(x,y,cc)            SETDISPLAY(0,0,7);printf("\033[%u;%uH",(x),(y));\
  153.                                        cc=getchar();SETDISPLAY(0,0,0)
  154.         /* at position x,y read inverse prompt for input cc */
  155. #define XKPROMPT(x,y,z)             HLON;XY((x),(y));printf(" \b");XKREAD(z);HLOFF
  156.         /* at position x,y read highlighted prompt for input z */
  157. #define WINDOW(a,b,c,d,e,f)            DRAW(a,b,c,d,f);FILL(a+1,b+2,c-1,d-2,e)
  158.         /* a rectangle determined by upper left-hand corner coordinates, */
  159.         /* row1 = a, col1 = b, and lower right-hand corner coordinates,  */
  160.         /* row2 = c, col2 = d, is filled with extended graphics character */
  161.         /* ASCII decimal code e, and the border is ASCII decimal code f  */
  162. #define WINDOW2(a,b,c,d,e,f)        DRAW(a,b,c,d,f);DRAW(a+1,b+1,c-1,d-1,255);\
  163.                                        FILL(a+1,b+2,c-1,d-2,e)
  164.         /* same as WINDOW(a,b,c,d,e,f) except use this one to overwrite other */
  165.         /* drawings because this one fills empty spaces with blanks */
  166.  
  167. /* ------------------------------------------------------------- */
  168. /** DRAW(row1,col1,row2,col2,icon)                               */
  169. /*                                                               */
  170. /*  can be rectangle, vertical line, horizontal line or point!   */
  171. /*                                                               */
  172. /* row1,col1=Upper Left-hand corner of border                    */
  173. /* row2,col2=Lower Right-hand corner                             */
  174. /* icon=ASCII Decimal number of Character want border made of    */
  175. /*                                                               */
  176. /*   (Note:  Error-trapping is up to you in calling program,     */
  177. /*           e.g., [0<=row<=24], [0<=col<=80], graphics mode,    */
  178. /*           etc.)                                               */
  179. /*                                                               */
  180. /* Dbl Lines=205;Sngl Line=196;Dark=176;Medium=177;Light=178     */
  181. /* White=219;Blank=255;Sunshine=15;Music notes=14;Asterisks=42   */
  182. /* Happy Face=1,2;Hearts=3;Diamonds=4;Clubs=5;Spades=6;Beeps=7   */
  183. /* ------------------------------------------------------------- */
  184. /**/
  185.  
  186. DRAW(row1,col1,row2,col2,icon)
  187. int row1,col1,row2,col2,icon;
  188. {
  189.    int hlen,vlen,r,c,hzl,vtl,ulc,llc,urc,lrc;
  190.  
  191.    hlen=col2-col1;
  192.    vlen=row2-row1;
  193.    if (hlen<0 || vlen<0) BEEP;         /* audibly alert possible input error */
  194.  
  195.    if (hlen<=0 && vlen<=0)             /* then it's a point or a corner */
  196.       {
  197.       CPR(row1,col1,icon);
  198.       return(0);
  199.       }
  200.  
  201.    if (vlen<=0)                        /* then it's a horizontal line */
  202.       {
  203.       CURSPOS(row1,col1);
  204.       for (c=0;c<=hlen;c++)
  205.          printf("%c",icon);
  206.          return(0);
  207.       }
  208.  
  209.    switch (icon)
  210.       {
  211.       case 196:                        /* for Single line border */
  212.       case 218:
  213.                 hzl=196;vtl=179;ulc=218;llc=192;urc=191;lrc=217;
  214.                 break;
  215.       case 201:                        /* for Double line border */
  216.       case 205:
  217.                 hzl=205;vtl=186;ulc=201;llc=200;urc=187;lrc=188;
  218.                 break;
  219.       case 213:                        /* for Double top, single side */
  220.                 hzl=205;vtl=179;ulc=213;llc=212;urc=184;lrc=190;
  221.                 break;
  222.       default:
  223.                 hzl=vtl=ulc=llc=urc=lrc=icon;  /* for same char all around */
  224.       }
  225.  
  226.    if (hlen<=0)              /* it's a vertical line -- use vtl from above */
  227.       {
  228.       CURSPOS(row1,col1);
  229.       for (r=row1;r<=row2;r++)
  230.          CPR(r,col1,vtl);
  231.          return(0);
  232.       }
  233.                              /* if it's fallen through this far it's a rectangle */
  234.    CURSPOS(row1,col1);
  235.    for (c=1;c<=hlen;c++)     /* print horizintal icon top row, left to right */
  236.       printf("%c",hzl);
  237.    CPR(row1,col2,urc);       /* print upper right-hand corner */
  238.    for (r=row1+1;r<row2;r++) /* print vertical right-hand column, top to bottom */
  239.      CPR(r,col2,vtl);
  240.    CPR(row2,col2,lrc);       /* print lower right-hand corner */
  241.    CURSPOS(row2,col2-1);
  242.    for (c=1;c<=hlen;c++)     /* print horizontal bottom row, right to left */
  243.       printf("%c\b\b",hzl);  /* one forward, two back (NOTE: this is slow) */
  244.    CPR(row2,col1,llc);       /* print lower left-hand corner */
  245.    for (r=row2-1;r>row1;r--) /* print vertical left-hand column, bottom to top */
  246.      CPR(r,col1,vtl);
  247.    CPR(row1,col1,ulc);       /* print upper left-hand corner to complete object */
  248.    return(0);
  249. }                            /* end DRAW() function */
  250.  
  251. /* ------------------------------------------------------------- */
  252. /** FILL(row1,col1,row2,col2,icon)                               */
  253. /*                                                               */
  254. /*  can be "window," vertical line, horizontal line or point!    */
  255. /*                                                               */
  256. /* row1,col1=Upper Left-hand corner of area-to-be-filled         */
  257. /* row2,col2=Lower Right-hand corner                             */
  258. /* icon=ASCII Decimal number of Character want area filled with  */
  259. /*                                                               */
  260. /*   (Note:  Error-trapping is up to you in calling program,     */
  261. /*           e.g., [0<=row<=24], [0<=col<=80], graphics mode,    */
  262. /*           etc.                                                */
  263. /*                                                               */
  264. /* Dbl Lines=205;Sngl Line=196;Dark=176;Medium=177;Light=178     */
  265. /* White=219;Blank=255;Sunshine=15;Music notes=14;Asterisks=42   */
  266. /* Happy Face=1,2;Hearts=3;Diamonds=4;Clubs=5;Spades=6;Beeps=7   */
  267. /* ------------------------------------------------------------- */
  268. /**/
  269.  
  270. FILL(row1,col1,row2,col2,icon)
  271. int row1,col1,row2,col2,icon;
  272. {
  273.    int hlen,vlen,r,c;
  274.  
  275.    hlen=col2-col1;
  276.    vlen=row2-row1;
  277.    if (hlen<0 || vlen<0) BEEP;  /* audibly alert possible input error */
  278.  
  279.    for (r=row1;r<=row2;r++)
  280.      {
  281.      CURSPOS(r,col1);
  282.        {
  283.        for (c=0;c<=hlen;c++)
  284.          printf("%c",icon);
  285.        }
  286.      }
  287.    return(0);
  288. }                            /* end FILL() function */
  289.  
  290.  
  291.