1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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)
25 #include "SMESH_File.hxx"
37 #include <boost/filesystem.hpp>
39 namespace boofs = boost::filesystem;
41 //================================================================================
43 * \brief Creator opening the file for reading by default
45 //================================================================================
47 SMESH_File::SMESH_File(const std::string& name, bool open)
48 :_name(name), _size(-1), _file(0), _map(0), _pos(0), _end(0)
50 if ( open ) this->open();
53 //================================================================================
55 * \brief Destructor closing the file
57 //================================================================================
59 SMESH_File::~SMESH_File()
64 //================================================================================
66 * \brief Open file for reading. Return true if there is something to read
68 //================================================================================
70 bool SMESH_File::open()
73 if ( !_map && length > 0 )
76 _file = CreateFile(_name.data(), GENERIC_READ, FILE_SHARE_READ,
77 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
78 bool ok = ( _file != INVALID_HANDLE_VALUE );
80 _file = ::open(_name.data(), O_RDONLY );
81 bool ok = ( _file >= 0 );
86 _mapObj = CreateFileMapping(_file, NULL, PAGE_READONLY, 0, (DWORD)length, NULL);
87 _map = (void*) MapViewOfFile( _mapObj, FILE_MAP_READ, 0, 0, 0 );
89 _map = ::mmap(0,length,PROT_READ,MAP_PRIVATE,_file,0);
90 if ( _map == MAP_FAILED ) _map = NULL;
101 CloseHandle(_mapObj);
108 else if ( _error.empty() )
110 _error = "Can't open for reading an existing file " + _name;
116 //================================================================================
118 * \brief Close the file
120 //================================================================================
122 void SMESH_File::close()
127 UnmapViewOfFile(_map);
128 CloseHandle(_mapObj);
131 ::munmap(_map, _size);
138 else if ( _file >= 0 )
142 _file = INVALID_HANDLE_VALUE;
150 //================================================================================
152 * \brief Remove the file
154 //================================================================================
156 bool SMESH_File::remove()
160 boost::system::error_code err;
161 boofs::remove( _name, err );
162 _error = err.message();
167 //================================================================================
169 * \brief Return file size
171 //================================================================================
173 long SMESH_File::size()
175 if ( _size >= 0 ) return _size; // size of an open file
177 boost::system::error_code err;
178 boost::uintmax_t size = boofs::file_size( _name, err );
179 _error = err.message();
181 return err ? -1 : (long) size;
184 //================================================================================
186 * \brief Check existence
188 //================================================================================
190 bool SMESH_File::exists()
192 boost::system::error_code err;
193 bool res = boofs::exists( _name, err );
194 _error = err.message();
196 return err ? false : res;
199 //================================================================================
201 * \brief Check existence
203 //================================================================================
205 bool SMESH_File::isDirectory()
207 boost::system::error_code err;
208 bool res = boofs::is_directory( _name, err );
209 _error = err.message();
211 return err ? false : res;
214 //================================================================================
216 * \brief Set cursor to the given position
218 //================================================================================
220 void SMESH_File::setPos(const char* pos)
222 if ( pos > (const char*)_map && pos < _end )
226 //================================================================================
228 * \brief Skip till current line end and return the skipped string
230 //================================================================================
232 std::string SMESH_File::getLine()
235 const char* p = _pos;
237 if ( *(++_pos) == '\n' )
239 line.append( p, _pos );
240 if ( !eof() ) _pos++;
244 //================================================================================
246 * \brief Move cursor to the file beginning
248 //================================================================================
250 void SMESH_File::rewind()
255 //================================================================================
257 * \brief Fill vector by reading out integers from file. Vector size gives number
258 * of integers to read
260 //================================================================================
262 bool SMESH_File::getInts(std::vector<int>& ints)
265 while ( i < ints.size() )
267 while ( !isdigit( *_pos ) && !eof()) ++_pos;
269 if ( _pos[-1] == '-' ) --_pos;
270 ints[ i++ ] = strtol( _pos, (char**)&_pos, 10 );
272 return ( i == ints.size() );
275 //================================================================================
277 * \brief Open for binary writing only.
279 //================================================================================
281 bool SMESH_File::openForWriting()
285 _file = CreateFile( _name.c_str(), // name of the write
286 GENERIC_WRITE, // open for writing
288 NULL, // default security
289 OPEN_ALWAYS, // CREATE NEW or OPEN EXISTING
290 FILE_ATTRIBUTE_NORMAL, // normal file
291 NULL); // no attr. template
292 return ( _file != INVALID_HANDLE_VALUE );
296 _file = ::open( _name.c_str(),
298 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); // rw-r--r--
304 //================================================================================
306 * \brief Write binary data
308 //================================================================================
310 bool SMESH_File::writeRaw(const void* data, size_t size)
315 BOOL err = WriteFile( _file, data, size, & nbWritten, NULL);
317 return (( err == FALSE ) &&
318 ( nbWritten == (DWORD) size ));
322 ssize_t nbWritten = ::write( _file, data, size );
323 return ( nbWritten == size );