Browse Source

+ Implemented (Custom)Memory and StringStream

michael 27 years ago
parent
commit
7b28eed642
1 changed files with 109 additions and 2 deletions
  1. 109 2
      fcl/inc/streams.inc

+ 109 - 2
fcl/inc/streams.inc

@@ -341,30 +341,52 @@ end;
 procedure TCustomMemoryStream.SetPointer(Ptr: Pointer; Size: Longint);
 procedure TCustomMemoryStream.SetPointer(Ptr: Pointer; Size: Longint);
 
 
 begin
 begin
+  FMemory:=Ptr;
+  FSize:=Size;
 end;
 end;
 
 
 
 
 function TCustomMemoryStream.Read(var Buffer; Count: Longint): Longint;
 function TCustomMemoryStream.Read(var Buffer; Count: Longint): Longint;
 
 
 begin
 begin
+  Result:=0;
+  If FSize>0 and FPosition<Fsize then
+    begin
+    Result:=FSize-FPosition;
+    If Result>Count then Result:=Count;
+    Move ((FMemory+FPosition)^,Buffer,Result);
+    FPosition:=Fposition+Result;
+    end;
 end;
 end;
 
 
 
 
 function TCustomMemoryStream.Seek(Offset: Longint; Origin: Word): Longint;
 function TCustomMemoryStream.Seek(Offset: Longint; Origin: Word): Longint;
 
 
 begin
 begin
+  Case Origin of
+    soFromBeginning : FPosition:=Offset;
+    soFromEnd       : FPosition:=FSize+Offset;
+    soFromCurrent   : FpoSition:=FPosition+Offset;
+  end;
+  Result:=FPosition;
 end;
 end;
 
 
 
 
 procedure TCustomMemoryStream.SaveToStream(Stream: TStream);
 procedure TCustomMemoryStream.SaveToStream(Stream: TStream);
 
 
 begin
 begin
+  if FSize>0 then Stream.WriteBuffer (FMemory^,FSize);
 end;
 end;
 
 
 
 
 procedure TCustomMemoryStream.SaveToFile(const FileName: string);
 procedure TCustomMemoryStream.SaveToFile(const FileName: string);
 
 
+Var S : TFileStream;
+
 begin
 begin
+  S:=TFileStream.Create (FileName,fmCreate);
+  SaveToStream(S);
+  S.free;
 end;
 end;
 
 
 
 
@@ -373,51 +395,104 @@ end;
 {****************************************************************************}
 {****************************************************************************}
 
 
 
 
+Const TMSGrow = 4096; { Use 4k blocks. }
+
 procedure TMemoryStream.SetCapacity(NewCapacity: Longint);
 procedure TMemoryStream.SetCapacity(NewCapacity: Longint);
 
 
 begin
 begin
+  SetPointer (Realloc(NewCapacity),Fsize);
+  FCapacity:=NewCapacity;
 end;
 end;
 
 
 
 
 function TMemoryStream.Realloc(var NewCapacity: Longint): Pointer;
 function TMemoryStream.Realloc(var NewCapacity: Longint): Pointer;
 
 
+Var MoveSize : Longint;
+
 begin
 begin
+  If NewCapacity>0 Then // round off to block size.
+    NewCapacity := (NewCapacity + (TMSGrow-1)) and not (TMSGROW-1);
+  // Only now check !
+  If NewCapacity<>FCapacity then
+    If NewCapacity=0 then
+      FreeMem (FMemory,Fcapacity)
+    else
+      begin
+      GetMem (Result,NewCapacity);
+      If FCapacity>0 then
+        begin
+        MoveSize:=FSize;
+        If MoveSize>NewCapacity then MoveSize:=NewCapacity;
+        Move (Fmemory^,Result^,MoveSize);
+        FreeMem (FMemory,FCapacity);
+        end;
+      end;
 end;
 end;
 
 
 
 
 destructor TMemoryStream.Destroy;
 destructor TMemoryStream.Destroy;
 
 
 begin
 begin
+  Clear;
+  Inherited Destroy;
 end;
 end;
 
 
 
 
 procedure TMemoryStream.Clear;
 procedure TMemoryStream.Clear;
 
 
 begin
 begin
+  FSize:=0;
+  FPosition:=0;
+  SetCapacity (0);
 end;
 end;
 
 
 
 
 procedure TMemoryStream.LoadFromStream(Stream: TStream);
 procedure TMemoryStream.LoadFromStream(Stream: TStream);
 
 
 begin
 begin
+  Stream.Position:=0;
+  SetSize(Stream.Size);
+  If FSize>0 then Stream.ReadBuffer(FMemory^,FSize);
 end;
 end;
 
 
 
 
 procedure TMemoryStream.LoadFromFile(const FileName: string);
 procedure TMemoryStream.LoadFromFile(const FileName: string);
 
 
+Var S : TFileStream;
+
 begin
 begin
