OSDN Git Service

2001-05-01 Tom Browder <tbrowder@home.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / config / basic_file_stdio.h
1 // Wrapper of C-language FILE struct -*- C++ -*-
2
3 // Copyright (C) 2000, 2001 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 <unistd.h>
35
36 namespace std 
37 {
38   // Generic definitions for __basic_file
39   template<typename _CharT>
40     __basic_file<_CharT>::__basic_file(__c_lock* /*__lock*/) 
41     : _M_cfile(NULL), _M_cfile_created(false) { }
42
43   template<typename _CharT>
44     __basic_file<_CharT>::~__basic_file()
45     {
46       if (this->is_open())
47         {
48           fflush(_M_cfile);
49           this->close();
50         }
51     }
52       
53   template<typename _CharT>
54     void 
55     __basic_file<_CharT>::_M_open_mode(ios_base::openmode __mode, 
56                                        int& /*__p_mode*/, int& /*__rw_mode*/, 
57                                        char* __c_mode)
58     {  
59       bool __testb = __mode & ios_base::binary;
60       bool __testi = __mode & ios_base::in;
61       bool __testo = __mode & ios_base::out;
62       bool __testt = __mode & ios_base::trunc;
63       bool __testa = __mode & ios_base::app;
64       
65       if (!__testi && __testo && !__testt && !__testa)
66         strcpy(__c_mode, "w");
67       if (!__testi && __testo && !__testt && __testa)
68         strcpy(__c_mode, "a");
69       if (!__testi && __testo && __testt && !__testa)
70         strcpy(__c_mode, "w");
71       if (__testi && !__testo && !__testt && !__testa)
72         strcpy(__c_mode, "r");
73       if (__testi && __testo && !__testt && !__testa)
74         strcpy(__c_mode, "r+");
75       if (__testi && __testo && __testt && !__testa)
76         strcpy(__c_mode, "w+");
77       if (__testb)
78         strcat(__c_mode, "b");
79     }
80   
81   template<typename _CharT>
82     __basic_file<_CharT>*
83     __basic_file<_CharT>::sys_open(__c_file_type* __file, ios_base::openmode) 
84     {
85       __basic_file* __ret = NULL;
86
87       if (!this->is_open() && __file)
88         {
89           _M_cfile = __file;
90           _M_cfile_created = false;
91           __ret = this;
92         }
93
94       return __ret;
95     }
96   
97   template<typename _CharT>
98     __basic_file<_CharT>* 
99     __basic_file<_CharT>::open(const char* __name, ios_base::openmode __mode, 
100                                int /*__prot*/)
101     {
102       __basic_file* __ret = NULL;
103       int __p_mode = 0;
104       int __rw_mode = 0;
105       char __c_mode[4];
106       
107       _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
108
109       if (!this->is_open())
110         {
111           if ((_M_cfile = fopen(__name, __c_mode)))
112             {
113               _M_cfile_created = true;
114               __ret = this;
115             }
116         }
117       return __ret;
118     }
119   
120   template<typename _CharT>
121     bool 
122     __basic_file<_CharT>::is_open() { return _M_cfile != 0; }
123   
124   template<typename _CharT>
125     __basic_file<_CharT>* 
126     __basic_file<_CharT>::close()
127     { 
128       __basic_file* __retval = static_cast<__basic_file*>(NULL);
129       if (_M_cfile_created && fclose(_M_cfile))
130         __retval = this;
131       return __retval;
132     }
133  
134   template<typename _CharT>
135     streamsize 
136     __basic_file<_CharT>::xsgetn(_CharT* __s, streamsize __n)
137     { return fread(__s, 1, __n, _M_cfile); }
138
139   template<typename _CharT>
140     streamsize 
141     __basic_file<_CharT>::xsputn(const _CharT* __s, streamsize __n)
142     { return fwrite(__s, 1, __n, _M_cfile); }
143  
144   template<typename _CharT>
145     streamoff
146     __basic_file<_CharT>::seekoff(streamoff __off, ios_base::seekdir __way, 
147                                   ios_base::openmode /*__mode*/)
148     { fseek(_M_cfile, __off, __way); return ftell(_M_cfile); }
149
150   template<typename _CharT>
151     streamoff
152     __basic_file<_CharT>::seekpos(streamoff __pos, 
153                                   ios_base::openmode /*__mode*/)
154     { fseek(_M_cfile, __pos, ios_base::beg); return ftell(_M_cfile); }
155
156   template<typename _CharT>
157     int 
158     __basic_file<_CharT>::sync()
159     { return fflush(_M_cfile); }
160
161   // NB: Unused.
162   template<typename _CharT>
163     int 
164     __basic_file<_CharT>::overflow(int /*__c*/) 
165     { return EOF; }
166
167   // NB: Unused.
168   template<typename _CharT>
169     int 
170     __basic_file<_CharT>::underflow()  
171     { return EOF; } 
172
173   // NB: Unused.
174   template<typename _CharT>
175     int 
176     __basic_file<_CharT>::uflow()  
177     { return EOF; }
178
179   // NB: Unused.
180   template<typename _CharT>
181     int 
182     __basic_file<_CharT>::pbackfail(int /*__c*/) 
183     { return EOF; } 
184  
185  // NB: Unused.
186   template<typename _CharT>
187     streambuf* 
188     __basic_file<_CharT>::setbuf(_CharT* /*__b*/, int /*__len*/)
189     { return reinterpret_cast<streambuf*>(this); }
190
191   // NB: Unused.
192   template<typename _CharT>
193     int 
194     __basic_file<_CharT>::doallocate() 
195     { return EOF; }
196
197   // NB: Unused.
198   template<typename _CharT>
199     streamsize 
200     __basic_file<_CharT>::sys_read(_CharT* __s, streamsize __n) 
201     { return fread(__s, 1, __n, _M_cfile); }
202
203   // NB: Unused.    
204   template<typename _CharT>
205     streamsize 
206     __basic_file<_CharT>::sys_write(const _CharT* __s, streamsize __n) 
207     { return fwrite(__s, 1, __n, _M_cfile); }
208
209   // NB: Unused.
210   template<typename _CharT>
211     streamoff
212     __basic_file<_CharT>::sys_seek(streamoff __pos, ios_base::seekdir __way)
213     { 
214       fseek(_M_cfile, __pos, __way); 
215       return ftell(_M_cfile); 
216     }
217   
218   // NB: Unused.
219   template<typename _CharT>
220     int 
221     __basic_file<_CharT>::sys_close() 
222     { return fclose(_M_cfile); }
223
224   // NB: Unused.
225   template<typename _CharT>
226     int 
227     __basic_file<_CharT>::sys_stat(void* /*__v*/) 
228     { return EOF; }
229
230   // NB: Unused.
231   template<typename _CharT>
232     int 
233     __basic_file<_CharT>::showmanyc() 
234     { return EOF; }
235
236   // NB: Unused.
237   template<typename _CharT>
238     void 
239     __basic_file<_CharT>::imbue(void* /*__v*/) { }
240 }  // namespace std