1 // Copyright (C) 2007-2010 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>
42 //================================================================================
44 * \brief Creator opening the file for reading by default
46 //================================================================================
48 SMESH_File::SMESH_File(const std::string& name, bool open)
49 :_name(name), _size(-1), _file(0), _map(0), _pos(0), _end(0)
51 if ( open ) this->open();
54 //================================================================================
56 * \brief Destructor closing the file
58 //================================================================================
60 SMESH_File::~SMESH_File()
65 //================================================================================
67 * \brief Open file for reading. Return true if there is something to read
69 //================================================================================
71 bool SMESH_File::open()
74 if ( !_map && length > 0 )
77 _file = CreateFile(_name.data(), GENERIC_READ, FILE_SHARE_READ,
78 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
79 bool ok = ( _file != INVALID_HANDLE_VALUE );
81 _file = ::open(_name.data(), O_RDONLY );
82 bool ok = ( _file > 0 );
87 _mapObj = CreateFileMapping(_file, NULL, PAGE_READONLY, 0, (DWORD)length, NULL);
88 _map = (void*) MapViewOfFile( _mapObj, FILE_MAP_READ, 0, 0, 0 );
90 _map = ::mmap(0,length,PROT_READ,MAP_PRIVATE,_file,0);
91 if ( _map == MAP_FAILED ) _map = NULL;
102 CloseHandle(_mapObj);
113 //================================================================================
115 * \brief Close the file
117 //================================================================================
119 void SMESH_File::close()
124 UnmapViewOfFile(_map);
125 CloseHandle(_mapObj);
128 ::munmap(_map, _size);
137 //================================================================================
139 * \brief Remove the file
141 //================================================================================
143 bool SMESH_File::remove()
147 OSD_Path filePath(TCollection_AsciiString((char*)_name.data()));
148 OSD_File(filePath).Remove();
150 catch ( Standard_ProgramError ) {
151 MESSAGE("Can't remove file: " << _name << " ; file does not exist or permission denied");
157 //================================================================================
159 * \brief Return file size
161 //================================================================================
163 int SMESH_File::size() const
165 if ( _size >= 0 ) return _size; // size of open file
168 int file = ::open( _name.data(), O_RDONLY );
172 int err = fstat( file, &status);
174 size = status.st_size;
180 //================================================================================
182 * \brief Set cursor to the given position
184 //================================================================================
186 void SMESH_File::setPos(const char* pos)
188 if ( pos > (const char*)_map && pos < _end )
192 //================================================================================
194 * \brief Skip till current line end and return the skipped string
196 //================================================================================
198 std::string SMESH_File::getLine()
201 const char* p = _pos;
203 if ( *(++_pos) == '\n' )
205 line.append( p, _pos );
206 if ( !eof() ) _pos++;
210 //================================================================================
212 * \brief Move cursor to the file beginning
214 //================================================================================
216 void SMESH_File::rewind()
221 //================================================================================
223 * \brief Fill vector by reading out integers from file. Vector size gives number
224 * of integers to read
226 //================================================================================
228 bool SMESH_File::getInts(std::vector<int>& ints)
231 while ( i < ints.size() )
233 while ( !isdigit( *_pos ) && !eof()) ++_pos;
235 if ( _pos[-1] == '-' ) --_pos;
236 ints[ i++ ] = strtol( _pos, (char**)&_pos, 10 );
238 return ( i == ints.size() );