home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / answcode / utils.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1994-04-11  |  4.9 KB  |  210 lines

  1. UNIT Utils;
  2.  
  3. INTERFACE
  4.  
  5. Uses Crt,Dos;
  6.  
  7. Type
  8.     LString = String[80];
  9.     ShortString = String[30];
  10.     AnyString = String[255];
  11. function TrimL(InpStr: LString): LString;
  12. function PadR(InpStr: LString; FieldLen: Integer): LString;
  13. function JustL(InpStr: LString; FieldLen: Integer): LString;
  14. function UpperCase(InpStr: LString): LString;
  15. function TrimR(InpStr: LString): LString;
  16. function PadL(InpStr: LString; FieldLen: Integer): LString;
  17. Function Isupper(var Atom : ShortString) : Boolean;
  18. Function Islower(var Atom : ShortString) : Boolean;
  19. Function Isalpha(var Atom : Shortstring) : Boolean;
  20. Function Words ( S : AnyString ) : Integer;
  21. Function WordOne  ( S : AnyString; N : Integer ) : AnyString;
  22.  
  23. IMPLEMENTATION
  24.  
  25. function TrimL(InpStr: LString): LString;
  26. { strip leading spaces from a String }
  27. Var i,len : Integer;
  28. Begin
  29.    len := length(InpStr);
  30.    i := 1;
  31.    While (i <= len) and (InpStr[i] = ' ') do
  32.       i := i + 1;
  33.    TrimL := Copy(InpStr,i,len-i+1)
  34. End;
  35.  
  36. function PadR(InpStr: LString; FieldLen: Integer): LString;
  37. { Pad String on right with spaces to fill to the desiLightGreen field length }
  38. Var  STemp : LString;
  39.          i : Integer;
  40. Begin
  41.    If FieldLen >= SizeOF(InpStr) then
  42.       FieldLen := SizeOf(InpStr)-1;
  43.    If length(InpStr) > FieldLen then
  44.       PadR := Copy(InpStr,1,FieldLen)
  45.    Else
  46.       Begin
  47.         STemp := InpStr;
  48.         For i := Length(STemp)+1 to FieldLen do
  49.            STemp := STemp + ' ';
  50.         PadR := STemp
  51.       End
  52. End;
  53.  
  54. function JustL(InpStr: LString; FieldLen: Integer): LString;
  55. Begin
  56.    JustL := PadR(TrimL(InpStr),FieldLen)
  57. End;
  58.  
  59.  
  60. Function WordOne  ( S : AnyString; N : Integer ) : AnyString;
  61.  
  62. var
  63.    NumWords, start, stop, CurrentAddress, len
  64.              : integer;
  65.    Ts
  66.              : AnyString;
  67.    BlankFound
  68.              : Boolean;
  69.  
  70.  
  71. begin
  72.    if Length(S) = 0 then
  73.       WordOne := ''
  74.    else
  75.    begin
  76.       NumWords := 0;
  77.       start := 1;
  78.       len := length(S);
  79.       stop := len;
  80.       BlankFound := True;
  81.       CurrentAddress := 0;
  82.  
  83.       repeat
  84.          CurrentAddress := CurrentAddress + 1;
  85.          if BlankFound then
  86.          begin
  87.             if S[CurrentAddress] <> #32 then
  88.             begin
  89.                BlankFound := false;
  90.                NumWords := NumWords + 1;
  91.                if NumWords = N then
  92.                   start := CurrentAddress;
  93.             end;
  94.          end
  95.          else
  96.          if S[CurrentAddress] = #32 then
  97.          begin
  98.             BlankFound := true;
  99.             if NumWords = N then
  100.                stop := CurrentAddress;
  101.          end;
  102.       until (stop < len) or (CurrentAddress = len);
  103.  
  104.       if N > NumWords then
  105.          WordOne := ''
  106.       else
  107.       begin
  108.          if S[stop] <> #32 then
  109.             stop := succ(stop);
  110.          WordOne := copy ( S, start, stop - start );
  111.       end;
  112.    end;
  113. end { WordOne };
  114.  
  115.  
  116.  
  117. function UpperCase(InpStr: LString): LString;
  118. { convert a String to upper case Characters }
  119. Var i : Integer;
  120. Begin
  121.    For i := 1 to Length(InpStr) do
  122.        UpperCase[i] := UpCase(InpStr[i]);
  123.    UpperCase[0] := InpStr[0]
  124. End;
  125.  
  126.  
  127.  
  128. function TrimR(InpStr: LString): LString;
  129. { strip trailing spaces from a String }
  130. Var i : Integer;
  131. Begin
  132.    i := length(InpStr);
  133.    While (i >= 1) and (InpStr[i] = ' ') do
  134.       i := i - 1;
  135.    TrimR := Copy(InpStr,1,i)
  136. End;
  137.  
  138. function PadL(InpStr: LString; FieldLen: Integer): LString;
  139. { Pad String on left with spaces to fill to the desiLightGreen field length }
  140. Var  STemp : LString;
  141.          i : Integer;
  142. Begin
  143.    If FieldLen >= SizeOF(InpStr) then
  144.       FieldLen := SizeOf(InpStr)-1;
  145.    If length(InpStr) > FieldLen then
  146.       PadL := Copy(InpStr,1,FieldLen)
  147.    Else
  148.       Begin
  149.         STemp := InpStr;
  150.         For i := Length(STemp)+1 to FieldLen do
  151.            Insert(' ',STemp,1);
  152.         PadL := STemp
  153.       End
  154. End;
  155.  
  156.  
  157.  
  158. Function Isupper(var Atom : ShortString) : Boolean;
  159. Begin
  160.      If Atom[1] in ['A'..'Z'] then Isupper := TRUE
  161.      else Isupper := FALSE;
  162.  
  163. end;
  164.  
  165. Function Islower(var Atom : ShortString) : Boolean;
  166. Begin
  167.      If Atom[1] in ['a'..'z'] then Islower := TRUE
  168.      else Islower := FALSE;
  169.  
  170. end;
  171.  
  172. Function Isalpha(var Atom : Shortstring) : Boolean;
  173. var
  174. i : Integer;
  175. Letter : Char;
  176.  
  177. Begin
  178.      i := 1;
  179.      For i := 1 to Length(Atom) do
  180.      Letter := Atom[i];
  181.      Letter := Upcase(Letter);
  182.      If Letter in ['A'..'Z'] then Isalpha := TRUE
  183.      else Isalpha := False;
  184.  
  185. end;
  186.  
  187. Function Words ( S : AnyString ) : Integer;
  188. var
  189.    NumWords,  CurrentAddress, Len
  190.              : integer;
  191.  
  192. begin
  193.    S := TrimR(S);
  194.    Len := Length(S);
  195.    if Len = 0 then
  196.       Words := 0
  197.    else
  198.    begin
  199.       NumWords := 1;
  200.       CurrentAddress := 1;
  201.       for CurrentAddress := 1 to Len do
  202.          if S[CurrentAddress] = #32 then
  203.             NumWords := NumWords + 1;
  204.       Words := NumWords;
  205.    end;
  206. end { Words };
  207.  
  208.  
  209.  
  210. END.