File indexing completed on 2025-01-18 09:38:51
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_IOSTREAMS_FILE_HPP_INCLUDED
0009 #define BOOST_IOSTREAMS_FILE_HPP_INCLUDED
0010
0011 #if defined(_MSC_VER)
0012 # pragma once
0013 #endif
0014
0015 #include <boost/iostreams/detail/config/wide_streams.hpp>
0016 #ifndef BOOST_IOSTREAMS_NO_LOCALE
0017 # include <locale>
0018 #endif
0019 #include <string> // pathnames, char_traits.
0020 #include <boost/iostreams/categories.hpp>
0021 #include <boost/iostreams/detail/ios.hpp> // openmode, seekdir, int types.
0022 #include <boost/iostreams/detail/fstream.hpp>
0023 #include <boost/iostreams/operations.hpp> // seek.
0024 #include <boost/shared_ptr.hpp>
0025
0026
0027 #include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
0028
0029 namespace boost { namespace iostreams {
0030
0031 template<typename Ch>
0032 class basic_file {
0033 public:
0034 typedef Ch char_type;
0035 struct category
0036 : public seekable_device_tag,
0037 public closable_tag,
0038 public localizable_tag,
0039 public flushable_tag
0040 { };
0041 basic_file( const std::string& path,
0042 BOOST_IOS::openmode mode =
0043 BOOST_IOS::in | BOOST_IOS::out,
0044 BOOST_IOS::openmode base_mode =
0045 BOOST_IOS::in | BOOST_IOS::out );
0046 std::streamsize read(char_type* s, std::streamsize n);
0047 bool putback(char_type c);
0048 std::streamsize write(const char_type* s, std::streamsize n);
0049 std::streampos seek( stream_offset off, BOOST_IOS::seekdir way,
0050 BOOST_IOS::openmode which =
0051 BOOST_IOS::in | BOOST_IOS::out );
0052 void open( const std::string& path,
0053 BOOST_IOS::openmode mode =
0054 BOOST_IOS::in | BOOST_IOS::out,
0055 BOOST_IOS::openmode base_mode =
0056 BOOST_IOS::in | BOOST_IOS::out );
0057 bool is_open() const;
0058 void close();
0059 bool flush();
0060 #ifndef BOOST_IOSTREAMS_NO_LOCALE
0061 void imbue(const std::locale& loc) { pimpl_->file_.pubimbue(loc); }
0062 #endif
0063 private:
0064 struct impl {
0065 impl(const std::string& path, BOOST_IOS::openmode mode)
0066 { file_.open(path.c_str(), mode); }
0067 ~impl() { if (file_.is_open()) file_.close(); }
0068 BOOST_IOSTREAMS_BASIC_FILEBUF(Ch) file_;
0069 };
0070 shared_ptr<impl> pimpl_;
0071 };
0072
0073 typedef basic_file<char> file;
0074 typedef basic_file<wchar_t> wfile;
0075
0076 template<typename Ch>
0077 struct basic_file_source : private basic_file<Ch> {
0078 typedef Ch char_type;
0079 struct category
0080 : input_seekable,
0081 device_tag,
0082 closable_tag
0083 { };
0084 using basic_file<Ch>::read;
0085 using basic_file<Ch>::putback;
0086 using basic_file<Ch>::seek;
0087 using basic_file<Ch>::is_open;
0088 using basic_file<Ch>::close;
0089 basic_file_source( const std::string& path,
0090 BOOST_IOS::openmode mode =
0091 BOOST_IOS::in )
0092 : basic_file<Ch>(path, mode & ~BOOST_IOS::out, BOOST_IOS::in)
0093 { }
0094 void open( const std::string& path,
0095 BOOST_IOS::openmode mode = BOOST_IOS::in )
0096 {
0097 basic_file<Ch>::open(path, mode & ~BOOST_IOS::out, BOOST_IOS::in);
0098 }
0099 };
0100
0101 typedef basic_file_source<char> file_source;
0102 typedef basic_file_source<wchar_t> wfile_source;
0103
0104 template<typename Ch>
0105 struct basic_file_sink : private basic_file<Ch> {
0106 typedef Ch char_type;
0107 struct category
0108 : output_seekable,
0109 device_tag,
0110 closable_tag,
0111 flushable_tag
0112 { };
0113 using basic_file<Ch>::write;
0114 using basic_file<Ch>::seek;
0115 using basic_file<Ch>::is_open;
0116 using basic_file<Ch>::close;
0117 using basic_file<Ch>::flush;
0118 basic_file_sink( const std::string& path,
0119 BOOST_IOS::openmode mode = BOOST_IOS::out )
0120 : basic_file<Ch>(path, mode & ~BOOST_IOS::in, BOOST_IOS::out)
0121 { }
0122 void open( const std::string& path,
0123 BOOST_IOS::openmode mode = BOOST_IOS::out )
0124 {
0125 basic_file<Ch>::open(path, mode & ~BOOST_IOS::in, BOOST_IOS::out);
0126 }
0127 };
0128
0129 typedef basic_file_sink<char> file_sink;
0130 typedef basic_file_sink<wchar_t> wfile_sink;
0131
0132
0133
0134 template<typename Ch>
0135 basic_file<Ch>::basic_file
0136 ( const std::string& path, BOOST_IOS::openmode mode,
0137 BOOST_IOS::openmode base_mode )
0138 {
0139 open(path, mode, base_mode);
0140 }
0141
0142 template<typename Ch>
0143 inline std::streamsize basic_file<Ch>::read
0144 (char_type* s, std::streamsize n)
0145 {
0146 std::streamsize result = pimpl_->file_.sgetn(s, n);
0147 return result != 0 ? result : -1;
0148 }
0149
0150 template<typename Ch>
0151 inline bool basic_file<Ch>::putback(char_type c)
0152 {
0153 return !!pimpl_->file_.sputbackc(c);
0154 }
0155
0156 template<typename Ch>
0157 inline std::streamsize basic_file<Ch>::write
0158 (const char_type* s, std::streamsize n)
0159 { return pimpl_->file_.sputn(s, n); }
0160
0161 template<typename Ch>
0162 std::streampos basic_file<Ch>::seek
0163 ( stream_offset off, BOOST_IOS::seekdir way,
0164 BOOST_IOS::openmode )
0165 { return iostreams::seek(pimpl_->file_, off, way); }
0166
0167 template<typename Ch>
0168 void basic_file<Ch>::open
0169 ( const std::string& path, BOOST_IOS::openmode mode,
0170 BOOST_IOS::openmode base_mode )
0171 {
0172 pimpl_.reset(new impl(path, mode | base_mode));
0173 }
0174
0175 template<typename Ch>
0176 bool basic_file<Ch>::is_open() const { return pimpl_->file_.is_open(); }
0177
0178 template<typename Ch>
0179 void basic_file<Ch>::close() { pimpl_->file_.close(); }
0180
0181 template<typename Ch>
0182 bool basic_file<Ch>::flush()
0183 { return pimpl_->file_.BOOST_IOSTREAMS_PUBSYNC() == 0; }
0184
0185
0186
0187 } }
0188
0189 #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC
0190
0191 #endif