1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
2 // (C) Copyright 2003-2007 Jonathan Turkanis
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
6 // See http://www.boost.org/libs/iostreams for documentation.
8 #ifndef BOOST_IOSTREAMS_FILE_HPP_INCLUDED
9 #define BOOST_IOSTREAMS_FILE_HPP_INCLUDED
15 #include <boost/iostreams/detail/config/wide_streams.hpp>
16 #ifndef BOOST_IOSTREAMS_NO_LOCALE
19 #include <string> // pathnames, char_traits.
20 #include <boost/iostreams/categories.hpp>
21 #include <boost/iostreams/detail/ios.hpp> // openmode, seekdir, int types.
22 #include <boost/iostreams/detail/fstream.hpp>
23 #include <boost/iostreams/operations.hpp> // seek.
24 #include <boost/shared_ptr.hpp>
27 #include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
29 namespace boost { namespace iostreams {
36 : public seekable_device_tag,
38 public localizable_tag,
41 basic_file( const std::string& path,
42 BOOST_IOS::openmode mode =
43 BOOST_IOS::in | BOOST_IOS::out,
44 BOOST_IOS::openmode base_mode =
45 BOOST_IOS::in | BOOST_IOS::out );
46 std::streamsize read(char_type* s, std::streamsize n);
47 bool putback(char_type c);
48 std::streamsize write(const char_type* s, std::streamsize n);
49 std::streampos seek( stream_offset off, BOOST_IOS::seekdir way,
50 BOOST_IOS::openmode which =
51 BOOST_IOS::in | BOOST_IOS::out );
52 void open( const std::string& path,
53 BOOST_IOS::openmode mode =
54 BOOST_IOS::in | BOOST_IOS::out,
55 BOOST_IOS::openmode base_mode =
56 BOOST_IOS::in | BOOST_IOS::out );
60 #ifndef BOOST_IOSTREAMS_NO_LOCALE
61 void imbue(const std::locale& loc) { pimpl_->file_.pubimbue(loc); }
65 impl(const std::string& path, BOOST_IOS::openmode mode)
66 { file_.open(path.c_str(), mode); }
67 ~impl() { if (file_.is_open()) file_.close(); }
68 BOOST_IOSTREAMS_BASIC_FILEBUF(Ch) file_;
70 shared_ptr<impl> pimpl_;
73 typedef basic_file<char> file;
74 typedef basic_file<wchar_t> wfile;
77 struct basic_file_source : private basic_file<Ch> {
84 using basic_file<Ch>::read;
85 using basic_file<Ch>::putback;
86 using basic_file<Ch>::seek;
87 using basic_file<Ch>::is_open;
88 using basic_file<Ch>::close;
89 basic_file_source( const std::string& path,
90 BOOST_IOS::openmode mode =
92 : basic_file<Ch>(path, mode & ~BOOST_IOS::out, BOOST_IOS::in)
94 void open( const std::string& path,
95 BOOST_IOS::openmode mode = BOOST_IOS::in )
97 basic_file<Ch>::open(path, mode & ~BOOST_IOS::out, BOOST_IOS::in);
101 typedef basic_file_source<char> file_source;
102 typedef basic_file_source<wchar_t> wfile_source;
104 template<typename Ch>
105 struct basic_file_sink : private basic_file<Ch> {
106 typedef Ch char_type;
113 using basic_file<Ch>::write;
114 using basic_file<Ch>::seek;
115 using basic_file<Ch>::is_open;
116 using basic_file<Ch>::close;
117 using basic_file<Ch>::flush;
118 basic_file_sink( const std::string& path,
119 BOOST_IOS::openmode mode = BOOST_IOS::out )
120 : basic_file<Ch>(path, mode & ~BOOST_IOS::in, BOOST_IOS::out)
122 void open( const std::string& path,
123 BOOST_IOS::openmode mode = BOOST_IOS::out )
125 basic_file<Ch>::open(path, mode & ~BOOST_IOS::in, BOOST_IOS::out);
129 typedef basic_file_sink<char> file_sink;
130 typedef basic_file_sink<wchar_t> wfile_sink;
132 //------------------Implementation of basic_file------------------------------//
134 template<typename Ch>
135 basic_file<Ch>::basic_file
136 ( const std::string& path, BOOST_IOS::openmode mode,
137 BOOST_IOS::openmode base_mode )
139 open(path, mode, base_mode);
142 template<typename Ch>
143 inline std::streamsize basic_file<Ch>::read
144 (char_type* s, std::streamsize n)
146 std::streamsize result = pimpl_->file_.sgetn(s, n);
147 return result != 0 ? result : -1;
150 template<typename Ch>
151 inline bool basic_file<Ch>::putback(char_type c)
153 return !!pimpl_->file_.sputbackc(c);
156 template<typename Ch>
157 inline std::streamsize basic_file<Ch>::write
158 (const char_type* s, std::streamsize n)
159 { return pimpl_->file_.sputn(s, n); }
161 template<typename Ch>
162 std::streampos basic_file<Ch>::seek
163 ( stream_offset off, BOOST_IOS::seekdir way,
164 BOOST_IOS::openmode )
165 { return iostreams::seek(pimpl_->file_, off, way); }
167 template<typename Ch>
168 void basic_file<Ch>::open
169 ( const std::string& path, BOOST_IOS::openmode mode,
170 BOOST_IOS::openmode base_mode )
172 pimpl_.reset(new impl(path, mode | base_mode));
175 template<typename Ch>
176 bool basic_file<Ch>::is_open() const { return pimpl_->file_.is_open(); }
178 template<typename Ch>
179 void basic_file<Ch>::close() { pimpl_->file_.close(); }
181 template<typename Ch>
182 bool basic_file<Ch>::flush()
183 { return pimpl_->file_.BOOST_IOSTREAMS_PUBSYNC() == 0; }
185 //----------------------------------------------------------------------------//
187 } } // End namespaces iostreams, boost.
189 #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC
191 #endif // #ifndef BOOST_IOSTREAMS_FILE_HPP_INCLUDED