Commit 9cf9620131a1ea9fe791b50c7b739a8a3115c30e
1 parent
a5cab082
fix-qdf: replace hard-coded std::cout with member variable
Showing
1 changed file
with
29 additions
and
26 deletions
qpdf/fix-qdf.cc
| ... | ... | @@ -20,7 +20,8 @@ usage() |
| 20 | 20 | class QdfFixer |
| 21 | 21 | { |
| 22 | 22 | public: |
| 23 | - QdfFixer(std::string const& filename); | |
| 23 | + QdfFixer(std::string const& filename, std::ostream& out); | |
| 24 | + ~QdfFixer() = default; | |
| 24 | 25 | void processLines(std::string const& input); |
| 25 | 26 | |
| 26 | 27 | private: |
| ... | ... | @@ -31,6 +32,7 @@ class QdfFixer |
| 31 | 32 | void writeBinary(unsigned long long val, size_t bytes); |
| 32 | 33 | |
| 33 | 34 | std::string filename; |
| 35 | + std::ostream& out; | |
| 34 | 36 | enum { |
| 35 | 37 | st_top, |
| 36 | 38 | st_in_obj, |
| ... | ... | @@ -67,8 +69,9 @@ class QdfFixer |
| 67 | 69 | std::string ostream_extends; |
| 68 | 70 | }; |
| 69 | 71 | |
| 70 | -QdfFixer::QdfFixer(std::string const& filename) : | |
| 71 | - filename(filename) | |
| 72 | +QdfFixer::QdfFixer(std::string const& filename, std::ostream& out) : | |
| 73 | + filename(filename), | |
| 74 | + out(out) | |
| 72 | 75 | { |
| 73 | 76 | } |
| 74 | 77 | |
| ... | ... | @@ -131,9 +134,9 @@ QdfFixer::processLines(std::string const& input) |
| 131 | 134 | xref_offset = last_offset; |
| 132 | 135 | state = st_at_xref; |
| 133 | 136 | } |
| 134 | - std::cout << line; | |
| 137 | + out << line; | |
| 135 | 138 | } else if (state == st_in_obj) { |
| 136 | - std::cout << line; | |
| 139 | + out << line; | |
| 137 | 140 | if (line.compare("stream\n"sv) == 0) { |
| 138 | 141 | state = st_in_stream; |
| 139 | 142 | stream_start = offset; |
| ... | ... | @@ -166,8 +169,8 @@ QdfFixer::processLines(std::string const& input) |
| 166 | 169 | auto esize = 1 + xref_f1_nbytes + xref_f2_nbytes; |
| 167 | 170 | xref_size = 1 + xref.size(); |
| 168 | 171 | auto length = xref_size * esize; |
| 169 | - std::cout << " /Length " << length << "\n" | |
| 170 | - << " /W [ 1 " << xref_f1_nbytes << " " << xref_f2_nbytes << " ]\n"; | |
| 172 | + out << " /Length " << length << "\n" | |
| 173 | + << " /W [ 1 " << xref_f1_nbytes << " " << xref_f2_nbytes << " ]\n"; | |
| 171 | 174 | state = st_in_xref_stream_dict; |
| 172 | 175 | } |
| 173 | 176 | } else if (state == st_in_ostream_dict) { |
| ... | ... | @@ -209,10 +212,10 @@ QdfFixer::processLines(std::string const& input) |
| 209 | 212 | if ((line.find("/Length"sv) != line.npos) || (line.find("/W"sv) != line.npos)) { |
| 210 | 213 | // already printed |
| 211 | 214 | } else if (line.find("/Size"sv) != line.npos) { |
| 212 | - auto xref_size = 1 + xref.size(); | |
| 213 | - std::cout << " /Size " << xref_size << "\n"; | |
| 215 | + auto size = 1 + xref.size(); | |
| 216 | + out << " /Size " << size << "\n"; | |
| 214 | 217 | } else { |
| 215 | - std::cout << line; | |
| 218 | + out << line; | |
| 216 | 219 | } |
| 217 | 220 | if (line.compare("stream\n"sv) == 0) { |
| 218 | 221 | writeBinary(0, 1); |
| ... | ... | @@ -232,9 +235,9 @@ QdfFixer::processLines(std::string const& input) |
| 232 | 235 | writeBinary(f1, xref_f1_nbytes); |
| 233 | 236 | writeBinary(f2, xref_f2_nbytes); |
| 234 | 237 | } |
| 235 | - std::cout << "\nendstream\nendobj\n\n" | |
| 236 | - << "startxref\n" | |
| 237 | - << xref_offset << "\n%%EOF\n"; | |
| 238 | + out << "\nendstream\nendobj\n\n" | |
| 239 | + << "startxref\n" | |
| 240 | + << xref_offset << "\n%%EOF\n"; | |
| 238 | 241 | state = st_done; |
| 239 | 242 | } |
| 240 | 243 | } else if (state == st_in_stream) { |
| ... | ... | @@ -242,7 +245,7 @@ QdfFixer::processLines(std::string const& input) |
| 242 | 245 | stream_length = QIntC::to_size(last_offset - stream_start); |
| 243 | 246 | state = st_after_stream; |
| 244 | 247 | } |
| 245 | - std::cout << line; | |
| 248 | + out << line; | |
| 246 | 249 | } else if (state == st_after_stream) { |
| 247 | 250 | if (line.compare("%QDF: ignore_newline\n"sv) == 0) { |
| 248 | 251 | if (stream_length > 0) { |
| ... | ... | @@ -252,7 +255,7 @@ QdfFixer::processLines(std::string const& input) |
| 252 | 255 | checkObjId(m[1].str()); |
| 253 | 256 | state = st_in_length; |
| 254 | 257 | } |
| 255 | - std::cout << line; | |
| 258 | + out << line; | |
| 256 | 259 | } else if (state == st_in_length) { |
| 257 | 260 | if (!matches(re_num)) { |
| 258 | 261 | fatal(filename + ":" + std::to_string(lineno) + ": expected integer"); |
| ... | ... | @@ -260,29 +263,29 @@ QdfFixer::processLines(std::string const& input) |
| 260 | 263 | std::string new_length = std::to_string(stream_length) + "\n"; |
| 261 | 264 | offset -= QIntC::to_offset(line.length()); |
| 262 | 265 | offset += QIntC::to_offset(new_length.length()); |
| 263 | - std::cout << new_length; | |
| 266 | + out << new_length; | |
| 264 | 267 | state = st_top; |
| 265 | 268 | } else if (state == st_at_xref) { |
| 266 | 269 | auto n = xref.size(); |
| 267 | - std::cout << "0 " << 1 + n << "\n0000000000 65535 f \n"; | |
| 270 | + out << "0 " << 1 + n << "\n0000000000 65535 f \n"; | |
| 268 | 271 | for (auto const& e: xref) { |
| 269 | - std::cout << QUtil::int_to_string(e.getOffset(), 10) << " 00000 n \n"; | |
| 272 | + out << QUtil::int_to_string(e.getOffset(), 10) << " 00000 n \n"; | |
| 270 | 273 | } |
| 271 | 274 | state = st_before_trailer; |
| 272 | 275 | } else if (state == st_before_trailer) { |
| 273 | 276 | if (line.compare("trailer <<\n"sv) == 0) { |
| 274 | - std::cout << line; | |
| 277 | + out << line; | |
| 275 | 278 | state = st_in_trailer; |
| 276 | 279 | } |
| 277 | 280 | // no output |
| 278 | 281 | } else if (state == st_in_trailer) { |
| 279 | 282 | if (matches(re_size_n)) { |
| 280 | - std::cout << " /Size " << 1 + xref.size() << "\n"; | |
| 283 | + out << " /Size " << 1 + xref.size() << "\n"; | |
| 281 | 284 | } else { |
| 282 | - std::cout << line; | |
| 285 | + out << line; | |
| 283 | 286 | } |
| 284 | 287 | if (line.compare(">>\n"sv) == 0) { |
| 285 | - std::cout << "startxref\n" << xref_offset << "\n%%EOF\n"; | |
| 288 | + out << "startxref\n" << xref_offset << "\n%%EOF\n"; | |
| 286 | 289 | state = st_done; |
| 287 | 290 | } |
| 288 | 291 | } else if (state == st_done) { |
| ... | ... | @@ -332,9 +335,9 @@ QdfFixer::writeOstream() |
| 332 | 335 | } |
| 333 | 336 | dict_data += ">>\n"; |
| 334 | 337 | offset_adjust += QIntC::to_offset(dict_data.length()); |
| 335 | - std::cout << dict_data << "stream\n" << offsets; | |
| 338 | + out << dict_data << "stream\n" << offsets; | |
| 336 | 339 | for (auto const& o: ostream) { |
| 337 | - std::cout << o; | |
| 340 | + out << o; | |
| 338 | 341 | } |
| 339 | 342 | |
| 340 | 343 | for (auto const& o: ostream_discarded) { |
| ... | ... | @@ -361,7 +364,7 @@ QdfFixer::writeBinary(unsigned long long val, size_t bytes) |
| 361 | 364 | data[i - 1] = static_cast<char>(val & 0xff); // i.e. val % 256 |
| 362 | 365 | val >>= 8; // i.e. val = val / 256 |
| 363 | 366 | } |
| 364 | - std::cout << data; | |
| 367 | + out << data; | |
| 365 | 368 | } |
| 366 | 369 | |
| 367 | 370 | static int |
| ... | ... | @@ -389,7 +392,7 @@ realmain(int argc, char* argv[]) |
| 389 | 392 | input = QUtil::read_file_into_string(filename); |
| 390 | 393 | } |
| 391 | 394 | QUtil::binary_stdout(); |
| 392 | - QdfFixer qf(filename); | |
| 395 | + QdfFixer qf(filename, std::cout); | |
| 393 | 396 | qf.processLines(input); |
| 394 | 397 | return 0; |
| 395 | 398 | } | ... | ... |