home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Runimage / Delphi50 / Source / Toolsapi / ISTREAMS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-11  |  5.0 KB  |  189 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Borland Delphi Visual Component Library         }
  5. {                                                       }
  6. {       Copyright (c) 1995,99 Inprise Corporation       }
  7. {                                                       }
  8. {*******************************************************}
  9.  
  10. unit IStreams;
  11.  
  12. interface
  13.  
  14. uses Windows, ActiveX, SysUtils, Classes, VirtIntf, Axctrls;
  15.  
  16. type
  17.  
  18.   { TIStreamAdapter }
  19.  
  20.   TIStreamAdapter = class(TStreamAdapter, IStreamModifyTime)
  21.   protected
  22.     FModifyTime: Longint;
  23.   public
  24.     constructor Create(Stream: TStream; Ownership: TStreamOwnership = soReference);
  25.     function Write(pv: Pointer; cb: Longint; pcbWritten: PLongint): HResult; override;
  26.     function Stat(out statstg: TStatStg; grfStatFlag: Longint): HResult; override;
  27.     function GetModifyTime: Longint; virtual; stdcall;
  28.     procedure SetModifyTime(Value: Longint); virtual; stdcall;
  29.   end;
  30.  
  31.   { TIMemoryStream }
  32.  
  33.   TIMemoryStream = class(TIStreamAdapter)
  34.   private
  35.     function GetMemoryStream: TMemoryStream;
  36.   public
  37.     constructor Create(Stream: TMemoryStream; Ownership: TStreamOwnership = soReference);
  38.     property MemoryStream: TMemoryStream read GetMemoryStream;
  39.   end;
  40.  
  41.   { TIFileStream }
  42.  
  43.   TIFileStream = class(TStreamAdapter, IStreamModifyTime)
  44.   private
  45.     function GetFileStream: TFileStream;
  46.   public
  47.     constructor Create(const FileName: string; Mode: Word);
  48.     function Commit(grfCommitFlags: Longint): HResult; override;
  49.     function Stat(out statstg: TStatStg; grfStatFlag: Longint): HResult; override;
  50.     function GetModifyTime: Longint; stdcall;
  51.     procedure SetModifyTime(Time: Longint); stdcall;
  52.     property FileStream: TFileStream read GetFileStream;
  53.   end;
  54.  
  55.   { TVirtualStream }
  56.  
  57.   TVirtualStream = class(TOleStream)
  58.   private
  59.     FStreamModifyTime: IStreamModifyTime;
  60.   public
  61.     constructor Create(AStream: IStream);
  62.     function GetModifyTime: Longint;
  63.     procedure SetModifyTime(Time: Longint);
  64.   end;
  65.  
  66.   TExceptionHandler = procedure;
  67.  
  68. const
  69.   ExceptionHandler: TExceptionHandler = nil;
  70.  
  71. implementation
  72.  
  73. { TIStreamAdapter }
  74.  
  75. constructor TIStreamAdapter.Create(Stream: TStream;
  76.   Ownership: TStreamOwnership);
  77. begin
  78.   inherited Create(Stream, Ownership);
  79.   FModifyTime := DateTimeToFileDate(Now);
  80. end;
  81.  
  82. function TIStreamAdapter.Write(pv: Pointer; cb: Longint;
  83.   pcbWritten: PLongint): HResult;
  84. begin
  85.   Result := inherited Write(pv, cb, pcbWritten);
  86.   FModifyTime := DateTimeToFileDate(Now);
  87. end;
  88.  
  89. function TIStreamAdapter.Stat(out statstg: TStatStg; grfStatFlag: Longint): HResult;
  90. var
  91.   DosFileTime: Longint;
  92.   LocalFileTime: TFileTime;
  93. begin
  94.   Result := inherited Stat(statstg, grfStatFlag);
  95.   if Result <> 0 then Exit;
  96.   DosFileTime := GetModifyTime;
  97.   DosDateTimeToFileTime(LongRec(DosFileTime).Hi, LongRec(DosFileTime).Lo, LocalFileTime);
  98.   LocalFileTimeToFileTime(LocalFileTime, statstg.mtime);
  99. end;
  100.  
  101. function TIStreamAdapter.GetModifyTime: Longint;
  102. begin
  103.   Result := FModifyTime;
  104. end;
  105.  
  106. procedure TIStreamAdapter.SetModifyTime(Value: Longint);
  107. begin
  108.   FModifyTime := Value;
  109. end;
  110.  
  111. { TIMemoryStream }
  112.  
  113. constructor TIMemoryStream.Create(Stream: TMemoryStream;
  114.   Ownership: TStreamOwnership);
  115. begin
  116.   if Stream = nil then
  117.   begin
  118.     Ownership := soOwned;
  119.     Stream := TMemoryStream.Create;
  120.   end;
  121.   inherited Create(Stream, Ownership);
  122. end;
  123.  
  124. function TIMemoryStream.GetMemoryStream: TMemoryStream;
  125. begin
  126.   Result := TMemoryStream(Stream);
  127. end;
  128.  
  129. { TIFileStream }
  130.  
  131. constructor TIFileStream.Create(const FileName: string; Mode: Word);
  132. begin
  133.   inherited Create(TFileStream.Create(FileName, Mode), soOwned);
  134. end;
  135.  
  136. function TIFileStream.GetFileStream: TFileStream;
  137. begin
  138.   Result := TFileStream(Stream);
  139. end;
  140.  
  141. function TIFileStream.Stat(out statstg: TStatStg; grfStatFlag: Longint): HResult;
  142. begin
  143.   Result := inherited Stat(statstg, grfStatFlag);
  144.   if Result <> 0 then Exit;
  145.   GetFileTime(TFileStream(Stream).Handle, @statstg.ctime, @statstg.atime, @statstg.mtime);
  146. end;
  147.  
  148. function TIFileStream.GetModifyTime: Longint;
  149. begin
  150.   Result := FileGetDate(FileStream.Handle);
  151. end;
  152.  
  153. procedure TIFileStream.SetModifyTime(Time: Longint);
  154. begin
  155.   FileSetDate(FileStream.Handle, Time);
  156. end;
  157.  
  158. function TIFileStream.Commit(grfCommitFlags: Longint): HResult;
  159. begin
  160.   FlushFileBuffers(FileStream.Handle);
  161.   Result := inherited Commit(grfCommitFlags);
  162. end;
  163.  
  164. { TVirtualStream }
  165.  
  166. constructor TVirtualStream.Create(AStream: IStream);
  167. begin
  168.   inherited Create(AStream);
  169.   if AStream.QueryInterface(IStreamModifyTime, FStreamModifyTime) <> 0 then
  170.     FStreamModifyTime := nil;
  171. end;
  172.  
  173. function TVirtualStream.GetModifyTime: Longint;
  174. begin
  175.   if FStreamModifyTime <> nil then
  176.     Result := FStreamModifyTime.GetModifyTime
  177.   else
  178.     Result := 0;
  179. end;
  180.  
  181. procedure TVirtualStream.SetModifyTime(Time: Longint);
  182. begin
  183.   if FStreamModifyTime <> nil then
  184.     FStreamModifyTime.SetModifyTime(Time);
  185. end;
  186.  
  187. end.
  188.  
  189.