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