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 <OSD_FileNode.hxx> 21 #include <OSD_FromWhere.hxx> 22 #include <OSD_KindFile.hxx> 23 #include <OSD_LockType.hxx> 24 #include <OSD_OpenMode.hxx> 25 26 class Standard_ProgramError; 27 class OSD_Path; 28 class OSD_Protection; 29 30 //! Basic tools to manage files 31 //! Warning: 'ProgramError' is raised when somebody wants to use the methods 32 //! Read, Write, Seek, Close when File is not open. 33 class OSD_File : public OSD_FileNode 34 { 35 public: 36 37 //! Creates File object. 38 Standard_EXPORT OSD_File(); 39 40 //! Instantiates the object file, storing its name 41 Standard_EXPORT OSD_File(const OSD_Path& Name); 42 43 //! Unlocks and closes a file, deletes a descriptor and destructs a file object. 44 Standard_EXPORT ~OSD_File(); 45 46 //! CREATES a file if it doesn't already exists or empties 47 //! an existing file. 48 //! After 'Build', the file is open. 49 //! If no name was given, ProgramError is raised. 50 Standard_EXPORT void Build (const OSD_OpenMode Mode, const OSD_Protection& Protect); 51 52 //! Opens a File with specific attributes 53 //! This works only on already existing file. 54 //! If no name was given, ProgramError is raised. 55 Standard_EXPORT void Open (const OSD_OpenMode Mode, const OSD_Protection& Protect); 56 57 //! Appends data to an existing file. 58 //! If file doesn't exist, creates it first. 59 //! After 'Append', the file is open. 60 //! If no name was given, ProgramError is raised. 61 Standard_EXPORT void Append (const OSD_OpenMode Mode, const OSD_Protection& Protect); 62 63 //! Attempts to read Nbyte bytes from the file associated with 64 //! the object file. 65 //! Upon successful completion, Read returns the number of 66 //! bytes actually read and placed in the Buffer. This number 67 //! may be less than Nbyte if the number of bytes left in the file 68 //! is less than Nbyte bytes. In this case only number of read 69 //! bytes will be placed in the buffer. 70 Standard_EXPORT void Read (TCollection_AsciiString& Buffer, const Standard_Integer Nbyte); 71 72 //! Reads bytes from the data pointed to by the object file 73 //! into the buffer <Buffer>. 74 //! Data is read until <NByte-1> bytes have been read, 75 //! until a newline character is read and transferred into 76 //! <Buffer>, or until an EOF (End-of-File) condition is 77 //! encountered. 78 //! Upon successful completion, Read returns the number of 79 //! bytes actually read into <NByteRead> and placed into the 80 //! Buffer <Buffer>. 81 Standard_EXPORT void ReadLine (TCollection_AsciiString& Buffer, const Standard_Integer NByte, Standard_Integer& NbyteRead); 82 83 //! Reads bytes from the data pointed to by the object file 84 //! into the buffer <Buffer>. 85 //! Data is read until <NByte-1> bytes have been read, 86 //! until a newline character is read and transferred into 87 //! <Buffer>, or until an EOF (End-of-File) condition is 88 //! encountered. 89 //! Upon successful completion, Read returns the number of 90 //! bytes actually read and placed into the Buffer <Buffer>. ReadLine(TCollection_AsciiString & Buffer,const Standard_Integer NByte)91 inline Standard_Integer ReadLine ( 92 TCollection_AsciiString& Buffer, const Standard_Integer NByte) 93 { 94 Standard_Integer NbyteRead; 95 ReadLine(Buffer, NByte, NbyteRead); 96 return NbyteRead; 97 } 98 99 100 //! Attempts to read Nbyte bytes from the files associated with 101 //! the object File. 102 //! Upon successful completion, Read returns the number of 103 //! bytes actually read and placed in the Buffer. This number 104 //! may be less than Nbyte if the number of bytes left in the file 105 //! is less than Nbyte bytes. For this reason the output 106 //! parameter Readbyte will contain the number of read bytes. 107 Standard_EXPORT void Read (const Standard_Address Buffer, const Standard_Integer Nbyte, Standard_Integer& Readbyte); 108 109 //! Attempts to write theNbBytes bytes from the AsciiString to the file. Write(const TCollection_AsciiString & theBuffer,const Standard_Integer theNbBytes)110 void Write (const TCollection_AsciiString& theBuffer, const Standard_Integer theNbBytes) 111 { 112 Write ((Standard_Address )theBuffer.ToCString(), theNbBytes); 113 } 114 115 //! Attempts to write theNbBytes bytes from the buffer pointed 116 //! to by theBuffer to the file associated to the object File. 117 Standard_EXPORT void Write (const Standard_Address theBuffer, const Standard_Integer theNbBytes); 118 119 //! Sets the seek pointer associated with the open file 120 Standard_EXPORT void Seek (const Standard_Integer Offset, const OSD_FromWhere Whence); 121 122 //! Closes the file (and deletes a descriptor) 123 Standard_EXPORT void Close(); 124 125 //! Returns TRUE if the seek pointer is at end of file. 126 Standard_EXPORT Standard_Boolean IsAtEnd(); 127 128 //! Returns the kind of file. A file can be a 129 //! file, a directory or a link. 130 Standard_EXPORT OSD_KindFile KindOfFile() const; 131 132 //! Makes a temporary File 133 //! This temporary file is already open ! 134 Standard_EXPORT void BuildTemporary(); 135 136 //! Locks current file 137 Standard_EXPORT void SetLock (const OSD_LockType Lock); 138 139 //! Unlocks current file 140 Standard_EXPORT void UnLock(); 141 142 //! Returns the current lock state GetLock() const143 OSD_LockType GetLock() const { return myLock; } 144 145 //! Returns TRUE if this file is locked. IsLocked() const146 Standard_Boolean IsLocked() const 147 { 148 #ifdef _WIN32 149 return ImperativeFlag; 150 #else 151 return myLock != OSD_NoLock; 152 #endif 153 } 154 155 //! Returns actual number of bytes of <me>. 156 Standard_EXPORT Standard_Size Size(); 157 158 //! Returns TRUE if <me> is open. 159 Standard_EXPORT Standard_Boolean IsOpen() const; 160 161 //! returns TRUE if the file exists and if the user 162 //! has the autorization to read it. 163 Standard_EXPORT Standard_Boolean IsReadable(); 164 165 //! returns TRUE if the file can be read and overwritten. 166 Standard_EXPORT Standard_Boolean IsWriteable(); 167 168 //! returns TRUE if the file can be executed. 169 Standard_EXPORT Standard_Boolean IsExecutable(); 170 171 //! Enables to emulate unix "tail -f" command. 172 //! If a line is available in the file <me> returns it. 173 //! Otherwise attemps to read again aNbTries times in the file 174 //! waiting aDelay seconds between each read. 175 //! If meanwhile the file increases returns the next line, otherwise 176 //! returns FALSE. 177 Standard_EXPORT Standard_Boolean ReadLastLine (TCollection_AsciiString& aLine, const Standard_Integer aDelay, const Standard_Integer aNbTries); 178 179 //! find an editor on the system and edit the given file 180 Standard_EXPORT Standard_Boolean Edit(); 181 182 //! Set file pointer position to the beginning of the file 183 Standard_EXPORT void Rewind(); 184 185 protected: 186 187 #ifdef _WIN32 188 Standard_Address myFileHandle; 189 #else 190 Standard_Integer myFileChannel; 191 Standard_Address myFILE; 192 #endif 193 Standard_Integer myIO; 194 195 private: 196 197 OSD_LockType myLock; 198 OSD_OpenMode myMode; 199 Standard_Boolean ImperativeFlag; 200 201 }; 202 203 #endif // _OSD_File_HeaderFile 204