• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

Storing files in a executable

Is there anyway I could use a component to store files in the main executable file. Perhaps using a TStream object in the component to store and access the file. And letting delphi store save the file data as it would with any other component property.
0
FRAME
Asked:
FRAME
1 Solution
 
bomeCommented:
Well, there is a component that does just that. I don't know where I got it, so I just post it here directly.
It lets you assign a file in the object inspector. The file is then stored along with other component data in the RCData of the respective form.
In the program you access the file with the Data property (pointer to the first byte) and the DataSize property.

AUTHOR:            (C) Glen Why, 1996

URL:            eugene@actcom.co.il
            (please specify "TFileResource" in the subject string)

STATUS:            FREEWARE (source included): you can freely use or distribute files
            included to this archive.  Anyway, I'll appreciate your donations       
            (in any form: $$, bug-reports, soft, registered versions of your
            components, ideas, etc.)

unit FileResource;

interface
uses
  SysUtils, Classes;


type

  TFileResource = class( TComponent )
  private
    FFileName :TFileName;
    FData :Pointer;
    FDataSize :Integer;
    procedure ReadData( stream :tstream );
    procedure WriteData( stream :tstream );
    procedure SetFileName( const Value :TFileName );
  protected
    procedure DefineProperties( Filer :TFiler ); override;
  public
    destructor Destroy; override;
    property Data :Pointer read FData;
    property DataSize :Integer read FDataSize;
  published
    property FileName :TFileName read FFileName write SetFileName;
  end;


procedure register;

implementation
uses
  DsgnIntf, FileNameProp;

procedure register;
begin
 RegisterPropertyEditor( TypeInfo( TFileName ),
  TFileResource, 'FileName', TFileNameProperty );
  RegisterComponents ( 'more...', [ TFileResource ] );
end;


procedure TFileResource.DefineProperties( Filer :TFiler );
begin
  inherited DefineProperties( Filer );
  Filer.DefineBinaryProperty( 'DATA', ReadData, WriteData, FData <> nil );
end;

procedure TFileResource.ReadData( stream :tstream );
begin
  with stream do
   begin
     read( FDataSize, sizeof( FDataSize ) );
     GetMem( FData, FDataSize + 32 );
     try
       read( FData^, FDataSize );
     except
       FreeMem( FData );
       FData := nil;
       FDataSize := 0;
       raise;
     end;
   end;
end;

procedure TFileResource.WriteData( stream :tstream );
begin
  with stream do
   begin
     Write( FDataSize, Sizeof( FDataSize ) );
     write( FData^, FDataSize );
   end;
end;


procedure TFileResource.SetFileName( const Value :String );
begin
  if Value <> FFileName then
   with TFileStream.Create( Value, fmOpenRead ) do
    try
      FFileName := '';
      if FData <> Nil  then
        begin
         FreeMem( FData );
         FData := nil;
        end;
      FDataSize := Size + 32;
      GetMem( FData, FDataSize );
      try
        read( FData^, FDataSize );
      except
        FreeMem( FData );
        FDataSize := 0;
        FData := nil;
        raise;
      end;
      FFileName := Value;
    finally
      free;
    end;
end;


destructor TFileResource.Destroy;
begin
  if FData <> Nil then FreeMem( FData );
  inherited Destroy;
end;

end.

bome
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now