home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / zkuste / delphi / kolekce / d345 / JWTOOL.ZIP / jwtool / JwBinRES.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-09-29  |  11.3 KB  |  384 lines

  1. unit JwBinRES;
  2.  
  3. interface
  4. {
  5.         OH Heaven Above this was a long time comming!!!!!!  Why the RTTI information is so poorly
  6. documented, I'll never know.  It was only through scowering the Classes.pas, DsgnIntf.pas, and
  7. a goodly amount from TurboPower's documentation of SysTools (good product by the way) was I
  8. able to finally do this thing.
  9.         Well, here's the deal.  As with the "TStringResourse" and the "TBMPResourse" I was trying
  10. to make non-graphic ways of storing files into the executable.  I realize of course I could just
  11. stick these in with the resource file, and the load it up from there, but the one that comes
  12. with delphi just doesn't cut it for me (maybe I'm just not skilled enough...) but I thought that
  13. since a TBitMap was stored in the DFM file, why not a zip file?  Well, it was easy for the first
  14. two, but for just plain binary stuff that had no property edit already made, I was out-of-luck.
  15. I think my biggest mistake was that I was trying to do it with TStream descendants.  I had such
  16. good luck with a file I made "TMemoryFile" that I used to read in raw files.  The problem, as I have
  17. found out, is that they are not descendant from TPersistent.  Finally I gave up with that Idea
  18. and went to a just plain Pointer variable wrapped up in a TPersistent class.  That was the big
  19. secret I had a hard time to figure out.
  20.         Well, my plans are to use this with Turbopower's abrevia (of course now that v3 is out
  21. I'm gonna have to scrape up some money for that...grumble....) that has a UnZip and Zip from a
  22. stream.  So I can use my WriteToStream property, unzip it with a call to abrevia, and then
  23. I'm on my way.
  24.  
  25. }
  26.  
  27. //  Version History
  28. //     Num     Date            Notes
  29. //     1.00  August 1, 1999   Initial Release
  30.  
  31. //  Created By:
  32. //    Joseph Wilcock
  33. //    Coockoo@hotmail.com
  34. //    http://msnhomepages.talkcity.com/RedmondAve/coockoo/
  35.  
  36.  
  37. uses Windows, SysUtils, Classes, Controls, DsgnIntf;
  38.  
  39. type
  40.   TJwMemoryDataProperty = Class( TPropertyEditor )
  41.     public
  42.       Function GetAttributes: TPropertyAttributes; Override;
  43.       Function GetValue: String; Override;
  44.       Procedure Edit; Override;
  45.     End;
  46.  
  47.   TJwMemoryData = Class( TPersistent )
  48.     Private
  49.       FData: Pointer;
  50.       FFileSize: Longint;
  51.       FOrgFileName: AnsiString;
  52.       FOnFailToRead: TNotifyEvent;
  53.       FOnFailToWrite: TNotifyEvent;
  54.       FOnFailToDelete: TNotifyEvent;
  55.     private
  56.       Procedure FillData( aSize: LongInt; aData: Pointer );
  57.       function GetSize: LongInt;
  58.       procedure SetFileName(const Value: String);
  59.     Protected
  60.       Procedure DefineProperties( Filer: TFiler ); Override;
  61.       Procedure ReadData( Stream: TStream ); Virtual;
  62.       Procedure WriteData( Stream: TStream ); Virtual;
  63.     Public
  64.       Constructor Create;
  65.       Destructor Destroy; Override;
  66.       Procedure Clear;
  67.       Procedure LoadFromFile( const FileName: String ); Virtual;
  68.       Procedure Assign( Source: TPersistent ); Override;
  69.       Procedure SaveToFile( const FileName: String ); Virtual;
  70.       Procedure SaveToStream( Stream: TStream );
  71.     published
  72.       Property FileName: String Read FOrgFileName Write SetFileName;
  73.       Property CurrentSize: LongInt Read GetSize;
  74.       Property OnFailToRead: TNotifyEvent Read FOnFailToRead Write FOnFailToRead;
  75.       Property OnFailToWrite: TNotifyEvent Read FOnFailToWrite Write FOnFailToWrite;
  76.       Property OnFailToDelete: TNotifyEvent Read FOnFailToDelete Write FOnFailToDelete;
  77.     End;
  78.  
  79.   TJwBinRes = Class( TComponent )
  80.     Private
  81.       FFileData: TJwMemoryData;
  82.       FTagName: String;
  83.     private
  84.       function GetOriginalFileName: String;
  85.       procedure SetTagName(const Value: String);
  86.       Procedure SetjwMemoryData( Value: TJwMemoryData );
  87.       function GetOnFailToDelete: TNotifyEvent;
  88.       function GetOnFailToRead: TNotifyEvent;
  89.       function GetOnFailToWrite: TNotifyEvent;
  90.       procedure SetOnFailToDelete(const Value: TNotifyEvent);
  91.       procedure SetOnFailToRead(const Value: TNotifyEvent);
  92.       procedure SetOnFailToWrite(const Value: TNotifyEvent);
  93.     Public
  94.       Procedure SaveToStream( Stream: TStream );
  95.       Procedure SaveThroughStream( const FileName: String );
  96.       Procedure SaveToFile( const FileName: String );
  97.       Constructor Create( AOwner: TComponent ); Override;
  98.       Destructor Destroy; Override;
  99.     Published
  100.       Property FileData: TJwMemoryData Read FFileData Write SetjwMemoryData;
  101.       Property TagName: String Read FTagName Write SetTagName;
  102.       Property OriginalFileName: String Read GetOriginalFileName;
  103.       Property OnFailToRead: TNotifyEvent Read GetOnFailToRead Write SetOnFailToRead;
  104.       Property OnFailToWrite: TNotifyEvent Read GetOnFailToWrite Write SetOnFailToWrite;
  105.       Property OnFailToDelete: TNotifyEvent Read GetOnFailToDelete Write SetOnFailToDelete;
  106.     End;
  107.  
  108. Procedure Register;
  109.  
  110. implementation
  111.  
  112. uses Dialogs;
  113.  
  114. Procedure Register;
  115. Begin
  116.   RegisterPropertyEditor( TypeInfo( TJwMemoryData ), TJwBinRes, '', TJwMemoryDataProperty );
  117.   RegisterComponents( 'JwTools', [ TJwBinRes ] );
  118. End;
  119.  
  120. /////////////////////////////////////////////////////////////////////////////////////
  121. /////////////////////////////////////////////////////////////////////////////////////
  122. Function TJwMemoryDataProperty.GetAttributes: TPropertyAttributes;
  123. Begin
  124.   Result := Inherited GetAttributes + [ paDialog ];
  125. End;
  126.  
  127. Function TJwMemoryDataProperty.GetValue: String;
  128. Begin
  129.   If TJwMemoryData( GetOrdValue ).CurrentSize=0 Then
  130.     Result := '<No Data>'
  131.   Else
  132.     Result := '<Data Loaded>';
  133. End;
  134.  
  135. Procedure TJwMemoryDataProperty.Edit;
  136. Var
  137.   NewData: TJwMemoryData;
  138. Begin
  139.   NewData := TJwMemoryData.Create;
  140.   With TOpenDialog.Create( Nil ) Do
  141.     Begin
  142.       If Execute Then
  143.         NewData.LoadFromFile( FileName );
  144.       Free;
  145.     End;
  146.   SetOrdValue( LongInt( NewData ) );
  147.   Modified;
  148.   NewData.Free;
  149. End;
  150. /////////////////////////////////////////////////////////////////////////////////////
  151. /////////////////////////////////////////////////////////////////////////////////////
  152. Procedure TJwMemoryData.Clear;
  153. Begin
  154.   If FFileSize > 0 Then
  155.     Begin
  156.       FreeMem( FData, FFileSize );
  157.       FFileSize := 0;
  158.       FOrgFileName := '';
  159.     End;
  160. End;
  161.  
  162. Procedure TJwMemoryData.FillData( aSize: LongInt; aData: Pointer );
  163. Begin
  164.   Self.Clear;
  165.   If aSize > 0 Then
  166.     Begin
  167.       FFileSize := aSize;
  168.       GetMem( FData, FFileSize );
  169.       Move( aData^, FData^, FFileSize );
  170.     End;
  171. End;
  172.  
  173. Procedure TJwMemoryData.Assign( Source: TPersistent );
  174. Begin
  175.   If Source = Nil Then
  176.     Self.Clear
  177.   Else
  178.     begin
  179.       If Source Is TJwMemoryData Then
  180.         FillData( TJwMemoryData( Source ).FFileSize, TJwMemoryData( Source ).FData )
  181.       Else
  182.         Inherited Assign( Source );
  183.     end;
  184. End;
  185.  
  186. Procedure TJwMemoryData.LoadFromFile( const FileName: String );
  187. var
  188.   FromF: File;
  189.   OrgFileMode, NumRead: LongInt;
  190. Begin
  191.   Self.Clear;
  192.   OrgFileMode := FileMode;
  193.   FileMode := 0;
  194.   try
  195.     If FileExists( FileName ) Then
  196.       Begin
  197.         Assignfile( FromF, FileName );
  198.         Reset( FromF, 1 );
  199.         try
  200.           If FileSize( FromF ) > 0 Then
  201.             Begin
  202.               FFileSize := FileSize( FromF );
  203.               GetMem( FData, FFileSize );
  204.               BlockRead( FromF, FData^, FFileSize, NumRead );
  205.               if ( NumRead < FFileSize ) and Assigned( FOnFailToRead ) then
  206.                 FOnFailToRead( Self.GetOwner );
  207.             End;
  208.         finally
  209.           CloseFile( FromF );
  210.         end;
  211.       End;
  212.   finally
  213.     FileMode := OrgFileMode;
  214.   end;
  215.   FOrgFileName := ExtractFileName( FileName );
  216. End;
  217.  
  218. Procedure TJwMemoryData.SaveToFile( const FileName: String );
  219. Var
  220.   ToF: File;
  221.   OrgFileMode, NumWritten: LongInt;
  222. Begin
  223.   OrgFileMode := FileMode;
  224.   FileMode := 2;
  225.   try
  226.     if FileExists( FileName ) then
  227.       begin
  228.         try
  229.           DeleteFile( FileName );
  230.         except  end;
  231.       end;
  232.     if Not( FileExists( FileName ) ) then
  233.       begin
  234.         AssignFile( ToF, FileName );
  235.         ReWrite( ToF, 1 );
  236.         try
  237.           BlockWrite( ToF, FData^, FFileSize, NumWritten );
  238.           if ( NumWritten < FFileSize ) and Assigned( FOnFailToWrite ) then
  239.             FOnFailToWrite( Self.GetOwner );
  240.         finally
  241.           CloseFile( ToF );
  242.         end;
  243.       end
  244.     else if Assigned( FOnFailToDelete ) then
  245.       FOnFailToDelete( Self.GetOwner );
  246.   finally
  247.     FileMode := OrgFileMode;
  248.   end;
  249. End;
  250.  
  251. procedure TJwMemoryData.SaveToStream(Stream: TStream);
  252. begin
  253.   if Assigned( Stream ) then
  254.     Stream.WriteBuffer( FData^, FFileSize );
  255. end;
  256.  
  257. function TJwMemoryData.GetSize: LongInt;
  258. begin
  259.   Result := FFileSize;
  260. end;
  261.  
  262. procedure TJwMemoryData.SetFileName(const Value: String);
  263. begin
  264.   FOrgFileName := Value;
  265. end;
  266.  
  267. Procedure TJwMemoryData.WriteData( Stream: TStream );
  268. Begin
  269.   Stream.Write( FFileSize, SizeOf( FFileSize ) );
  270.   Stream.Write( FData^, FFileSize );
  271. End;
  272.  
  273. Procedure TJwMemoryData.ReadData( Stream: TStream );
  274. Begin
  275.   Clear;
  276.   Stream.Read( FFileSize, SizeOf( FFileSize ) );
  277.   If FFileSize > 0 Then
  278.     Begin
  279.       GetMem( FData, FFileSize );
  280.       Stream.Read( FData^, FFileSize );
  281.     End;
  282. End;
  283.  
  284. Procedure TJwMemoryData.DefineProperties( Filer: TFiler );
  285. Begin
  286.   Inherited DefineProperties( Filer );
  287.   Filer.DefineBinaryProperty( 'jwBinData', ReadData, WriteData, FFileSize > 0 );
  288. End;
  289.  
  290. Constructor TJwMemoryData.Create;
  291. begin
  292.   Inherited Create;
  293.   fFileSize := 0;
  294.   FOrgFileName := '';
  295. end;
  296.  
  297. Destructor TJwMemoryData.destroy;
  298. Begin
  299.   Clear;
  300.   Inherited destroy;
  301. End;
  302. /////////////////////////////////////////////////////////////////////////////////////
  303. /////////////////////////////////////////////////////////////////////////////////////
  304. Procedure TJwBinRes.SaveToFile( const FileName: String );
  305. Begin
  306.   FFileData.SaveToFile( FileName );
  307. End;
  308.  
  309. Procedure TJwBinRes.SetjwMemoryData( Value: TJwMemoryData );
  310. Begin
  311.   FFileData.Assign( Value );
  312. End;
  313.  
  314. Constructor TJwBinRes.Create( AOwner: TComponent );
  315. Begin
  316.   Inherited Create( AOwner );
  317.   FFileData := TJwMemoryData.Create;
  318. End;
  319.  
  320. Destructor TJwBinRes.Destroy;
  321. Begin
  322.   FFileData.Free;
  323.   Inherited Destroy;
  324. End;
  325.  
  326. function TJwBinRes.GetOnFailToDelete: TNotifyEvent;
  327. begin
  328.   Result := FFileData.FOnFailToDelete;
  329. end;
  330.  
  331. function TJwBinRes.GetOnFailToRead: TNotifyEvent;
  332. begin
  333.   Result := FFileData.FOnFailToRead;
  334. end;
  335.  
  336. function TJwBinRes.GetOnFailToWrite: TNotifyEvent;
  337. begin
  338.   Result := FFileData.FOnFailToWrite;
  339. end;
  340.  
  341. procedure TJwBinRes.SetOnFailToDelete(const Value: TNotifyEvent);
  342. begin
  343.   FFileData.FOnFailToDelete := Value;
  344. end;
  345.  
  346. procedure TJwBinRes.SetOnFailToRead(const Value: TNotifyEvent);
  347. begin
  348.   FFileData.FOnFailToRead := Value;
  349. end;
  350.  
  351. procedure TJwBinRes.SetOnFailToWrite(const Value: TNotifyEvent);
  352. begin
  353.   FFileData.FOnFailToWrite := Value;
  354. end;
  355.  
  356. procedure TJwBinRes.SaveThroughStream(const FileName: String);
  357. var
  358.   outStream: TFileStream;
  359. begin
  360.   outStream := TFileStream.Create( FileName, fmCreate );
  361.   try
  362.     FFileData.SaveToStream( outStream );
  363.   finally
  364.     outStream.Free;
  365.   end;
  366. end;
  367.  
  368. procedure TJwBinRes.SaveToStream(Stream: TStream);
  369. begin
  370.   FFileData.SaveToStream( Stream );
  371. end;
  372.  
  373. procedure TJwBinRes.SetTagName(const Value: String);
  374. begin
  375.   FTagName := Value;
  376. end;
  377.  
  378. function TJwBinRes.GetOriginalFileName: String;
  379. begin
  380.   Result := FFileData.FOrgFileName;
  381. end;
  382.  
  383. end.
  384.