forked from artyom-beilis/cppcms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy_filter.cpp
More file actions
68 lines (62 loc) · 1.8 KB
/
copy_filter.cpp
File metadata and controls
68 lines (62 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#define CPPCMS_SOURCE
#include <cppcms/copy_filter.h>
namespace cppcms {
class copy_filter::tee_device : public booster::io_device{
public:
tee_device(std::ostream &output,std::list<std::string> &output_buffer) :
output_(output),
output_buffer_(&output_buffer)
{
}
size_t write(char const *pos,size_t len)
{
output_.write(pos,len);
if(!output_)
return 0;
output_buffer_->push_back(std::string());
output_buffer_->back().assign(pos,len);
return len;
}
private:
std::ostream &output_;
std::list<std::string> *output_buffer_;
};
struct copy_filter::data {};
copy_filter::copy_filter(std::ostream &output) :
output_(output),
real_output_stream_(output.rdbuf(©_buffer_)),
detached_(false)
{
std::auto_ptr<booster::io_device> device(new tee_device(real_output_stream_,data_));
copy_buffer_.device(device);
}
std::string copy_filter::detach()
{
output_ << std::flush;
copy_buffer_.reset_device();
detached_ = true;
output_.rdbuf(real_output_stream_.rdbuf(0));
size_t len = 0;
std::list<std::string>::iterator p;
for(p=data_.begin();p!=data_.end();p++)
len+=p->size();
std::string result;
result.reserve(len);
for(p=data_.begin();p!=data_.end();p++)
result+=*p;
return result;
}
copy_filter::~copy_filter()
{
if(!detached_) {
output_.rdbuf(real_output_stream_.rdbuf(0));
}
}
}