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

  1. Unit ACLPCharUtility;
  2.  
  3. Interface
  4.  
  5. Procedure NPCharToString( const P: PChar; const Length: integer; var S: String );
  6.  
  7. // Returns a - b
  8. Function PCharDiff( const a: PChar; const b: Pchar ): longword;
  9.  
  10. // trims spaces and carriage returns of the end of Text
  11. procedure TrimWhitespace( Text: PChar );
  12.  
  13. // Concatenates a pascal string onto a PCHar string
  14. // Resizes if needed
  15. procedure StrPCat( Var Dest: PChar;
  16.                    StringToAdd: string );
  17.  
  18. // Trim endlines (#10 or #13) off the end of
  19. // the given string.
  20. Procedure TrimEndLines( S: PChar );
  21.  
  22. // Allocates enough memory for a copy of s as a PChar
  23. // and copies s to it.
  24. Function StrDupPas( const s: string ): PChar;
  25.  
  26. // Returns a copy of the first n chars of s
  27. Function StrNDup( const s: PChar; const n: integer ): PChar;
  28.  
  29. // Returns a copy of the first line starting at lineStart
  30. Function CopyFirstLine( const lineStart: PChar ): PChar;
  31.  
  32. // Returns next line p points to
  33. Function NextLine( const p: PChar): PChar;
  34.  
  35. // Concatentate AddText to Text. Reallocate and expand
  36. // Text if necessary. This is a size-safe StrCat
  37. Procedure AddAndResize( Var Text: PChar;
  38.                         AddText: PChar );
  39.  
  40. Implementation
  41.  
  42. Uses
  43.   SysUtils, ACLConstants, ACLUtility;
  44.  
  45. Procedure NPCharToString( const P: PChar; const Length: integer; var S: String );
  46. var
  47.   i: integer;
  48. begin
  49.   S:= '';
  50.   i:= 0;
  51.   while ( P[ i ] <> #0 ) and ( i < Length ) do
  52.   begin
  53.     S:= S + P[ i ];
  54.     inc( i );
  55.   end;
  56. {  MemCopy( P, Addr( S ) + 1, Length );
  57.   S[ 0 ]:= Char( Length );}
  58. end;
  59.  
  60. Function PCharDiff( const a: PChar; const b: Pchar ): longword;
  61. begin
  62.   Result:= longword( a ) - longword( b );
  63. end;
  64.  
  65. Procedure CheckPCharSize( Var Text: PChar;
  66.                           AddSize: longword );
  67. var
  68.   temp: PChar;
  69.   NeededSize: longword;
  70.   NewBufferSize: longword;
  71. begin
  72.   NeededSize:= strlen( Text ) + AddSize + 2;
  73.   if NeededSize > StrBufSize( Text ) then
  74.   begin
  75.     // allocate new buffer, double the size...
  76.     NewBufferSize:= StrBufSize( Text ) * 2;
  77.     // or if that's not enough...
  78.     if NewBufferSize < NeededSize then
  79.       // double what we are going to need
  80.       NewBufferSize:= NeededSize * 2;
  81.     temp:= StrAlloc( NewBufferSize );
  82.  
  83.     StrCopy( temp, Text );
  84.     StrDispose( Text );
  85.     Text:= temp;
  86.   end;
  87. end;
  88.  
  89. Procedure AddAndResize( Var Text: PChar;
  90.                         AddText: PChar );
  91. begin
  92.   CheckPCharSize( Text, strlen( AddText ) );
  93.   StrCat( Text, AddText );
  94. end;
  95.  
  96. // trims spaces and carriage returns of the end of Text
  97. procedure TrimWhitespace( Text: PChar );
  98. var
  99.   P: PChar;
  100.   IsWhitespace: boolean;
  101.   TheChar: Char;
  102. begin
  103.   P:= Text + StrLen( Text );
  104.   while P > Text do
  105.   begin
  106.     dec( P );
  107.     TheChar:= P^;
  108.     IsWhitespace:= TheChar in [ ' ', #13, #10, #9 ];
  109.     if not IsWhiteSpace then
  110.       // done
  111.       break;
  112.     P^:= #0;
  113.   end;
  114. end;
  115.  
  116. procedure StrPCat( Var Dest: PChar;
  117.                    StringToAdd: string );
  118. var
  119.   Index: longint;
  120.   DestP: PChar;
  121. begin
  122.   CheckPCharSize( Dest, Length( StringToAdd ) );
  123.   DestP:= Dest + StrLen( Dest );
  124.   for Index:= 1 to Length( StringToAdd ) do
  125.   begin
  126.     DestP^:= StringToAdd[ Index ];
  127.     inc( DestP );
  128.   end;
  129.   DestP^:= #0;
  130. end;
  131.  
  132. Procedure TrimEndLines( S: PChar );
  133. var
  134.   StringIndex: integer;
  135. begin
  136.   StringIndex:= strlen( S );
  137.   while StringIndex > 0 do
  138.   begin
  139.     dec( StringIndex );
  140.     if S[ StringIndex ] in [ #10, #13 ] then
  141.       S[ StringIndex ]:= #0
  142.     else
  143.       break;
  144.   end;
  145. end;
  146.  
  147. Function StrDupPas( const s: string ): PChar;
  148. Begin
  149.   Result:=StrAlloc( length( s )+1 );
  150.   StrPCopy( Result, S );
  151. //  Result^:=s;
  152. End;
  153.  
  154. // Returns a copy of the first n chars of s
  155. Function StrNDup( const s: PChar; const n: integer ): PChar;
  156. Begin
  157.   Result:= StrAlloc( n+1 );
  158.   Result[ n ]:= '6';
  159.   StrLCopy( Result, s, n );
  160. End;
  161.  
  162. // Returns a copy of the first line starting at lineStart
  163. Function CopyFirstLine( const lineStart: PChar ): PChar;
  164. Var
  165.   lineEnd: PChar;
  166.   lineLength: integer;
  167. Begin
  168.   // look for an end of line
  169.   lineEnd:= strpos( lineStart, EndLine );
  170.   if lineEnd <> nil then
  171.   begin
  172.     // found, line length is difference between line end position and start of line
  173.     lineLength:= longword( lineEnd )-longword( lineStart ); // ugly but how else can it be done?
  174.     Result:= StrNDup( lineStart, lineLength );
  175.     exit;
  176.   end;
  177.  
  178.   // no eol found, return copy of remainder of string
  179.   Result:= StrNew( lineStart );
  180. end;
  181.  
  182. // Returns next line p points to
  183. Function NextLine( const p: PChar): PChar;
  184. Var
  185.   lineEnd: PChar;
  186. Begin
  187.   // look for an end of line
  188.   lineEnd:=strpos( p, EndLine );
  189.   if lineEnd<>nil then
  190.   begin
  191.     // Advance the linestart over the eol
  192.     Result:=lineEnd+length( EndLine );
  193.     exit;
  194.   end;
  195.  
  196.   // no eol found, return pointer to null term
  197.   Result:=p+strlen( p );
  198. end;
  199.  
  200. Initialization
  201. End.
  202.