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 #include <Basics_Utils.hxx>
41 namespace boofs = boost::filesystem;
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),
52 _file(INVALID_HANDLE_VALUE),
56 _map(0), _pos(0), _end(0)
58 if ( open ) this->open();
61 //================================================================================
63 * \brief Destructor closing the file
65 //================================================================================
67 SMESH_File::~SMESH_File()
72 //================================================================================
74 * \brief Open file for reading. Return true if there is something to read
76 //================================================================================
78 bool SMESH_File::open()
81 if ( !_map && length > 0 )
85 const wchar_t* name = Kernel_Utils::decode(_name.data());
87 char* name = name.data();
89 _file = CreateFile(name, GENERIC_READ, FILE_SHARE_READ,
90 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
91 bool ok = ( _file != INVALID_HANDLE_VALUE );
96 _file = ::open(_name.data(), O_RDONLY );
97 bool ok = ( _file >= 0 );
102 _mapObj = CreateFileMapping(_file, NULL, PAGE_READONLY, 0, (DWORD)length, NULL);
103 _map = (void*) MapViewOfFile( _mapObj, FILE_MAP_READ, 0, 0, 0 );
105 _map = ::mmap(0,length,PROT_READ,MAP_PRIVATE,_file,0);
106 if ( _map == MAP_FAILED ) _map = NULL;
117 CloseHandle(_mapObj);
124 else if ( _error.empty() )
126 _error = "Can't open for reading an existing file " + _name;
132 //================================================================================
134 * \brief Close the file
136 //================================================================================
138 void SMESH_File::close()
143 UnmapViewOfFile(_map);
144 CloseHandle(_mapObj);
147 ::munmap(_map, _size);
154 else if ( _file >= 0 )
157 if(_file != INVALID_HANDLE_VALUE) {
159 _file = INVALID_HANDLE_VALUE;
170 //================================================================================
172 * \brief Remove the file
174 //================================================================================
176 bool SMESH_File::remove()
180 boost::system::error_code err;
181 boofs::remove( _name, err );
182 _error = err.message();
187 //================================================================================
189 * \brief Return file size
191 //================================================================================
193 long SMESH_File::size()
195 if ( _size >= 0 ) return _size; // size of an open file
197 boost::system::error_code err;
198 boost::uintmax_t size = boofs::file_size( _name, err );
199 _error = err.message();
201 return !err ? (long) size : -1;
204 //================================================================================
206 * \brief Check existence
208 //================================================================================
210 bool SMESH_File::exists()
212 boost::system::error_code err;
213 bool res = boofs::exists( _name, err );
214 _error = err.message();
216 return err ? false : res;
219 //================================================================================
221 * \brief Check existence
223 //================================================================================
225 bool SMESH_File::isDirectory()
227 boost::system::error_code err;
228 bool res = boofs::is_directory( _name, err );
229 _error = err.message();
231 return err ? false : res;
234 //================================================================================
236 * \brief Set cursor to the given position
238 //================================================================================
240 void SMESH_File::setPos(const char* pos)
242 if ( pos > (const char*)_map && pos < _end )
246 //================================================================================
248 * \brief Skip till current line end and return the skipped string
250 //================================================================================
252 std::string SMESH_File::getLine()
255 const char* p = _pos;
257 if ( *(++_pos) == '\n' )
259 line.append( p, _pos );
260 if ( !eof() ) _pos++;
264 //================================================================================
266 * \brief Move cursor to the file beginning
268 //================================================================================
270 void SMESH_File::rewind()
275 //================================================================================
277 * \brief Fill vector by reading out integers from file. Vector size gives number
278 * of integers to read
280 //================================================================================
282 bool SMESH_File::getInts(std::vector<int>& ints)
285 while ( i < ints.size() )
287 while ( !isdigit( *_pos ) && !eof()) ++_pos;
289 if ( _pos[-1] == '-' ) --_pos;
290 ints[ i++ ] = strtol( _pos, (char**)&_pos, 10 );
292 return ( i == ints.size() );
295 //================================================================================
297 * \brief Open for binary writing only.
299 //================================================================================
301 bool SMESH_File::openForWriting()
305 const wchar_t* name = Kernel_Utils::decode(_name.data());
307 char* name = name.data();
309 _file = CreateFile( name, // name of the write
310 GENERIC_WRITE, // open for writing
312 NULL, // default security
313 OPEN_ALWAYS, // CREATE NEW or OPEN EXISTING
314 FILE_ATTRIBUTE_NORMAL, // normal file
315 NULL); // no attr. template
319 return ( _file != INVALID_HANDLE_VALUE );
323 _file = ::open( _name.c_str(),
325 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); // rw-r--r--
331 //================================================================================
333 * \brief Write binary data
335 //================================================================================
337 bool SMESH_File::writeRaw(const void* data, size_t size)
342 BOOL err = WriteFile( _file, data, size, & nbWritten, NULL);
344 return (( err == FALSE ) &&
345 ( nbWritten == (DWORD) size ));
349 ssize_t nbWritten = ::write( _file, data, size );
350 return ( nbWritten == (ssize_t) size );