Okay... let's keep this topic alive shall we.
I just baked this piece of code...
[pascal]Use this routine to read a Null-Terminated String from a MemoryStream. The function reads MaxLength - 1 Characters from the stream and adds the null-Char at the end. If the function encounters a NullChar while reading, Buffer will only contain the characters precending the NullChar and the NullChar itsself. The routine returns the ammount of bytes read.
var
Stream: TMemoryStream; //Global stream
function ReadStr(Buffer: PChar; MaxLength: Integer): Integer;
type
TCharArray = array [0..MaxInt-1] of Char;
PCharArray = ^TCharArray;
var
I: Integer;
Chars: PCharArray;
begin
Result := 0;
if (Stream.Size - Stream.Position <= 0) then Exit;
Chars := Stream.Memory;
I := -1;
repeat
Inc(I);
if Chars[Stream.Position+I] = #0 then Break;
until (I = MaxLength -1);
Result := Stream.Read( Buffer^, I ) + 1;
Buffer[Result-1] := #0;
end;
[/pascal]
I have tested this code, and it's working correctly AFAIK.
Might become handy when you are trying to make a C compatible API wich relies on Null-Terminated strings.
Bookmarks