sl@0
|
1 |
// (C) Copyright Jonathan Turkanis 2003.
|
sl@0
|
2 |
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
sl@0
|
3 |
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
|
sl@0
|
4 |
|
sl@0
|
5 |
// See http://www.boost.org/libs/iostreams for documentation.
|
sl@0
|
6 |
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Contains wrappers for standard file buffers, together
|
sl@0
|
9 |
// with convenience typedefs:
|
sl@0
|
10 |
// - basic_file_source
|
sl@0
|
11 |
// - basic_file_sink
|
sl@0
|
12 |
// - basic_file
|
sl@0
|
13 |
//
|
sl@0
|
14 |
|
sl@0
|
15 |
#ifndef BOOST_IOSTREAMS_FILE_HPP_INCLUDED
|
sl@0
|
16 |
#define BOOST_IOSTREAMS_FILE_HPP_INCLUDED
|
sl@0
|
17 |
|
sl@0
|
18 |
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
sl@0
|
19 |
# pragma once
|
sl@0
|
20 |
#endif
|
sl@0
|
21 |
|
sl@0
|
22 |
#include <boost/iostreams/detail/config/wide_streams.hpp>
|
sl@0
|
23 |
#ifndef BOOST_IOSTREAMS_NO_LOCALE
|
sl@0
|
24 |
# include <locale>
|
sl@0
|
25 |
#endif
|
sl@0
|
26 |
#include <string> // pathnames, char_traits.
|
sl@0
|
27 |
#include <boost/iostreams/categories.hpp>
|
sl@0
|
28 |
#include <boost/iostreams/detail/ios.hpp> // openmode, seekdir, int types.
|
sl@0
|
29 |
#include <boost/iostreams/detail/fstream.hpp>
|
sl@0
|
30 |
#include <boost/iostreams/operations.hpp> // seek.
|
sl@0
|
31 |
#include <boost/shared_ptr.hpp>
|
sl@0
|
32 |
|
sl@0
|
33 |
// Must come last.
|
sl@0
|
34 |
#include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
|
sl@0
|
35 |
|
sl@0
|
36 |
namespace boost { namespace iostreams {
|
sl@0
|
37 |
|
sl@0
|
38 |
template<typename Ch>
|
sl@0
|
39 |
class basic_file {
|
sl@0
|
40 |
public:
|
sl@0
|
41 |
typedef Ch char_type;
|
sl@0
|
42 |
struct category
|
sl@0
|
43 |
: public seekable_device_tag,
|
sl@0
|
44 |
public closable_tag,
|
sl@0
|
45 |
public localizable_tag
|
sl@0
|
46 |
{ };
|
sl@0
|
47 |
basic_file( const std::string& path,
|
sl@0
|
48 |
BOOST_IOS::openmode mode =
|
sl@0
|
49 |
BOOST_IOS::in | BOOST_IOS::out,
|
sl@0
|
50 |
BOOST_IOS::openmode base_mode =
|
sl@0
|
51 |
BOOST_IOS::in | BOOST_IOS::out );
|
sl@0
|
52 |
std::streamsize read(char_type* s, std::streamsize n);
|
sl@0
|
53 |
std::streamsize write(const char_type* s, std::streamsize n);
|
sl@0
|
54 |
stream_offset seek( stream_offset off, BOOST_IOS::seekdir way,
|
sl@0
|
55 |
BOOST_IOS::openmode which =
|
sl@0
|
56 |
BOOST_IOS::in | BOOST_IOS::out );
|
sl@0
|
57 |
void open( const std::string& path,
|
sl@0
|
58 |
BOOST_IOS::openmode mode =
|
sl@0
|
59 |
BOOST_IOS::in | BOOST_IOS::out,
|
sl@0
|
60 |
BOOST_IOS::openmode base_mode =
|
sl@0
|
61 |
BOOST_IOS::in | BOOST_IOS::out );
|
sl@0
|
62 |
bool is_open() const;
|
sl@0
|
63 |
void close();
|
sl@0
|
64 |
#ifndef BOOST_IOSTREAMS_NO_LOCALE
|
sl@0
|
65 |
void imbue(const std::locale& loc) { pimpl_->file_.pubimbue(loc); }
|
sl@0
|
66 |
#endif
|
sl@0
|
67 |
private:
|
sl@0
|
68 |
struct impl {
|
sl@0
|
69 |
impl(const std::string& path, BOOST_IOS::openmode mode)
|
sl@0
|
70 |
{ file_.open(path.c_str(), mode); }
|
sl@0
|
71 |
~impl() { if (file_.is_open()) file_.close(); }
|
sl@0
|
72 |
BOOST_IOSTREAMS_BASIC_FILEBUF(Ch) file_;
|
sl@0
|
73 |
};
|
sl@0
|
74 |
shared_ptr<impl> pimpl_;
|
sl@0
|
75 |
};
|
sl@0
|
76 |
|
sl@0
|
77 |
typedef basic_file<char> file;
|
sl@0
|
78 |
typedef basic_file<wchar_t> wfile;
|
sl@0
|
79 |
|
sl@0
|
80 |
template<typename Ch>
|
sl@0
|
81 |
struct basic_file_source : private basic_file<Ch> {
|
sl@0
|
82 |
typedef Ch char_type;
|
sl@0
|
83 |
struct category
|
sl@0
|
84 |
: input_seekable,
|
sl@0
|
85 |
device_tag,
|
sl@0
|
86 |
closable_tag
|
sl@0
|
87 |
{ };
|
sl@0
|
88 |
using basic_file<Ch>::read;
|
sl@0
|
89 |
using basic_file<Ch>::seek;
|
sl@0
|
90 |
using basic_file<Ch>::is_open;
|
sl@0
|
91 |
using basic_file<Ch>::close;
|
sl@0
|
92 |
basic_file_source( const std::string& path,
|
sl@0
|
93 |
BOOST_IOS::openmode mode =
|
sl@0
|
94 |
BOOST_IOS::in )
|
sl@0
|
95 |
: basic_file<Ch>(path, mode & ~BOOST_IOS::out, BOOST_IOS::in)
|
sl@0
|
96 |
{ }
|
sl@0
|
97 |
void open( const std::string& path,
|
sl@0
|
98 |
BOOST_IOS::openmode mode = BOOST_IOS::in )
|
sl@0
|
99 |
{
|
sl@0
|
100 |
basic_file<Ch>::open(path, mode & ~BOOST_IOS::out, BOOST_IOS::in);
|
sl@0
|
101 |
}
|
sl@0
|
102 |
};
|
sl@0
|
103 |
|
sl@0
|
104 |
typedef basic_file_source<char> file_source;
|
sl@0
|
105 |
typedef basic_file_source<wchar_t> wfile_source;
|
sl@0
|
106 |
|
sl@0
|
107 |
template<typename Ch>
|
sl@0
|
108 |
struct basic_file_sink : private basic_file<Ch> {
|
sl@0
|
109 |
typedef Ch char_type;
|
sl@0
|
110 |
struct category
|
sl@0
|
111 |
: output_seekable,
|
sl@0
|
112 |
device_tag,
|
sl@0
|
113 |
closable_tag
|
sl@0
|
114 |
{ };
|
sl@0
|
115 |
using basic_file<Ch>::write;
|
sl@0
|
116 |
using basic_file<Ch>::seek;
|
sl@0
|
117 |
using basic_file<Ch>::is_open;
|
sl@0
|
118 |
using basic_file<Ch>::close;
|
sl@0
|
119 |
basic_file_sink( const std::string& path,
|
sl@0
|
120 |
BOOST_IOS::openmode mode = BOOST_IOS::out )
|
sl@0
|
121 |
: basic_file<Ch>(path, mode & ~BOOST_IOS::in, BOOST_IOS::out)
|
sl@0
|
122 |
{ }
|
sl@0
|
123 |
void open( const std::string& path,
|
sl@0
|
124 |
BOOST_IOS::openmode mode = BOOST_IOS::out )
|
sl@0
|
125 |
{
|
sl@0
|
126 |
basic_file<Ch>::open(path, mode & ~BOOST_IOS::in, BOOST_IOS::out);
|
sl@0
|
127 |
}
|
sl@0
|
128 |
};
|
sl@0
|
129 |
|
sl@0
|
130 |
typedef basic_file_sink<char> file_sink;
|
sl@0
|
131 |
typedef basic_file_sink<wchar_t> wfile_sink;
|
sl@0
|
132 |
|
sl@0
|
133 |
//------------------Implementation of basic_file------------------------------//
|
sl@0
|
134 |
|
sl@0
|
135 |
template<typename Ch>
|
sl@0
|
136 |
basic_file<Ch>::basic_file
|
sl@0
|
137 |
( const std::string& path, BOOST_IOS::openmode mode,
|
sl@0
|
138 |
BOOST_IOS::openmode base_mode )
|
sl@0
|
139 |
{
|
sl@0
|
140 |
open(path, mode, base_mode);
|
sl@0
|
141 |
}
|
sl@0
|
142 |
|
sl@0
|
143 |
template<typename Ch>
|
sl@0
|
144 |
inline std::streamsize basic_file<Ch>::read
|
sl@0
|
145 |
(char_type* s, std::streamsize n)
|
sl@0
|
146 |
{
|
sl@0
|
147 |
std::streamsize result = pimpl_->file_.sgetn(s, n);
|
sl@0
|
148 |
return result != 0 ? result : -1;
|
sl@0
|
149 |
}
|
sl@0
|
150 |
|
sl@0
|
151 |
template<typename Ch>
|
sl@0
|
152 |
inline std::streamsize basic_file<Ch>::write
|
sl@0
|
153 |
(const char_type* s, std::streamsize n)
|
sl@0
|
154 |
{ return pimpl_->file_.sputn(s, n); }
|
sl@0
|
155 |
|
sl@0
|
156 |
template<typename Ch>
|
sl@0
|
157 |
stream_offset basic_file<Ch>::seek
|
sl@0
|
158 |
( stream_offset off, BOOST_IOS::seekdir way,
|
sl@0
|
159 |
BOOST_IOS::openmode )
|
sl@0
|
160 |
{ return iostreams::seek(pimpl_->file_, off, way); }
|
sl@0
|
161 |
|
sl@0
|
162 |
template<typename Ch>
|
sl@0
|
163 |
void basic_file<Ch>::open
|
sl@0
|
164 |
( const std::string& path, BOOST_IOS::openmode mode,
|
sl@0
|
165 |
BOOST_IOS::openmode base_mode )
|
sl@0
|
166 |
{
|
sl@0
|
167 |
pimpl_.reset(new impl(path, mode | base_mode));
|
sl@0
|
168 |
}
|
sl@0
|
169 |
|
sl@0
|
170 |
template<typename Ch>
|
sl@0
|
171 |
bool basic_file<Ch>::is_open() const { return pimpl_->file_.is_open(); }
|
sl@0
|
172 |
|
sl@0
|
173 |
template<typename Ch>
|
sl@0
|
174 |
void basic_file<Ch>::close() { pimpl_->file_.close(); }
|
sl@0
|
175 |
|
sl@0
|
176 |
//----------------------------------------------------------------------------//
|
sl@0
|
177 |
|
sl@0
|
178 |
} } // End namespaces iostreams, boost.
|
sl@0
|
179 |
|
sl@0
|
180 |
#include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC
|
sl@0
|
181 |
|
sl@0
|
182 |
#endif // #ifndef BOOST_IOSTREAMS_FILE_HPP_INCLUDED
|