home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / CONSULTP.ZIP / LIBRARY.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-06-24  |  9.4 KB  |  403 lines

  1.  
  2. Unit Library;
  3.  
  4. INTERFACE
  5. {$R-}    {Range checking off}
  6. {$B+}    {Boolean complete evaluation on}
  7. {$S+}    {Stack checking on}
  8. {$I+}    {I/O checking on}
  9. {$N-}    {No numeric coprocessor}
  10. {$M 65500,16384,655360} {Turbo 3 default stack and heap}
  11.  
  12.  
  13. {Library of Turbo Pascal Routines}
  14. {Brian Corll November 27, 1987}
  15.  
  16.  
  17.  
  18. Uses
  19.   Crt,
  20.   Dos;
  21.  
  22. Type
  23.   TextArrayType = Array[1..3000] of String[6];
  24.   ParseType = String[6];
  25.   ShortString = String[30];
  26.   String80 = String[80];
  27.   Regs =
  28.              Record Case Boolean of
  29.              True : (al,ah,bl,bh,cl,ch,dl,dh : Byte);
  30.              False : (ax, bx, cx, dx, bp,si,di,ds,es,Flags : Registers);
  31.                  end;
  32. Procedure Off;
  33. Procedure On;
  34. procedure Beep(Tone,Duration : integer);
  35. function UpperCase(InpStr: String80): String80;
  36. function TrimL(InpStr: String80): String80;
  37. function TrimR(InpStr: String80): String80;
  38. function PadR(InpStr: String80; FieldLen: Integer): String80;
  39. Procedure BlockCurs;
  40. Function Words ( S : String ) : Integer;
  41. Function OneWord  ( S : String; N : Integer ) : String;
  42. Procedure FindWord(var TextArray : TextArrayType;LineCount,Position : Integer;
  43.           TextKey : ParseType;var Result : Integer);
  44.  
  45. IMPLEMENTATION
  46.  
  47. Procedure On;
  48. begin
  49.   Inline($31/$C0/$8E/$C0/$26/$A1/$60/$04/$80/$E4/$DF/$89/$C1/$B4/$01/$CD/$10);
  50. end;
  51.  
  52. Procedure Off;
  53. begin
  54.   Inline($31/$C0/$8E/$C0/$26/$A1/$60/$04/$80/$E4/$BF/$80/$CC/$20/$89/$C1/$B4/
  55.     $01/$CD/$10);
  56. end;
  57.  
  58. Function STRIP ( S : String80;
  59.                  C : Char) : String80;
  60.                  { Removes all leading and trailing
  61.                    C characters from S }
  62. begin
  63.    InLine ( $1E/
  64.             $8D/$7E/$07/
  65.             $8A/$4E/$06/
  66.             $30/$ED/
  67.             $8C/$D0/
  68.             $8E/$C0/
  69.             $8B/$46/$04/
  70.             $83/$F9/$01/
  71.             $77/$0E/
  72.             $8A/$5E/$07/
  73.             $30/$FF/
  74.             $39/$D8/
  75.             $74/$35/
  76.             $8B/$D7/
  77.             $EB/$1D/$90/
  78.             $FC/
  79.             $F3/$AE/
  80.             $E3/$2B/
  81.             $4F/
  82.             $8B/$D7/
  83.             $8A/$4E/$06/
  84.             $30/$ED/
  85.             $8D/$7E/$07/
  86.             $01/$CF/
  87.             $4F/
  88.             $FD/
  89.             $F3/$AE/
  90.             $47/
  91.             $8B/$CF/
  92.             $29/$D1/
  93.             $41/
  94.             $88/$8E/$06/$01/
  95.             $8B/$F2/
  96.             $8D/$BE/$07/$01/
  97.             $8C/$D0/
  98.             $8E/$D8/
  99.             $FC/
  100.             $F3/$A4/
  101.             $EB/$07/$90/
  102.             $C7/$86/$06/$01/$00/$00/
  103.             $1F/$5D);
  104. end;
  105. procedure Beep(Tone,Duration : integer);
  106.  begin
  107.    Sound(Tone); Delay(Duration); NoSound;
  108.  end;
  109.  
  110. function LoCase(InChar: Char): Char;
  111. { convert a Character to lower case }
  112. Begin
  113.    If InChar IN ['A'..'Z'] then
  114.       LoCase := Chr(Ord(InChar)+32)
  115.    Else
  116.       LoCase := InChar
  117. End;
  118.  
  119. function LowerCase(InpStr: String80): String80;
  120. { convert a String to lower case Characters }
  121. Var i : Integer;
  122. Begin
  123.    For i := 1 to Length(InpStr) do
  124.        LowerCase[i] := LoCase(InpStr[i]);
  125.    LowerCase[0] := InpStr[0]
  126. End;
  127.  
  128. function UpperCase(InpStr: String80): String80;
  129. { convert a String to upper case Characters }
  130. Var i : Integer;
  131. Begin
  132.    For i := 1 to Length(InpStr) do
  133.        UpperCase[i] := UpCase(InpStr[i]);
  134.    UpperCase[0] := InpStr[0]
  135. End;
  136.  
  137. function TrimL(InpStr: String80): String80;
  138. { strip leading spaces from a String }
  139. Var i,len : Integer;
  140. Begin
  141.    len := length(InpStr);
  142.    i := 1;
  143.    While (i <= len) and (InpStr[i] = ' ') do
  144.       i := i + 1;
  145.    TrimL := Copy(InpStr,i,len-i+1)
  146. End;
  147.  
  148. function TrimR(InpStr: String80): String80;
  149. { strip trailing spaces from a String }
  150. Var i : Integer;
  151. Begin
  152.    i := length(InpStr);
  153.    While (i >= 1) and (InpStr[i] = ' ') do
  154.       i := i - 1;
  155.    TrimR := Copy(InpStr,1,i)
  156. End;
  157.  
  158. function PadL(InpStr: String80; FieldLen: Integer): String80;
  159. { Pad String on left with spaces to fill to the desired field length }
  160. Var  STemp : String80;
  161.          i : Integer;
  162. Begin
  163.    If FieldLen >= SizeOF(InpStr) then
  164.       FieldLen := SizeOf(InpStr)-1;
  165.    If length(InpStr) > FieldLen then
  166.       PadL := Copy(InpStr,1,FieldLen)
  167.    Else
  168.       Begin
  169.         STemp := InpStr;
  170.         For i := Length(STemp)+1 to FieldLen do
  171.            Insert(' ',STemp,1);
  172.         PadL := STemp
  173.       End
  174. End;
  175.  
  176. function PadR(InpStr: String80; FieldLen: Integer): String80;
  177. { Pad String on right with spaces to fill to the desired field length }
  178. Var  STemp : String80;
  179.          i : Integer;
  180. Begin
  181.    If FieldLen >= SizeOF(InpStr) then
  182.       FieldLen := SizeOf(InpStr)-1;
  183.    If length(InpStr) > FieldLen then
  184.       PadR := Copy(InpStr,1,FieldLen)
  185.    Else
  186.       Begin
  187.         STemp := InpStr;
  188.         For i := Length(STemp)+1 to FieldLen do
  189.             STemp := STemp + ' ';
  190.         PadR := STemp
  191.       End
  192. End;
  193.  
  194. function JustL(InpStr: String80; FieldLen: Integer): String80;
  195. { Left justify the String within the given field length }
  196. Begin
  197.    JustL := PadR(TrimL(InpStr),FieldLen)
  198. End;
  199.  
  200. function JustR(InpStr: String80; FieldLen: Integer): String80;
  201. { Right justify the String within the given field length }
  202. Begin
  203.    JustR := PadL(TrimR(InpStr),FieldLen)
  204. End;
  205.  
  206.  
  207.  
  208. Function Isupper(var Atom : ShortString) : Boolean;
  209. Begin
  210.      If Atom[1] in ['A'..'Z'] then Isupper := TRUE
  211.      else Isupper := FALSE;
  212.  
  213. end;
  214.  
  215. Function Islower(var Atom : ShortString) : Boolean;
  216. Begin
  217.      If Atom[1] in ['a'..'z'] then Islower := TRUE
  218.      else Islower := FALSE;
  219.  
  220. end;
  221.  
  222. Function Isalpha(var Atom : Shortstring) : Boolean;
  223. var
  224. i : Integer;
  225. Letter : Char;
  226.  
  227. Begin
  228.      i := 1;
  229.      For i := 1 to Length(Atom) do
  230.      Letter := Atom[i];
  231.      Letter := Upcase(Letter);
  232.      If Letter in ['A'..'Z'] then Isalpha := TRUE
  233.      else Isalpha := False;
  234.  
  235. end;
  236.  
  237. { ---------------------------------------
  238.   WORDS returns the number of words in S.
  239.   --------------------------------------- }
  240. Function Words ( S : String ) : Integer;
  241. var
  242.    NumWords,  CurrentAddress, Len
  243.              : integer;
  244.  
  245. begin
  246.    S := TrimL(TrimR(S));
  247.    Len := Length(S);
  248.    if Len = 0 then
  249.       Words := 0
  250.    else
  251.    begin
  252.       NumWords := 1;
  253.       CurrentAddress := 1;
  254.       for CurrentAddress := 1 to Len do
  255.          if S[CurrentAddress] = #32 then
  256.             NumWords := NumWords + 1;
  257.       Words := NumWords;
  258.    end;
  259. end { Words };
  260.  
  261. { ----------------------------
  262.   WORDIND returns the position
  263.   of WordNumber in S.
  264.   ---------------------------- }
  265. Function WordInd (          S : String80;
  266.                    WordNumber : Integer ) : Integer;
  267.  
  268. { Example: if S = 'I like Turbo Pascal' then
  269.               WordInd ( S, 3 ) = 8.  }
  270.  
  271. var
  272.    NumWords,  CurrentAddress, Len, Index
  273.              : integer;
  274.    NonBlank :  Boolean;
  275.  
  276. begin
  277.    Len := Length(S);
  278.    if Len = 0 then
  279.       WordInd := 0
  280.    else
  281.    begin
  282.       Index := 0;
  283.       NumWords := 0;
  284.       CurrentAddress := 0;
  285.       NonBlank := false;
  286.       repeat
  287.          CurrentAddress := CurrentAddress + 1;
  288.          if NonBlank then
  289.          begin
  290.             if S[CurrentAddress] = #32 then
  291.                NonBlank := false;
  292.          end
  293.          else
  294.          if S[CurrentAddress] <> #32 then
  295.          begin
  296.             NumWords := NumWords + 1;
  297.             if NumWords = WordNumber then
  298.                Index := CurrentAddress;
  299.             NonBlank := true;
  300.          end;
  301.       until (CurrentAddress = Len) or (Index > 0);
  302.       WordInd := Index;
  303.    end;
  304. end { WordInd };
  305.  
  306. Procedure BlockCurs;
  307. Var
  308.    Reg : Registers;
  309.  
  310. begin
  311.      with Reg do
  312.      begin
  313.      ch := 01;
  314.      cl := 12;
  315.      ah := 1;
  316.      Intr($10,Reg)
  317.      end
  318. end;
  319.  
  320. Function OneWord  ( S : String; N : Integer ) : String;
  321.  
  322. var
  323.    NumWords, start, stop, CurrentAddress, len
  324.              : integer;
  325.    Ts
  326.              : String80;
  327.    BlankFound
  328.              : Boolean;
  329.  
  330.  
  331. begin
  332.    if Length(S) = 0 then
  333.       OneWord := ''
  334.    else
  335.    begin
  336.       NumWords := 0;
  337.       start := 1;
  338.       len := length(S);
  339.       stop := len;
  340.       BlankFound := True;
  341.       CurrentAddress := 0;
  342.  
  343.       repeat
  344.          CurrentAddress := CurrentAddress + 1;
  345.          if BlankFound then
  346.          begin
  347.             if S[CurrentAddress] <> #32 then
  348.             begin
  349.                BlankFound := false;
  350.                NumWords := NumWords + 1;
  351.                if NumWords = N then
  352.                   start := CurrentAddress;
  353.             end;
  354.          end
  355.          else
  356.          if S[CurrentAddress] = #32 then
  357.          begin
  358.             BlankFound := true;
  359.             if NumWords = N then
  360.                stop := CurrentAddress;
  361.          end;
  362.       until (stop < len) or (CurrentAddress = len);
  363.  
  364.       if N > NumWords then
  365.          OneWord := ''
  366.       else
  367.       begin
  368.          if S[stop] <> #32 then
  369.             stop := succ(stop);
  370.          OneWord := copy ( S, start, stop - start );
  371.       end;
  372.    end;
  373. end { OneWord };
  374.  
  375. Procedure FindWord(var TextArray : TextArrayType;LineCount,Position : Integer;
  376.           TextKey : ParseType;var Result : Integer);
  377.  
  378.  
  379. var
  380.    Low,High,J,Width : Integer;
  381.  
  382. begin
  383.    Result := -1;
  384.    Width := Length(TextKey);
  385.    if width <1 then exit;
  386.    low := 1;
  387.    high := LineCount;
  388.    while high>=low do
  389.    begin
  390.         J := (low + high) div 2;
  391.         if textkey<copy(textarray[J],Position,width) then
  392.         high := j-1
  393.         else
  394.         if textkey>copy(textarray[J],position,width) then
  395.         low := j+1
  396.         else
  397.         begin
  398.         result :=j;
  399.         exit
  400.         end
  401.         end
  402.         end;
  403. END.