6d1fe2006b81b67531149eb7e380659f1672dd7d
[occt.git] / src / OSD / OSD_File.hxx
1 // Created on: 1992-02-17
2 // Created by: Stephan GARNAUD
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #ifndef _OSD_File_HeaderFile
18 #define _OSD_File_HeaderFile
19
20 #include <Standard.hxx>
21 #include <Standard_DefineAlloc.hxx>
22 #include <Standard_Handle.hxx>
23
24 #include <Standard_Boolean.hxx>
25 #include <OSD_LockType.hxx>
26 #include <OSD_OpenMode.hxx>
27 #include <Standard_Integer.hxx>
28 #include <Standard_Address.hxx>
29 #include <OSD_FileNode.hxx>
30 #include <OSD_FromWhere.hxx>
31 #include <OSD_KindFile.hxx>
32 #include <Standard_Size.hxx>
33 class Standard_ProgramError;
34 class OSD_Path;
35 class OSD_Protection;
36 class TCollection_AsciiString;
37
38
39 //! Basic tools to manage files
40 //! Warning: 'ProgramError' is raised when somebody wants to use the methods
41 //! Read, Write, Seek, Close when File is not open.
42 class OSD_File  : public OSD_FileNode
43 {
44 public:
45
46   DEFINE_STANDARD_ALLOC
47
48   
49   //! Creates File object.
50   Standard_EXPORT OSD_File();
51   
52   //! Instantiates the object file, storing its name
53   Standard_EXPORT OSD_File(const OSD_Path& Name);
54
55   //! Unlocks and closes a file, deletes a descriptor and destructs a file object.
56   Standard_EXPORT ~OSD_File();
57   
58   //! CREATES a file if it doesn't already exists or empties
59   //! an existing file.
60   //! After 'Build', the file is open.
61   //! If no name was given, ProgramError is raised.
62   Standard_EXPORT void Build (const OSD_OpenMode Mode, const OSD_Protection& Protect);
63   
64   //! Opens a File with specific attributes
65   //! This works only on already existing file.
66   //! If no name was given, ProgramError is raised.
67   Standard_EXPORT void Open (const OSD_OpenMode Mode, const OSD_Protection& Protect);
68   
69   //! Appends data to an existing file.
70   //! If file doesn't exist, creates it first.
71   //! After 'Append', the file is open.
72   //! If no name was given, ProgramError is raised.
73   Standard_EXPORT void Append (const OSD_OpenMode Mode, const OSD_Protection& Protect);
74   
75   //! Attempts to read Nbyte bytes from the file associated with
76   //! the object file.
77   //! Upon successful completion, Read returns the number of
78   //! bytes actually read and placed in the Buffer. This number
79   //! may be less than Nbyte if the number of bytes left in the file
80   //! is less than Nbyte bytes. In this case only number of read
81   //! bytes will be placed in the buffer.
82   Standard_EXPORT void Read (TCollection_AsciiString& Buffer, const Standard_Integer Nbyte);
83   
84   //! Reads bytes from the data pointed to by the object file
85   //! into the buffer <Buffer>.
86   //! Data is read until <NByte-1> bytes have been read,
87   //! until     a newline character is read and transferred into
88   //! <Buffer>, or until an EOF (End-of-File) condition is
89   //! encountered.
90   //! Upon successful completion, Read returns the number of
91   //! bytes actually read into <NByteRead> and placed into the
92   //! Buffer <Buffer>.
93   Standard_EXPORT void ReadLine (TCollection_AsciiString& Buffer, const Standard_Integer NByte, Standard_Integer& NbyteRead);
94
95   //! Reads bytes from the data pointed to by the object file
96   //! into the buffer <Buffer>.
97   //! Data is read until <NByte-1> bytes have been read,
98   //! until     a newline character is read and transferred into
99   //! <Buffer>, or until an EOF (End-of-File) condition is
100   //! encountered.
101   //! Upon successful completion, Read returns the number of
102   //! bytes actually read and placed into the Buffer <Buffer>.
103   inline Standard_Integer ReadLine (
104     TCollection_AsciiString& Buffer, const Standard_Integer NByte) 
105   {
106     Standard_Integer NbyteRead;
107     ReadLine(Buffer, NByte, NbyteRead);
108     return NbyteRead;
109   }
110
111   
112   //! Attempts to read Nbyte bytes from the files associated with
113   //! the object File.
114   //! Upon successful completion, Read returns the number of
115   //! bytes actually read and placed in the Buffer. This number
116   //! may be less than Nbyte if the number of bytes left in the file
117   //! is less than Nbyte bytes. For this reason the output
118   //! parameter Readbyte will contain the number of read bytes.
119   Standard_EXPORT void Read (const Standard_Address Buffer, const Standard_Integer Nbyte, Standard_Integer& Readbyte);
120   
121   //! Attempts to write Nbyte bytes from the AsciiString to the file
122   //! associated to the object File.
123   Standard_EXPORT void Write (const TCollection_AsciiString& Buffer, const Standard_Integer Nbyte);
124   
125   //! Attempts to write Nbyte bytes from the buffer pointed
126   //! to by Buffer to the file associated to the object File.
127   Standard_EXPORT void Write (const Standard_Address Buffer, const Standard_Integer Nbyte);
128   
129   //! Sets the seek pointer associated with the open file
130   Standard_EXPORT void Seek (const Standard_Integer Offset, const OSD_FromWhere Whence);
131   
132   //! Closes the file (and deletes a descriptor)
133   Standard_EXPORT void Close();
134   
135   //! Returns TRUE if the seek pointer is at end of file.
136   Standard_EXPORT Standard_Boolean IsAtEnd();
137   
138   //! Returns the kind of file. A file can be a
139   //! file, a directory or a link.
140   Standard_EXPORT OSD_KindFile KindOfFile() const;
141   
142   //! Makes a temporary File
143   //! This temporary file is already open !
144   Standard_EXPORT void BuildTemporary();
145   
146   //! Locks current file
147   Standard_EXPORT void SetLock (const OSD_LockType Lock);
148   
149   //! Unlocks current file
150   Standard_EXPORT void UnLock();
151   
152   //! Returns the current lock state
153   Standard_EXPORT OSD_LockType GetLock();
154   
155   //! Returns TRUE if this file is locked.
156   Standard_EXPORT Standard_Boolean IsLocked();
157   
158   //! Returns actual number of bytes of <me>.
159   Standard_EXPORT Standard_Size Size();
160
161   //! Returns TRUE if <me> is open.
162   Standard_EXPORT Standard_Boolean IsOpen() const;
163   
164   //! returns TRUE if the file exists and if the user
165   //! has the autorization to read it.
166   Standard_EXPORT Standard_Boolean IsReadable();
167   
168   //! returns TRUE if the file can be read and overwritten.
169   Standard_EXPORT Standard_Boolean IsWriteable();
170   
171   //! returns TRUE if the file can be executed.
172   Standard_EXPORT Standard_Boolean IsExecutable();
173   
174   //! Enables to emulate unix "tail -f" command.
175   //! If a line is available in the file <me> returns it.
176   //! Otherwise attemps to read again aNbTries times in the file
177   //! waiting aDelay seconds between each read.
178   //! If meanwhile the file increases returns the next line, otherwise
179   //! returns FALSE.
180   Standard_EXPORT Standard_Boolean ReadLastLine (TCollection_AsciiString& aLine, const Standard_Integer aDelay, const Standard_Integer aNbTries);
181   
182   //! find an editor on the system and edit the given file
183   Standard_EXPORT Standard_Boolean Edit();
184
185   //! Set file pointer position to the beginning of the file
186   Standard_EXPORT void Rewind();
187
188   //! Redirect a standard handle (fileno(stdout), fileno(stdin) or 
189   //! fileno(stderr) to this OSD_File and return the copy of the original
190   //! standard handle.
191   //! Example:
192   //!    OSD_File aTmp;
193   //!    aTmp.BuildTemporary();
194   //!    int stdfd = _fileno(stdout);
195   //!
196   //!    int oldout = aTmp.Capture(stdfd);
197   //!    cout << "Some output to the file" << endl;
198   //!    cout << flush;
199   //!    fflush(stdout);
200   //!
201   //!    _dup2(oldout, stdfd); // Restore standard output
202   //!    aTmp.Close();
203   Standard_EXPORT int Capture(int theDescr);
204
205
206 protected:
207
208
209
210   Standard_Integer myIO;
211   Standard_Address myFILE;
212   Standard_Integer myFileChannel;
213   Standard_Address myFileHandle;
214
215
216 private:
217
218
219
220   Standard_Boolean ImperativeFlag;
221   OSD_LockType myLock;
222   OSD_OpenMode myMode;
223
224
225 };
226
227
228
229
230
231
232
233 #endif // _OSD_File_HeaderFile