Commit d01bd98390bc474a6082bc2a9b95b97ba62d08fc

_Filebuf_base: replace three chars by unsigned and bit flags.

Replace three chars, used as flags by single unsigned and bit
flags.

In many platforms access to unsigned is better then to chars.

As about alignment, single unsigned at least the same as
three chars...
  
150150
151151size_t _Filebuf_base::_M_page_size = 4096;
152152
153_Filebuf_base::_Filebuf_base()
154 : _M_file_id(INVALID_STLP_FD),
153_Filebuf_base::_Filebuf_base() :
154 _M_file_id(INVALID_STLP_FD),
155155 _M_openmode(0),
156 _M_is_open(false),
157 _M_should_close(false)
156 int_flags_(0)
158157{}
159158
160159void _Filebuf_base::_S_initialize()
173173{
174174 _STLP_fd file_no;
175175
176 if (_M_is_open)
176 if ( __is_open() ) {
177177 return false;
178 }
178179
179180 // use FILE-based i/o
180181 const char* flags;
240240 // unset buffering immediately
241241 setbuf(_M_file, 0);
242242
243 _M_is_open = true;
243 int_flags_ |= _is_open;
244244
245245 if (openmode & ios_base::ate) {
246 if (FSEEK(_M_file, 0, SEEK_END) != 0)
247 _M_is_open = false;
246 if (FSEEK(_M_file, 0, SEEK_END) != 0) {
247 int_flags_ &= ~_is_open;
248 }
248249 }
249250
250251 _M_file_id = file_no;
251 _M_should_close = _M_is_open;
252 int_flags_ |= (int_flags_ & _is_open) << 1; // _should_close == _is_open
252253 _M_openmode = openmode;
253254
254 if (_M_is_open)
255 _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
255 if ( (int_flags_ & _is_open) && _STLP_PRIV __is_regular_file(_M_file_id) ) {
256 int_flags_ |= _regular;
257 }
256258
257 return (_M_is_open != 0);
259 return (int_flags_ & _is_open) != 0;
258260}
259261
260262
274274// was opened.
275275bool _Filebuf_base::_M_open( int file_no, ios_base::openmode )
276276{
277 if (_M_is_open || file_no < 0)
277 if ( (int_flags_ & _is_open) || (file_no < 0) ) {
278278 return false;
279 }
279280
280281 struct STAT buf;
281282 if (FSTAT(file_no, &buf) != 0)
297297 return false;
298298 }
299299 _M_file_id = file_no;
300 _M_is_open = true;
301 _M_should_close = false;
302 _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
300 int_flags_ |= _is_open;
301 if ( _STLP_PRIV __is_regular_file(_M_file_id) ) {
302 int_flags_ |= _regular;
303 }
304
303305 return true;
304306}
305307
306308bool _Filebuf_base::_M_close()
307309{
308 if (!_M_is_open)
310 if ( (int_flags_ & _is_open) == 0 ) {
309311 return false;
312 }
310313
311 bool ok = _M_should_close ? (fclose(_M_file) == 0) : true;
314 bool ok = (int_flags_ & _should_close) != 0 ? (fclose(_M_file) == 0) : true;
312315
313 _M_is_open = _M_should_close = false;
316 int_flags_ = 0;
314317 _M_openmode = 0;
318
315319 return ok;
316320}
317321
  
