1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SMESH_File.cxx
21 // Created : Wed Mar 10 11:23:25 2010
22 // Author : Edward AGAPOV (eap)
24 #include "SMESH_File.hxx"
25 #include "utilities.h"
27 #include <OSD_File.hxx>
28 #include <OSD_Path.hxx>
29 #include <Standard_ProgramError.hxx>
30 #include <Standard_ErrorHandler.hxx>
31 #include <Standard_Failure.hxx>
43 //================================================================================
45 * \brief Creator opening the file for reading by default
47 //================================================================================
49 SMESH_File::SMESH_File(const std::string& name, bool open)
50 :_name(name), _size(-1), _file(0), _map(0), _pos(0), _end(0)
52 if ( open ) this->open();
55 //================================================================================
57 * \brief Destructor closing the file
59 //================================================================================
61 SMESH_File::~SMESH_File()
66 //================================================================================
68 * \brief Open file for reading. Return true if there is something to read
70 //================================================================================
72 bool SMESH_File::open()
75 if ( !_map && length > 0 )
78 _file = CreateFile(_name.data(), GENERIC_READ, FILE_SHARE_READ,
79 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
80 bool ok = ( _file != INVALID_HANDLE_VALUE );
82 _file = ::open(_name.data(), O_RDONLY );
83 bool ok = ( _file > 0 );
88 _mapObj = CreateFileMapping(_file, NULL, PAGE_READONLY, 0, (DWORD)length, NULL);
89 _map = (void*) MapViewOfFile( _mapObj, FILE_MAP_READ, 0, 0, 0 );
91 _map = ::mmap(0,length,PROT_READ,MAP_PRIVATE,_file,0);
92 if ( _map == MAP_FAILED ) _map = NULL;
103 CloseHandle(_mapObj);
114 //================================================================================
116 * \brief Close the file
118 //================================================================================
120 void SMESH_File::close()
125 UnmapViewOfFile(_map);
126 CloseHandle(_mapObj);
129 ::munmap(_map, _size);
138 //================================================================================
140 * \brief Remove the file
142 //================================================================================
144 bool SMESH_File::remove()
148 OSD_Path filePath(TCollection_AsciiString((char*)_name.data()));
149 OSD_File(filePath).Remove();
151 catch ( Standard_ProgramError ) {
152 MESSAGE("Can't remove file: " << _name << " ; file does not exist or permission denied");
158 //================================================================================
160 * \brief Return file size
162 //================================================================================
164 int SMESH_File::size() const
166 if ( _size >= 0 ) return _size; // size of open file
169 int file = ::open( _name.data(), O_RDONLY );
173 int err = fstat( file, &status);
175 size = status.st_size;
181 //================================================================================
183 * \brief Set cursor to the given position
185 //================================================================================
187 void SMESH_File::setPos(const char* pos)
189 if ( pos > (const char*)_map && pos < _end )
193 //================================================================================
195 * \brief Skip till current line end and return the skipped string
197 //================================================================================
199 std::string SMESH_File::getLine()
202 const char* p = _pos;
204 if ( *(++_pos) == '\n' )
206 line.append( p, _pos );
207 if ( !eof() ) _pos++;
211 //================================================================================
213 * \brief Move cursor to the file beginning
215 //================================================================================
217 void SMESH_File::rewind()
222 //================================================================================
224 * \brief Fill vector by reading out integers from file. Vector size gives number
225 * of integers to read
227 //================================================================================
229 bool SMESH_File::getInts(std::vector<int>& ints)
232 while ( i < ints.size() )
234 while ( !isdigit( *_pos ) && !eof()) ++_pos;
236 if ( _pos[-1] == '-' ) --_pos;
237 ints[ i++ ] = strtol( _pos, (char**)&_pos, 10 );
239 return ( i == ints.size() );