home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / ChipCD_1.03.iso / zkuste / delphi / unity / d56 / FNDUTL.ZIP / Streams / cWriters.pas < prev   
Encoding:
Pascal/Delphi Source File  |  2002-10-29  |  11.8 KB  |  356 lines

  1. {$INCLUDE ..\cDefines.inc}
  2. unit cWriters;
  3.  
  4. {                                                                              }
  5. {                              Writers v3.01                                   }
  6. {                                                                              }
  7. {         This unit is copyright ⌐ 2002 by David Butler (david@e.co.za)        }
  8. {                                                                              }
  9. {                  This unit is part of Delphi Fundamentals.                   }
  10. {                   Its original file name is cWriters.pas                     }
  11. {       The latest version is available from the Fundamentals home page        }
  12. {                     http://fundementals.sourceforge.net/                     }
  13. {                                                                              }
  14. {                I invite you to use this unit, free of charge.                }
  15. {        I invite you to distibute this unit, but it must be for free.         }
  16. {             I also invite you to contribute to its development,              }
  17. {             but do not distribute a modified copy of this file.              }
  18. {                                                                              }
  19. {          A forum is available on SourceForge for general discussion          }
  20. {             http://sourceforge.net/forum/forum.php?forum_id=2117             }
  21. {                                                                              }
  22. {                                                                              }
  23. { Revision history:                                                            }
  24. {   12/05/2002  3.01  Created cWriters unit from cStreams.                     }
  25. {                     AWriter, TFileWriter.                                    }
  26. {                                                                              }
  27.  
  28. interface
  29.  
  30. uses
  31.   // Delphi
  32.   SysUtils;
  33.  
  34.  
  35.  
  36. {                                                                              }
  37. { AWriter                                                                      }
  38. {   Writer abstract base class.                                                }
  39. {                                                                              }
  40. type
  41.   AWriter = class
  42.     protected
  43.     Function  GetPosition : Int64; virtual; abstract;
  44.     Procedure SetPosition (const Position : Int64); virtual; abstract;
  45.     Function  GetSize : Int64; virtual; abstract;
  46.     Procedure SetSize (const Size : Int64); virtual; abstract;
  47.  
  48.     public
  49.     Function  Write (const Buffer; const Size : Integer) : Integer; virtual; abstract;
  50.  
  51.     Property  Position : Int64 read GetPosition write SetPosition;
  52.     Property  Size : Int64 read GetSize write SetSize;
  53.   end;
  54.   EWriter = class (Exception);
  55.  
  56.  
  57.  
  58. {                                                                              }
  59. { AWriterEx                                                                    }
  60. {   Base class for Writer implementations. AWriteEx extends AWriter with       }
  61. {   commonly used functions.                                                   }                                    
  62. {                                                                              }
  63. {   All methods in AWriterEx is implemented using calls to the abstract        }
  64. {   methods in AWriter. Writer implementations can override the virtual        }
  65. {   methods in AWriterEx with more efficient versions.                         }
  66. {                                                                              }
  67. type
  68.   TWriterNewLineType = (nlCR, nlLF, nlCRLF, nlLFCR);
  69.   AWriterEx = class (AWriter)
  70.     public
  71.     Procedure RaiseWriteError;
  72.  
  73.     Procedure Append;
  74.     Procedure Truncate; virtual;
  75.  
  76.     Procedure WriteBuffer (const Buffer; const Size : Integer);
  77.     Procedure WriteStr (const Buffer : String); virtual;
  78.     Procedure SetAsString (const S : String);
  79.  
  80.     Procedure WriteByte (const V : Byte);
  81.     Procedure WriteWord (const V : Word);
  82.     Procedure WriteLongWord (const V : LongWord);
  83.     Procedure WriteLongInt (const V : LongInt);
  84.     Procedure WriteInt64 (const V : Int64);
  85.  
  86.     Procedure WriteBufLine (const Buffer; const Size : Integer;
  87.               const NewLineType : TWriterNewLineType = nlCRLF);
  88.     Procedure WriteLine (const S : String; const NewLineType : TWriterNewLineType = nlCRLF);
  89.   end;
  90.  
  91.  
  92.  
  93. {                                                                              }
  94. { TFileWriter                                                                  }
  95. {   Writer implementation for a file.                                          }
  96. {                                                                              }
  97. type
  98.   TFileWriterOpenMode = (fwomOpen,              // Open existing
  99.                          fwomTruncate,          // Open existing and truncate
  100.                          fwomCreate,            // Always create
  101.                          fwomCreateIfNotExist); // Create if not exist else open existing
  102.   TFileWriter = class (AWriterEx)
  103.     protected
  104.     FFileName    : String;
  105.     FHandle      : Integer;
  106.     FHandleOwner : Boolean;
  107.     FFileCreated : Boolean;
  108.  
  109.     Function  GetPosition : Int64; override;
  110.     Procedure SetPosition (const Position : Int64); override;
  111.     Function  GetSize : Int64; override;
  112.     Procedure SetSize (const Size : Int64); override;
  113.  
  114.     public
  115.     Constructor Create (const FileName : String;
  116.                 const OpenMode : TFileWriterOpenMode = fwomCreateIfNotExist); overload;
  117.     Constructor Create (const FileHandle : Integer; const HandleOwner : Boolean); overload;
  118.     Destructor Destroy; override;
  119.  
  120.     Property  Handle : Integer read FHandle;
  121.     Property  HandleOwner : Boolean read FHandleOwner;
  122.     Property  FileCreated : Boolean read FFileCreated;
  123.  
  124.     Function  Write (const Buffer; const Size : Integer) : Integer; override;
  125.  
  126.     Procedure DeleteFile;
  127.   end;
  128.   EFileWriter = class (EWriter);
  129.  
  130.  
  131.  
  132. {                                                                              }
  133. { TOutputWriter                                                                }
  134. {   Writer implementation for standard system output.                          }
  135. {                                                                              }
  136. type
  137.   TOutputWriter = class (AWriterEx)
  138.     public
  139.     Function  Write (const Buffer; const Size : Integer) : Integer; override;
  140.   end;
  141.  
  142.  
  143.  
  144. implementation
  145.  
  146. uses
  147.   // Delphi
  148.   Windows;
  149.  
  150.  
  151.  
  152. {                                                                              }
  153. { AWriterEx                                                                    }
  154. {                                                                              }
  155. Procedure AWriterEx.RaiseWriteError;
  156.   Begin
  157.     raise EWriter.Create ('Write error');
  158.   End;
  159.  
  160. Procedure AWriterEx.Append;
  161.   Begin
  162.     Position := Size;
  163.   End;
  164.  
  165. Procedure AWriterEx.Truncate;
  166.   Begin
  167.     Size := Position;
  168.   End;
  169.  
  170. Procedure AWriterEx.WriteBuffer (const Buffer; const Size : Integer);
  171.   Begin
  172.     if Size <= 0 then
  173.       exit;
  174.     if Write (Buffer, Size) <> Size then
  175.       RaiseWriteError;
  176.   End;
  177.  
  178. Procedure AWriterEx.WriteStr (const Buffer : String);
  179.   Begin
  180.     WriteBuffer (Pointer (Buffer)^, Length (Buffer));
  181.   End;
  182.  
  183. Procedure AWriterEx.SetAsString (const S : String);
  184.   Begin
  185.     Position := 0;
  186.     WriteStr (S);
  187.     Truncate;
  188.   End;
  189.  
  190. Procedure AWriterEx.WriteByte (const V : Byte);
  191.   Begin
  192.     WriteBuffer (V, Sizeof (Byte));
  193.   End;
  194.  
  195. Procedure AWriterEx.WriteWord (const V : Word);
  196.   Begin
  197.     WriteBuffer (V, Sizeof (Word));
  198.   End;
  199.  
  200. Procedure AWriterEx.WriteLongWord (const V : LongWord);
  201.   Begin
  202.     WriteBuffer (V, Sizeof (LongWord));
  203.   End;
  204.  
  205. Procedure AWriterEx.WriteLongInt (const V : LongInt);
  206.   Begin
  207.     WriteBuffer (V, Sizeof (LongInt));
  208.   End;
  209.  
  210. Procedure AWriterEx.WriteInt64 (const V : Int64);
  211.   Begin
  212.     WriteBuffer (V, Sizeof (Int64));
  213.   End;
  214.  
  215. Procedure AWriterEx.WriteBufLine (const Buffer; const Size : Integer; const NewLineType : TWriterNewLineType);
  216.   Begin
  217.     WriteBuffer (Buffer, Size);
  218.     Case NewLineType of
  219.       nlCR   : WriteByte (13);
  220.       nlLF   : WriteByte (10);
  221.       nlCRLF : WriteStr (#13#10);
  222.       nlLFCR : WriteStr (#10#13);
  223.     end;
  224.   End;
  225.  
  226. Procedure AWriterEx.WriteLine (const S : String; const NewLineType : TWriterNewLineType);
  227.   Begin
  228.     WriteBufLine (Pointer (S)^, Length (S), NewLineType);
  229.   End;
  230.  
  231.  
  232.  
  233. {                                                                              }
  234. { TFileWriter                                                                  }
  235. {                                                                              }
  236. Constructor TFileWriter.Create (const FileName : String; const OpenMode : TFileWriterOpenMode);
  237. var CreateFile : Boolean;
  238.   Begin
  239.     inherited Create;
  240.     FFileName := FileName;
  241.     Case OpenMode of
  242.       fwomCreate           : CreateFile := True;
  243.       fwomCreateIfNotExist : CreateFile := not FileExists (FileName);
  244.     else
  245.       CreateFile := False;
  246.     end;
  247.     if CreateFile then
  248.       FHandle := FileCreate (FileName) else
  249.       FHandle := FileOpen (FileName, fmOpenReadWrite);
  250.     if FHandle = -1 then {$IFDEF DELPHI6_UP}
  251.       RaiseLastOSError; {$ELSE}
  252.       RaiseLastWin32Error; {$ENDIF}
  253.     FHandleOwner := True;
  254.     FFileCreated := CreateFile;
  255.     if OpenMode = fwomTruncate then
  256.       if not SetEndOfFile (FHandle) then
  257.         raise EFileWriter.Create ('File truncate error');
  258.   End;
  259.  
  260. Constructor TFileWriter.Create (const FileHandle : Integer; const HandleOwner : Boolean);
  261.   Begin
  262.     inherited Create;
  263.     FHandle := FileHandle;
  264.     FHandleOwner := HandleOwner;
  265.   End;
  266.  
  267. Destructor TFileWriter.Destroy;
  268.   Begin
  269.     if FHandleOwner and (FHandle <> -1) and (FHandle <> 0) then
  270.       FileClose (FHandle);
  271.     inherited Destroy;
  272.   End;
  273.  
  274. Function TFileWriter.GetPosition : Int64;
  275.   Begin
  276.     Result := FileSeek (FHandle, Int64 (0), 1);
  277.     if Result = -1 then
  278.       raise EFileWriter.Create ('File error');
  279.   End;
  280.  
  281. Procedure TFileWriter.SetPosition (const Position : Int64);
  282.   Begin
  283.     if FileSeek (FHandle, Position, 0) = -1 then
  284.       raise EFileWriter.Create ('File seek error');
  285.   End;
  286.  
  287. Function TFileWriter.GetSize : Int64;
  288. var I : Int64;
  289.   Begin
  290.     I := GetPosition;
  291.     Result := FileSeek (FHandle, Int64 (0), 2);
  292.     SetPosition (I);
  293.     if Result = -1 then
  294.       raise EFileWriter.Create ('File error');
  295.   End;
  296.  
  297. Procedure TFileWriter.SetSize (const Size : Int64);
  298.   Begin
  299.     SetPosition (Size);
  300.     if not SetEndOfFile (FHandle) then
  301.       raise EFileWriter.Create ('File resize error');
  302.   End;
  303.  
  304. Function TFileWriter.Write (const Buffer; const Size : Integer) : Integer;
  305. var I : Integer;
  306.   Begin
  307.     if Size <= 0 then
  308.       begin
  309.         Result := 0;
  310.         exit;
  311.       end;
  312.     I := FileWrite (FHandle, Buffer, Size);
  313.     if I < 0 then {$IFDEF DELPHI6_UP}
  314.       RaiseLastOSError; {$ELSE}
  315.       RaiseLastWin32Error; {$ENDIF}
  316.     Result := I;
  317.   End;
  318.  
  319. Procedure TFileWriter.DeleteFile;
  320.   Begin
  321.     if FFileName = '' then
  322.       raise EFileWriter.Create ('No filename');
  323.     if (FHandle <> -1) and (FHandle <> 0) then
  324.       FileClose (FHandle);
  325.     FHandle := -1;
  326.     SysUtils.DeleteFile (FFileName);
  327.   End;
  328.  
  329.  
  330.  
  331. {                                                                              }
  332. { TOutputWriter                                                                }
  333. {                                                                              }
  334. Function TOutputWriter.Write (const Buffer; const Size : Integer) : Integer;
  335. var I : Integer;
  336.     P : PByte;
  337.   Begin
  338.     if Size <= 0 then
  339.       begin
  340.         Result := 0;
  341.         exit;
  342.       end;
  343.     P := @Buffer;
  344.     For I := 1 to Size do
  345.       begin
  346.         System.Write (Char (P^));
  347.         Inc (P);
  348.       end;
  349.     Result := Size;
  350.   End;
  351.  
  352.  
  353.  
  354. end.
  355.  
  356.