121121
122122size_t _Filebuf_base::_M_page_size = 4096;
123123
124_Filebuf_base::_Filebuf_base()
125 : _M_file_id(INVALID_STLP_FD),
124_Filebuf_base::_Filebuf_base() :
125 _M_file_id(INVALID_STLP_FD),
126126 _M_openmode(0),
127 _M_is_open(false),
128 _M_should_close(false)
127 int_flags_(0)
129128{}
130129
131130void _Filebuf_base::_S_initialize()
156156{
157157 _STLP_fd file_no;
158158
159 if (_M_is_open)
159 if ( __is_open() ) {
160160 return false;
161 }
161162
162163 int flags = 0;
163164
192192
193193 file_no = OPEN(name, flags, permission);
194194
195 if (file_no < 0)
195 if ( file_no < 0 ) {
196196 return false;
197 }
197198
198 _M_is_open = true;
199 int_flags_ |= _is_open;
199200
200201 if ((openmode & (ios_base::ate | ios_base::app)) && (LSEEK(file_no, 0, SEEK_END) == -1)) {
201 _M_is_open = false;
202 int_flags_ &= ~_is_open;
202203 }
203204
204205 _M_file_id = file_no;
205 _M_should_close = _M_is_open;
206 int_flags_ |= (int_flags_ & _is_open) << 1; // _should_close == _is_open
206207 _M_openmode = openmode;
207208
208 if (_M_is_open)
209 _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
209 if ( (int_flags_ & _is_open) && _STLP_PRIV __is_regular_file(_M_file_id) ) {
210 int_flags_ |= _regular;
211 }
210212
211 return (_M_is_open != 0);
213 return (int_flags_ & _is_open) != 0;
212214}
213215
214216
228228// was opened.
229229bool _Filebuf_base::_M_open(int file_no, ios_base::openmode)
230230{
231 if (_M_is_open || file_no < 0)
231 if ( (int_flags_ & _is_open) || (file_no < 0) ) {
232232 return false;
233 }
233234
234235 int mode = fcntl(file_no, F_GETFL);
235236
236 if (mode == -1)
237 if ( mode == -1 ) {
237238 return false;
239 }
238240
239241 _M_openmode = flag_to_openmode(mode);
240242 _M_file_id = file_no;
241243
242 _M_is_open = true;
243 _M_should_close = false;
244 _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
244 int_flags_ |= _is_open;
245 if ( _STLP_PRIV __is_regular_file(_M_file_id) ) {
246 int_flags_ |= _regular;
247 }
248
245249 return true;
246250}
247251
248252bool _Filebuf_base::_M_close()
249253{
250 if (!_M_is_open)
254 if ( (int_flags_ & _is_open) == 0 ) {
251255 return false;
256 }
252257
253 bool ok = _M_should_close ? (close(_M_file_id) == 0) : true;
258 bool ok = (int_flags_ & _should_close) != 0 ? (close(_M_file_id) == 0) : true;
254259
255 _M_is_open = _M_should_close = false;
260 int_flags_ = 0;
256261 _M_openmode = 0;
262
257263 return ok;
258264}
259265
  
