1 // File : SMESH_File.cxx
2 // Created : Wed Mar 10 11:23:25 2010
3 // Author : Edward AGAPOV (eap)
6 #include "SMESH_File.hxx"
9 #include <OSD_File.hxx>
10 #include <OSD_Path.hxx>
11 #include <Standard_ProgramError.hxx>
12 #include <Standard_ErrorHandler.hxx>
13 #include <Standard_Failure.hxx>
24 //================================================================================
26 * \brief Creator opening the file for reading by default
28 //================================================================================
30 SMESH_File::SMESH_File(const std::string& name, bool open)
31 :_name(name), _size(-1), _file(0), _map(0), _pos(0), _end(0)
33 if ( open ) this->open();
36 //================================================================================
38 * \brief Destructor closing the file
40 //================================================================================
42 SMESH_File::~SMESH_File()
47 //================================================================================
49 * \brief Open file for reading. Return true if there is something to read
51 //================================================================================
53 bool SMESH_File::open()
56 if ( !_map && length > 0 )
59 _file = CreateFile(_name.data(), GENERIC_READ, FILE_SHARE_READ,
60 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
61 bool ok = ( _file != INVALID_HANDLE_VALUE );
63 _file = ::open(_name.data(), O_RDONLY );
64 bool ok = ( _file > 0 );
69 _mapObj = CreateFileMapping(_file, NULL, PAGE_READONLY, 0, (DWORD)length, NULL);
70 _map = (void*) MapViewOfFile( _mapObj, FILE_MAP_READ, 0, 0, 0 );
72 _map = ::mmap(0,length,PROT_READ,MAP_PRIVATE,_file,0);
73 if ( _map == MAP_FAILED ) _map = NULL;
95 //================================================================================
97 * \brief Close the file
99 //================================================================================
101 void SMESH_File::close()
106 UnmapViewOfFile(_map);
107 CloseHandle(_mapObj);
110 ::munmap(_map, _size);
119 //================================================================================
121 * \brief Remove the file
123 //================================================================================
125 bool SMESH_File::remove()
129 OSD_Path filePath(TCollection_AsciiString((char*)_name.data()));
130 OSD_File(filePath).Remove();
132 catch ( Standard_ProgramError ) {
133 MESSAGE("Can't remove file: " << _name << " ; file does not exist or permission denied");
139 //================================================================================
141 * \brief Return file size
143 //================================================================================
145 int SMESH_File::size() const
147 if ( _size >= 0 ) return _size; // size of open file
150 int file = ::open( _name.data(), O_RDONLY );
154 int err = fstat( file, &status);
156 size = status.st_size;
162 //================================================================================
164 * \brief Set cursor to the given position
166 //================================================================================
168 void SMESH_File::setPos(const char* pos)
170 if ( pos > (const char*)_map && pos < _end )
174 //================================================================================
176 * \brief Skip till current line end and return the skipped string
178 //================================================================================
180 std::string SMESH_File::getLine()
183 const char* p = _pos;
185 if ( *(++_pos) == '\n' )
187 line.append( p, _pos );
188 if ( !eof() ) _pos++;
192 //================================================================================
194 * \brief Move cursor to the file beginning
196 //================================================================================
198 void SMESH_File::rewind()
203 //================================================================================
205 * \brief Fill vector by reading out integers from file. Vector size gives number
206 * of integers to read
208 //================================================================================
210 bool SMESH_File::getInts(std::vector<int>& ints)
213 while ( i < ints.size() )
215 while ( !isdigit( *_pos ) && !eof()) ++_pos;
217 if ( _pos[-1] == '-' ) --_pos;
218 ints[ i++ ] = strtol( _pos, (char**)&_pos, 10 );
220 return ( i == ints.size() );