home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / MADTRB15.ZIP / TOOLS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1985-07-31  |  11.5 KB  |  451 lines

  1. {
  2. TOOLS.PAS - Screen & I/O Tools for MS and IBM Pascal
  3. copyright 1984 Ronald Florence
  4.  
  5.    WRXY - writes an lstring, with specified screen attribute, at row/col
  6.    DOXY - sets a row/col/len to a char and screen attribute
  7.    CLS - clears from 1 to 25 rows of the screen
  8.    LOCATE - places cursor at a row/col (1..25, 1..80)
  9.    CURSOR_ROW, CURSOR_COL - returns cursor location (1..25, 1..80)
  10.    INKEY - returns the next char pressed
  11.    ESCAPE - returns true if Esc is pressed
  12.    RDCHAR - waits for a char in a declared set
  13.    YES - waits for y/n; returns true if y
  14.    UPCASE - changes a string to upper case
  15.    RDSTR - inputs a string
  16.    RDINT - inputs an integer between low/high
  17.    RDREAL - inputs a decimal real
  18.       (RDSTR, RDINT, RDREAL all clear and start over if Esc is pressed during
  19.        entry. If Esc is pressed with no entry, they return false. All three
  20.        need a writeln if used in tty-type entry. Usage:
  21.    PEEK, POKE - segmented direct address procedure/functions
  22.    OK_DISP - sets video address, returns false if not 80 col text display
  23.    PUSHSCREEN - saves current screen
  24.    POPSCREEN - retrieves saved screen
  25.    PRESSED - returns next key (inc. extended ASCII, function keys, etc.)
  26.       (usage:
  27.          var key: keytype;
  28.             begin
  29.                 key:= pressed;
  30.                 if key.reg=chr(27) then do_escape
  31.                 else if key.ex=35 {alt H} then do_help
  32.                 else...)
  33.  
  34.  
  35. To use the whole package, compile it as a unit, $include the interface and
  36. put a "uses TOOLS" statement in your program heading. If you only need a few
  37. of the functions and procedures, put the declarations back on the ones you
  38. need and $include just the code you need in your program. Please include the
  39. statement "copyright 1984 Ronald Florence" in any program incorporating these
  40. procedures and functions.
  41.  
  42. Good luck. If you make any useful additions or changes, please write me:
  43.  
  44.       Ronald Florence
  45.       114 Five Mile River Road
  46.       Darien, CT 06820
  47. }
  48.  
  49.  
  50. interface;
  51.  
  52. unit tools
  53.    (wrxy, doxy, cls, locate, cursor_row, cursor_col,
  54.     inkey, escape, rdchar, yes, upcase, rdint, rdreal, rdstr,
  55.     peek, poke, ok_disp, pushscreen, popscreen, pressed);
  56.  
  57. type
  58.    charset = set of char;
  59.    keytype = record
  60.                ex: byte;
  61.                reg: char
  62.              end;
  63.  
  64. procedure wrxy (const msg: lstring; row, col: sint; att: char);
  65. procedure doxy (ch: char; row, col: sint; att: char; len:sint);
  66. procedure cls (upper, lower: sint);
  67. procedure locate (y,x: sint);
  68. function cursor_row: sint;
  69. function cursor_col: sint;
  70. function inkey: char;
  71. function escape: boolean;
  72. function rdchar (okchars:charset): char;
  73. function yes: boolean;
  74. procedure upcase (var s: string);
  75. function rdstr (var s: string): boolean;
  76. function rdint (var i:integer; low, high: integer): boolean;
  77. function rdreal (var r:real): boolean;
  78. function peek (segment, offset: word): byte;
  79. procedure poke (segment, offset: word; argval: byte);
  80. function ok_disp: boolean;
  81. procedure pushscreen;
  82. procedure popscreen;
  83. function pressed: keytype;
  84. end;
  85.  
  86.  
  87.  
  88. implementation of tools;
  89.  
  90. type
  91.    screenchar = record
  92.                    character, attribute: char;
  93.                 end;
  94.    screentype = array [1..25, 1..80] of screenchar;
  95.    curs_pos = record
  96.                  col, row: byte;
  97.               end;
  98.  
  99. const
  100.    blank = ' ';
  101.    norm = chr(7);
  102.  
  103. var [static]
  104.    screen: ads of screentype;
  105.    curs : ads of curs_pos;
  106.    cls_start: ads of char;
  107.    video_ads: word;
  108.    snapscreen : ^screentype;
  109.    snapcurs : curs_pos;
  110.  
  111. value
  112.    curs.s:= #0040;
  113.    curs.r:= #0050;   
  114.    screen.r:= #0;
  115.  
  116. procedure ptyuqq (len:word; loc:adsmem); extern;
  117. function dosxqq (comm, parm: word): byte; extern;
  118.  
  119. procedure wrxy;
  120. var [static]
  121.    i: sint;
  122. begin
  123.    for i := 1 to ord(msg.len) do begin
  124.       screen^[row, col].character := msg[i];
  125.       screen^[row, col].attribute := att;
  126.       col := col+1
  127.    end
  128. end;
  129.  
  130. procedure doxy;
  131. var [static]
  132.    i: sint;
  133. begin
  134.    for i := 1 to len do begin
  135.       screen^[row, col].character := ch;
  136.       screen^[row, col].attribute := att;
  137.       col := col+1
  138.    end;
  139. end;
  140.  
  141. procedure cls;
  142. type
  143.    screenline = array [1..80] of screenchar;
  144. var [static]
  145.    blankline: screenline;
  146. value
  147.    blankline:= screenline (do 80 of screenchar (blank, norm));
  148. begin
  149.    cls_start.r:= 160 * wrd(upper-1);
  150.    for var line:= upper to lower do
  151.       [movesl (ads blankline, cls_start, 160);
  152.        cls_start.r:= cls_start.r + 160]
  153. end;
  154.  
  155. procedure locate;
  156. const
  157.    bs = chr(8);
  158. begin
  159.    curs^.col:= wrd(x);
  160.    curs^.row:= wrd(y-1);
  161.    ptyuqq (1, ads bs)
  162. end;
  163.  
  164. function cursor_row;
  165. begin
  166.    cursor_row:= ord(curs^.row + 1)
  167. end;   
  168.  
  169. function cursor_col;
  170. begin
  171.    cursor_col:= ord (curs^.col + 1)
  172. end;
  173.  
  174. function inkey;
  175. var 
  176.    b: byte;
  177. begin
  178.    repeat b:= dosxqq(6,255) until b <> 0;
  179.    inkey:= chr(b)
  180. end;
  181.  
  182. function escape;
  183. var 
  184.    b: byte;
  185. begin
  186.    b:= dosxqq(6,255);
  187.    escape:= b=27
  188. end;
  189.  
  190. function rdchar;
  191. var  
  192.    c: char;
  193. begin
  194.    repeat
  195.       c:= inkey;
  196.       if c in ['a'..'z'] then c:= chr (ord(c) - 32)
  197.    until c in okchars;
  198.    write (c);
  199.    rdchar:= c
  200. end;
  201.  
  202. function yes;
  203. var 
  204.    c: char;
  205. begin
  206.    repeat c:= inkey until c in ['y','Y','n','N'];
  207.    write (c);
  208.    yes:= c in ['y','Y']
  209. end;
  210.  
  211. procedure upcase;
  212. begin
  213.    for var c:= 1 to upper(s) do 
  214.       if s[c] in ['a'..'z'] then s[c]:= chr(ord(s[c])-32)
  215. end;
  216.  
  217. function rdstr;
  218. label
  219.    again;
  220. var 
  221.    c: char;
  222.    k: sint;
  223. begin
  224.    again:
  225.    k:= 1;
  226.    repeat 
  227.       c:= inkey; 
  228.       case c of
  229.          chr(8):  if k > 1 then begin
  230.                      write (chr(8)*blank*chr(8));
  231.                      s[k]:= blank;
  232.                      k:= k-1
  233.                   end;
  234.          chr(27): if k = 1 then begin
  235.                      rdstr:= false;
  236.                      return
  237.                   end
  238.                   else begin
  239.                      for var d:= 1 to k do s[d]:= blank;
  240.                      doxy (blank, cursor_row, cursor_col-k+1, norm, k-1);
  241.                      locate (cursor_row, cursor_col-k+1); 
  242.                      goto again
  243.                   end;                       
  244.          chr(32)..chr(126): if k <= upper(s) then 
  245.                                begin
  246.                                   write (c);
  247.                                   s[k]:= c;
  248.                                   k:= k+1 
  249.                                end
  250.                              else write (chr(7))
  251.          otherwise
  252.       end
  253.    until c=chr(13);
  254.    if k < upper(s) then for var d:= k to upper(s) do s[d]:= blank;
  255.    rdstr:= true
  256. end;
  257.  
  258. function rdint;
  259. label
  260.    again;
  261. var  
  262.    neg: boolean;
  263.    k: sint;
  264.    c: char;
  265. begin
  266.    again:
  267.    k:= 1;
  268.    i:= 0;
  269.    neg:= false;
  270.    repeat
  271.       c:= inkey;
  272.       case c of 
  273.          chr(45):  if k=1 then begin
  274.                       write (c);
  275.                       neg:= true;
  276.                       k:= k+1
  277.                    end
  278.                    else write (chr(7));
  279.          '0'..'9': begin
  280.                       write (c);
  281.                       i:= i * 10 + ord(c) - ord('0');
  282.                       k:= k+1
  283.                    end; 
  284.          chr(8) :  if k > 1 then begin
  285.                       write (chr(8)*blank*chr(8));
  286.                       if neg and (k=2) then neg:= false
  287.                       else i:= i div 10;
  288.                       k:= k-1;
  289.                    end;
  290.          chr (13): ;
  291.          chr(27):  if k = 1 then begin
  292.                       rdint:= false;
  293.                       return
  294.                    end
  295.                    else begin
  296.                       doxy (blank, cursor_row, cursor_col-k+1, norm, k-1);
  297.                       locate (cursor_row, cursor_col-k+1);
  298.                       goto again
  299.                    end;
  300.          otherwise write (chr(7))
  301.       end
  302.    until c = chr(13);
  303.    if neg then i:= - i;
  304.    if (i < low) or (i > high) then begin
  305.       write (chr(7));
  306.       doxy (blank, cursor_row, cursor_col-k+1, norm, k-1);
  307.       locate (cursor_row, cursor_col-k+1);
  308.       goto again
  309.    end
  310.    else rdint:= true
  311. end;
  312.  
  313. function rdreal;
  314. label
  315.    again;
  316. var  
  317.    left, right: integer4;
  318.    expon: real;
  319.    neg, decimal : boolean;
  320.    k: sint;
  321.    c: char;   
  322. begin
  323.    again:
  324.    k:= 1;
  325.    expon:= 1;
  326.    left:= 0;
  327.    right:= 0;
  328.    neg:= false;
  329.    decimal:= false;
  330.    repeat
  331.       c:= inkey;
  332.       case c of 
  333.          chr(45):  if k=1 then begin
  334.                       write (c);
  335.                       neg:= true;
  336.                       k:= k+1
  337.                    end
  338.                    else write (chr(7));
  339.          chr(46):  if not decimal then begin
  340.                       write (c);
  341.                       decimal:= true;
  342.                       k:= k+1;
  343.                    end
  344.                    else write (chr(7));
  345.          '0'..'9': begin
  346.                       write (c);
  347.                       if not decimal then begin
  348.                          left:= left * 10 + ord(c) - ord('0');
  349.                          k:= k+1
  350.                       end
  351.                       else begin
  352.                          right:= right * 10 + ord (c) - ord ('0');
  353.                          expon:= expon / 10;
  354.                          k:= k+1
  355.                       end
  356.                    end;
  357.          chr(8) :  if k > 1 then begin
  358.                       write (chr(8)*blank*chr(8));
  359.                       if neg and (k=2) then neg:= false
  360.                       else if not decimal then left:= left div 10
  361.                       else if decimal and (expon=1) then decimal:= false
  362.                       else begin
  363.                          right:= right div 10;
  364.                          expon:= expon * 10
  365.                       end;
  366.                       k:= k-1
  367.                    end;
  368.          chr (13): ;
  369.          chr(27):  if k = 1 then begin
  370.                       rdreal:= false;
  371.                       return
  372.                    end
  373.                    else begin
  374.                       doxy (blank, cursor_row, cursor_col-k+1, norm, k-1);
  375.                       locate (cursor_row, cursor_col-k+1);
  376.                       goto again
  377.                    end;
  378.          otherwise write (chr(7))
  379.       end;
  380.    until c = chr(13);
  381.    r:= left + expon * float4(right);
  382.    if neg then r:= - r;
  383.    rdreal:= true
  384. end;
  385.  
  386. function peek;
  387. var addr: ads of byte;
  388. begin
  389.    addr.s:= segment;
  390.    addr.r:= offset;
  391.    peek:= addr^
  392. end;
  393.  
  394. procedure poke;
  395. var addr: ads of byte;
  396. begin
  397.    addr.s:= segment;
  398.    addr.r:= offset;
  399.    addr^:= argval
  400. end;
  401.  
  402. function ok_disp;
  403. begin
  404.    case peek(#0040, #0049) of
  405.       7 : video_ads:= #B000;    {monochrome board}
  406.       2,3: video_ads:= #B800    {80 col graphics board}
  407.       otherwise
  408.          [writeln ('Program requires 80 column text display');
  409.           ok_disp:= false;
  410.           return]
  411.    end;
  412.    screen.s:= video_ads;
  413.    cls_start.s:= video_ads;
  414.    ok_disp:= true 
  415. end;
  416.  
  417. procedure pushscreen;
  418. var
  419.    oldscreen : ads of byte;
  420. begin
  421.    oldscreen.s := video_ads;
  422.    oldscreen.r := 0;
  423.    new(snapscreen);
  424.    movesl(oldscreen, ads snapscreen^, 4000);
  425.    snapcurs.row:= wrd(cursor_row);
  426.    snapcurs.col:= wrd(cursor_col)
  427. end;
  428.  
  429. procedure popscreen;
  430. var
  431.    oldscreen : ads of byte;
  432. begin
  433.    oldscreen.s := video_ads;
  434.    oldscreen.r := 0;
  435.    movesl(ads snapscreen^, oldscreen, 4000);
  436.    locate (ord(snapcurs.row), ord(snapcurs.col));
  437.    dispose(snapscreen)
  438. end;
  439.  
  440. function pressed;
  441. var
  442.    b: byte;
  443. begin
  444.    b:= dosxqq (7, 0);
  445.    pressed.reg:= chr(b);
  446.    if b <> 0 then pressed.ex:= 0
  447.    else pressed.ex:= dosxqq (7, 0)
  448. end;
  449.  
  450. end.
  451.