194194
195195size_t _Filebuf_base::_M_page_size = 4096;
196196
197_Filebuf_base::_Filebuf_base()
198 : _M_file_id(INVALID_STLP_FD),
197_Filebuf_base::_Filebuf_base() :
198 _M_file_id(INVALID_STLP_FD),
199199 _M_openmode(0),
200 _M_is_open(false),
201 _M_should_close(false),
200 int_flags_(0),
202201 _M_view_id(0)
203202{}
204203
218218 long permission) {
219219 _STLP_fd file_no;
220220
221 if (_M_is_open)
221 if ( __is_open() ) {
222222 return false;
223 }
223224
224225 DWORD dwDesiredAccess, dwCreationDisposition;
225226 bool doTruncate = false;
280280 return false;
281281 }
282282
283 _M_is_open = true;
283 int_flags_ |= _is_open | _should_close;
284284 _M_file_id = file_no;
285 _M_should_close = _M_is_open;
286285 _M_openmode = openmode;
287286
288 if (_M_is_open)
289 _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
287 if ( _STLP_PRIV __is_regular_file(_M_file_id) ) {
288 int_flags_ |= _regular;
289 }
290290
291 return (_M_is_open != 0);
291 return true;
292292}
293293
294294bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode) {
298298 return this->_M_open(name, openmode, FILE_ATTRIBUTE_NORMAL);
299299}
300300
301bool _Filebuf_base::_M_open(_STLP_fd __id, ios_base::openmode init_mode) {
301bool _Filebuf_base::_M_open(_STLP_fd __id, ios_base::openmode init_mode)
302{
302303#if (defined (_STLP_MSVC_LIB) && !defined (_STLP_WCE)) || \
303304 (defined (__MINGW32__) && defined (__MSVCRT__)) || defined (__DMC__)
304305
305 if (_M_is_open || __id == INVALID_STLP_FD)
306 if ( (int_flags_ & _is_open) || (__id == INVALID_STLP_FD) ) {
306307 return false;
308 }
307309
308 if (init_mode != ios_base::__default_mode)
310 if (init_mode != ios_base::__default_mode) {
309311 _M_openmode = init_mode;
310 else
312 } else {
311313 _M_openmode = _get_osfflags(-1, __id);
314 }
312315
313 _M_is_open = true;
316 int_flags_ |= _is_open;
314317 _M_file_id = __id;
315 _M_should_close = false;
316 _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
318 if ( _STLP_PRIV __is_regular_file(_M_file_id) ) {
319 int_flags_ |= _regular;
320 }
317321
318322 return true;
319323#else
333333// Associated the filebuf with a file descriptor pointing to an already-
334334// open file. Mode is set to be consistent with the way that the file
335335// was opened.
336bool _Filebuf_base::_M_open(int file_no, ios_base::openmode init_mode) {
337 if (_M_is_open || file_no < 0)
336bool _Filebuf_base::_M_open(int file_no, ios_base::openmode init_mode)
337{
338 if ( (int_flags_ & _is_open) || (file_no < 0) ) {
338339 return false;
340 }
339341
340342#if (defined (_STLP_MSVC_LIB) && !defined (_STLP_WCE)) || \
341343 (defined (__MINGW32__) && defined (__MSVCRT__)) || defined (__DMC__)
352352 _M_openmode = _get_osfflags(file_no, oshandle);
353353
354354 _M_file_id = oshandle;
355 _M_is_open = true;
356 _M_should_close = false;
357 _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
355 int_flags_ |= _is_open;
356 if ( _STLP_PRIV __is_regular_file(_M_file_id) ) {
357 int_flags_ |= _regular;
358 }
359
358360 return true;
359361#else
360362 _STLP_MARK_PARAMETER_AS_UNUSED(&init_mode)
365365#endif
366366}
367367
368bool _Filebuf_base::_M_close() {
369 if (!_M_is_open)
368bool _Filebuf_base::_M_close()
369{
370 if ( (int_flags_ & _is_open) == 0 ) {
370371 return false;
371
372 bool ok;
373
374 if (!_M_should_close)
375 ok = true;
376 else {
377 if (_M_file_id != INVALID_STLP_FD) {
378 ok = (CloseHandle(_M_file_id) != 0);
379 }
380 else {
381 ok = false;
382 }
383372 }
384373
385 _M_is_open = _M_should_close = false;
374 bool ok = (int_flags_ & _should_close) == 0 ? true : (_M_file_id == INVALID_STLP_FD ? false : (CloseHandle(_M_file_id) != 0));
375
376 int_flags_ = 0;
386377 _M_openmode = 0;
378
387379 return ok;
388380}
389381
  
5252// Class _Filebuf_base, a private base class to factor out the system-
5353// dependent code from basic_filebuf<>.
5454
55class _STLP_CLASS_DECLSPEC _Filebuf_base {
55class _STLP_CLASS_DECLSPEC _Filebuf_base
56{
5657public: // Opening and closing files.
5758 _Filebuf_base();
5859
110110protected: // Static data members.
111111 static size_t _M_page_size;
112112
113protected: // Data members.
114 _STLP_fd _M_file_id;
113 protected:
114 _STLP_fd _M_file_id;
115115#if defined (_STLP_USE_STDIO_IO)
116 // for stdio, the whole FILE* is being kept here
117 FILE* _M_file;
116 // for stdio, the whole FILE* is being kept here
117 FILE* _M_file;
118118#endif
119 ios_base::openmode _M_openmode ;
120 unsigned char _M_is_open ;
121 unsigned char _M_should_close ;
122 unsigned char _M_regular_file ;
119 ios_base::openmode _M_openmode;
123120
121 enum {
122 _is_open = 0x1,
123 _should_close = 0x2,
124 _regular = 0x4
125 };
126
127 unsigned int_flags_;
128
124129#if defined (_STLP_USE_WIN32_IO)
125 _STLP_fd _M_view_id;
130 _STLP_fd _M_view_id;
126131#endif
127132
128public :
129 static size_t _STLP_CALL __page_size() { return _M_page_size; }
130 int __o_mode() const { return (int)_M_openmode; }
131 bool __is_open() const { return (_M_is_open !=0 ); }
132 bool __should_close() const { return (_M_should_close != 0); }
133 bool __regular_file() const { return (_M_regular_file != 0); }
134 _STLP_fd __get_fd() const { return _M_file_id; }
133 public:
134 static size_t _STLP_CALL __page_size()
135 { return _M_page_size; }
136 int __o_mode() const
137 { return (int)_M_openmode; }
138 bool __is_open() const
139 { return (int_flags_ & _is_open) != 0; }
140 bool __should_close() const
141 { return (int_flags_ & _should_close) != 0; }
142 bool __regular_file() const
143 { return (int_flags_ & _regular) != 0; }
144 _STLP_fd __get_fd() const
145 { return _M_file_id; }
135146};
136147
137148//----------------------------------------------------------------------