Mostly work by Pedro to port libstdc++
[cegcc.git] / cegcc / src / gcc-4.4.0 / libstdc++-v3 / include / ext / stdio_filebuf.h
blob2f4c0474f4b508c935b18f131feb298c12c37209
1 // File descriptor layer for filebuf -*- C++ -*-
3 // Copyright (C) 2002, 2003, 2004, 2005, 2009 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 3, or (at your option)
9 // any later version.
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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file ext/stdio_filebuf.h
26 * This file is a GNU extension to the Standard C++ Library.
29 #ifndef _STDIO_FILEBUF_H
30 #define _STDIO_FILEBUF_H 1
32 #pragma GCC system_header
34 #include <fstream>
36 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
38 /**
39 * @brief Provides a layer of compatibility for C/POSIX.
41 * This GNU extension provides extensions for working with standard C
42 * FILE*'s and POSIX file descriptors. It must be instantiated by the
43 * user with the type of character used in the file stream, e.g.,
44 * stdio_filebuf<char>.
46 template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
47 class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits>
49 public:
50 // Types:
51 typedef _CharT char_type;
52 typedef _Traits traits_type;
53 typedef typename traits_type::int_type int_type;
54 typedef typename traits_type::pos_type pos_type;
55 typedef typename traits_type::off_type off_type;
56 typedef std::size_t size_t;
58 public:
59 /**
60 * deferred initialization
62 stdio_filebuf() : std::basic_filebuf<_CharT, _Traits>() {}
64 #ifndef __MINGW32CE__
65 /**
66 * @param fd An open file descriptor.
67 * @param mode Same meaning as in a standard filebuf.
68 * @param size Optimal or preferred size of internal buffer, in chars.
70 * This constructor associates a file stream buffer with an open
71 * POSIX file descriptor. The file descriptor will be automatically
72 * closed when the stdio_filebuf is closed/destroyed.
74 stdio_filebuf(int __fd, std::ios_base::openmode __mode,
75 size_t __size = static_cast<size_t>(BUFSIZ));
76 #endif
78 /**
79 * @param f An open @c FILE*.
80 * @param mode Same meaning as in a standard filebuf.
81 * @param size Optimal or preferred size of internal buffer, in chars.
82 * Defaults to system's @c BUFSIZ.
84 * This constructor associates a file stream buffer with an open
85 * C @c FILE*. The @c FILE* will not be automatically closed when the
86 * stdio_filebuf is closed/destroyed.
88 stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
89 size_t __size = static_cast<size_t>(BUFSIZ));
91 /**
92 * Closes the external data stream if the file descriptor constructor
93 * was used.
95 virtual
96 ~stdio_filebuf();
98 #ifndef __MINGW32CE__
99 /**
100 * @return The underlying file descriptor.
102 * Once associated with an external data stream, this function can be
103 * used to access the underlying POSIX file descriptor. Note that
104 * there is no way for the library to track what you do with the
105 * descriptor, so be careful.
108 fd() { return this->_M_file.fd(); }
109 #endif
112 * @return The underlying FILE*.
114 * This function can be used to access the underlying "C" file pointer.
115 * Note that there is no way for the library to track what you do
116 * with the file, so be careful.
118 std::__c_file*
119 file() { return this->_M_file.file(); }
122 template<typename _CharT, typename _Traits>
123 stdio_filebuf<_CharT, _Traits>::~stdio_filebuf()
126 #ifndef __MINGW32CE__
127 template<typename _CharT, typename _Traits>
128 stdio_filebuf<_CharT, _Traits>::
129 stdio_filebuf(int __fd, std::ios_base::openmode __mode, size_t __size)
131 this->_M_file.sys_open(__fd, __mode);
132 if (this->is_open())
134 this->_M_mode = __mode;
135 this->_M_buf_size = __size;
136 this->_M_allocate_internal_buffer();
137 this->_M_reading = false;
138 this->_M_writing = false;
139 this->_M_set_buffer(-1);
142 #endif
144 template<typename _CharT, typename _Traits>
145 stdio_filebuf<_CharT, _Traits>::
146 stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
147 size_t __size)
149 this->_M_file.sys_open(__f, __mode);
150 if (this->is_open())
152 this->_M_mode = __mode;
153 this->_M_buf_size = __size;
154 this->_M_allocate_internal_buffer();
155 this->_M_reading = false;
156 this->_M_writing = false;
157 this->_M_set_buffer(-1);
161 _GLIBCXX_END_NAMESPACE
163 #endif