home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / acl-lib.zip / ACLStringUtility.pas < prev    next >
Pascal/Delphi Source File  |  2000-07-22  |  11KB  |  453 lines

  1. Unit ACLStringUtility;
  2.  
  3. Interface
  4.  
  5. Uses
  6.   Classes;
  7.  
  8. function NewPString( s: string ): PString;
  9.  
  10. procedure FreePString( Var ps: PString );
  11.  
  12. // Case insensitive compare
  13. Function StringsSame( const a, b: string ): boolean;
  14.  
  15. // Returns S in double quotes
  16. Function DQuote( s: string ): string;
  17.  
  18. // Substitutes given characters
  19. Function SubstituteChar( const S: string; const Find: Char; const Replace: Char ): string;
  20.  
  21. // Returns the count rightmost chars of S
  22. Function StrRight( const S:string; const count:integer ):string;
  23.  
  24. // Returns the remainder of S starting at start
  25. Function RightFrom( const S:string; const start:integer ):string;
  26.  
  27. // Returns the count leftmost chars of S
  28. Function StrLeft( const S:string; const count:integer ):string;
  29.  
  30. // Returns S minus count characters from the right
  31. Function LeftWithout( const S:string; const count:integer ):string;
  32.  
  33. // Returns S with leftCount chars removed from the left and
  34. // rightCount chars removed from the right.
  35. Function RemoveEnds( const S:string; const leftCount:integer; const rightCount:integer ):string;
  36.  
  37. // Parses a line of the form
  38. // key = value into it's components
  39. Procedure ParseConfigLine( const S: string;
  40.                            var keyName: string;
  41.                            var keyValue: string );
  42.  
  43. // Removes and returns the first value in a separated
  44. // value list (removes quotes if found)
  45. Function ExtractNextValue( var CSVString: string;
  46.                            const Separator: string ): string;
  47.  
  48. // Removes spaces around the separator in the given CSV string
  49. Procedure RemoveSeparatorSpaces( var CSVString: string;
  50.                                  const Separator:string );
  51.  
  52. // Returns true if c is a digit 0..9
  53. Function IsDigit( const c: char ): boolean;
  54.  
  55. // Produces a string from n padded on the left with 0's
  56. // to width chars
  57. Function Left0Pad( const n: integer; const width: integer ): string;
  58.  
  59. // Returns true if s starts with start (case insensitive)
  60. Function Starts( const start: string; const s: string ): boolean;
  61.  
  62. // Returns true if s ends with endstr (case insensitive)
  63. Function Ends( const endStr: string; const s: string ): boolean;
  64.  
  65. // Adds NewValue to S as a separated list
  66. Procedure AddToStringList( Var S: string;
  67.                            const NewValue: string;
  68.                            const Separator: string );
  69.  
  70. Function ListToString( List: TStrings;
  71.                        const Separator: string ): string;
  72.  
  73. procedure StringToList( S: String;
  74.                         List: TStrings;
  75.                         const Separator: string );
  76.  
  77. Function FirstWord( const S: String ): string;
  78.  
  79. // Given a string with a number on the end, increments that
  80. // number by one.
  81. // If there is no number it adds a one.
  82. // If the number is left zero padded then the result is similarly
  83. // padded
  84. Function IncrementNumberedString( StartString: string ): string;
  85.  
  86. {$ifdef os2}
  87. // Right & left trim that works with AnsiStrings.
  88. Function AnsiTrim( const S: AnsiString ): AnsiString;
  89.  
  90. Procedure AnsiParseConfigLine( const S: Ansistring;
  91.                                var keyName: Ansistring;
  92.                                var keyValue: Ansistring );
  93.  
  94. Function AnsiExtractNextValue( var CSVString: AnsiString;
  95.                                const Separator: AnsiString ): AnsiString;
  96.  
  97. {$endif}
  98.  
  99. Implementation
  100.  
  101. Uses
  102.   SysUtils;
  103.  
  104. procedure FreePString( Var ps: PString );
  105. begin
  106.   if ps <> nil then
  107.     FreeMem( ps, Length( ps^ ) + 1 );
  108.   ps:= nil;
  109. end;
  110.  
  111. function NewPString( s: string ): PString;
  112. begin
  113.   GetMem( Result, Length( S ) + 1 );
  114.   Result^:= S;
  115. end;
  116.  
  117. Function StringsSame( const a, b: string ): boolean;
  118. begin
  119.   Result:= CompareText( a, b ) = 0;
  120. end;
  121.  
  122. Function DQuote( s: string ): string;
  123. begin
  124.   Result:= '"' + s + '"';
  125. end;
  126.  
  127. Function SubstituteChar( const S: string; const Find: Char; const Replace: Char ): string;
  128. Var
  129.   i: longint;
  130. Begin
  131.   Result:= S;
  132.   for i:=1 to length( S ) do
  133.     if Result[ i ] = Find then
  134.       Result[ i ]:= Replace;
  135. End;
  136.  
  137. Function StrRight( const S:string; const count:integer ):string;
  138. Begin
  139.   if count>=length(s) then
  140.   begin
  141.     Result:=S;
  142.   end
  143.   else
  144.   begin
  145.     Result:=copy( S, length( s )-count+1, count );
  146.   end;
  147. end;
  148.  
  149. Function StrLeft( const S:string; const count:integer ):string;
  150. Begin
  151.   if count>=length(s) then
  152.     Result:=S
  153.   else
  154.     Result:=copy( S, 1, count );
  155. end;
  156.  
  157. // Returns S minus count characters from the right
  158. Function LeftWithout( const S:string; const count:integer ):string;
  159. Begin
  160.   Result:= copy( S, 1, length( S )-count );
  161. End;
  162.  
  163. Function RemoveEnds( const S:string; const leftCount:integer; const rightCount:integer ):string;
  164. Begin
  165.   Result:= S;
  166.   Delete( Result, 1, leftCount );
  167.   Delete( Result, length( S )-rightCount, rightCount );
  168. End;
  169.  
  170. Function RightFrom( const S:string; const start:integer ):string;
  171. Begin
  172.   Result:= copy( S, start, length( S )-start+1 );
  173. end;
  174.  
  175. Procedure ParseConfigLine( const S: string;
  176.                            var keyName: string;
  177.                            var keyValue: string );
  178. Var
  179.   line: String;
  180.   EqualsPos: longint;
  181. Begin
  182.   KeyName:= '';
  183.   KeyValue:= '';
  184.  
  185.   line:= trim( S );
  186.   EqualsPos:= Pos( '=', line );
  187.  
  188.   if ( EqualsPos>0 ) then
  189.   begin
  190.     KeyName:= line;
  191.     Delete( KeyName, EqualsPos, length( KeyName )-EqualsPos+1 );
  192.     KeyName:= Trim( KeyName );
  193.  
  194.     KeyValue:= line;
  195.     Delete( KeyValue, 1, EqualsPos );
  196.     KeyValue:= Trim( KeyValue );
  197.   end;
  198. end;
  199.  
  200. Function ExtractNextValue( var CSVString: string;
  201.                            const Separator: string ): string;
  202. Var
  203.   SeparatorPos: integer;
  204. Begin
  205.   SeparatorPos:= Pos( Separator, CSVString );
  206.   if SeparatorPos>0 then
  207.   begin
  208.     Result:= Copy( CSVString, 1, SeparatorPos-1 );
  209.     Delete( CSVString, 1, SeparatorPos + length( Separator ) - 1 );
  210.   end
  211.   else
  212.   begin
  213.     Result:= CSVString;
  214.     CSVString:= '';
  215.   end;
  216.   Result:= trim( Result );
  217.   // Remove qyotes if present
  218.   if Result <> '' then
  219.   if ( Result[1] = chr(34) )
  220.      and ( Result[ length(Result) ] = chr(34) ) then
  221.   begin
  222.     Delete( Result, 1, 1 );
  223.     Delete( Result, length( Result ), 1 );
  224.     // Result:=trim( RemoveEnds( Result, 1, 1 ) );
  225.   end;
  226. end;
  227.  
  228. Function IsDigit( const c: char ): boolean;
  229. Begin
  230.   Result:=( c>='0' ) and ( c<='9' );
  231. End;
  232.  
  233. Function Left0Pad( const n: integer; const width: integer ): string;
  234. Begin
  235.   Result:= IntToStr( n );
  236.   while length( Result )<width do
  237.     Result:= '0' +Result;
  238. End;
  239.  
  240. // Returns true if s starts with start
  241. Function Starts( const start: string; const s: string ): boolean;
  242. Var
  243.   i: integer;
  244. Begin
  245.   Result:= false;
  246.   for i:= 1 to length( start ) do
  247.     if UpCase( s[ i ] ) <> UpCase( start[ i ] ) then
  248.       exit;
  249.   Result:= true;
  250. End;
  251.  
  252. // Returns true if s ends with endstr (case insensitive)
  253. Function Ends( const endStr: string; const s: string ): boolean;
  254. Var
  255.   i, j: integer;
  256. Begin
  257.   Result:= false;
  258.   if Length( s ) < length( endStr ) then
  259.     exit;
  260.   j:= Length( s );
  261.   for i:= length( endstr ) downto 1 do
  262.   begin
  263.     if UpCase( s[ j ] ) <> UpCase( endStr[ i ] ) then
  264.       exit;
  265.     dec( j );
  266.   end;
  267.   Result:= true;
  268. End;
  269.  
  270. Procedure RemoveSeparatorSpaces( var CSVString: string;
  271.                                  const Separator:string );
  272. Var
  273.   SeparatorPos:integer;
  274.   NewString: string;
  275. Begin
  276.   NewString:='';
  277.   while CSVString<>'' do
  278.   begin
  279.     SeparatorPos:=pos( Separator, CSVString );
  280.     if SeparatorPos>0 then
  281.     begin
  282.       NewString:=NewString+trim( copy( CSVString, 1, SeparatorPos-1 ) )+Separator;
  283.       Delete( CSVString, 1, SeparatorPos );
  284.     end
  285.     else
  286.     begin
  287.       NewString:=NewString+trim( CSVString );
  288.       CSVString:='';
  289.     end;
  290.   end;
  291.   CSVString:=NewString;
  292. End;
  293.  
  294. Procedure AddToStringList( Var S: string;
  295.                            const NewValue: string;
  296.                            const Separator: string );
  297. Begin
  298.   if trim( S )<>'' then
  299.     S:=S+Separator;
  300.   S:=S+NewValue;
  301. End;
  302.  
  303. Function ListToString( List: TStrings;
  304.                        const Separator: string ): string;
  305. Var
  306.   i: longint;
  307. Begin
  308.   Result:= '';
  309.   for i:=0 to List.Count-1 do
  310.     AddToStringList( Result, List[ i ], Separator );
  311. End;
  312.  
  313. procedure StringToList( S: String;
  314.                         List: TStrings;
  315.                         const Separator: string );
  316. var
  317.   Item: string;
  318. begin
  319.   List.Clear;
  320.   while S <> '' do
  321.   begin
  322.     Item:= ExtractNextValue( S, Separator );
  323.     List.Add( Item );
  324.   end;
  325. end;
  326.  
  327. Function FirstWord( const S: String ): string;
  328. Var
  329.   SpacePos: longint;
  330.   temp: string;
  331. Begin
  332.   temp:= trimleft( S );
  333.   SpacePos:= pos( ' ', temp );
  334.   if SpacePos>0 then
  335.     Result:= Copy( temp, 1, SpacePos-1 )
  336.   else
  337.     Result:= temp;
  338. End;
  339.  
  340. Function IncrementNumberedString( StartString: string ): string;
  341. Var
  342.   Number: string;
  343.   NewNumber: string;
  344.   i: integer;
  345. begin
  346.   // Extract any digits at the end of the string
  347.   i:= length( StartString );
  348.   Number:= '';
  349.   while i>0 do
  350.   begin
  351.     if isDigit( StartString[i] ) then
  352.     begin
  353.        Number:= StartString[i] + Number;
  354.        i:= i - 1;
  355.     end
  356.     else
  357.       break;
  358.   end;
  359.  
  360.   if Number<>'' then
  361.   begin
  362.     // Found a numeric bit to play with
  363.     // Copy the first part
  364.     Result:= LeftWithout( StartString, length( Number ) );
  365.     NewNumber:= Left0Pad( StrToInt( Number ) + 1,
  366.                                length( Number ) );
  367.     Result:= Result + NewNumber;
  368.   end
  369.   else
  370.     // No build number, add a 1
  371.     Result:= StartString + '1';
  372. end;
  373.  
  374. {$ifdef OS2}
  375.  
  376. Function AnsiTrim( const S: AnsiString ): AnsiString;
  377. Var
  378.   i: longint;
  379. Begin
  380.   i:= 1;
  381.   while i<length( S) do
  382.   begin
  383.     if S[ i ]<>' ' then
  384.       break;
  385.     inc( i );
  386.   end;
  387.   Result:= S;
  388.   if i>1 then
  389.     AnsiDelete( Result, 1, i-1 );
  390.   i:= length( Result );
  391.   while i>=1 do
  392.   begin
  393.     if S[ i ]<>' ' then
  394.       break;
  395.     dec( i );
  396.   end;
  397.   AnsiSetLength( Result, i );
  398. End;
  399.  
  400. Procedure AnsiParseConfigLine( const S: Ansistring;
  401.                                var keyName: Ansistring;
  402.                                var keyValue: Ansistring );
  403. Var
  404.   line: AnsiString;
  405.   EqualsPos: longint;
  406. Begin
  407.   KeyName:= '';
  408.   KeyValue:= '';
  409.  
  410.   line:= AnsiTrim( S );
  411.   EqualsPos:= AnsiPos( '=', line );
  412.  
  413.   if ( EqualsPos>0 ) then
  414.   begin
  415.     KeyName:= AnsiCopy( line, 1, EqualsPos-1 );
  416.     KeyName:= AnsiTrim( KeyName );
  417.  
  418.     KeyValue:= AnsiCopy( line, EqualsPos+1, length( line )-EqualsPos );
  419.     KeyValue:= AnsiTrim( KeyValue );
  420.   end;
  421. end;
  422.  
  423. Function AnsiExtractNextValue( var CSVString: AnsiString;
  424.                                const Separator: AnsiString ): AnsiString;
  425. Var
  426.   SeparatorPos: integer;
  427. Begin
  428.   SeparatorPos:= AnsiPos( Separator, CSVString );
  429.   if SeparatorPos>0 then
  430.   begin
  431.     Result:= AnsiCopy( CSVString, 1, SeparatorPos-1 );
  432.     AnsiDelete( CSVString, 1, SeparatorPos + length( Separator ) - 1 );
  433.   end
  434.   else
  435.   begin
  436.     Result:= CSVString;
  437.     CSVString:= '';
  438.   end;
  439.   Result:= AnsiTrim( Result );
  440.   // Remove qyotes if present
  441.   if ( Result[1] = chr(34) )
  442.      and ( Result[ length(Result) ] = chr(34) ) then
  443.   begin
  444.     AnsiDelete( Result, 1, 1 );
  445.     AnsiDelete( Result, length( Result ), 1 );
  446.     Result:= AnsiTrim( Result );
  447.   end;
  448. end;
  449. {$Endif}
  450.  
  451. Initialization
  452. End.
  453.