Solved

Storing files in a executable

Posted on 1998-07-22
1
151 Views
Last Modified: 2010-04-04
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
Comment
Question by:FRAME
1 Comment
 

Accepted Solution

by:
bome earned 100 total points
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now