Changeset 199


Ignore:
Timestamp:
Jun 7, 2007, 8:53:31 PM (18 years ago)
Author:
RBRi
Message:

another update

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Library/CharUtilsUnit.pas

    r173 r199  
    3434  // ---------------
    3535
    36  
     36
     37  // Allocates enough memory for a copy of aString as a PChar
     38  // and copies aString to it.
     39  Function NewPCharAsCopyOfStr(const aString: String) : PChar;
     40
    3741  // Converts a PChar into a String like StrPas
    3842  // but conversts at least the first aLength chars
     
    4145  // Returns the difference of the pointers
    4246  Function PCharPointerDiff(const aMinuend: PChar; const aSubtrahend : PChar)  : Longword;
     47
     48  // Concatentate AddText to Text. Reallocate and expand
     49  // Text if necessary. This is a size-safe StrCat
     50  Procedure AddAndResize(var aText: PChar; const aTextToAdd: PChar);
     51
    4352
    4453Implementation
     
    6372
    6473
    65   // Converts a PChar into a String like StrPas
    66   // but conversts at least the first aLength chars
    6774  Function StrPasWithLength(const aPChar: PChar; const aLength: integer) : String;
    6875  var
     
    8592
    8693
     94  Procedure CheckPCharSize(Var aText: PChar; const aNeededSize: longword);
     95  var
     96    tmpPChar: PChar;
     97    tmpNewBufferSize: longword;
     98  begin
     99    if (aNeededSize + 1) // + 1 to allow for null terminator
     100       > StrBufSize(aText) then
     101    begin
     102      // allocate new buffer, double the size...
     103      tmpNewBufferSize := StrBufSize(aText) * 2;
     104      // or if that's not enough...
     105      if tmpNewBufferSize < (aNeededSize + 1) then
     106      begin
     107        // double what we are going to need
     108        tmpNewBufferSize:= aNeededSize * 2;
     109      end;
     110
     111      tmpPChar := StrAlloc(tmpNewBufferSize);
     112
     113      // copy string to new buffer
     114      StrCopy(tmpPChar, aText);
     115      StrDispose(aText);
     116      aText:= tmpPChar;
     117    end;
     118  end;
     119
     120
     121  Procedure AddAndResize(var aText: PChar; const aTextToAdd: PChar);
     122  begin
     123    CheckPCharSize(aText, strlen(aText) + strlen(aTextToAdd));
     124    StrCat(aText, aTextToAdd);
     125  end;
     126
     127  Function NewPCharAsCopyOfStr(const aString: String) : PChar;
     128  begin
     129    Result := StrAlloc(length(aString) + 1);
     130    StrPCopy(Result, aString);
     131  end;
     132
    87133END.
Note: See TracChangeset for help on using the changeset viewer.