+  S:=TFileStream.Create (FileName,fmOpenRead);
+  LoadFromStream(S);
+  S.free;
 end;
 end;
 
 
 
 
 procedure TMemoryStream.SetSize(NewSize: Longint);
 procedure TMemoryStream.SetSize(NewSize: Longint);
 
 
 begin
 begin
+  SetCapacity (NewSize);
+  If FSize>NewSize then FSize:=NewSize;
+  IF FPosition>FSize then FPosition:=FSize;
 end;
 end;
 
 
 
 
 function TMemoryStream.Write(const Buffer; Count: Longint): Longint; 
 function TMemoryStream.Write(const Buffer; Count: Longint): Longint; 
 
 
+Var NewPos : Longint;
+
 begin
 begin
+  If Count=0 then
+    exit(0);
+  NewPos:=FPosition+Count;
+  If NewPos>Fsize then
+    begin
+    IF NewPos>FCapacity then
+      SetCapacity (NewPos);
+    FSize:=Newpos;
+    end;
+  System.Move (Buffer,(FMemory+FPosition)^,Count);
+  FPosition:=NewPos;
+  Result:=Count;
 end;
 end;
 
 
 
 
@@ -428,42 +503,71 @@ end;
 procedure TStringStream.SetSize(NewSize: Longint); 
 procedure TStringStream.SetSize(NewSize: Longint); 
 
 
 begin
 begin
+ //!! Setlength(FDataString,NewSize);
+ If FPosition>NewSize then FPosition:=NewSize;
 end;
 end;
 
 
 
 
 constructor TStringStream.Create(const AString: string);
 constructor TStringStream.Create(const AString: string);
 
 
 begin
 begin
+  Inherited create;
+  FDataString:=AString;
 end;
 end;
 
 
 
 
 function TStringStream.Read(var Buffer; Count: Longint): Longint; 
 function TStringStream.Read(var Buffer; Count: Longint): Longint; 
 
 
 begin
 begin
+  Result:=Length(FDataString)-FPosition;
+  If Result>Count then Result:=Count;
+  // This supposes FDataString to be of type AnsiString !
+  //!! Move (Pchar(FDataString)[FPosition],Buffer,Count);
+  FPosition:=FPosition+Count;
 end;
 end;
 
 
 
 
 function TStringStream.ReadString(Count: Longint): string;
 function TStringStream.ReadString(Count: Longint): string;
 
 
+Var NewLen : Longint;
+
 begin
 begin
+  NewLen:=Length(FDataString)-FPosition;
+  If NewLen>Count then NewLen:=Count;
+  //!! SetLength(Result,NewLen);
+  //!! Read (Pointer(Result)^,NewLen);
 end;
 end;
 
 
 
 
 function TStringStream.Seek(Offset: Longint; Origin: Word): Longint; 
 function TStringStream.Seek(Offset: Longint; Origin: Word): Longint; 
 
 
 begin
 begin
+  Case Origin of
+    soFromBeginning : FPosition:=Offset;
+    soFromEnd       : FPosition:=Length(FDataString)+Offset;
+    soFromCurrent   : FpoSition:=FPosition+Offset;
+  end;
+  If FPosition>Length(FDataString) then FPosition:=Length(FDataString);
+  If FPosition<0 then FPosition:=0;
+  Result:=FPosition;
 end;
 end;
 
 
 
 
 function TStringStream.Write(const Buffer; Count: Longint): Longint; 
 function TStringStream.Write(const Buffer; Count: Longint): Longint; 
 
 
 begin
 begin
+  Result:=Count;
+  SetSize(FPosition+Count);
+  // This supposes that FDataString is of type AnsiString)
+  //!! Move (Buffer,PCHar(FDataString)[Fposition],Count);
+  FPosition:=FPosition+Count;
 end;
 end;
 
 
 
 
 procedure TStringStream.WriteString(const AString: string);
 procedure TStringStream.WriteString(const AString: string);
 
 
 begin
 begin
+  //!! Write (PChar(Astring)[0],Length(AString));
 end;
 end;
 
 
 
 
@@ -504,7 +608,10 @@ end;
 
 
 {
 {
   $Log$
   $Log$
-  Revision 1.5  1998-06-11 13:46:33  michael
+  Revision 1.6  1998-06-11 21:15:28  michael
+  + Implemented (Custom)Memory and StringStream
+
+  Revision 1.5  1998/06/11 13:46:33  michael
   + Fixed some functions. TFileStream OK.
   + Fixed some functions. TFileStream OK.
 
 
   Revision 1.4  1998/06/10 21:53:07  michael
   Revision 1.4  1998/06/10 21:53:07  michael
@@ -519,4 +626,4 @@ end;
   Revision 1.1  1998/05/04 14:30:12  michael
   Revision 1.1  1998/05/04 14:30:12  michael
   * Split file according to Class; implemented dummys for all methods, so unit compiles.
   * Split file according to Class; implemented dummys for all methods, so unit compiles.
 
 
-}
+}