home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / tcsel003.zip / SUNDRY.PAS < prev    next >
Pascal/Delphi Source File  |  1992-10-28  |  13KB  |  458 lines

  1. unit sundry;
  2.  
  3. interface
  4.  
  5. uses
  6.   dos,
  7.   scrt,
  8.   strings;
  9.  
  10. type
  11.   LongWds = record
  12.               loword,hiword : word;
  13.             end;
  14.   ica_rec = record
  15.               case integer of
  16.                 0: (bytes   : array[0..15] of byte);
  17.                 1: (words   : array[0..7] of word);
  18.                 2: (integers: array[0..7] of integer);
  19.                 3: (strg    : string[15]);
  20.                 4: (longs   : array[0..3] of longint);
  21.                 5: (dummy   : string[13]; chksum: integer);
  22.                 6: (mix     : byte; wds : word; lng : longint);
  23.             end;
  24. {-This simply creates a variant record which is mapped to 0000:04F0
  25.   which is the intra-applications communications area in the bios area
  26.   of memory. A program may make use of any of the 16 bytes in this area
  27.   and be assured that dos and the bios will not interfere with it. This
  28.   means that it can be effectively used to pass values/information
  29.   between different programs. It can conceivably be used to store
  30.   information from an application, then terminate from that application,
  31.   run several other programs, and then have another program use the
  32.   stored information. As the area can be used by any program, it is wise
  33.   to incorporate a checksum to ensure that the intermediate applications
  34.   have not altered any values. It is of most use when executing child
  35.   processes or passing values between related programs that are run
  36.   consecutively.}
  37.  
  38.   IOproc = procedure(derror:byte; msg : string);
  39.  
  40. const
  41.   ValidChars : string[40] = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-'+#39;
  42.   HexChars : array[0..15] of char = '0123456789ABCDEF';
  43.  
  44. var
  45.   ica : ica_rec absolute $0000:$04f0;
  46.   FilePosition : longint;
  47. (*  OldRecSize   : word; *)
  48.   TempStr      : string;
  49.  
  50. procedure CheckIO(Error_action : IOproc; msg : string);
  51.  
  52. function CompressStr(var n): string;
  53.   {-Will compress 3 alpha-numeric bytes into 2 bytes}
  54.  
  55. function Decompress(var s): string;
  56.   {-Decompresses a string compressed by CompressStr}
  57.  
  58. function NumbOfElements(var s; size : word): word;
  59.   {-returns the number of active elements in a set}
  60.  
  61. function PrinterStatus : byte;
  62.   {-Gets the printer status}
  63.  
  64. function PrinterReady(var b : byte): boolean;
  65.  
  66. function TestBbit(n,b: byte): boolean;
  67. function TestWbit(var n; b: byte): boolean;
  68. function TestLbit(n: longint; b: byte): boolean;
  69.  
  70. procedure SetBbit(var n: byte; b: byte);
  71. procedure SetWbit(var n; b: byte);
  72. procedure SetLbit(var n: longint; b: byte);
  73.  
  74. procedure ResetBbit(var n: byte; b: byte);
  75. procedure ResetWbit(var n; b: byte);
  76. procedure ResetLbit(var n: longint; b: byte);
  77.  
  78. function right(var s; n : byte): string;
  79. function left(var s; n : byte): string;
  80. function shleft(var s; n : byte): string;
  81. function nextstr(var s1; s2 : string; n : byte): string;
  82. procedure WriteAtCr(st: string; col,row: byte);
  83. procedure WriteLnAtCr(st: string; col,row: byte);
  84. procedure WriteLNCenter(st: string; width: byte);
  85. procedure WriteCenter(st: string; width: byte);
  86. procedure gotoCR(col,row: byte);
  87.  
  88.   {-These functions and procedures unit provides the means to do random
  89.     access reads on text files.  }
  90.  
  91. function Exist(fn : string) : boolean;
  92.  
  93. function Asc2Str(var s; max: byte): string;
  94.  
  95. procedure DisableBlink(State:Boolean);
  96.  
  97. function Byte2Hex(numb : byte) : string;
  98.  
  99. function Numb2Hex(var numb) : string;
  100.  
  101. function Long2Hex(long : longint): string;
  102.  
  103. function Hex2Byte(HexStr : string) : byte;
  104.  
  105. function Hex2Word(HexStr : string) : word;
  106.  
  107. function Hex2Integer(HexStr : string) : integer;
  108.  
  109. function Hex2Long(HexStr : string) : longint;
  110.  
  111. {======================================================================}
  112.  
  113.  
  114. implementation
  115.  
  116. procedure CheckIO(error_action : IOproc;msg : string);
  117.   var c : word;
  118.   begin
  119.     c := IOResult;
  120.     if c <> 0 then error_action(c,msg);
  121.   end;
  122.  
  123.  
  124. {$F+}
  125. procedure ReportError(c : byte; st : string);
  126.   begin
  127.     writeln('I/O Error ',c);
  128.     writeln(st);
  129.     halt(c);
  130.   end;
  131. {$F-}
  132.  
  133. function CompressStr(var n): string;
  134.   var
  135.     S      : string absolute n;
  136.     InStr  : string;
  137.     len    : byte absolute InStr;
  138.     Compstr: record
  139.               case byte of
  140.                 0: (Outlen  : byte;
  141.                     Outarray: array[0..84] of word);
  142.                 1: (Out     : string[170]);
  143.              end;
  144.     temp,
  145.     x,
  146.     count : word;
  147.   begin
  148.     FillChar(InStr,256,32);
  149.     InStr := S;
  150.     len   := (len + 2) div 3 * 3;
  151.     FillChar(CompStr.Out,171,0);
  152.     InStr := StUpCase(InStr);
  153.     x := 1; count := 0;
  154.     while x <= len do begin
  155.       temp  := pos(InStr[x+2],ValidChars);
  156.       inc(temp,pos(InStr[x+1],ValidChars) * 40);
  157.       inc(temp,pos(InStr[x],ValidChars) * 1600);
  158.       inc(x,3);
  159.       CompStr.Outarray[count] := temp;
  160.       inc(count);
  161.     end;
  162.     CompStr.Outlen := count shl 1;
  163.     CompressStr := CompStr.Out;
  164.   end;  {-CompressStr}
  165.  
  166. function Decompress(var s): string;
  167.   var
  168.     CompStr : record
  169.                 clen : byte;
  170.                 arry : array[0..84] of word;
  171.               end absolute s;
  172.     x,
  173.     count,
  174.     temp    : word;
  175.   begin
  176.     with CompStr do begin
  177.       Decompress[0] := char((clen shr 1) * 3);
  178.       x := 0; count := 1;
  179.       while x <= clen shr 1 do begin
  180.         temp := arry[x] div 1600;
  181.         dec(arry[x],temp*1600);
  182.         Decompress[count] := ValidChars[temp];
  183.         temp := arry[x] div 40;
  184.         dec(arry[x],temp*40);
  185.         Decompress[count+1] := ValidChars[temp];
  186.         temp := arry[x];
  187.         Decompress[count+2] := ValidChars[temp];
  188.         inc(count,3);
  189.         inc(x);
  190.       end;
  191.     end;
  192.   end;
  193.  
  194. function NumbOfElements(var s; size : word): word;
  195.  {-The variable s can be any set type and size is the SizeOf(s)}
  196.   var
  197.     TheSet : array[1..32] of byte absolute s;
  198.     count,x,y : word;
  199.   begin
  200.     count := 0;
  201.     for x := 1 to size do
  202.       for y := 0 to 7 do
  203.         inc(count, 1 and (TheSet[x] shr y));
  204.     NumbOfElements := count;
  205.   end;
  206.  
  207. function PrinterStatus : byte;
  208.    var regs   : registers; {-from the dos unit                         }
  209.    begin
  210.      with regs do begin
  211.        dx := 0;            {-The printer number   LPT2 = 1             }
  212.        ax := $0200;        {-The function code for service wanted      }
  213.        intr($17,regs);     {-$17= ROM bios int to return printer status}
  214.        PrinterStatus := ah;{-Bit 0 set = timed out                     }
  215.      end;                  {     1     = unused                        }
  216.    end;                    {     2     = unused                        }
  217.                            {     3     = I/O error                     }
  218.                            {     4     = printer selected              }
  219.                            {     5     = out of paper                  }
  220.                            {     6     = acknowledge                   }
  221.                            {     7     = printer not busy              }
  222.  
  223. function PrinterReady(var b : byte): boolean;
  224.   begin
  225.     b := PrinterStatus;
  226.     PrinterReady := (b = $90) {-This may vary between printers}
  227.   end;
  228.  
  229. function TestBbit(n,b: byte): boolean;
  230.   begin
  231.     TestBbit := odd(n shr b);
  232.   end;
  233.  
  234. function TestWbit(var n; b: byte): boolean;
  235.   var t: word absolute n;
  236.   begin
  237.     if b < 16 then
  238.       TestWbit := odd(t shr b);
  239.   end;
  240.  
  241. function TestLbit(n: longint; b: byte): boolean;
  242.   begin
  243.     if b < 32 then
  244.       TestLbit := odd(n shr b);
  245.   end;
  246.  
  247. procedure SetBbit(var n: byte; b: byte);
  248.   begin
  249.     if b < 8 then
  250.       n := n or (1 shl b);
  251.   end;
  252.  
  253. procedure SetWbit(var n; b: byte);
  254.   var t : word absolute n; {-this allows either a word or integer}
  255.   begin
  256.     if b < 16 then
  257.       t := t or (1 shl b);
  258.   end;
  259.  
  260. procedure SetLbit(var n: longint; b: byte);
  261.   begin
  262.     if b < 32 then
  263.       n := n or (longint(1) shl b);
  264.   end;
  265.  
  266. procedure ResetBbit(var n: byte; b: byte);
  267.   begin
  268.     if b < 8 then
  269.       n := n and not (1 shl b);
  270.   end;
  271.  
  272. procedure ResetWbit(var n; b: byte);
  273.   var t: word absolute n;
  274.   begin
  275.     if b < 16 then
  276.       t := t and not (1 shl b);
  277.   end;
  278.  
  279. procedure ResetLbit(var n: longint; b: byte);
  280.   begin
  281.     if b < 32 then
  282.       n := n and not (longint(1) shl b);
  283.   end;
  284.  
  285. function right(var s; n : byte): string;
  286.   var
  287.     st : string absolute s;
  288.     len: byte absolute s;
  289.   begin
  290.     if n >= len then right := st else
  291.     right := copy(st,len+1-n,n);
  292.   end;
  293.  
  294. function shleft(var s; n : byte): string;
  295.   var
  296.     st   : string absolute s;
  297.     stlen: byte absolute s;
  298.     temp : string;
  299.     len  : byte absolute temp;
  300.   begin
  301.     if n < stlen then begin
  302.       move(st[n+1],temp[1],255);
  303.       len := stlen - n;
  304.       shleft := temp;
  305.     end;
  306.   end;
  307.  
  308. function left(var s; n : byte): string;
  309.   var
  310.     st  : string absolute s;
  311.     temp: string;
  312.     len : byte absolute temp;
  313.   begin
  314.     temp := st;
  315.     if n < len then len := n;
  316.     left := temp;
  317.   end;
  318.  
  319. function nextstr(var s1;s2 : string; n : byte): string;
  320.   var
  321.     main   : string absolute s1;
  322.     second : string absolute s2;
  323.     len    : byte absolute s2;
  324.   begin
  325.     nextstr := copy(main,pos(second,main)+len,n);
  326.   end;
  327.  
  328. procedure WriteAtCr(st: string; col,row: byte);
  329.   begin
  330.     Gotoxy(col,row);
  331.     write(st);
  332.   end;
  333.  
  334.  
  335. procedure WriteLnAtCr(st: string; col,row: byte);
  336.   begin
  337.     Gotoxy(col,row);
  338.     writeln(st);
  339.   end;
  340.  
  341. procedure WriteLnCenter(st: string; width: byte);
  342.   begin
  343.     TempStr := CharStr(' ',(width div 2)-succ((length(st) div 2)));
  344.     st      := TempStr + st;
  345.     writeln(st);
  346.   end;
  347.  
  348. procedure WriteCenter(st: string; width: byte);
  349.   begin
  350.     TempStr := CharStr(' ',(width div 2)-succ((length(st) div 2)));
  351.     st      := TempStr + st;
  352.     write(st);
  353.   end;
  354.  
  355. procedure gotoCR(col,row: byte);
  356.   begin
  357.     GotoXY(col,row);
  358.   end;
  359.  
  360. function Exist(fn : string): boolean;
  361.   var
  362.     f         : file;
  363.     OldMode   : byte;
  364.   begin
  365.     OldMode := FileMode;
  366.     FileMode:= 0;
  367.     assign(f,fn);
  368.     {$I-}  reset(f,1); {$I+}
  369.     if IOResult = 0 then begin
  370.       close(f);
  371.       Exist := true;
  372.     end
  373.     else
  374.       Exist := false;
  375.     FileMode:= OldMode;
  376.   end; {-Exist}
  377.  
  378. function Asc2Str(var s; max: byte): string;
  379.   var starray : array[0..255] of byte absolute s;
  380.       st      : string;
  381.       len     : byte absolute st;
  382.   begin
  383.     move(starray[0],st[1],255);
  384.     len := max;
  385.     len := (max + word(1)) * ord(pos(#0,st) = 0) + pos(#0,st)-1;
  386.     Asc2Str := st;
  387.   end;
  388.  
  389.  
  390. procedure DisableBlink(state : boolean);
  391.    { DisableBlink(true) allows use of upper eight colors as background }
  392.    { colours. DisableBlink(false) restores the normal mode and should  }
  393.    { be called before program exit                                     }
  394. var
  395.    regs : registers;
  396. begin
  397.   with regs do
  398.   begin
  399.     ax := $1003;
  400.     bl := ord(not(state));
  401.   end;
  402.   intr($10,regs);
  403. end;  { DisableBlink }
  404.  
  405. function Byte2Hex(numb : byte) : string;
  406.   begin
  407.     Byte2Hex[0] := #2;
  408.     Byte2Hex[1] := HexChars[numb shr  4];
  409.     Byte2Hex[2] := HexChars[numb and 15];
  410.   end;
  411.  
  412. function Numb2Hex(var numb) : string;
  413.   { converts an integer or a word to a string. Using an untyped
  414.     argument makes this possible. }
  415.   var n : word absolute numb;
  416.   begin
  417.     Numb2Hex := Byte2Hex(hi(n))+Byte2Hex(lo(n));
  418.   end;
  419.  
  420. function Long2Hex(long : longint): string;
  421.   begin
  422.     with LongWds(long) do { type casting makes the split up easy}
  423.       Long2Hex := Numb2Hex(hiword) + Numb2Hex(loword);
  424.   end;
  425.  
  426. function Hex2Byte(HexStr : string) : byte;
  427.   begin
  428.     Hex2Byte := pos(UpCase(HexStr[2]),HexChars)-1  +
  429.                ((pos(UpCase(HexStr[1]),HexChars))-1) shl  4 { *  16}
  430.   end;
  431.  
  432. function Hex2Word(HexStr : string) : word;
  433.   { This requires that the string passed is a true hex string  of 4
  434.     chars and not in a format like $FDE0 }
  435.   begin
  436.     Hex2Word := pos(UpCase(HexStr[4]),HexChars)-1  +
  437.                ((pos(UpCase(HexStr[3]),HexChars))-1) shl  4 + { *  16}
  438.                ((pos(UpCase(HexStr[2]),HexChars))-1) shl  8 + { * 256}
  439.                ((pos(UpCase(HexStr[1]),HexChars))-1) shl 12;  { *4096}
  440.   end;
  441.  
  442. function Hex2Integer(HexStr : string) : integer;
  443.   begin
  444.     Hex2Integer := integer(Hex2Word(HexStr));
  445.   end;
  446.  
  447. function Hex2Long(HexStr : string) : longint;
  448.   var Long : LongWds;
  449.   begin
  450.     Long.hiword := Hex2Word(copy(HexStr,1,4));
  451.     Long.loword := Hex2Word(copy(HexStr,5,4));
  452.     Hex2Long := longint(Long);
  453.   end;
  454.  
  455. begin
  456.   FilePosition := 0;
  457. end.
  458.