home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / TURBSTRG.ZIP / STRING.INC < prev   
Encoding:
Text File  |  1985-01-13  |  20.9 KB  |  697 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.                 then the resultant string is padded equally with
  201.                 leading and trailing spaces.
  202. Parameters:
  203.      Input:     InpStr = string to be justified.
  204.     Output:     none
  205.  
  206. Function Value: Center = centered string.
  207. }
  208.  
  209. {==================================================================}
  210. function GetStr(var InpStr: LString; Delim: Char): LString; forward;
  211. {==================================================================}
  212. {
  213. Purpose:       Strating at the first position of the input string,
  214.                return a substring containing all characters up to
  215.                (but not including) the fisrt occurence of the given
  216.                delimiter.  If the delimiter is not found, then the
  217.                entire input string is returned.  The substring and
  218.                delimiter are then deleted from the input string.
  219.  
  220. Parameters:
  221.      Input:     InpStr = string from which substring is removed.
  222.                 Delim  = delimiter to be used.
  223.     Output:     InStr  = remainder of input string.
  224.  
  225. Function Value: GetStr = Extracted substring.
  226. }
  227.  
  228. {=====================================================================}
  229. function Break(var InpStr: LString; DelStr: LString): LString; forward;
  230. {=====================================================================}
  231. {
  232. Purpose:       Emulates the SNOBOL BREAK function.  Operation is
  233.                similar to GetStr except that several delimiters
  234.                may be used.  The substring returns all characters
  235.                up to the first of any delimiter in DelStr.  Unlike
  236.                GetStr, the Delimiter found is NOT removed from
  237.                the input string.
  238.  
  239. Parameters:
  240.      Input:     InpStr = string from which substring is removed.
  241.                 DelStr = list of delimiters.
  242.     Output:     InStr  = remainder of input string.
  243.  
  244. Function Value: Break  = Extracted substring (Break on delimiter).
  245. }
  246.  
  247. {====================================================================}
  248. function Span(var InpStr: LString; DelStr: LString): LString; forward;
  249. {====================================================================}
  250. {
  251. Purpose:       Emulates the SNOBOL Span function.  Operation is
  252.                is the reverse of Break; The input string is scanned
  253.                for characters IN DelStr.  It returns a  substring
  254.                containing ONLY delimiters found starting at the
  255.                first position up the the first NON delimiter.  That
  256.                character is NOT removed from the input string.
  257.  
  258. Parameters:
  259.      Input:     InpStr = string from which substring is removed.
  260.                 DelStr = list of delimiters.
  261.     Output:     InStr  = remainder of input string.
  262.  
  263. Function Value: Span   = Extracted substring (Span of delimiters).
  264. }
  265.  
  266. {=======================================================================}
  267. procedure GString(InpStr, DelStr: LString; span: boolean;
  268.                   var cpos, dpos: integer; var OutStr: LString); forward;
  269. {=======================================================================}
  270. {
  271. Purpose:       Emulates both the SPAN and BREAK functions of SNOBOL.
  272.  
  273.                SPAN:  If span is true, then starting from position, cpos,
  274.                the input string is scanned for characters in the string,
  275.                DelStr.  These characters are copied to the output string
  276.                until either a character NOT in DelStr is found or the end
  277.                of the string is reached.  Position pointer, cpos, is reset
  278.                to point at the break character.  If the end of the string
  279.                is reached, cpos is set to zero.
  280.  
  281.                BREAK: If span is false, then the input string is scanned
  282.                for characters NOT in the string, DelStr.  The output string
  283.                contains all characters up to the first delimiter.  Position
  284.                pointer, cpos, is set to point at the delimiter found.  If a
  285.                delimiter was not found, cpos is set to zero.
  286.  
  287.                Dpos is set to position in DelStr of the delimiter found.  If
  288.                none found, dpos is set to zero.
  289.  
  290. Parameters:
  291.      Input:     InpStr = string from which subs9ring is Copied.
  292.                 DelStr = delimiters to be used.
  293.                 span   = true = span, false = break.
  294.                 cpos   = starting position in input string.
  295.  
  296.     Output:     cpos   = position past found delimiter.
  297.                 dpos   = which delimiter was found.
  298.                 OutStr = substring copied from the input string.
  299. }
  300.  
  301. {=================================================}
  302. Procedure RealStr(Valu: Real; Base, Trail: integer;
  303.                   var OutStr: LString); forward;
  304. {=================================================}
  305. {
  306. Purpose:        Convert a real value to an equivalent string representation.
  307.                 The value can be represented in any base from 1 to 36 with
  308.                 a specified number of digits to the right of the radix point.
  309.                 Digits 10 thru 35 are represeted by the letters A thru Z.
  310.  
  311. Parameters:
  312.  
  313.      Input:     Valu   = Real value to be converted to a string.
  314.                 Base   = Desired base.
  315.                 Trail  = number of digits to the right of the radix point.
  316.  
  317.     Output:     OutStr = string representation.
  318. }
  319.  
  320. {===========================================================}
  321. Procedure RealVal(InpStr: LString; Base: integer;
  322.                   Var Err: integer; Var Valu: real); forward;
  323. {===========================================================}
  324. {
  325. Purpose:        Convert a string representation of a number to a real value.
  326.                 The value can be represented in any base from 1 to 36 and
  327.                 can have a fractional part.  Digits 10 thru 35 are represeted
  328.                 by the letters A thru Z respectively.  If an illegial
  329.                 character is encounterd, conversion halts and the error
  330.                 postion is reported through the variable, Err.
  331.  
  332. Parameters:
  333.  
  334.      Input:     InpStr = String representation to be converted to a real value.
  335.                 Base   = Base the value is represented in.
  336.  
  337.     Output:     Err    = position of illegial character; set to zero
  338.                          if no error is encountered.
  339.                 Valu   = converted value.
  340. }
  341.  
  342. {
  343. FUNCTION BODIES:
  344. }
  345.  
  346. {==============}
  347. function LoCase;
  348. {==============}
  349. { convert a character to lower case }
  350. begin
  351.    if InChar IN ['A'..'Z'] then
  352.       LoCase := Chr(Ord(Inchar)+32)
  353.    else
  354.       LoCase := InChar
  355. end;
  356.  
  357. {=================}
  358. function LowerCase;
  359. {=================}
  360.  
  361. { convert a string to lower case characters }
  362.  
  363. var i : integer;
  364.  
  365. begin
  366.    for i := 1 to Length(InpStr) do
  367.        LowerCase[i] := LoCase(InpStr[i]);
  368.    LowerCase[0] := InpStr[0]
  369. end;
  370.  
  371. {=================}
  372. function UpperCase;
  373. {=================}
  374.  
  375. { convert a string to upper case characters }
  376.  
  377. var i : integer;
  378.  
  379. begin
  380.    for i := 1 to Length(InpStr) do
  381.        UpperCase[i] := UpCase(InpStr[i]);
  382.    UpperCase[0] := InpStr[0]
  383. end;
  384.  
  385. {=============}
  386. function TrimL;
  387. {=============}
  388.  
  389. { strip leading spaces from a string }
  390.  
  391. var i,len : integer;
  392.  
  393. begin
  394.    len := length(InpStr);
  395.    i := 1;
  396.    while (i <= len) and (InpStr[i] = ' ') do
  397.       i := i + 1;
  398.    TrimL := Copy(InpStr,i,len-i+1)
  399. end;
  400.  
  401. {=============}
  402. function TrimR;
  403. {=============}
  404.  
  405. { strip trailing spaces from a string }
  406.  
  407. var i : integer;
  408.  
  409. begin
  410.    i := length(InpStr);
  411.    while (i >= 1) and (InpStr[i] = ' ') do
  412.       i := i - 1;
  413.    TrimR := Copy(InpStr,1,i)
  414. end;
  415.  
  416. {============}
  417. function PadL;
  418. {============}
  419.  
  420. { Pad string on left with spaces to fill to the desired field length }
  421.  
  422. var  STemp : LString;
  423.          i : integer;
  424.  
  425. begin
  426.    If FieldLen >= SizeOF(InpStr) then FieldLen := SizeOf(InpStr)-1;
  427.    if length(InpStr) > FieldLen then
  428.       PadL := Copy(InpStr,1,FieldLen)
  429.    else begin
  430.       STemp := InpStr;
  431.       for i := Length(STemp)+1 to FieldLen do
  432.          Insert(' ',STemp,1);
  433.       PadL := STemp
  434.       end
  435. end;
  436.  
  437. {============}
  438. function PadR;
  439. {============}
  440.  
  441. { Pad string on right with spaces to fill to the desired field length }
  442.  
  443. var  STemp : LString;
  444.          i : integer;
  445.  
  446. begin
  447.    If FieldLen >= SizeOF(InpStr) then FieldLen := SizeOf(InpStr)-1;
  448.    if length(InpStr) > FieldLen then
  449.       PadR := Copy(InpStr,1,FieldLen)
  450.    else begin
  451.       STemp := InpStr;
  452.       for i := Length(STemp)+1 to FieldLen do
  453.          STemp := STemp + ' ';
  454.       PadR := STemp
  455.       end
  456. end;
  457.  
  458. {=============}
  459. function JustL;
  460. {=============}
  461.  
  462. { Left justify the string within the given field length }
  463.  
  464. begin
  465.    JustL := PadR(TrimL(InpStr),FieldLen)
  466. end;
  467.  
  468. {=============}
  469. function JustR;
  470. {=============}
  471.  
  472. { Right justify the string within the given field length }
  473.  
  474. begin
  475.    JustR := PadL(TrimR(InpStr),FieldLen)
  476. end;
  477.  
  478. {==============}
  479. function Center;
  480. {==============}
  481.  
  482. { Center a string within a specified field length;  the string
  483.   is padded on both sides with spaces }
  484.  
  485. var LeadSpaces : integer;
  486.         STemp : LString;
  487. begin
  488.    { strip leading and trailing spaces; determine the
  489.      Number of spaces needed to center the string }
  490.  
  491.    STemp := TrimR(TrimL(InpStr));
  492.    LeadSpaces := (FieldLen - Length(STemp) + 1) div 2;
  493.  
  494.    { insert leading spaces then trailing spaces }
  495.    Center := PadR(PadL(STemp,FieldLen-LeadSpaces),FieldLen)
  496. end;
  497.  
  498. {==============}
  499. function GetStr;
  500. {==============}
  501.  
  502. { Return a string containing all characters starting at the
  503.   first position of the source string up to the first delimiter.
  504. }
  505.  
  506. var i : integer;
  507. begin
  508.    i := Pos(Delim,InpStr);
  509.    if i = 0 then begin
  510.       GetStr := InpStr;
  511.       InpStr := ''
  512.       end
  513.    else begin
  514.       GetStr := Copy(InpStr,1,i-1);
  515.       Delete(InpStr,1,i)
  516.       end
  517. end;
  518.  
  519. {=============}
  520. function Break;
  521. {=============}
  522.  
  523. { Emulate SNOBOL BREAK function }
  524.  
  525. var cp, dp : integer;
  526.     OutStr : LString;
  527.  
  528. begin
  529.    cp := 1;
  530.    GString(InpStr,DelStr,false,cp,dp,OutStr);
  531.    Break := OutStr;
  532.    if cp = 0 then
  533.       InpStr := ''
  534.    else
  535.       Delete(InpStr,1,cp-1)
  536. end;
  537.  
  538. {============}
  539. function Span;
  540. {============}
  541.  
  542. { Emulate SNOBOL SPAN function }
  543.  
  544. var cp, dp : integer;
  545.     OutStr : LString;
  546.  
  547. begin
  548.    cp := 1;
  549.    GString(InpStr,DelStr,true,cp,dp,OutStr);
  550.    Span := OutStr;
  551.    if cp = 0 then
  552.       InpStr := ''
  553.    else
  554.       Delete(InpStr,1,cp-1)
  555. end;
  556.  
  557. {================}
  558. procedure GString;
  559. {================}
  560.  
  561. { Return a string containing all characters starting at position, cpos,
  562.  of the source string up to the first first occurence of any of several
  563.  delimiters.  The position of the found delimiter is returned as well
  564.  as which delimiter.
  565. }
  566. var done : boolean;
  567.  
  568. begin
  569.    OutStr := ''; dpos := 0;
  570.    if cpos > 0 then begin
  571.       done := false;
  572.       while (cpos <= Length(InpStr)) and not done do begin
  573.          dpos := pos(InpStr[cpos],DelStr);
  574.          if span xor (dpos = 0) then begin
  575.             OutStr := OutStr + InpStr[cpos];
  576.             cpos := cpos + 1
  577.             end
  578.          else
  579.             done := true
  580.          end;
  581.       if (span xor (dpos = 0)) or (cpos > length(InpStr)) then cpos := 0
  582.       end
  583. end;
  584.  
  585. {================}
  586. procedure RealStr;
  587. {================}
  588.  
  589. { Convert a real value to a string }
  590.  
  591. var     i, digit, MaxLen : integer;
  592.        IntValu, FracValu : real;
  593.                     Sign : boolean;
  594.  
  595. {-----------------------------------}
  596. function NewDigit(num:integer): char;
  597. {-----------------------------------}
  598.  
  599. begin
  600.    if num < 10 then
  601.       NewDigit := chr(num + ord('0'))
  602.    else
  603.       NewDigit := chr(num + ord('A') - 10)
  604. end;
  605.  
  606. begin
  607.    MaxLen := SizeOf(OutStr);
  608.    if Valu < 0 then begin
  609.       Valu := - Valu;
  610.       Sign := true
  611.       end
  612.    else
  613.       Sign := false;
  614.    IntValu := Int(Valu);
  615.    FracValu := Frac(Valu);
  616.    if Valu < 1 then
  617.       OutStr := '0'
  618.    else begin
  619.       { convert Leading digits to a string }
  620.       OutStr := '';
  621.       While (IntValu >= 1) and (Length(OutStr) < MaxLen) do begin
  622.          Valu := IntValu / Base;
  623.          Digit := Trunc(Round(Frac(Valu)*Base));
  624.          IntValu := Int(Valu);
  625.          Insert(NewDigit(digit),OutStr,1);
  626.          end
  627.       end;
  628.    if (Trail > 0) and ( length(OutStr) < MaxLen) then begin
  629.       { convert trialing digits }
  630.       OutStr := OutStr + '.';
  631.       i := 1;
  632.       While (Length(OutStr) < MaxLen) and (i <= Trail) do begin
  633.          Valu := FracValu * Base;
  634.          Digit := Trunc(Valu);
  635.          FracValu := Frac(Valu);
  636.          OutStr := OutStr + NewDigit(Digit);
  637.          i := i + 1
  638.          end
  639.       end;
  640.     if sign then Insert('-',OutStr,1);
  641. end;
  642.  
  643. {================}
  644. procedure RealVal;
  645. {================}
  646.  
  647. { convert a string to a real value }
  648.  
  649. var          i, digit : integer;
  650.       GotRadixPoint,
  651.       GotDigit,Negate : boolean;
  652.                InChar : char;
  653.               InvBase : real;
  654. begin
  655.    Valu := 0; Err := 0; negate := false; i := 0;
  656.    InvBase := 1; GotRadixPoint := false;
  657.  
  658.    while (i < length(InpStr)) and (err = 0) do begin
  659.       i := i + 1;
  660.       GotDigit := false;
  661.       InChar := UpCase(InpStr[i]);
  662.       case InChar of
  663.       '0'..'9': begin
  664.                    digit := ord(InpStr[i]) - ord('0');
  665.                    GotDigit := true
  666.                    end;
  667.       'A'..'Z': begin
  668.                    digit := ord(InChar) - ord('A') + 10;
  669.                    GotDigit := true
  670.                    end;
  671.           '-' : begin
  672.                    if negate then
  673.                       err := i
  674.                    else
  675.                       negate := true
  676.                    end;
  677.           '+' : if negate then err := i;
  678.           '.' : if GotRadixPoint then
  679.                       err := i
  680.                    else
  681.                       GotRadixPoint := true;
  682.          else    err := i
  683.          end  {case} ;
  684.       if GotDigit then
  685.          if digit >= base then
  686.             err := i
  687.          else
  688.             if GotRadixPoint then begin
  689.                InvBase := InvBase / base;
  690.                Valu := Valu + InvBase * digit
  691.                end
  692.             else
  693.                Valu := Valu * base + digit
  694.       end; { while }
  695.    if negate then valu := - valu;
  696. end;
  697.