Changeset 2050


Ignore:
Timestamp:
Sep 6, 2009, 3:54:05 AM (11 years ago)
Author:
Peter
Message:

refs #521. added constructor taking ostream and string. It does not take char* (as ofstream does) because I don't see the point with that. Needed to split member variable ios_ into a ostream* and istream* because the two cases need separate treatment (ostream needs to be flushed in destructor).

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/stream_redirect_test.cc

    r2048 r2050  
    3535void test3(test::Suite&);
    3636void test_cout(test::Suite&);
     37void test_ofstream(test::Suite&);
    3738
    3839int main(int argc, char* argv[])
     
    4344  test2(suite);
    4445  test3(suite);
     46  test_ofstream(suite);
    4547
    4648  // keep this last in case it screws up cout
     
    103105
    104106
     107void 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
    105135void test_cout(test::Suite& suite)
    106136{
  • trunk/yat/utility/StreamRedirect.h

    r2048 r2050  
    2525#include "yat_assert.h"
    2626
     27#include <fstream>
    2728#include <istream>
    2829#include <ostream>
    2930#include <streambuf>
     31#include <string>
    3032
    3133namespace theplu {
     
    6466   
    6567    /**
    66        Constructor for istream
     68       Constructor for ostream
    6769
    6870       \a os1 is redirected to send its output to the buffer of \a os2
     
    7577                        std::basic_ostream<charT, traits>& os2,
    7678                        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);
    7791   
    7892    /**
     
    98112   
    99113    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   
    104116    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_;
    105121  };
    106122 
     
    122138  BasicStreamRedirect(std::basic_istream<charT, traits>& is1,
    123139                      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    }
    126146  }
    127147 
     
    131151  BasicStreamRedirect(std::basic_ostream<charT, traits>& os1,
    132152                      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    }
    135173  }
    136174 
     
    140178  ~BasicStreamRedirect(void)
    141179  {
     180    YAT_ASSERT(os_==NULL || is_==NULL);
     181
    142182    // 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_;
    146192  }
    147193 
     
    150196  void
    151197  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());
    166204  }
    167205 
Note: See TracChangeset for help on using the changeset viewer.