home *** CD-ROM | disk | FTP | other *** search
/ ftp.update.uu.se / ftp.update.uu.se.2014.03.zip / ftp.update.uu.se / pub / rainbow / msdos / misc / messages.lzh / STRING.INC < prev   
Text File  |  1984-08-31  |  22KB  |  698 lines

  1. { Suplementry String functions and procedures for Turbo Pascal  }
  2.  
  3. (*
  4.        Written by: Tryg Helseth
  5.                    Minneapolis, Minnesota
  6.  
  7.     Last Revision: 1/4/85
  8.  
  9. USAGE NOTES:
  10.  
  11.   The following routines provide common string functions that are
  12.   not supplied with Turbo Pascal.  Many are patterned (and named)
  13.   after the General Electric Information Service COompany (GEISCO)
  14.   FORTRAN 77 string routines; others mimic SNOBOL primatives.
  15.  
  16.   The general calling sequence is:
  17.  
  18.      OutString := Func(InpString[,Parms])
  19.  
  20.   where:
  21.  
  22.      OutString = the output or target string,
  23.           Func = function name,
  24.         InpStr = Input String,
  25.        [Parms] = Additional parameter(s) used by some functions.
  26.  
  27. AVAILABLE FUNCTIONS:
  28.  
  29.     LoCase      Convert a single character to lower case.
  30.     LowerCase   Convert a string to lower case.
  31.     UpperCase   Convert a string to upper case.
  32.     TrimL       Trim Left: remove leading spaces from a string.
  33.     TrimR       Trim Right: remove trailing spaces from a string.
  34.     PadL        Pad Left: Add leading spaces to give desired field length.
  35.     PadR        Pad Right: Add trailing spaces to give desired field length.
  36.     JustL       Left Justify a string within a desired field length.
  37.     JustR       Right Justify a string within a desired field length.
  38.     Center      Center a string within a desired field length.
  39.     GetStr      Get String: Extracts a substring up to a specified delimiter.
  40.     Break       Extracts a substring up to the first of several delimters.
  41.     Span        Extracts a substring of delimiters up to a NON delimiter.
  42.  
  43.     Note: GetStr, Span, and Break, modify the input string.  The other
  44.           functions do not modify any parameters.
  45.  
  46. AVAILABLE PROCEDURES:
  47.  
  48.     GString     Get String: Used by Span and Break functions.  It performs
  49.                 both functions and allows more control by the programmer.
  50.  
  51.     RealStr     Convert a value of type REAL to a string representation in
  52.                 any base from 2 to 36.
  53.  
  54.     RealVal     Convert a string representation of a number to a REAL value.
  55.                 The number may be in any base from 2 to 36.
  56.  
  57. TYPE DECLARATION:
  58.  
  59.   All strings are of the type, LString, which should be declared in the main
  60.   program as:
  61.  
  62.       Type LString = string[n]
  63.  
  64.   where n is a constant in the range of 1 to 255.
  65.  
  66.   If you wish to use these functions with strings of different declared
  67.   lengths, then you must use the compiler option, {$V-}.  If you choose
  68.   to do this, be sure that the defined length of LString is greater than
  69.   or equal to the longest string you will be using.
  70.  
  71. FUNCTION DECLARATIONS:   *)
  72.  
  73. {===========================================}
  74. function LoCase(InChar: char): char; forward;
  75. {===========================================}
  76. {
  77. Purpose:        Convert a single character to lower case.
  78.  
  79. Parameters:
  80.      Input:     InChar = character to be converted.
  81.     Output:     none
  82.  
  83. Function Value: LoCase = converted character.
  84. }
  85.  
  86. {====================================================}
  87. function LowerCase(InpStr: LString): LString; forward;
  88. {====================================================}
  89. {
  90. Purpose:        Convert a string of characters to lower case.
  91.  
  92. Parameters:
  93.      Input:     InpStr = string to be converted.
  94.     Output:     none
  95.  
  96. Function Value: LowerCase = converted string.
  97. }
  98.  
  99. {====================================================}
  100. function UpperCase(InpStr: LString): LString; forward;
  101. {====================================================}
  102. {
  103. Purpose:        Convert a string of characters to upper case.
  104.  
  105. Parameters:
  106.      Input:     InpStr = string to be converted.
  107.     Output:     none
  108.  
  109. Function Value: UpperCase = converted string.
  110. }
  111.  
  112. {================================================}
  113. function TrimL(InpStr: LString): LString; forward;
  114. {================================================}
  115. {
  116. Purpose:        Trim Left: Remove leading spaces from a string.
  117.  
  118. Parameters:
  119.      Input:     InpStr = string to be trimmed.
  120.     Output:     none
  121.  
  122. Function Value: TrimL = trimmed string.
  123. }
  124.  
  125. {================================================}
  126. function TrimR(InpStr: LString): LString; forward;
  127. {================================================}
  128. {
  129. Purpose:        Trim Right: Remove trailing spaces from a string.
  130.  
  131. Parameters:
  132.      Input:     InpStr = string to be trimmed.
  133.     Output:     none
  134.  
  135. Function Value: TrimR = trimmed string.
  136. }
  137.  
  138. {==================================================================}
  139. function PadL(InpStr: LString; FieldLen: integer): LString; forward;
  140. {==================================================================}
  141. {
  142. Purpose:        Pad Left: Pad a string on the left with spaces to
  143.                 fill it to a desired field length.  Trailing spaces
  144.                 are not removed.
  145. Parameters:
  146.      Input:     InpStr = string to be padded.
  147.     Output:     none
  148.  
  149. Function Value: PadL = padded string.
  150. }
  151.  
  152. {==================================================================}
  153. function PadR(InpStr: LString; FieldLen: integer): LString; forward;
  154. {==================================================================}
  155. {
  156. Purpose:        Pad Right: Pad a string on the right with spaces to
  157.                 fill it to a desired field length.  Leading spaces
  158.                 are not removed.
  159. Parameters:
  160.      Input:     InpStr = string to be padded.
  161.     Output:     none
  162.  
  163. Function Value: PadR = padded string.
  164. }
  165.  
  166. {===================================================================}
  167. function JustL(InpStr: LString; FieldLen: integer): LString; forward;
  168. {===================================================================}
  169. {
  170. Purpose:        Left justify a string within a desired field length.
  171.                 First leading spaces are removed, then the string is
  172.                 padded with trailing spaces to the desired length.
  173. Parameters:
  174.      Input:     InpStr = string to be justified.
  175.     Output:     none
  176.  
  177. Function Value: JustL = justified string.
  178. }
  179.  
  180. {===================================================================}
  181. function JustR(InpStr: LString; FieldLen: integer): LString; forward;
  182. {===================================================================}
  183. {
  184. Purpose:        Right justify a string within a desired field length.
  185.                 First trailing spaces are removed, then leading spaces
  186.                 are inserted fill to the desired length.
  187. Parameters:
  188.      Input:     InpStr = string to be justified.
  189.     Output:     none
  190.  
  191. Function Value: JustR = justified string.
  192. }
  193.  
  194. {====================================================================}
  195. function Center(InpStr: LString; FieldLen: integer): LString; forward;
  196. {====================================================================}
  197. {
  198. Purpose:        Center a string within a desired field length.  First
  199.                 the string is stripped of leading and trailing spaces,
  200.                 [Only padded on left - 6/17/85 - Stew Stryker]
  201.                 then the resultant string is padded equally with
  202.                 leading and trailing spaces.
  203. Parameters:
  204.      Input:     InpStr = string to be justified.
  205.     Output:     none
  206.  
  207. Function Value: Center = centered string.
  208. }
  209.  
  210. {==================================================================}
  211. function GetStr(var InpStr: LString; Delim: Char): LString; forward;
  212. {==================================================================}
  213. {
  214. Purpose:       Strating at the first position of the input string,
  215.                return a substring containing all characters up to
  216.                (but not including) the fisrt occurence of the given
  217.                delimiter.  If the delimiter is not found, then the
  218.                entire input string is returned.  The substring and
  219.                delimiter are then deleted from the input string.
  220.  
  221. Parameters:
  222.      Input:     InpStr = string from which substring is removed.
  223.                 Delim  = delimiter to be used.
  224.     Output:     InStr  = remainder of input string.
  225.  
  226. Function Value: GetStr = Extracted substring.
  227. }
  228.  
  229. {=====================================================================}
  230. function Break(var InpStr: LString; DelStr: LString): LString; forward;
  231. {=====================================================================}
  232. {
  233. Purpose:       Emulates the SNOBOL BREAK function.  Operation is
  234.                similar to GetStr except that several delimiters
  235.                may be used.  The substring returns all characters
  236.                up to the first of any delimiter in DelStr.  Unlike
  237.                GetStr, the Delimiter found is NOT removed from
  238.                the input string.
  239.  
  240. Parameters:
  241.      Input:     InpStr = string from which substring is removed.
  242.                 DelStr = list of delimiters.
  243.     Output:     InStr  = remainder of input string.
  244.  
  245. Function Value: Break  = Extracted substring (Break on delimiter).
  246. }
  247.  
  248. {====================================================================}
  249. function Span(var InpStr: LString; DelStr: LString): LString; forward;
  250. {====================================================================}
  251. {
  252. Purpose:       Emulates the SNOBOL Span function.  Operation is
  253.                is the reverse of Break; The input string is scanned
  254.                for characters IN DelStr.  It returns a  substring
  255.                containing ONLY delimiters found starting at the
  256.                first position up the the first NON delimiter.  That
  257.                character is NOT removed from the input string.
  258.  
  259. Parameters:
  260.      Input:     InpStr = string from which substring is removed.
  261.                 DelStr = list of delimiters.
  262.     Output:     InStr  = remainder of input string.
  263.  
  264. Function Value: Span   = Extracted substring (Span of delimiters).
  265. }
  266.  
  267. {=======================================================================}
  268. procedure GString(InpStr, DelStr: LString; span: boolean;
  269.                   var cpos, dpos: integer; var OutStr: LString); forward;
  270. {=======================================================================}
  271. {
  272. Purpose:       Emulates both the SPAN and BREAK functions of SNOBOL.
  273.  
  274.                SPAN:  If span is true, then starting from position, cpos,
  275.                the input string is scanned for characters in the string,
  276.                DelStr.  These characters are copied to the output string
  277.                until either a character NOT in DelStr is found or the end
  278.                of the string is reached.  Position pointer, cpos, is reset
  279.                to point at the break character.  If the end of the string
  280.                is reached, cpos is set to zero.
  281.  
  282.                BREAK: If span is false, then the input string is scanned
  283.                for characters NOT in the string, DelStr.  The output string
  284.                contains all characters up to the first delimiter.  Position
  285.                pointer, cpos, is set to point at the delimiter found.  If a
  286.                delimiter was not found, cpos is set to zero.
  287.  
  288.                Dpos is set to position in DelStr of the delimiter found.  If
  289.                none found, dpos is set to zero.
  290.  
  291. Parameters:
  292.      Input:     InpStr = string from which subs9ring is Copied.
  293.                 DelStr = delimiters to be used.
  294.                 span   = true = span, false = break.
  295.                 cpos   = starting position in input string.
  296.  
  297.     Output:     cpos   = position past found delimiter.
  298.                 dpos   = which delimiter was found.
  299.                 OutStr = substring copied from the input string.
  300. }
  301.  
  302. {=================================================}
  303. Procedure RealStr(Valu: Real; Base, Trail: integer;
  304.                   var OutStr: LString); forward;
  305. {=================================================}
  306. {
  307. Purpose:        Convert a real value to an equivalent string representation.
  308.                 The value can be represented in any base from 1 to 36 with
  309.                 a specified number of digits to the right of the radix point.
  310.                 Digits 10 thru 35 are represeted by the letters A thru Z.
  311.  
  312. Parameters:
  313.  
  314.      Input:     Valu   = Real value to be converted to a string.
  315.                 Base   = Desired base.
  316.                 Trail  = number of digits to the right of the radix point.
  317.  
  318.     Output:     OutStr = string representation.
  319. }
  320.  
  321. {===========================================================}
  322. Procedure RealVal(InpStr: LString; Base: integer;
  323.                   Var Err: integer; Var Valu: real); forward;
  324. {===========================================================}
  325. {
  326. Purpose:        Convert a string representation of a number to a real value.
  327.                 The value can be represented in any base from 1 to 36 and
  328.                 can have a fractional part.  Digits 10 thru 35 are represeted
  329.                 by the letters A thru Z respectively.  If an illegal
  330.                 character is encounterd, conversion halts and the error
  331.                 postion is reported through the variable, Err.
  332.  
  333. Parameters:
  334.  
  335.      Input:     InpStr = String representation to be converted to a real value.
  336.                 Base   = Base the value is represented in.
  337.  
  338.     Output:     Err    = position of illegial character; set to zero
  339.                          if no error is encountered.
  340.                 Valu   = converted value.
  341. }
  342.  
  343. {
  344. FUNCTION BODIES:
  345. }
  346.  
  347. {==============}
  348. function LoCase;
  349. {==============}
  350. { convert a character to lower case }
  351. begin
  352.    if InChar IN ['A'..'Z'] then
  353.       LoCase := Chr(Ord(Inchar)+32)
  354.    else
  355.       LoCase := InChar
  356. end;
  357.  
  358. {=================}
  359. function LowerCase;
  360. {=================}
  361.  
  362. { convert a string to lower case characters }
  363.  
  364. var i : integer;
  365.  
  366. begin
  367.    for i := 1 to Length(InpStr) do
  368.        LowerCase[i] := LoCase(InpStr[i]);
  369.    LowerCase[0] := InpStr[0]
  370. end;
  371.  
  372. {=================}
  373. function UpperCase;
  374. {=================}
  375.  
  376. { convert a string to upper case characters }
  377.  
  378. var i : integer;
  379.  
  380. begin
  381.    for i := 1 to Length(InpStr) do
  382.        UpperCase[i] := UpCase(InpStr[i]);
  383.    UpperCase[0] := InpStr[0]
  384. end;
  385.  
  386. {=============}
  387. function TrimL;
  388. {=============}
  389.  
  390. { strip leading spaces from a string }
  391.  
  392. var i,len : integer;
  393.  
  394. begin
  395.    len := length(InpStr);
  396.    i := 1;
  397.    while (i <= len) and (InpStr[i] = ' ') do
  398.       i := i + 1;
  399.    TrimL := Copy(InpStr,i,len-i+1)
  400. end;
  401.  
  402. {=============}
  403. function TrimR;
  404. {=============}
  405.  
  406. { strip trailing spaces from a string }
  407.  
  408. var i : integer;
  409.  
  410. begin
  411.    i := length(InpStr);
  412.    while (i >= 1) and (InpStr[i] = ' ') do
  413.       i := i - 1;
  414.    TrimR := Copy(InpStr,1,i)
  415. end;
  416.  
  417. {============}
  418. function PadL;
  419. {============}
  420.  
  421. { Pad string on left with spaces to fill to the desired field length }
  422.  
  423. var  STemp : LString;
  424.          i : integer;
  425.  
  426. begin
  427.    If FieldLen >= SizeOF(InpStr) then FieldLen := SizeOf(InpStr)-1;
  428.    if length(InpStr) > FieldLen then
  429.       PadL := Copy(InpStr,1,FieldLen)
  430.    else begin
  431.       STemp := InpStr;
  432.       for i := Length(STemp)+1 to FieldLen do
  433.          Insert(' ',STemp,1);
  434.       PadL := STemp
  435.       end
  436. end;
  437.  
  438. {============}
  439. function PadR;
  440. {============}
  441.  
  442. { Pad string on right with spaces to fill to the desired field length }
  443.  
  444. var  STemp : LString;
  445.          i : integer;
  446.  
  447. begin
  448.    If FieldLen >= SizeOF(InpStr) then FieldLen := SizeOf(InpStr)-1;
  449.    if length(InpStr) > FieldLen then
  450.       PadR := Copy(InpStr,1,FieldLen)
  451.    else begin
  452.       STemp := InpStr;
  453.       for i := Length(STemp)+1 to FieldLen do
  454.          STemp := STemp + ' ';
  455.       PadR := STemp
  456.       end
  457. end;
  458.  
  459. {=============}
  460. function JustL;
  461. {=============}
  462.  
  463. { Left justify the string within the given field length }
  464.  
  465. begin
  466.    JustL := PadR(TrimL(InpStr),FieldLen)
  467. end;
  468.  
  469. {=============}
  470. function JustR;
  471. {=============}
  472.  
  473. { Right justify the string within the given field length }
  474.  
  475. begin
  476.    JustR := PadL(TrimR(InpStr),FieldLen)
  477. end;
  478.  
  479. {==============}
  480. function Center;
  481. {==============}
  482.  
  483. { Center a string within a specified field length;  the string
  484.   is padded on both sides with spaces }
  485.  
  486. var LeadSpaces : integer;
  487.         STemp : LString;
  488. begin
  489.    { strip leading and trailing spaces; determine the
  490.      Number of spaces needed to center the string }
  491.  
  492.    STemp := TrimR(TrimL(InpStr));
  493.    LeadSpaces := (FieldLen - Length(STemp) + 1) div 2;
  494.  
  495.    { insert leading spaces then trailing spaces }
  496.    Center := PadL(STemp,FieldLen-LeadSpaces)
  497. end;
  498.  
  499. {==============}
  500. function GetStr;
  501. {==============}
  502.  
  503. { Return a string containing all characters starting at the
  504.   first position of the source string up to the first delimiter.
  505. }
  506.  
  507. var i : integer;
  508. begin
  509.    i := Pos(Delim,InpStr);
  510.    if i = 0 then begin
  511.       GetStr := InpStr;
  512.       InpStr := ''
  513.       end
  514.    else begin
  515.       GetStr := Copy(InpStr,1,i-1);
  516.       Delete(InpStr,1,i)
  517.       end
  518. end;
  519.  
  520. {=============}
  521. function Break;
  522. {=============}
  523.  
  524. { Emulate SNOBOL BREAK function }
  525.  
  526. var cp, dp : integer;
  527.     OutStr : LString;
  528.  
  529. begin
  530.    cp := 1;
  531.    GString(InpStr,DelStr,false,cp,dp,OutStr);
  532.    Break := OutStr;
  533.    if cp = 0 then
  534.       InpStr := ''
  535.    else
  536.       Delete(InpStr,1,cp-1)
  537. end;
  538.  
  539. {============}
  540. function Span;
  541. {============}
  542.  
  543. { Emulate SNOBOL SPAN function }
  544.  
  545. var cp, dp : integer;
  546.     OutStr : LString;
  547.  
  548. begin
  549.    cp := 1;
  550.    GString(InpStr,DelStr,true,cp,dp,OutStr);
  551.    Span := OutStr;
  552.    if cp = 0 then
  553.       InpStr := ''
  554.    else
  555.       Delete(InpStr,1,cp-1)
  556. end;
  557.  
  558. {================}
  559. procedure GString;
  560. {================}
  561.  
  562. { Return a string containing all characters starting at position, cpos,
  563.  of the source string up to the first first occurence of any of several
  564.  delimiters.  The position of the found delimiter is returned as well
  565.  as which delimiter.
  566. }
  567. var done : boolean;
  568.  
  569. begin
  570.    OutStr := ''; dpos := 0;
  571.    if cpos > 0 then begin
  572.       done := false;
  573.       while (cpos <= Length(InpStr)) and not done do begin
  574.          dpos := pos(InpStr[cpos],DelStr);
  575.          if span xor (dpos = 0) then begin
  576.             OutStr := OutStr + InpStr[cpos];
  577.             cpos := cpos + 1
  578.             end
  579.          else
  580.             done := true
  581.          end;
  582.       if (span xor (dpos = 0)) or (cpos > length(InpStr)) then cpos := 0
  583.       end
  584. end;
  585.  
  586. {================}
  587. procedure RealStr;
  588. {================}
  589.  
  590. { Convert a real value to a string }
  591.  
  592. var     i, digit, MaxLen : integer;
  593.        IntValu, FracValu : real;
  594.                     Sign : boolean;
  595.  
  596. {-----------------------------------}
  597. function NewDigit(num:integer): char;
  598. {-----------------------------------}
  599.  
  600. begin
  601.    if num < 10 then
  602.       NewDigit := chr(num + ord('0'))
  603.    else
  604.       NewDigit := chr(num + ord('A') - 10)
  605. end;
  606.  
  607. begin
  608.    MaxLen := SizeOf(OutStr);
  609.    if Valu < 0 then begin
  610.       Valu := - Valu;
  611.       Sign := true
  612.       end
  613.    else
  614.       Sign := false;
  615.    IntValu := Int(Valu);
  616.    FracValu := Frac(Valu);
  617.    if Valu < 1 then
  618.       OutStr := '0'
  619.    else begin
  620.       { convert Leading digits to a string }
  621.       OutStr := '';
  622.       While (IntValu >= 1) and (Length(OutStr) < MaxLen) do begin
  623.          Valu := IntValu / Base;
  624.          Digit := Trunc(Round(Frac(Valu)*Base));
  625.          IntValu := Int(Valu);
  626.          Insert(NewDigit(digit),OutStr,1);
  627.          end
  628.       end;
  629.    if (Trail > 0) and ( length(OutStr) < MaxLen) then begin
  630.       { convert trialing digits }
  631.       OutStr := OutStr + '.';
  632.       i := 1;
  633.       While (Length(OutStr) < MaxLen) and (i <= Trail) do begin
  634.          Valu := FracValu * Base;
  635.          Digit := Trunc(Valu);
  636.          FracValu := Frac(Valu);
  637.          OutStr := OutStr + NewDigit(Digit);
  638.          i := i + 1
  639.          end
  640.       end;
  641.     if sign then Insert('-',OutStr,1);
  642. end;
  643.  
  644. {================}
  645. procedure RealVal;
  646. {================}
  647.  
  648. { convert a string to a real value }
  649.  
  650. var          i, digit : integer;
  651.       GotRadixPoint,
  652.       GotDigit,Negate : boolean;
  653.                InChar : char;
  654.               InvBase : real;
  655. begin
  656.    Valu := 0; Err := 0; negate := false; i := 0;
  657.    InvBase := 1; GotRadixPoint := false;
  658.  
  659.    while (i < length(InpStr)) and (err = 0) do begin
  660.       i := i + 1;
  661.       GotDigit := false;
  662.       InChar := UpCase(InpStr[i]);
  663.       case InChar of
  664.       '0'..'9': begin
  665.                    digit := ord(InpStr[i]) - ord('0');
  666.                    GotDigit := true
  667.                    end;
  668.       'A'..'Z': begin
  669.                    digit := ord(InChar) - ord('A') + 10;
  670.                    GotDigit := true
  671.                    end;
  672.           '-' : begin
  673.                    if negate then
  674.                       err := i
  675.                    else
  676.                       negate := true
  677.                    end;
  678.           '+' : if negate then err := i;
  679.           '.' : if GotRadixPoint then
  680.                       err := i
  681.                    else
  682.                       GotRadixPoint := true;
  683.          else    err := i
  684.          end  {case} ;
  685.       if GotDigit then
  686.          if digit >= base then
  687.             err := i
  688.          else
  689.             if GotRadixPoint then begin
  690.                InvBase := InvBase / base;
  691.                Valu := Valu + InvBase * digit
  692.                end
  693.             else
  694.                Valu := Valu * base + digit
  695.       end; { while }
  696.    if negate then valu := - valu;
  697. end;
  698.