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
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #ifndef _OSD_File_HeaderFile
18 #define _OSD_File_HeaderFile
20 #include <Standard.hxx>
21 #include <Standard_DefineAlloc.hxx>
22 #include <Standard_Handle.hxx>
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;
36 class TCollection_AsciiString;
40 //! Basic tools to manage files
41 //! Warning: 'ProgramError' is raised when somebody wants to use the methods
42 //! Read, Write, Seek, Close when File is not open.
43 class OSD_File : public OSD_FileNode
50 //! Creates File object.
51 Standard_EXPORT OSD_File();
53 //! Instantiates the object file, storing its name
54 Standard_EXPORT OSD_File(const OSD_Path& Name);
56 //! Unlocks and closes a file, deletes a descriptor and destructs a file object.
57 Standard_EXPORT ~OSD_File();
59 //! CREATES a file if it doesn't already exists or empties
61 //! After 'Build', the file is open.
62 //! If no name was given, ProgramError is raised.
63 Standard_EXPORT void Build (const OSD_OpenMode Mode, const OSD_Protection& Protect);
65 //! Opens a File with specific attributes
66 //! This works only on already existing file.
67 //! If no name was given, ProgramError is raised.
68 Standard_EXPORT void Open (const OSD_OpenMode Mode, const OSD_Protection& Protect);
70 //! Appends data to an existing file.
71 //! If file doesn't exist, creates it first.
72 //! After 'Append', the file is open.
73 //! If no name was given, ProgramError is raised.
74 Standard_EXPORT void Append (const OSD_OpenMode Mode, const OSD_Protection& Protect);
76 //! Attempts to read Nbyte bytes from the file associated with
78 //! Upon successful completion, Read returns the number of
79 //! bytes actually read and placed in the Buffer. This number
80 //! may be less than Nbyte if the number of bytes left in the file
81 //! is less than Nbyte bytes. In this case only number of read
82 //! bytes will be placed in the buffer.
83 Standard_EXPORT void Read (TCollection_AsciiString& Buffer, const Standard_Integer Nbyte);
85 //! Reads bytes from the data pointed to by the object file
86 //! into the buffer <Buffer>.
87 //! Data is read until <NByte-1> bytes have been read,
88 //! until a newline character is read and transferred into
89 //! <Buffer>, or until an EOF (End-of-File) condition is
91 //! Upon successful completion, Read returns the number of
92 //! bytes actually read into <NByteRead> and placed into the
94 Standard_EXPORT void ReadLine (TCollection_AsciiString& Buffer, const Standard_Integer NByte, Standard_Integer& NbyteRead);
96 //! Reads bytes from the data pointed to by the object file
97 //! into the buffer <Buffer>.
98 //! Data is read until <NByte-1> bytes have been read,
99 //! until a newline character is read and transferred into
100 //! <Buffer>, or until an EOF (End-of-File) condition is
102 //! Upon successful completion, Read returns the number of
103 //! bytes actually read and placed into the Buffer <Buffer>.
104 inline Standard_Integer ReadLine (
105 TCollection_AsciiString& Buffer, const Standard_Integer NByte)
107 Standard_Integer NbyteRead;
108 ReadLine(Buffer, NByte, NbyteRead);
113 //! Attempts to read Nbyte bytes from the files associated with
115 //! Upon successful completion, Read returns the number of
116 //! bytes actually read and placed in the Buffer. This number
117 //! may be less than Nbyte if the number of bytes left in the file
118 //! is less than Nbyte bytes. For this reason the output
119 //! parameter Readbyte will contain the number of read bytes.
120 Standard_EXPORT void Read (Standard_Address& Buffer, const Standard_Integer Nbyte, Standard_Integer& Readbyte);
122 //! Attempts to write Nbyte bytes from the AsciiString to the file
123 //! associated to the object File.
124 Standard_EXPORT void Write (const TCollection_AsciiString& Buffer, const Standard_Integer Nbyte);
126 //! Attempts to write Nbyte bytes from the buffer pointed
127 //! to by Buffer to the file associated to the object File.
128 Standard_EXPORT void Write (const Standard_Address Buffer, const Standard_Integer Nbyte);
130 //! Sets the seek pointer associated with the open file
131 Standard_EXPORT void Seek (const Standard_Integer Offset, const OSD_FromWhere Whence);
133 //! Closes the file (and deletes a descriptor)
134 Standard_EXPORT void Close();
136 //! Returns TRUE if the seek pointer is at end of file.
137 Standard_EXPORT Standard_Boolean IsAtEnd();
139 //! Returns the kind of file. A file can be a
140 //! file, a directory or a link.
141 Standard_EXPORT OSD_KindFile KindOfFile() const;
143 //! Makes a temporary File
144 //! This temporary file is already open !
145 Standard_EXPORT void BuildTemporary();
147 //! Locks current file
148 Standard_EXPORT void SetLock (const OSD_LockType Lock);
150 //! Unlocks current file
151 Standard_EXPORT void UnLock();
153 //! Returns the current lock state
154 Standard_EXPORT OSD_LockType GetLock();
156 //! Returns TRUE if this file is locked.
157 Standard_EXPORT Standard_Boolean IsLocked();
159 //! Returns actual number of bytes of <me>.
160 Standard_EXPORT Standard_Size Size();
162 //! Prints a file on selected printer.
163 Standard_EXPORT void Print (const OSD_Printer& WhichPrinter);
165 //! Returns TRUE if <me> is open.
166 Standard_EXPORT Standard_Boolean IsOpen() const;
168 //! returns TRUE if the file exists and if the user
169 //! has the autorization to read it.
170 Standard_EXPORT Standard_Boolean IsReadable();
172 //! returns TRUE if the file can be read and overwritten.
173 Standard_EXPORT Standard_Boolean IsWriteable();
175 //! returns TRUE if the file can be executed.
176 Standard_EXPORT Standard_Boolean IsExecutable();
178 //! Enables to emulate unix "tail -f" command.
179 //! If a line is available in the file <me> returns it.
180 //! Otherwise attemps to read again aNbTries times in the file
181 //! waiting aDelay seconds between each read.
182 //! If meanwhile the file increases returns the next line, otherwise
184 Standard_EXPORT Standard_Boolean ReadLastLine (TCollection_AsciiString& aLine, const Standard_Integer aDelay, const Standard_Integer aNbTries);
186 //! find an editor on the system and edit the given file
187 Standard_EXPORT Standard_Boolean Edit();
189 //! Set file pointer position to the beginning of the file
190 Standard_EXPORT void Rewind();
192 //! Redirect a standard handle (fileno(stdout), fileno(stdin) or
193 //! fileno(stderr) to this OSD_File and return the copy of the original
197 //! aTmp.BuildTemporary();
198 //! int stdfd = _fileno(stdout);
200 //! int oldout = aTmp.Capture(stdfd);
201 //! cout << "Some output to the file" << endl;
205 //! _dup2(oldout, stdfd); // Restore standard output
207 Standard_EXPORT int Capture(int theDescr);
214 Standard_Integer myIO;
215 Standard_Address myFILE;
216 Standard_Integer myFileChannel;
217 Standard_Address myFileHandle;
224 Standard_Boolean ImperativeFlag;
237 #endif // _OSD_File_HeaderFile