OSDN Git Service

e610912158552bd55e9540000342c5491f71f9d8
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / ext / stdio_sync_filebuf.h
1 // Iostreams wrapper for stdio FILE* -*- C++ -*-
2
3 // Copyright (C) 2003 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 /** @file ext/stdiostream.h
31  *  This file is a GNU extension to the Standard C++ Library.
32  */
33
34 #ifndef _STDIO_SYNC_FILEBUF_H
35 #define _STDIO_SYNC_FILEBUF_H 1
36
37 #pragma GCC system_header
38
39 #include <streambuf>
40 #include <unistd.h>
41
42 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
43 # include <sys/stat.h>
44 # ifdef _GLIBCXX_HAVE_S_ISREG
45 #  define _GLIBCXX_ISREG(x) S_ISREG(x)
46 # else
47 #  define _GLIBCXX_ISREG(x) (((x) & S_IFMT) == S_IFREG)
48 # endif
49 #endif
50
51 #include <cstdio>
52
53 #ifdef _GLIBCXX_USE_WCHAR_T
54 #include <cwchar>
55 #endif
56
57 namespace __gnu_cxx
58 {
59   template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
60     class stdio_sync_filebuf : public std::basic_streambuf<_CharT, _Traits>
61     {
62     public:
63       // Types:
64       typedef _CharT                                    char_type;
65       typedef _Traits                                   traits_type;
66       typedef typename traits_type::int_type            int_type;
67       typedef typename traits_type::pos_type            pos_type;
68       typedef typename traits_type::off_type            off_type;
69
70     private:
71       // Underlying stdio FILE
72       std::__c_file* const _M_file;
73       
74       // Last character gotten. This is used when pbackfail is
75       // called from basic_streambuf::sungetc()
76       int_type _M_unget_buf;
77
78     public:
79       explicit 
80       stdio_sync_filebuf(std::__c_file* __f)
81       : _M_file(__f), _M_unget_buf(traits_type::eof())
82       { }
83
84     protected:
85
86       int_type
87       syncgetc();
88
89       int_type
90       syncungetc(int_type __c);
91
92       int_type
93       syncputc(int_type __c);
94
95       virtual int_type
96       underflow()
97       {
98         int_type __c = this->syncgetc();
99         return this->syncungetc(__c);
100       }
101
102       virtual int_type
103       uflow()
104       {
105         // Store the gotten character in case we need to unget it.
106         _M_unget_buf = this->syncgetc();
107         return _M_unget_buf;
108       }
109
110       virtual int_type
111       pbackfail(int_type __c = traits_type::eof())
112       {
113         int_type __ret;
114         const int_type __eof = traits_type::eof();
115
116         // Check if the unget or putback was requested
117         if (traits_type::eq_int_type(__c, __eof)) // unget
118           {
119             if (!traits_type::eq_int_type(_M_unget_buf, __eof))
120               __ret = this->syncungetc(_M_unget_buf);
121             else // buffer invalid, fail.
122               __ret = __eof;
123           }
124         else // putback
125           __ret = this->syncungetc(__c);
126
127         // The buffered character is no longer valid, discard it.
128         _M_unget_buf = __eof;
129         return __ret;
130       }
131
132       virtual std::streamsize
133       xsgetn(char_type* __s, std::streamsize __n);
134       
135       virtual std::streamsize
136       showmanyc()
137       { 
138 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
139         // Regular files.
140         struct stat __buffer;
141         int __ret = fstat(fileno(_M_file), &__buffer);
142         if (!__ret && _GLIBCXX_ISREG(__buffer.st_mode))
143           return __buffer.st_size - ftell(_M_file);
144 #endif
145         return 0; 
146       }
147
148       virtual int_type
149       overflow(int_type __c = traits_type::eof())
150       {
151         int_type __ret;
152         if (traits_type::eq_int_type(__c, traits_type::eof()))
153           {
154             if (std::fflush(_M_file))
155               __ret = traits_type::eof();
156             else
157               __ret = traits_type::not_eof(__c);
158           }
159         else
160           __ret = this->syncputc(__c);
161         return __ret;
162       }
163
164       virtual std::streamsize
165       xsputn(const char_type* __s, std::streamsize __n);
166
167       virtual int
168       sync()
169       { return std::fflush(_M_file); }
170
171       virtual std::streampos
172       seekoff(std::streamoff __off, std::ios_base::seekdir __dir,
173               std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
174       {
175         std::streampos __ret(std::streamoff(-1));
176         int __whence;
177         if (__dir == std::ios_base::beg)
178           __whence = SEEK_SET;
179         else if (__dir == std::ios_base::cur)
180           __whence = SEEK_CUR;
181         else
182           __whence = SEEK_END;
183 #ifdef _GLIBCXX_USE_LFS
184         if (!fseeko64(_M_file, __off, __whence))
185           __ret = std::streampos(ftello64(_M_file));
186 #else   
187         if (!fseek(_M_file, __off, __whence))
188           __ret = std::streampos(std::ftell(_M_file));
189 #endif
190         return __ret;
191       }
192
193       virtual std::streampos
194       seekpos(std::streampos __pos,
195               std::ios_base::openmode __mode =
196               std::ios_base::in | std::ios_base::out)
197       { return seekoff(std::streamoff(__pos), std::ios_base::beg, __mode); }
198     };
199
200   template<>
201     inline stdio_sync_filebuf<char>::int_type
202     stdio_sync_filebuf<char>::syncgetc()
203     { return std::getc(_M_file); }
204
205   template<>
206     inline stdio_sync_filebuf<char>::int_type
207     stdio_sync_filebuf<char>::syncungetc(int_type __c)
208     { return std::ungetc(__c, _M_file); }
209
210   template<>
211     inline stdio_sync_filebuf<char>::int_type
212     stdio_sync_filebuf<char>::syncputc(int_type __c)
213     { return std::putc(__c, _M_file); }
214
215   template<>
216     inline std::streamsize
217     stdio_sync_filebuf<char>::xsgetn(char* __s, std::streamsize __n)
218     {
219       std::streamsize __ret = std::fread(__s, 1, __n, _M_file);
220       if (__ret > 0)
221         _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
222       else
223         _M_unget_buf = traits_type::eof();
224       return __ret;
225     }
226
227   template<>
228     inline std::streamsize
229     stdio_sync_filebuf<char>::xsputn(const char* __s, std::streamsize __n)
230     { return std::fwrite(__s, 1, __n, _M_file); }
231
232 #ifdef _GLIBCXX_USE_WCHAR_T
233   template<>
234     inline stdio_sync_filebuf<wchar_t>::int_type
235     stdio_sync_filebuf<wchar_t>::syncgetc()
236     { return std::getwc(_M_file); }
237
238   template<>
239     inline stdio_sync_filebuf<wchar_t>::int_type
240     stdio_sync_filebuf<wchar_t>::syncungetc(int_type __c)
241     { return std::ungetwc(__c, _M_file); }
242
243   template<>
244     inline stdio_sync_filebuf<wchar_t>::int_type
245     stdio_sync_filebuf<wchar_t>::syncputc(int_type __c)
246     { return std::putwc(__c, _M_file); }
247
248   template<>
249     inline std::streamsize
250     stdio_sync_filebuf<wchar_t>::xsgetn(wchar_t* __s, std::streamsize __n)
251     {  
252       std::streamsize __ret = 0;
253       const int_type __eof = traits_type::eof();
254       while (__n--)
255         {
256           int_type __c = this->syncgetc();
257           if (traits_type::eq_int_type(__c, __eof))
258             break;
259           __s[__ret] = traits_type::to_char_type(__c);
260           ++__ret;
261         }
262
263       if (__ret > 0)
264         _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
265       else
266         _M_unget_buf = traits_type::eof();
267       return __ret;
268     }
269       
270   template<>
271     inline std::streamsize
272     stdio_sync_filebuf<wchar_t>::xsputn(const wchar_t* __s, 
273                                         std::streamsize __n)
274     {
275       std::streamsize __ret = 0;
276       const int_type __eof = traits_type::eof();
277       while (__n--)
278         {
279           if (traits_type::eq_int_type(this->syncputc(*__s++), __eof))
280             break;
281           ++__ret;
282         }
283       return __ret;
284     }
285 #endif
286
287 #if _GLIBCXX_EXTERN_TEMPLATE
288   extern template class stdio_sync_filebuf<char>;
289 #ifdef _GLIBCXX_USE_WCHAR_T
290   extern template class stdio_sync_filebuf<wchar_t>;
291 #endif
292 #endif
293 } // namespace __gnu_cxx
294
295 #endif