unit RVFileDocObject;

interface

uses
  Classes,
  RVDocParams;

{ TRVFileDocObject allows storing files in RVF documents,
  in DocObjects collection }

type
  TRVFileDocObject = class (TRVDocObject)
  private
    FContent: TMemoryStream;
    FFileName: String;
    procedure ReadFileProperty(Stream: TStream);
    procedure WriteFileProperty(Stream: TStream);
  protected
    procedure DefineProperties(Filer: TFiler); override;
  public
    function LoadFromFile(const AFileName: String): Boolean;
    function SaveToFile(const AFileName: String): Boolean;
    destructor Destroy; override;
  published
    property FileName: String read FFileName write FFileName;
  end;


implementation

{ TRVFileDocObject }

destructor TRVFileDocObject.Destroy;
begin
  FContent.Free;
  inherited;
end;

procedure TRVFileDocObject.DefineProperties(Filer: TFiler);
begin
  inherited;
  Filer.DefineBinaryProperty( 'File', ReadFileProperty, WriteFileProperty,
    FContent <> nil);
end;

function TRVFileDocObject.LoadFromFile(const AFileName: String): Boolean;
begin
  Result := False;
  if FContent = nil then
    FContent := TMemoryStream.Create;
  try
    FContent.LoadFromFile(AFileName);
    Result := True;
  except
    FContent.Free;

  end;
end;

function TRVFileDocObject.SaveToFile(const AFileName: String): Boolean;
begin
  Result := False;
  if FContent = nil then
    exit;
  try
    FContent.Position := 0;
    FContent.SaveToFile(AFileName);
    Result := True;
  except
  end;
end;

procedure TRVFileDocObject.ReadFileProperty(Stream: TStream);
var
  Size: Integer;
begin
  if FContent = nil then
    FContent := TMemoryStream.Create;
  Stream.ReadBuffer(Size, sizeof(Size));
  FContent.Clear;
  FContent.CopyFrom(Stream, Size);
end;

procedure TRVFileDocObject.WriteFileProperty(Stream: TStream);
var
  Size: Integer;
begin
  Size := FContent.Size;
  Stream.WriteBuffer(Size, sizeof(Size));
  FContent.Position := 0;
  Stream.CopyFrom(FContent, Size);
end;

initialization
  RegisterClass(TRVFileDocObject);
finalization
  UnRegisterClass(TRVFileDocObject);


end.
