Salome HOME
0022098: EDF 2036 SMESH: Create groups from none conected parts of a mesh
[modules/smesh.git] / src / SMESHUtils / SMESH_File.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File      : SMESH_File.cxx
21 // Created   : Wed Mar 10 11:23:25 2010
22 // Author    : Edward AGAPOV (eap)
23 //
24 #include "SMESH_File.hxx"
25 #include "utilities.h"
26
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>
32
33 #include <fcntl.h>
34 #include <sys/stat.h>
35
36 #ifdef WIN32
37 #include <io.h>
38 #else
39 #include <unistd.h>
40 #include <sys/mman.h>
41 #endif
42
43 //================================================================================
44 /*!
45  * \brief Creator opening the file for reading by default
46  */
47 //================================================================================
48
49 SMESH_File::SMESH_File(const std::string& name, bool open)
50   :_name(name), _size(-1), _file(0), _map(0), _pos(0), _end(0)
51 {
52   if ( open ) this->open();
53 }
54
55 //================================================================================
56 /*!
57  * \brief Destructor closing the file
58  */
59 //================================================================================
60
61 SMESH_File::~SMESH_File()
62 {
63   close();
64 }
65
66 //================================================================================
67 /*!
68  * \brief Open file for reading. Return true if there is something to read
69  */
70 //================================================================================
71
72 bool SMESH_File::open()
73 {
74   int length = size();
75   if ( !_map && length > 0 )
76   {
77 #ifdef WNT
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 );
81 #else
82     _file = ::open(_name.data(), O_RDONLY );
83     bool ok = ( _file > 0 );
84 #endif
85     if ( ok )
86     {
87 #ifdef WNT
88       _mapObj = CreateFileMapping(_file, NULL, PAGE_READONLY, 0, (DWORD)length, NULL);
89       _map = (void*) MapViewOfFile( _mapObj, FILE_MAP_READ, 0, 0, 0 );
90 #else
91       _map = ::mmap(0,length,PROT_READ,MAP_PRIVATE,_file,0);
92       if ( _map == MAP_FAILED ) _map = NULL;
93 #endif
94       if ( _map != NULL )
95       {
96         _size = length;
97         _pos = (char*) _map;
98         _end = _pos + _size;
99       }
100       else
101       {
102 #ifdef WNT
103         CloseHandle(_mapObj);
104         CloseHandle(_file);
105 #else
106         ::close(_file);
107 #endif
108       }
109     }
110   }
111   return _pos;
112 }
113
114 //================================================================================
115 /*!
116  * \brief Close the file
117  */
118 //================================================================================
119
120 void SMESH_File::close()
121 {
122   if ( _map != NULL )
123   {
124 #ifdef WNT
125     UnmapViewOfFile(_map);
126     CloseHandle(_mapObj);
127     CloseHandle(_file);
128 #else
129     ::munmap(_map, _size);
130     ::close(_file);
131 #endif
132     _map = NULL;
133     _pos = _end = 0;
134     _size = -1;
135   }
136 }
137
138 //================================================================================
139 /*!
140  * \brief Remove the file
141  */
142 //================================================================================
143
144 bool SMESH_File::remove()
145 {
146   close();
147   try {
148     OSD_Path filePath(TCollection_AsciiString((char*)_name.data()));
149     OSD_File(filePath).Remove();
150   }
151   catch ( Standard_ProgramError ) {
152     MESSAGE("Can't remove file: " << _name << " ; file does not exist or permission denied");
153     return false;
154   }
155   return true;
156 }
157
158 //================================================================================
159 /*!
160  * \brief Return file size
161  */
162 //================================================================================
163
164 int SMESH_File::size() const
165 {
166   if ( _size >= 0 ) return _size; // size of open file
167
168   int size = -1;
169   int file = ::open( _name.data(), O_RDONLY );
170   if ( file > 0 )
171   {
172     struct stat status;
173     int err = fstat( file, &status);
174     if ( !err )
175       size = status.st_size;
176     ::close( file );
177   }
178   return size;
179 }
180
181 //================================================================================
182 /*!
183  * \brief Set cursor to the given position
184  */
185 //================================================================================
186
187 void SMESH_File::setPos(const char* pos)
188 {
189   if ( pos > (const char*)_map && pos < _end )
190     _pos = (char*) pos;
191 }
192
193 //================================================================================
194 /*!
195  * \brief Skip till current line end and return the skipped string
196  */
197 //================================================================================
198
199 std::string SMESH_File::getLine()
200 {
201   std::string line;
202   const char* p = _pos;
203   while ( !eof() )
204     if ( *(++_pos) == '\n' )
205       break;
206   line.append( p, _pos );
207   if ( !eof() ) _pos++;
208   return line;
209 }
210
211 //================================================================================
212 /*!
213  * \brief Move cursor to the file beginning
214  */
215 //================================================================================
216
217 void SMESH_File::rewind()
218 {
219   _pos = (char*) _map;
220 }
221
222 //================================================================================
223 /*!
224  * \brief Fill vector by reading out integers from file. Vector size gives number
225  * of integers to read
226  */
227 //================================================================================
228
229 bool SMESH_File::getInts(std::vector<int>& ints)
230 {
231   int i = 0;
232   while ( i < ints.size() )
233   {
234     while ( !isdigit( *_pos ) && !eof()) ++_pos;
235     if ( eof() ) break;
236     if ( _pos[-1] == '-' ) --_pos;
237     ints[ i++ ] = strtol( _pos, (char**)&_pos, 10 );
238   }
239   return ( i == ints.size() );
240 }