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