fix doc example typo
[boost.git] / boost / iostreams / device / file.hpp
blob0cbc152ae3dbb1596babff7393f39a76ad81a7f1
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
11 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
12 # pragma once
13 #endif
15 #include <boost/iostreams/detail/config/wide_streams.hpp>
16 #ifndef BOOST_IOSTREAMS_NO_LOCALE
17 # include <locale>
18 #endif
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>
26 // Must come last.
27 #include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
29 namespace boost { namespace iostreams {
31 template<typename Ch>
32 class basic_file {
33 public:
34 typedef Ch char_type;
35 struct category
36 : public seekable_device_tag,
37 public closable_tag,
38 public localizable_tag
39 { };
40 basic_file( const std::string& path,
41 BOOST_IOS::openmode mode =
42 BOOST_IOS::in | BOOST_IOS::out,
43 BOOST_IOS::openmode base_mode =
44 BOOST_IOS::in | BOOST_IOS::out );
45 std::streamsize read(char_type* s, std::streamsize n);
46 bool putback(char_type c);
47 std::streamsize write(const char_type* s, std::streamsize n);
48 std::streampos seek( stream_offset off, BOOST_IOS::seekdir way,
49 BOOST_IOS::openmode which =
50 BOOST_IOS::in | BOOST_IOS::out );
51 void open( const std::string& path,
52 BOOST_IOS::openmode mode =
53 BOOST_IOS::in | BOOST_IOS::out,
54 BOOST_IOS::openmode base_mode =
55 BOOST_IOS::in | BOOST_IOS::out );
56 bool is_open() const;
57 void close();
58 #ifndef BOOST_IOSTREAMS_NO_LOCALE
59 void imbue(const std::locale& loc) { pimpl_->file_.pubimbue(loc); }
60 #endif
61 private:
62 struct impl {
63 impl(const std::string& path, BOOST_IOS::openmode mode)
64 { file_.open(path.c_str(), mode); }
65 ~impl() { if (file_.is_open()) file_.close(); }
66 BOOST_IOSTREAMS_BASIC_FILEBUF(Ch) file_;
68 shared_ptr<impl> pimpl_;
71 typedef basic_file<char> file;
72 typedef basic_file<wchar_t> wfile;
74 template<typename Ch>
75 struct basic_file_source : private basic_file<Ch> {
76 typedef Ch char_type;
77 struct category
78 : input_seekable,
79 device_tag,
80 closable_tag
81 { };
82 using basic_file<Ch>::read;
83 using basic_file<Ch>::putback;
84 using basic_file<Ch>::seek;
85 using basic_file<Ch>::is_open;
86 using basic_file<Ch>::close;
87 basic_file_source( const std::string& path,
88 BOOST_IOS::openmode mode =
89 BOOST_IOS::in )
90 : basic_file<Ch>(path, mode & ~BOOST_IOS::out, BOOST_IOS::in)
91 { }
92 void open( const std::string& path,
93 BOOST_IOS::openmode mode = BOOST_IOS::in )
95 basic_file<Ch>::open(path, mode & ~BOOST_IOS::out, BOOST_IOS::in);
99 typedef basic_file_source<char> file_source;
100 typedef basic_file_source<wchar_t> wfile_source;
102 template<typename Ch>
103 struct basic_file_sink : private basic_file<Ch> {
104 typedef Ch char_type;
105 struct category
106 : output_seekable,
107 device_tag,
108 closable_tag
109 { };
110 using basic_file<Ch>::write;
111 using basic_file<Ch>::seek;
112 using basic_file<Ch>::is_open;
113 using basic_file<Ch>::close;
114 basic_file_sink( const std::string& path,
115 BOOST_IOS::openmode mode = BOOST_IOS::out )
116 : basic_file<Ch>(path, mode & ~BOOST_IOS::in, BOOST_IOS::out)
118 void open( const std::string& path,
119 BOOST_IOS::openmode mode = BOOST_IOS::out )
121 basic_file<Ch>::open(path, mode & ~BOOST_IOS::in, BOOST_IOS::out);
125 typedef basic_file_sink<char> file_sink;
126 typedef basic_file_sink<wchar_t> wfile_sink;
128 //------------------Implementation of basic_file------------------------------//
130 template<typename Ch>
131 basic_file<Ch>::basic_file
132 ( const std::string& path, BOOST_IOS::openmode mode,
133 BOOST_IOS::openmode base_mode )
135 open(path, mode, base_mode);
138 template<typename Ch>
139 inline std::streamsize basic_file<Ch>::read
140 (char_type* s, std::streamsize n)
142 std::streamsize result = pimpl_->file_.sgetn(s, n);
143 return result != 0 ? result : -1;
146 template<typename Ch>
147 inline bool basic_file<Ch>::putback(char_type c)
149 return !!pimpl_->file_.sputbackc(c);
152 template<typename Ch>
153 inline std::streamsize basic_file<Ch>::write
154 (const char_type* s, std::streamsize n)
155 { return pimpl_->file_.sputn(s, n); }
157 template<typename Ch>
158 std::streampos basic_file<Ch>::seek
159 ( stream_offset off, BOOST_IOS::seekdir way,
160 BOOST_IOS::openmode )
161 { return iostreams::seek(pimpl_->file_, off, way); }
163 template<typename Ch>
164 void basic_file<Ch>::open
165 ( const std::string& path, BOOST_IOS::openmode mode,
166 BOOST_IOS::openmode base_mode )
168 pimpl_.reset(new impl(path, mode | base_mode));
171 template<typename Ch>
172 bool basic_file<Ch>::is_open() const { return pimpl_->file_.is_open(); }
174 template<typename Ch>
175 void basic_file<Ch>::close() { pimpl_->file_.close(); }
177 //----------------------------------------------------------------------------//
179 } } // End namespaces iostreams, boost.
181 #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC
183 #endif // #ifndef BOOST_IOSTREAMS_FILE_HPP_INCLUDED