Pipeline_private.hh
3.71 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifndef PIPELINE_PRIVATE_HH
#define PIPELINE_PRIVATE_HH
#include <qpdf/Pipeline.hh>
#include <qpdf/Pl_Flate.hh>
#include <qpdf/Types.h>
namespace qpdf::pl
{
class String final: public Pipeline
{
public:
String(char const* identifier, Pipeline*, std::string& str) :
Pipeline(identifier, nullptr),
str(str)
{
}
String(std::string& str) :
Pipeline("", nullptr),
str(str)
{
}
~String() final = default;
void
write(unsigned char const* buf, size_t len) final
{
if (len) {
str.append(reinterpret_cast<char const*>(buf), len);
}
}
void
finish() final
{
}
private:
std::string& str;
};
class Count final: public Pipeline
{
public:
// Count the number of characters written. If 'next' is not set, the content written will be
// discarded.
Count(unsigned long id, Pipeline* next = nullptr) :
Pipeline("", next),
id_(id),
pass_immediately_to_next(next)
{
}
// Write to 'str'. If 'next' is set, 'str' will be written to 'next' when 'finish' is
// called.
Count(unsigned long id, std::string& str, std::unique_ptr<Pipeline> link = nullptr) :
Pipeline("", link.get()),
str(&str),
link(std::move(link)),
id_(id)
{
}
~Count() final = default;
void
write(unsigned char const* buf, size_t len) final
{
if (len) {
Count::write(std::string_view(reinterpret_cast<char const*>(buf), len));
}
}
void
write(std::string_view sv)
{
if (sv.size()) {
if (str) {
str->append(sv);
return;
}
count += static_cast<qpdf_offset_t>(sv.size());
if (pass_immediately_to_next) {
next()->write(reinterpret_cast<char const*>(sv.data()), sv.size());
}
}
}
void
write(size_t len, char c)
{
if (len) {
if (str) {
str->append(len, c);
return;
}
count += static_cast<qpdf_offset_t>(len);
if (pass_immediately_to_next) {
next()->writeString(std::string(len, c));
}
}
}
void
finish() final
{
if (next()) {
if (!pass_immediately_to_next) {
next()->write(reinterpret_cast<unsigned char const*>(str->data()), str->size());
}
next()->finish();
}
}
qpdf_offset_t
getCount() const
{
return str ? static_cast<qpdf_offset_t>(str->size()) : count;
}
unsigned long long
id() const
{
return id_;
}
private:
qpdf_offset_t count{0};
std::string* str{nullptr};
std::unique_ptr<Pipeline> link{nullptr};
unsigned long id_{0};
bool pass_immediately_to_next{false};
};
template <typename P, typename... Args>
std::string
pipe(std::string_view data, Args&&... args)
{
std::string result;
String s("", nullptr, result);
P pl("", &s, std::forward<Args>(args)...);
pl.write(reinterpret_cast<unsigned char const*>(data.data()), data.size());
pl.finish();
return result;
}
} // namespace qpdf::pl
#endif // PIPELINE_PRIVATE_HH