]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/config/io/basic_file_stdio.cc
1 // Wrapper of C-language FILE struct -*- C++ -*-
3 // Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 // ISO C++ 14882: 27.8 File-based streams
34 #include <bits/basic_file.h>
39 // Definitions for __basic_file<char>.
40 __basic_file
<char>::__basic_file(__c_lock
* /*__lock*/)
41 : _M_cfile(NULL
), _M_cfile_created(false) { }
43 __basic_file
<char>::~__basic_file()
47 __basic_file
<char>::_M_open_mode(ios_base::openmode __mode
, int& __p_mode
,
50 bool __testb
= __mode
& ios_base::binary
;
51 bool __testi
= __mode
& ios_base::in
;
52 bool __testo
= __mode
& ios_base::out
;
53 bool __testt
= __mode
& ios_base::trunc
;
54 bool __testa
= __mode
& ios_base::app
;
56 // Set __c_mode for use in fopen.
57 // Set __p_mode for use in open.
58 if (!__testi
&& __testo
&& !__testt
&& !__testa
)
60 strcpy(__c_mode
, "w");
61 __p_mode
= (O_WRONLY
| O_CREAT
);
63 if (!__testi
&& __testo
&& !__testt
&& __testa
)
65 strcpy(__c_mode
, "a");
66 __p_mode
|= O_WRONLY
| O_CREAT
| O_APPEND
;
68 if (!__testi
&& __testo
&& __testt
&& !__testa
)
70 strcpy(__c_mode
, "w");
71 __p_mode
|= O_WRONLY
| O_CREAT
| O_TRUNC
;
74 if (__testi
&& !__testo
&& !__testt
&& !__testa
)
76 strcpy(__c_mode
, "r");
77 #if defined (O_NONBLOCK)
78 __p_mode
|= O_RDONLY
| O_NONBLOCK
;
83 if (__testi
&& __testo
&& !__testt
&& !__testa
)
85 strcpy(__c_mode
, "r+");
86 __p_mode
|= O_RDWR
| O_CREAT
;
88 if (__testi
&& __testo
&& __testt
&& !__testa
)
90 strcpy(__c_mode
, "w+");
91 __p_mode
|= O_RDWR
| O_CREAT
| O_TRUNC
;
94 strcat(__c_mode
, "b");
98 __basic_file
<char>::sys_open(__c_file
* __file
, ios_base::openmode
)
100 __basic_file
* __ret
= NULL
;
101 if (!this->is_open() && __file
)
104 _M_cfile_created
= false;
111 __basic_file
<char>::sys_open(int __fd
, ios_base::openmode __mode
,
114 __basic_file
* __ret
= NULL
;
119 _M_open_mode(__mode
, __p_mode
, __rw_mode
, __c_mode
);
120 if (!this->is_open() && (_M_cfile
= fdopen(__fd
, __c_mode
)))
122 // Iff __del is true, then close will fclose the fd.
123 _M_cfile_created
= __del
;
126 setvbuf(_M_cfile
, reinterpret_cast<char*>(NULL
), _IONBF
, 0);
134 __basic_file
<char>::sys_getc()
135 { return getc(_M_cfile
); }
138 __basic_file
<char>::sys_ungetc(int __c
)
139 { return ungetc(__c
, _M_cfile
); }
142 __basic_file
<char>::open(const char* __name
, ios_base::openmode __mode
,
145 __basic_file
* __ret
= NULL
;
150 _M_open_mode(__mode
, __p_mode
, __rw_mode
, __c_mode
);
152 if (!this->is_open())
154 if ((_M_cfile
= fopen(__name
, __c_mode
)))
156 _M_cfile_created
= true;
158 #if defined (F_SETFL) && defined (O_NONBLOCK)
159 // Set input to nonblocking for fifos.
160 if (__mode
& ios_base::in
)
161 fcntl(this->fd(), F_SETFL
, O_NONBLOCK
);
171 __basic_file
<char>::is_open() const
172 { return _M_cfile
!= 0; }
175 __basic_file
<char>::fd()
176 { return fileno(_M_cfile
) ; }
179 __basic_file
<char>::close()
181 __basic_file
* __retval
= static_cast<__basic_file
*>(NULL
);
185 if ((_M_cfile_created
&& fclose(_M_cfile
) == 0) || !_M_cfile_created
)
195 __basic_file
<char>::xsgetn(char* __s
, streamsize __n
)
196 { return fread(__s
, 1, __n
, _M_cfile
); }
199 __basic_file
<char>::xsputn(const char* __s
, streamsize __n
)
200 { return fwrite(__s
, 1, __n
, _M_cfile
); }
203 __basic_file
<char>::seekoff(streamoff __off
, ios_base::seekdir __way
,
204 ios_base::openmode
/*__mode*/)
206 if (!fseek(_M_cfile
, __off
, __way
))
207 return ftell(_M_cfile
);
214 __basic_file
<char>::seekpos(streamoff __pos
, ios_base::openmode
/*__mode*/)
216 if (!fseek(_M_cfile
, __pos
, ios_base::beg
))
217 return ftell(_M_cfile
);
224 __basic_file
<char>::sync()
225 { return fflush(_M_cfile
); }