ReactOS  0.4.13-dev-235-g7373cb3
fstream_stdio.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1999
3  * Silicon Graphics Computer Systems, Inc.
4  *
5  * Copyright (c) 1999
6  * Boris Fomitchev
7  *
8  * This material is provided "as is", with absolutely no warranty expressed
9  * or implied. Any use is at your own risk.
10  *
11  * Permission to use or copy this software for any purpose is hereby granted
12  * without fee, provided the above notices are retained on all copies.
13  * Permission to modify the code and to distribute modified code is granted,
14  * provided the above notices are retained, and a notice that the code was
15  * modified is included with the above copyright notice.
16  *
17  */
18 
19 #if defined (__SUNPPRO_CC) && !defined (_STLP_NO_NEW_C_HEADERS)
20 # include <time.h>
21 // For sunpro, it chokes if time.h is included through stat.h
22 #endif
23 
24 #include <fstream>
25 
26 #ifdef __CYGWIN__
27 # define __int64 long long
28 #endif
29 
30 #include <cstdio>
31 #if !defined(__ISCPP__)
32 extern "C" {
33 # include <sys/stat.h>
34 }
35 #endif
36 
37 #if defined( __MSL__ )
38 # include <unix.h>
39 #endif
40 
41 #if defined(__ISCPP__)
42 # include <c_locale_is/filestat.h>
43 #endif
44 
45 #if defined(__BEOS__) && defined(__INTEL__)
46 # include <fcntl.h>
47 # include <sys/stat.h> // For _fstat
48 #endif
49 
50 #if defined (_STLP_MSVC) || defined (__MINGW32__)
51 # include <fcntl.h>
52 # define S_IREAD _S_IREAD
53 # define S_IWRITE _S_IWRITE
54 # define S_IFREG _S_IFREG
55  // map permission masks
56 # ifndef S_IRUSR
57 # define S_IRUSR _S_IREAD
58 # define S_IWUSR _S_IWRITE
59 # endif
60 # ifndef S_IRGRP
61 # define S_IRGRP _S_IREAD
62 # define S_IWGRP _S_IWRITE
63 # endif
64 # ifndef S_IROTH
65 # define S_IROTH _S_IREAD
66 # define S_IWOTH _S_IWRITE
67 # endif
68 
69 # ifndef O_RDONLY
70 # define O_RDONLY _O_RDONLY
71 # define O_WRONLY _O_WRONLY
72 # define O_RDWR _O_RDWR
73 # define O_APPEND _O_APPEND
74 # define O_CREAT _O_CREAT
75 # define O_TRUNC _O_TRUNC
76 # define O_TEXT _O_TEXT
77 # define O_BINARY _O_BINARY
78 # endif
79 
80 # ifndef O_ACCMODE
81 # define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
82 # endif
83 #endif
84 
85 const _STLP_fd INVALID_STLP_FD = -1;
86 
87 
88 # ifdef __MSL__
89 # define _O_TEXT 0x0
90 # if !defined( O_TEXT )
91 # define O_TEXT _O_TEXT
92 # endif
93 # define _S_IFREG S_IFREG
94 # define S_IREAD S_IRUSR
95 # define S_IWRITE S_IWUSR
96 # define S_IEXEC S_IXUSR
97 # define _S_IWRITE S_IWRITE
98 # define _S_IREAD S_IREAD
99 # define _open open
100 # define _close close
101 # define _read read
102 # define _write write
103 # endif
104 
106 
107 // Compare with streamoff definition in stl/char_traits.h!
108 
109 #if defined (_STLP_USE_DEFAULT_FILE_OFFSET) || \
110  (!defined(_LARGEFILE_SOURCE) && !defined(_LARGEFILE64_SOURCE))
111 # define FOPEN fopen
112 # define FSEEK fseek
113 # define FSTAT fstat
114 # define STAT stat
115 # define FTELL ftell
116 #else
117 # define FOPEN fopen64
118 # define FSEEK fseeko64
119 # define FSTAT fstat64
120 # define STAT stat64
121 # define FTELL ftello64
122 #endif
123 
125 
126 // Helper functions for _Filebuf_base.
127 
128 static bool __is_regular_file(_STLP_fd fd) {
129  struct STAT buf;
130  return FSTAT(fd, &buf) == 0 && (buf.st_mode & S_IFREG) != 0 ;
131 }
132 
133 // Number of characters in the file.
134 static streamoff __file_size(_STLP_fd fd) {
135  streamoff ret = 0;
136 
137  struct STAT buf;
138  if (FSTAT(fd, &buf) == 0 && (buf.st_mode & S_IFREG) != 0)
139  ret = buf.st_size > 0 ? buf.st_size : 0;
140 
141  return ret;
142 }
143 
145 
146 // All version of Unix have mmap and lseek system calls. Some also have
147 // longer versions of those system calls to accommodate 64-bit offsets.
148 // If we're on a Unix system, define some macros to encapsulate those
149 // differences.
150 
151 size_t _Filebuf_base::_M_page_size = 4096;
152 
154  : _M_file_id(INVALID_STLP_FD),
155  _M_openmode(0),
156  _M_is_open(false),
157  _M_should_close(false)
158 {}
159 
161 {
162 
163 }
164 
165 // Return the size of the file. This is a wrapper for stat.
166 // Returns zero if the size cannot be determined or is ill-defined.
168 {
170 }
171 
172 bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode,
173  long permission)
174 {
175  _STLP_fd file_no;
176 
177  if (_M_is_open)
178  return false;
179 
180  // use FILE-based i/o
181  const char* flags;
182 
183  switch (openmode & (~ios_base::ate)) {
184  case ios_base::out:
186  flags = "w";
187  break;
188 
191  flags = "wb";
192  break;
193 
194  case ios_base::out | ios_base::app:
195  flags = "a";
196  break;
197 
198  case ios_base::out | ios_base::app | ios_base::binary:
199  flags = "ab";
200  break;
201 
202  case ios_base::in:
203  flags = "r";
204  break;
205 
207  flags = "rb";
208  break;
209 
211  flags = "r+";
212  break;
213 
215  flags = "r+b";
216  break;
217 
219  flags = "w+";
220  break;
221 
223  flags = "w+b";
224  break;
225 
226  default: // The above are the only combinations of
227  return false; // flags allowed by the C++ standard.
228  }
229 
230  // fbp : TODO : set permissions !
231  (void)permission; // currently unused //*TY 02/26/2000 - added to suppress warning message
232  _M_file = FOPEN(name, flags);
233 
234  if (_M_file) {
235  file_no = fileno(_M_file);
236  } else {
237  return false;
238  }
239 
240  // unset buffering immediately
241  setbuf(_M_file, 0);
242 
243  _M_is_open = true;
244 
245  if (openmode & ios_base::ate) {
246  if (FSEEK(_M_file, 0, SEEK_END) != 0)
247  _M_is_open = false;
248  }
249 
250  _M_file_id = file_no;
252  _M_openmode = openmode;
253 
254  if (_M_is_open)
256 
257  return (_M_is_open != 0);
258 }
259 
260 
261 bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode)
262 {
263  // This doesn't really grant everyone in the world read/write
264  // access. On Unix, file-creation system calls always clear
265  // bits that are set in the umask from the permissions flag.
266  return this->_M_open(name, openmode, S_IRUSR | S_IWUSR | S_IRGRP |
267  S_IWGRP | S_IROTH | S_IWOTH);
268 }
269 
270 // Associated the filebuf with a file descriptor pointing to an already-
271 // open file. Mode is set to be consistent with the way that the file
272 // was opened.
274 {
275  if (_M_is_open || file_no < 0)
276  return false;
277 
278  struct STAT buf;
279  if (FSTAT(file_no, &buf) != 0)
280  return false;
281  int mode = buf.st_mode;
282 
283  switch ( mode & (S_IWRITE | S_IREAD) ) {
284  case S_IREAD:
286  break;
287  case S_IWRITE:
289  break;
290  case (S_IWRITE | S_IREAD):
292  break;
293  default:
294  return false;
295  }
296  _M_file_id = file_no;
297  _M_is_open = true;
298  _M_should_close = false;
300  return true;
301 }
302 
304 {
305  _STLP_fd file_no;
306 
307  if (_M_is_open)
308  return false;
309 
310  _M_file = file;
311 
312  if (_M_file) {
313  file_no = fileno(_M_file);
314  } else {
315  return false;
316  }
317 
318  // unset buffering immediately
319  setbuf(_M_file, 0);
320 
321  _M_is_open = true;
322 
323  if (openmode & ios_base::ate) {
324  if (FSEEK(_M_file, 0, SEEK_END) != 0)
325  _M_is_open = false;
326  }
327 
328  _M_file_id = file_no;
330  _M_openmode = openmode;
331 
332  if (_M_is_open)
334 
335  return (_M_is_open != 0);
336 }
337 
339 {
340  if (!_M_is_open)
341  return false;
342 
343  bool ok = _M_should_close ? (fclose(_M_file) == 0) : true;
344 
345  _M_is_open = _M_should_close = false;
346  _M_openmode = 0;
347  return ok;
348 }
349 
350 // Read up to n characters into a buffer. Return value is number of
351 // characters read.
353  return fread(buf, 1, n, _M_file);
354 }
355 
356 // Write n characters from a buffer. Return value: true if we managed
357 // to write the entire buffer, false if we didn't.
359 {
360  for (;;) {
361  ptrdiff_t written = fwrite(buf, 1, n, _M_file);
362 
363  if (n == written) {
364  return true;
365  }
366 
367  if (written > 0 && written < n) {
368  n -= written;
369  buf += written;
370  } else {
371  return false;
372  }
373  }
374 }
375 
376 // Wrapper for lseek or the like.
378 {
379  int whence;
380 
381  switch ( dir ) {
382  case ios_base::beg:
384  return streamoff(-1);
385  whence = SEEK_SET;
386  break;
387  case ios_base::cur:
388  whence = SEEK_CUR;
389  break;
390  case ios_base::end:
391  if (/* offset > 0 || */ -offset > _M_file_size() )
392  return streamoff(-1);
393  whence = SEEK_END;
394  break;
395  default:
396  return streamoff(-1);
397  }
398 
399  if ( FSEEK(_M_file, offset, whence) == 0 ) {
400  return FTELL(_M_file);
401  }
402 
403  return streamoff(-1);
404 }
405 
406 
407 // Attempts to memory-map len bytes of the current file, starting
408 // at position offset. Precondition: offset is a multiple of the
409 // page size. Postcondition: return value is a null pointer if the
410 // memory mapping failed. Otherwise the return value is a pointer to
411 // the memory-mapped file and the file position is set to offset.
413 {
414  return 0;
415 }
416 
418 {
419  // precondition : there is a valid mapping at the moment
420 }
421 
off_t streamoff
Definition: char_traits.h:74
#define SEEK_CUR
Definition: util.h:63
#define FTELL
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
static size_t _M_page_size
Definition: _fstream.h:113
unsigned char _M_regular_file
Definition: _fstream.h:124
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLintptr offset
Definition: glext.h:5920
ptrdiff_t _M_read(char *__buf, ptrdiff_t __n)
GLdouble n
Definition: glext.h:7729
static int fd
Definition: io.c:51
#define S_IFREG
Definition: ext2fs.h:356
GLuint GLuint end
Definition: gl.h:1545
_Check_return_opt_ _CRTIMP size_t __cdecl fwrite(_In_reads_bytes_(_Size *_Count) const void *_Str, _In_ size_t _Size, _In_ size_t _Count, _Inout_ FILE *_File)
int seekdir
Definition: _ios_base.h:60
unsigned char _M_should_close
Definition: _fstream.h:123
static streamoff __file_size(_STLP_fd fd)
#define _STLP_MOVE_TO_PRIV_NAMESPACE
Definition: features.h:524
void _M_unmap(void *__mmap_base, streamoff __len)
_Check_return_opt_ _CRTIMP size_t __cdecl fread(_Out_writes_bytes_(_ElementSize *_Count) void *_DstBuf, _In_ size_t _ElementSize, _In_ size_t _Count, _Inout_ FILE *_File)
static void _S_initialize()
#define S_IRGRP
Definition: propsheet.h:41
#define ok(value,...)
double __cdecl trunc(double)
ios_base::openmode _M_openmode
Definition: _fstream.h:121
streamoff _M_file_size()
unsigned int dir
Definition: maze.c:112
#define SEEK_SET
Definition: jmemansi.c:26
#define S_IWGRP
Definition: propsheet.h:45
#define _STLP_MOVE_TO_STD_NAMESPACE
Definition: features.h:525
#define S_IWUSR
Definition: propsheet.h:33
_Check_return_ _CRTIMP int __cdecl fileno(_In_ FILE *_File)
int openmode
Definition: _ios_base.h:59
const char file[]
Definition: icontest.c:11
void * _M_mmap(streamoff __offset, streamoff __len)
static FILE * out
Definition: regtests2xml.c:44
const GLuint GLenum const GLvoid * binary
Definition: glext.h:7538
#define false
Definition: stdbool.h:38
GLbitfield flags
Definition: glext.h:7161
#define FSEEK
int ret
streamoff _M_seek(streamoff __offset, ios_base::seekdir __dir)
#define S_IROTH
Definition: propsheet.h:53
#define _STLP_PRIV
Definition: _dm.h:70
unsigned char _M_is_open
Definition: _fstream.h:122
#define FSTAT
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
GLenum mode
Definition: glext.h:6217
#define S_IREAD
Definition: acwin.h:87
#define S_IWOTH
Definition: propsheet.h:57
#define S_IWRITE
Definition: acwin.h:88
bool _M_write(char *__buf, ptrdiff_t __n)
GLuint in
Definition: glext.h:9616
#define _STLP_END_NAMESPACE
Definition: features.h:503
#define S_IRUSR
Definition: propsheet.h:29
__kernel_ptrdiff_t ptrdiff_t
Definition: linux.h:247
bool _M_open(const char *, ios_base::openmode, long __protection)
Definition: name.c:36
_STLP_fd _M_file_id
Definition: _fstream.h:116
#define STAT
#define SEEK_END
Definition: cabinet.c:27
_CRTIMP void __cdecl setbuf(_Inout_ FILE *_File, _Inout_updates_opt_(BUFSIZ) _Post_readable_size_(0) char *_Buffer)
#define _STLP_BEGIN_NAMESPACE
Definition: features.h:501
const _STLP_fd INVALID_STLP_FD
#define FOPEN
static _STLP_MOVE_TO_PRIV_NAMESPACE bool __is_regular_file(_STLP_fd fd)
Definition: fci.c:126