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