1 // Copyright (C) 2007-2016 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),
50 _file(INVALID_HANDLE_VALUE),
54 _map(0), _pos(0), _end(0)
56 if ( open ) this->open();
59 //================================================================================
61 * \brief Destructor closing the file
63 //================================================================================
65 SMESH_File::~SMESH_File()
70 //================================================================================
72 * \brief Open file for reading. Return true if there is something to read
74 //================================================================================
76 bool SMESH_File::open()
79 if ( !_map && length > 0 )
82 _file = CreateFile(_name.data(), GENERIC_READ, FILE_SHARE_READ,
83 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
84 bool ok = ( _file != INVALID_HANDLE_VALUE );
86 _file = ::open(_name.data(), O_RDONLY );
87 bool ok = ( _file >= 0 );
92 _mapObj = CreateFileMapping(_file, NULL, PAGE_READONLY, 0, (DWORD)length, NULL);
93 _map = (void*) MapViewOfFile( _mapObj, FILE_MAP_READ, 0, 0, 0 );
95 _map = ::mmap(0,length,PROT_READ,MAP_PRIVATE,_file,0);
96 if ( _map == MAP_FAILED ) _map = NULL;
107 CloseHandle(_mapObj);
114 else if ( _error.empty() )
116 _error = "Can't open for reading an existing file " + _name;
122 //================================================================================
124 * \brief Close the file
126 //================================================================================
128 void SMESH_File::close()
133 UnmapViewOfFile(_map);
134 CloseHandle(_mapObj);
137 ::munmap(_map, _size);
144 else if ( _file >= 0 )
147 if(_file != INVALID_HANDLE_VALUE) {
149 _file = INVALID_HANDLE_VALUE;
160 //================================================================================
162 * \brief Remove the file
164 //================================================================================
166 bool SMESH_File::remove()
170 boost::system::error_code err;
171 boofs::remove( _name, err );
172 _error = err.message();
177 //================================================================================
179 * \brief Return file size
181 //================================================================================
183 long SMESH_File::size()
185 if ( _size >= 0 ) return _size; // size of an open file
187 boost::system::error_code err;
188 boost::uintmax_t size = boofs::file_size( _name, err );
189 _error = err.message();
191 return !err ? (long) size : -1;
194 //================================================================================
196 * \brief Check existence
198 //================================================================================
200 bool SMESH_File::exists()
202 boost::system::error_code err;
203 bool res = boofs::exists( _name, err );
204 _error = err.message();
206 return err ? false : res;
209 //================================================================================
211 * \brief Check existence
213 //================================================================================
215 bool SMESH_File::isDirectory()
217 boost::system::error_code err;
218 bool res = boofs::is_directory( _name, err );
219 _error = err.message();
221 return err ? false : res;
224 //================================================================================
226 * \brief Set cursor to the given position
228 //================================================================================
230 void SMESH_File::setPos(const char* pos)
232 if ( pos > (const char*)_map && pos < _end )
236 //================================================================================
238 * \brief Skip till current line end and return the skipped string
240 //================================================================================
242 std::string SMESH_File::getLine()
245 const char* p = _pos;
247 if ( *(++_pos) == '\n' )
249 line.append( p, _pos );
250 if ( !eof() ) _pos++;
254 //================================================================================
256 * \brief Move cursor to the file beginning
258 //================================================================================
260 void SMESH_File::rewind()
265 //================================================================================
267 * \brief Fill vector by reading out integers from file. Vector size gives number
268 * of integers to read
270 //================================================================================
272 bool SMESH_File::getInts(std::vector<int>& ints)
275 while ( i < ints.size() )
277 while ( !isdigit( *_pos ) && !eof()) ++_pos;
279 if ( _pos[-1] == '-' ) --_pos;
280 ints[ i++ ] = strtol( _pos, (char**)&_pos, 10 );
282 return ( i == ints.size() );
285 //================================================================================
287 * \brief Open for binary writing only.
289 //================================================================================
291 bool SMESH_File::openForWriting()
295 _file = CreateFile( _name.c_str(), // name of the write
296 GENERIC_WRITE, // open for writing
298 NULL, // default security
299 OPEN_ALWAYS, // CREATE NEW or OPEN EXISTING
300 FILE_ATTRIBUTE_NORMAL, // normal file
301 NULL); // no attr. template
302 return ( _file != INVALID_HANDLE_VALUE );
306 _file = ::open( _name.c_str(),
308 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); // rw-r--r--
314 //================================================================================
316 * \brief Write binary data
318 //================================================================================
320 bool SMESH_File::writeRaw(const void* data, size_t size)
325 BOOL err = WriteFile( _file, data, size, & nbWritten, NULL);
327 return (( err == FALSE ) &&
328 ( nbWritten == (DWORD) size ));
332 ssize_t nbWritten = ::write( _file, data, size );
333 return ( nbWritten == (ssize_t) size );