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 <Standard_ProgramError.hxx>
11 #include <Standard_ErrorHandler.hxx>
12 #include <Standard_Failure.hxx>
23 //================================================================================
25 * \brief Creator opening the file for reading by default
27 //================================================================================
29 SMESH_File::SMESH_File(const std::string& name, bool open)
30 :_name(name), _size(-1), _file(0), _map(0), _pos(0), _end(0)
32 if ( open ) this->open();
35 //================================================================================
37 * \brief Destructor closing the file
39 //================================================================================
41 SMESH_File::~SMESH_File()
46 //================================================================================
48 * \brief Open file for reading. Return true if there is something to read
50 //================================================================================
52 bool SMESH_File::open()
55 if ( !_map && length > 0 )
58 _file = CreateFile(_name.data(), GENERIC_READ, FILE_SHARE_READ,
59 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
60 bool ok = ( _file != INVALID_HANDLE_VALUE );
62 _file = ::open(_name.data(), O_RDONLY );
63 bool ok = ( _file > 0 );
68 _mapObj = CreateFileMapping(_file, NULL, PAGE_READONLY, 0, (DWORD)length, NULL);
69 _map = (void*) MapViewOfFile( _mapObj, FILE_MAP_READ, 0, 0, 0 );
71 _map = ::mmap(0,length,PROT_READ,MAP_PRIVATE,_file,0);
72 if ( _map == MAP_FAILED ) _map = NULL;
94 //================================================================================
96 * \brief Close the file
98 //================================================================================
100 void SMESH_File::close()
105 UnmapViewOfFile(_map);
106 CloseHandle(_mapObj);
109 ::munmap(_map, _size);
118 //================================================================================
120 * \brief Remove the file
122 //================================================================================
124 bool SMESH_File::remove()
128 OSD_File( TCollection_AsciiString((char*)_name.data()) ).Remove();
130 catch ( Standard_ProgramError ) {
131 MESSAGE("Can't remove file: " << _name << " ; file does not exist or permission denied");
137 //================================================================================
139 * \brief Return file size
141 //================================================================================
143 int SMESH_File::size() const
145 if ( _size >= 0 ) return _size; // size of open file
148 int file = ::open( _name.data(), O_RDONLY );
152 int err = fstat( file, &status);
154 size = status.st_size;
160 //================================================================================
162 * \brief Set cursor to the given position
164 //================================================================================
166 void SMESH_File::setPos(const char* pos)
168 if ( pos > (const char*)_map && pos < _end )
172 //================================================================================
174 * \brief Skip till current line end and return the skipped string
176 //================================================================================
178 std::string SMESH_File::getLine()
181 const char* p = _pos;
183 if ( *(++_pos) == '\n' )
185 line.append( p, _pos );
186 if ( !eof() ) _pos++;
190 //================================================================================
192 * \brief Move cursor to the file beginning
194 //================================================================================
196 void SMESH_File::rewind()
201 //================================================================================
203 * \brief Fill vector by reading out integers from file. Vector size gives number
204 * of integers to read
206 //================================================================================
208 bool SMESH_File::getInts(std::vector<int>& ints)
211 while ( i < ints.size() )
213 while ( !isdigit( *_pos ) && !eof()) ++_pos;
215 if ( _pos[-1] == '-' ) --_pos;
216 ints[ i++ ] = strtol( _pos, (char**)&_pos, 10 );
218 return ( i == ints.size() );