1 // Wrapper of C-language FILE struct -*- C++ -*-
2 
3 // Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10 
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.
15 
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,
19 // USA.
20 
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.
29 
30 //
31 // ISO C++ 14882: 27.8  File-based streams
32 //
33 
34 #include <bits/basic_file.h>
35 
36 namespace std
37 {
38   // Definitions for __basic_file<char>.
39   __basic_file<char>::__basic_file(__c_lock* /*__lock*/)
40   : _M_cfile(NULL), _M_cfile_created(false) { }
41 
42   __basic_file<char>::~__basic_file()
43   { this->close(); }
44 
45   void
46   __basic_file<char>::_M_open_mode(ios_base::openmode __mode, int&, int&,
47 				   char* __c_mode)
48   {
49     bool __testb = __mode & ios_base::binary;
50     bool __testi = __mode & ios_base::in;
51     bool __testo = __mode & ios_base::out;
52     bool __testt = __mode & ios_base::trunc;
53     bool __testa = __mode & ios_base::app;
54 
55     if (!__testi && __testo && !__testt && !__testa)
56       strcpy(__c_mode, "w");
57     if (!__testi && __testo && !__testt && __testa)
58       strcpy(__c_mode, "a");
59     if (!__testi && __testo && __testt && !__testa)
60       strcpy(__c_mode, "w");
61     if (__testi && !__testo && !__testt && !__testa)
62       strcpy(__c_mode, "r");
63     if (__testi && __testo && !__testt && !__testa)
64       strcpy(__c_mode, "r+");
65     if (__testi && __testo && __testt && !__testa)
66       strcpy(__c_mode, "w+");
67     if (__testb)
68       strcat(__c_mode, "b");
69   }
70 
71   __basic_file<char>*
72   __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode)
73   {
74     __basic_file* __ret = NULL;
75     if (!this->is_open() && __file)
76       {
77  	_M_cfile = __file;
78  	_M_cfile_created = false;
79   	__ret = this;
80       }
81     return __ret;
82   }
83 
84   __basic_file<char>*
85   __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode,
86 			       bool __del)
87   {
88     __basic_file* __ret = NULL;
89     int __p_mode = 0;
90     int __rw_mode = 0;
91     char __c_mode[4];
92 
93     _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
94     if (!this->is_open() && (_M_cfile = fdopen(__fd, __c_mode)))
95       {
96 	// Iff __del is true, then close will fclose the fd.
97 	_M_cfile_created = __del;
98 
99 	if (__fd == 0)
100 	  setvbuf(_M_cfile, reinterpret_cast<char*>(NULL), _IONBF, 0);
101 
102 	__ret = this;
103       }
104     return __ret;
105   }
106 
107   int
108   __basic_file<char>::sys_getc()
109   { return getc(_M_cfile); }
110 
111   int
112   __basic_file<char>::sys_ungetc(int __c)
113   { return ungetc(__c, _M_cfile); }
114 
115   __basic_file<char>*
116   __basic_file<char>::open(const char* __name, ios_base::openmode __mode,
117 			   int /*__prot*/)
118   {
119     __basic_file* __ret = NULL;
120     int __p_mode = 0;
121     int __rw_mode = 0;
122     char __c_mode[4];
123 
124     _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
125 
126     if (!this->is_open())
127       {
128 	if ((_M_cfile = fopen(__name, __c_mode)))
129 	  {
130 	    _M_cfile_created = true;
131 	    __ret = this;
132 	  }
133       }
134     return __ret;
135   }
136 
137   bool
138   __basic_file<char>::is_open() const
139   { return _M_cfile != 0; }
140 
141   int
142   __basic_file<char>::fd()
143   { return fileno(_M_cfile) ; }
144 
145   __basic_file<char>*
146   __basic_file<char>::close()
147   {
148     __basic_file* __retval = static_cast<__basic_file*>(NULL);
149     if (this->is_open())
150       {
151 	fflush(_M_cfile);
152 	if ((_M_cfile_created && fclose(_M_cfile) == 0) || !_M_cfile_created)
153 	  {
154 	    _M_cfile = 0;
155 	    __retval = this;
156 	  }
157       }
158     return __retval;
159   }
160 
161   streamsize
162   __basic_file<char>::xsgetn(char* __s, streamsize __n)
163   { return fread(__s, 1, __n, _M_cfile); }
164 
165   streamsize
166   __basic_file<char>::xsputn(const char* __s, streamsize __n)
167   { return fwrite(__s, 1, __n, _M_cfile); }
168 
169   streamoff
170   __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way,
171 			      ios_base::openmode /*__mode*/)
172   {
173     fseek(_M_cfile, __off, __way);
174     return ftell(_M_cfile);
175   }
176 
177   streamoff
178   __basic_file<char>::seekpos(streamoff __pos, ios_base::openmode /*__mode*/)
179   {
180     fseek(_M_cfile, __pos, ios_base::beg);
181     return ftell(_M_cfile);
182   }
183 
184   int
185   __basic_file<char>::sync()
186   { return fflush(_M_cfile); }
187 }  // namespace std
188