home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / full / delphi / RUNIMAGE / DELPHI30 / SOURCE / TOOLSAPI / ISTREAMS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-08-04  |  5.9 KB  |  246 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Delphi Visual Component Library                 }
  5. {                                                       }
  6. {       Copyright (c) 1995,97 Borland International     }
  7. {                                                       }
  8. {*******************************************************}
  9.  
  10. unit IStreams;
  11.  
  12. interface
  13.  
  14. uses VirtIntf, Classes, SysUtils;
  15.  
  16. type
  17.  
  18.   { TIVCLStreamAdapter }
  19.  
  20.   TIVCLStreamAdapter = class(TIStream)
  21.   protected
  22.     FStream: TStream;
  23.     FOwnStream: Boolean;
  24.   public
  25.     constructor Create(AStream: TStream);
  26.     destructor Destroy; override;
  27.     function Read(var Buffer; Count: Longint): Longint; override;
  28.     function Write(const Buffer; Count: Longint): Longint; override;
  29.     function Seek(Offset: Longint; Origin: Word): Longint; override;
  30.     function GetModifyTime: Longint; override;
  31.     procedure Flush; override;
  32.     procedure SetModifyTime(Value: Longint); override;
  33.     property Stream: TStream read FStream;
  34.   end;
  35.  
  36.   { TIStreamAdapter }
  37.  
  38.   TIStreamAdapter = class(TIVCLStreamAdapter)
  39.   protected
  40.     FModifyTime: Longint;
  41.   public
  42.     constructor Create(AStream: TStream; AOwnStream: Boolean);
  43.     function GetModifyTime: Longint; override;
  44.     procedure SetModifyTime(Value: Longint); override;
  45.     function Write(const Buffer; Count: Longint): Longint; override;
  46.     property OwnStream: Boolean read FOwnStream write FOwnStream;
  47.   end;
  48.  
  49.   { TIMemoryStream }
  50.  
  51.   TIMemoryStream = class(TIStreamAdapter)
  52.   private
  53.     function GetMemoryStream: TMemoryStream;
  54.   public
  55.     constructor Create(AMemoryStream: TMemoryStream);
  56.     property MemoryStream: TMemoryStream read GetMemoryStream;
  57.   end;
  58.  
  59.   { TIFileStream }
  60.  
  61.   TIFileStream = class(TIVCLStreamAdapter)
  62.   private
  63.     function GetFileStream: TFileStream;
  64.   public
  65.     constructor Create(const FileName: string; Mode: Word);
  66.     function GetModifyTime: Longint; override;
  67.     procedure SetModifyTime(Time: Longint); override;
  68.     procedure Flush; override;
  69.     property FileStream: TFileStream read GetFileStream;
  70.   end;
  71.  
  72.   { TVirtualStream }
  73.  
  74.   TVirtualStream = class(TStream)
  75.   private
  76.     FIStream: TIStream;
  77.   public
  78.     constructor Create(AIStream: TIStream);
  79.     destructor Destroy; override;
  80.     function Read(var Buffer; Count: Longint): Longint; override;
  81.     function Write(const Buffer; Count: Longint): Longint; override;
  82.     function Seek(Offset: Longint; Origin: Word): Longint; override;
  83.     function GetModifyTime: Longint;
  84.     procedure SetModifyTime(Time: Longint);
  85.   end;
  86.  
  87.   TExceptionHandler = procedure;
  88.  
  89. const
  90.   ExceptionHandler: TExceptionHandler = nil;
  91.  
  92. implementation
  93.  
  94. uses Windows;
  95.  
  96. { TIVCLStreamAdapter }
  97.  
  98. constructor TIVCLStreamAdapter.Create(AStream: TStream);
  99. begin
  100.   inherited Create;
  101.   FStream := AStream;
  102. end;
  103.  
  104. destructor TIVCLStreamAdapter.Destroy;
  105. begin
  106.   if FOwnStream then FStream.Free;
  107.   inherited Destroy;
  108. end;
  109.  
  110. function TIVCLStreamAdapter.Read(var Buffer; Count: Longint): Longint;
  111. begin
  112.   Result := FStream.Read(Buffer, Count);
  113. end;
  114.  
  115. function TIVCLStreamAdapter.Write(const Buffer; Count: Longint): Longint;
  116. begin
  117.   Result := FStream.Write(Buffer, Count);
  118. end;
  119.  
  120. function TIVCLStreamAdapter.Seek(Offset: Longint; Origin: Word): Longint;
  121. begin
  122.   Result := FStream.Seek(Offset, Origin);
  123. end;
  124.  
  125. function TIVCLStreamAdapter.GetModifyTime: Longint;
  126. begin
  127.   Result := 0;
  128. end;
  129.  
  130. procedure TIVCLStreamAdapter.SetModifyTime(Value: Longint);
  131. begin
  132. end;
  133.  
  134. procedure TIVCLStreamAdapter.Flush;
  135. begin
  136. end;
  137.  
  138. { TIStreamAdapter }
  139.  
  140. constructor TIStreamAdapter.Create(AStream: TStream; AOwnStream: Boolean);
  141. begin
  142.   FOwnStream := AOwnStream;
  143.   inherited Create(AStream);
  144.   FModifyTime := DateTimeToFileDate(Now);
  145. end;
  146.  
  147. function TIStreamAdapter.Write(const Buffer; Count: Longint): Longint;
  148. begin
  149.   Result := inherited Write(Buffer, Count);
  150.   FModifyTime := DateTimeToFileDate(Now);
  151. end;
  152.  
  153. function TIStreamAdapter.GetModifyTime: Longint;
  154. begin
  155.   Result := FModifyTime;
  156. end;
  157.  
  158. procedure TIStreamAdapter.SetModifyTime(Value: Longint);
  159. begin
  160.   FModifyTime := Value;
  161. end;
  162.  
  163. { TIMemoryStream }
  164.  
  165. constructor TIMemoryStream.Create(AMemoryStream: TMemoryStream);
  166. begin
  167.   FOwnStream := AMemoryStream = nil;
  168.   if AMemoryStream = nil then AMemoryStream := TMemoryStream.Create;
  169.   inherited Create(AMemoryStream, FOwnStream);
  170. end;
  171.  
  172. function TIMemoryStream.GetMemoryStream: TMemoryStream;
  173. begin
  174.   Result := TMemoryStream(FStream);
  175. end;
  176.  
  177. { TIFileStream }
  178.  
  179. constructor TIFileStream.Create(const FileName: string; Mode: Word);
  180. begin
  181.   FOwnStream := True;
  182.   inherited Create(TFileStream.Create(FileName, Mode));
  183. end;
  184.  
  185. function TIFileStream.GetFileStream: TFileStream;
  186. begin
  187.   Result := TFileStream(FStream);
  188. end;
  189.  
  190. function TIFileStream.GetModifyTime: Longint;
  191. begin
  192.   Result := FileGetDate(FileStream.Handle);
  193. end;
  194.  
  195. procedure TIFileStream.SetModifyTime(Time: Longint);
  196. begin
  197.   FileSetDate(FileStream.Handle, Time);
  198. end;
  199.  
  200. procedure TIFileStream.Flush;
  201. begin
  202.   FlushFileBuffers(FileStream.Handle);
  203. end;
  204.  
  205. { TVirtualStream }
  206.  
  207. constructor TVirtualStream.Create(AIStream: TIStream);
  208. begin
  209.   inherited Create;
  210.   FIStream := AIStream;
  211. end;
  212.  
  213. destructor TVirtualStream.Destroy;
  214. begin
  215.   FIStream.Free;
  216.   inherited Destroy;
  217. end;
  218.  
  219. function TVirtualStream.Read(var Buffer; Count: Longint): Longint;
  220. begin
  221.   Result := FIStream.Read(Buffer, Count);
  222. end;
  223.  
  224. function TVirtualStream.Write(const Buffer; Count: Longint): Longint;
  225. begin
  226.   Result := FIStream.Write(Buffer, Count);
  227. end;
  228.  
  229. function TVirtualStream.Seek(Offset: Longint; Origin: Word): Longint;
  230. begin
  231.   Result := FIStream.Seek(Offset, Origin);
  232. end;
  233.  
  234. function TVirtualStream.GetModifyTime: Longint;
  235. begin
  236.   Result := FIStream.GetModifyTime;
  237. end;
  238.  
  239. procedure TVirtualStream.SetModifyTime(Time: Longint);
  240. begin
  241.   FIStream.SetModifyTime(Time);
  242. end;
  243.  
  244. end.
  245.  
  246.