00001 #ifndef _theplu_yat_utility_stream_readirect_
00002 #define _theplu_yat_utility_stream_readirect_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <fstream>
00026 #include <istream>
00027 #include <ostream>
00028 #include <streambuf>
00029 #include <string>
00030
00031 namespace theplu {
00032 namespace yat {
00033 namespace utility {
00034
00053 template<class charT, class traits = std::char_traits<charT> >
00054 class BasicStreamRedirect
00055 {
00056 public:
00066 BasicStreamRedirect(std::basic_istream<charT, traits>& is1,
00067 std::basic_istream<charT, traits>& is2,
00068 bool active=true);
00069
00078 BasicStreamRedirect(std::basic_istream<charT, traits>& is,
00079 const std::string& file, bool active=true);
00080
00090 BasicStreamRedirect(std::basic_ostream<charT, traits>& os1,
00091 std::basic_ostream<charT, traits>& os2,
00092 bool active=true);
00093
00102 BasicStreamRedirect(std::basic_ostream<charT, traits>& os,
00103 const std::string& file, bool active=true);
00104
00120 ~BasicStreamRedirect(void);
00121
00122 private:
00123
00124 BasicStreamRedirect(const BasicStreamRedirect&);
00125 BasicStreamRedirect& operator=(const BasicStreamRedirect&);
00126
00127 void init(std::basic_ios<charT, traits>& ios1,
00128 std::basic_ios<charT, traits>& ios2,
00129 bool active);
00130
00131 std::basic_streambuf<charT, traits>* buf_;
00132 std::basic_ifstream<charT, traits>* ifs_;
00133 std::basic_ios<charT, traits>* ios_;
00134 std::basic_ofstream<charT, traits>* ofs_;
00135 };
00136
00140 typedef BasicStreamRedirect<char> StreamRedirect;
00141
00145 typedef BasicStreamRedirect<wchar_t> wStreamRedirect;
00146
00147
00148
00149
00150 template<class charT, class traits>
00151 BasicStreamRedirect<charT, traits>::
00152 BasicStreamRedirect(std::basic_istream<charT, traits>& is1,
00153 std::basic_istream<charT, traits>& is2, bool active)
00154 : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL)
00155 {
00156 init(is1, is2, active);
00157 }
00158
00159
00160 template<class charT, class traits>
00161 BasicStreamRedirect<charT, traits>::
00162 BasicStreamRedirect(std::basic_istream<charT, traits>& is,
00163 const std::string& file, bool active)
00164 : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL)
00165 {
00166 if (active) {
00167 ifs_ = new std::basic_ifstream<charT, traits>(file.c_str());
00168 init(is, *ofs_, active);
00169 }
00170 }
00171
00172
00173 template<class charT, class traits>
00174 BasicStreamRedirect<charT, traits>::
00175 BasicStreamRedirect(std::basic_ostream<charT, traits>& os1,
00176 std::basic_ostream<charT, traits>& os2, bool active)
00177 : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL)
00178 {
00179 init(os1, os2, active);
00180 }
00181
00182
00183 template<class charT, class traits>
00184 BasicStreamRedirect<charT, traits>::
00185 BasicStreamRedirect(std::basic_ostream<charT, traits>& os,
00186 const std::string& file, bool active)
00187 : buf_(NULL), ifs_(NULL), ios_(NULL), ofs_(NULL)
00188 {
00189 if (active) {
00190 ofs_ = new std::basic_ofstream<charT, traits>(file.c_str());
00191 init(os, *ofs_, active);
00192 }
00193 }
00194
00195
00196 template<class charT, class traits>
00197 BasicStreamRedirect<charT, traits>::
00198 ~BasicStreamRedirect(void)
00199 {
00200
00201 if (ios_) {
00202 ios_->rdbuf(buf_);
00203 }
00204 delete ifs_;
00205 delete ofs_;
00206 }
00207
00208
00209 template<class charT, class traits>
00210 void
00211 BasicStreamRedirect<charT, traits>::init(std::basic_ios<charT, traits>& s1,
00212 std::basic_ios<charT, traits>& s2,
00213 bool active)
00214 {
00215 if (active) {
00216 ios_ = &s1;
00217
00218 buf_ = s1.rdbuf();
00219
00220 s1.rdbuf(s2.rdbuf());
00221 }
00222 }
00223
00224 }}}
00225
00226 #endif