1 // boost/filesystem/fstream.hpp ------------------------------------------------------// 2 3 // Copyright Beman Dawes 2002 4 5 // Distributed under the Boost Software License, Version 1.0. 6 // See http://www.boost.org/LICENSE_1_0.txt 7 8 // Library home page: http://www.boost.org/libs/filesystem 9 10 //--------------------------------------------------------------------------------------// 11 12 #ifndef BOOST_FILESYSTEM3_FSTREAM_HPP 13 #define BOOST_FILESYSTEM3_FSTREAM_HPP 14 15 #include <boost/config.hpp> 16 17 # if defined( BOOST_NO_STD_WSTRING ) 18 # error Configuration not supported: Boost.Filesystem V3 and later requires std::wstring support 19 # endif 20 21 #include <boost/filesystem/path.hpp> 22 #include <iosfwd> 23 #include <fstream> 24 25 #include <boost/config/abi_prefix.hpp> // must be the last #include 26 27 // on Windows, except for standard libaries known to have wchar_t overloads for 28 // file stream I/O, use path::string() to get a narrow character c_str() 29 #if defined(BOOST_WINDOWS_API) \ 30 && (!defined(_CPPLIB_VER) || _CPPLIB_VER < 405 || defined(_STLPORT_VERSION)) 31 // !Dinkumware || early Dinkumware || STLPort masquerading as Dinkumware 32 # define BOOST_FILESYSTEM_C_STR string().c_str() // use narrow, since wide not available 33 #else // use the native c_str, which will be narrow on POSIX, wide on Windows 34 # define BOOST_FILESYSTEM_C_STR c_str() 35 #endif 36 37 namespace boost 38 { 39 namespace filesystem 40 { 41 42 //--------------------------------------------------------------------------------------// 43 // basic_filebuf // 44 //--------------------------------------------------------------------------------------// 45 46 template < class charT, class traits = std::char_traits<charT> > 47 class basic_filebuf : public std::basic_filebuf<charT,traits> 48 { 49 private: // disallow copying 50 basic_filebuf(const basic_filebuf&); 51 const basic_filebuf& operator=(const basic_filebuf&); 52 53 public: 54 basic_filebuf() {} 55 virtual ~basic_filebuf() {} 56 57 basic_filebuf<charT,traits>* 58 open(const path& p, std::ios_base::openmode mode) 59 { 60 return std::basic_filebuf<charT,traits>::open(p.BOOST_FILESYSTEM_C_STR, mode) 61 ? this : 0; 62 } 63 }; 64 65 //--------------------------------------------------------------------------------------// 66 // basic_ifstream // 67 //--------------------------------------------------------------------------------------// 68 69 template < class charT, class traits = std::char_traits<charT> > 70 class basic_ifstream : public std::basic_ifstream<charT,traits> 71 { 72 private: // disallow copying 73 basic_ifstream(const basic_ifstream&); 74 const basic_ifstream& operator=(const basic_ifstream&); 75 76 public: 77 basic_ifstream() {} 78 79 // use two signatures, rather than one signature with default second 80 // argument, to workaround VC++ 7.1 bug (ID VSWhidbey 38416) 81 82 explicit basic_ifstream(const path& p) 83 : std::basic_ifstream<charT,traits>(p.BOOST_FILESYSTEM_C_STR, std::ios_base::in) {} 84 85 basic_ifstream(const path& p, std::ios_base::openmode mode) 86 : std::basic_ifstream<charT,traits>(p.BOOST_FILESYSTEM_C_STR, mode) {} 87 88 void open(const path& p) 89 { std::basic_ifstream<charT,traits>::open(p.BOOST_FILESYSTEM_C_STR, std::ios_base::in); } 90 91 void open(const path& p, std::ios_base::openmode mode) 92 { std::basic_ifstream<charT,traits>::open(p.BOOST_FILESYSTEM_C_STR, mode); } 93 94 virtual ~basic_ifstream() {} 95 }; 96 97 //--------------------------------------------------------------------------------------// 98 // basic_ofstream // 99 //--------------------------------------------------------------------------------------// 100 101 template < class charT, class traits = std::char_traits<charT> > 102 class basic_ofstream : public std::basic_ofstream<charT,traits> 103 { 104 private: // disallow copying 105 basic_ofstream(const basic_ofstream&); 106 const basic_ofstream& operator=(const basic_ofstream&); 107 108 public: 109 basic_ofstream() {} 110 111 // use two signatures, rather than one signature with default second 112 // argument, to workaround VC++ 7.1 bug (ID VSWhidbey 38416) 113 114 explicit basic_ofstream(const path& p) 115 : std::basic_ofstream<charT,traits>(p.BOOST_FILESYSTEM_C_STR, std::ios_base::out) {} 116 117 basic_ofstream(const path& p, std::ios_base::openmode mode) 118 : std::basic_ofstream<charT,traits>(p.BOOST_FILESYSTEM_C_STR, mode) {} 119 120 void open(const path& p) 121 { std::basic_ofstream<charT,traits>::open(p.BOOST_FILESYSTEM_C_STR, std::ios_base::out); } 122 123 void open(const path& p, std::ios_base::openmode mode) 124 { std::basic_ofstream<charT,traits>::open(p.BOOST_FILESYSTEM_C_STR, mode); } 125 126 virtual ~basic_ofstream() {} 127 }; 128 129 //--------------------------------------------------------------------------------------// 130 // basic_fstream // 131 //--------------------------------------------------------------------------------------// 132 133 template < class charT, class traits = std::char_traits<charT> > 134 class basic_fstream : public std::basic_fstream<charT,traits> 135 { 136 private: // disallow copying 137 basic_fstream(const basic_fstream&); 138 const basic_fstream & operator=(const basic_fstream&); 139 140 public: 141 basic_fstream() {} 142 143 // use two signatures, rather than one signature with default second 144 // argument, to workaround VC++ 7.1 bug (ID VSWhidbey 38416) 145 146 explicit basic_fstream(const path& p) 147 : std::basic_fstream<charT,traits>(p.BOOST_FILESYSTEM_C_STR, 148 std::ios_base::in | std::ios_base::out) {} 149 150 basic_fstream(const path& p, std::ios_base::openmode mode) 151 : std::basic_fstream<charT,traits>(p.BOOST_FILESYSTEM_C_STR, mode) {} 152 153 void open(const path& p) 154 { std::basic_fstream<charT,traits>::open(p.BOOST_FILESYSTEM_C_STR, 155 std::ios_base::in | std::ios_base::out); } 156 157 void open(const path& p, std::ios_base::openmode mode) 158 { std::basic_fstream<charT,traits>::open(p.BOOST_FILESYSTEM_C_STR, mode); } 159 160 virtual ~basic_fstream() {} 161 162 }; 163 164 //--------------------------------------------------------------------------------------// 165 // typedefs // 166 //--------------------------------------------------------------------------------------// 167 168 typedef basic_filebuf<char> filebuf; 169 typedef basic_ifstream<char> ifstream; 170 typedef basic_ofstream<char> ofstream; 171 typedef basic_fstream<char> fstream; 172 173 typedef basic_filebuf<wchar_t> wfilebuf; 174 typedef basic_ifstream<wchar_t> wifstream; 175 typedef basic_ofstream<wchar_t> wofstream; 176 typedef basic_fstream<wchar_t> wfstream; 177 178 } // namespace filesystem 179 } // namespace boost 180 181 #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas 182 #endif // BOOST_FILESYSTEM3_FSTREAM_HPP 183