Changeset 2050
- Timestamp:
- Sep 6, 2009, 3:54:05 AM (13 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/stream_redirect_test.cc
r2048 r2050 35 35 void test3(test::Suite&); 36 36 void test_cout(test::Suite&); 37 void test_ofstream(test::Suite&); 37 38 38 39 int main(int argc, char* argv[]) … … 43 44 test2(suite); 44 45 test3(suite); 46 test_ofstream(suite); 45 47 46 48 // keep this last in case it screws up cout … … 103 105 104 106 107 void test_ofstream(test::Suite& suite) 108 { 109 suite.out() << "testing redirect to ofstream\n"; 110 std::ostringstream my_out; 111 std::string msg("some message"); 112 std::string tmp_file("tmp.txt"); 113 { 114 StreamRedirect sr(my_out, tmp_file.c_str()); 115 my_out << msg; 116 } 117 if (my_out.str().size()) { 118 suite.err() << "my_out contains: '" << my_out.str() 119 << "'\n expected empty\n"; 120 suite.add(false); 121 } 122 std::ifstream is(tmp_file.c_str()); 123 std::string line; 124 getline(is, line); 125 if (line!=msg) { 126 suite.add(false); 127 suite.err() << "expected to find '" << msg << "' in file `" 128 << tmp_file << "'\nfound: '" << line << "'\n"; 129 } 130 // cleaning up 131 unlink(tmp_file.c_str()); 132 } 133 134 105 135 void test_cout(test::Suite& suite) 106 136 { -
trunk/yat/utility/StreamRedirect.h
r2048 r2050 25 25 #include "yat_assert.h" 26 26 27 #include <fstream> 27 28 #include <istream> 28 29 #include <ostream> 29 30 #include <streambuf> 31 #include <string> 30 32 31 33 namespace theplu { … … 64 66 65 67 /** 66 Constructor for istream68 Constructor for ostream 67 69 68 70 \a os1 is redirected to send its output to the buffer of \a os2 … … 75 77 std::basic_ostream<charT, traits>& os2, 76 78 bool active=true); 79 80 /** 81 Constructor for ostream 82 83 \a os1 is redirected to send its output to the buffer of \a os2 84 (rather than its own buffer). A reference to \a os1 and its 85 buffer is stored, so \a os1 can be restored in the destructor. 86 87 If \a active is false, the class does nothing. 88 */ 89 BasicStreamRedirect(std::basic_ostream<charT, traits>& os1, 90 const std::string& file, bool active=true); 77 91 78 92 /** … … 98 112 99 113 void init(std::basic_ios<charT, traits>& ios1, 100 std::basic_ios<charT, traits>& ios2, 101 bool active); 102 103 std::basic_ios<charT, traits>* ios_; 114 std::basic_ios<charT, traits>& ios2); 115 104 116 std::basic_streambuf<charT, traits>* buf_; 117 std::basic_ifstream<charT, traits>* ifs_; 118 std::basic_istream<charT, traits>* is_; 119 std::basic_ofstream<charT, traits>* ofs_; 120 std::basic_ostream<charT, traits>* os_; 105 121 }; 106 122 … … 122 138 BasicStreamRedirect(std::basic_istream<charT, traits>& is1, 123 139 std::basic_istream<charT, traits>& is2, bool active) 124 { 125 init(is1, is2, active); 140 : buf_(NULL), ifs_(NULL), is_(NULL), ofs_(NULL), os_(NULL) 141 { 142 if (active) { 143 is_ = &is1; 144 init(is1, is2); 145 } 126 146 } 127 147 … … 131 151 BasicStreamRedirect(std::basic_ostream<charT, traits>& os1, 132 152 std::basic_ostream<charT, traits>& os2, bool active) 133 { 134 init(os1, os2, active); 153 : buf_(NULL), ifs_(NULL), is_(NULL), ofs_(NULL), os_(NULL) 154 { 155 if (active) { 156 os_ = &os1; 157 init(os1, os2); 158 } 159 } 160 161 162 template<class charT, class traits> 163 BasicStreamRedirect<charT, traits>:: 164 BasicStreamRedirect(std::basic_ostream<charT, traits>& os1, 165 const std::string& file, bool active) 166 : buf_(NULL), ifs_(NULL), is_(NULL), ofs_(NULL), os_(NULL) 167 { 168 if (active) { 169 ofs_ = new std::basic_ofstream<charT, traits>(file.c_str()); 170 os_ = &os1; 171 init(os1, *ofs_); 172 } 135 173 } 136 174 … … 140 178 ~BasicStreamRedirect(void) 141 179 { 180 YAT_ASSERT(os_==NULL || is_==NULL); 181 142 182 // only restore stream if active is true 143 if (ios_) { 144 ios_->rdbuf(buf_); 145 } 183 if (os_) { 184 std::flush(*os_); 185 os_->rdbuf(buf_); 186 } 187 else if (is_) { 188 is_->rdbuf(buf_); 189 } 190 delete ifs_; 191 delete ofs_; 146 192 } 147 193 … … 150 196 void 151 197 BasicStreamRedirect<charT, traits>::init(std::basic_ios<charT, traits>& s1, 152 std::basic_ios<charT, traits>& s2, 153 bool active) 154 { 155 if (active) { 156 ios_ = &s1; 157 // save the buffer 158 buf_ = ios_->rdbuf(); 159 // redirect os1 to os2 160 ios_->rdbuf(s2.rdbuf()); 161 } 162 else { 163 ios_ = NULL; 164 buf_ = NULL; 165 } 198 std::basic_ios<charT, traits>& s2) 199 { 200 // save the buffer 201 buf_ = s1.rdbuf(); 202 // redirect os1 to os2 203 s1.rdbuf(s2.rdbuf()); 166 204 } 167 205
Note: See TracChangeset
for help on using the changeset viewer.