Commit bb0ea2f8e7d8fffa575b291004e4426138c7bb1a
1 parent
87412eb0
Add qpdfjob_register_progress_reporter
Showing
8 changed files
with
53 additions
and
9 deletions
ChangeLog
| 1 | 2022-06-18 Jay Berkenbilt <ejb@ql.org> | 1 | 2022-06-18 Jay Berkenbilt <ejb@ql.org> |
| 2 | 2 | ||
| 3 | + * Add QPDFJob::registerProgressReporter, making it possible to | ||
| 4 | + override the progress reporter that is used when --progress (or | ||
| 5 | + the equivalent) is configured with QPDFJob. This is | ||
| 6 | + qpdfjob_register_progress_reporter in the C API. | ||
| 7 | + | ||
| 3 | * Add examples that show how to capture QPDFJob's output by | 8 | * Add examples that show how to capture QPDFJob's output by |
| 4 | configuring the default logger (qpdfjob-save-attachment.cc, | 9 | configuring the default logger (qpdfjob-save-attachment.cc, |
| 5 | qpdfjob-c-save-attachment.c). Fixes #691. | 10 | qpdfjob-c-save-attachment.c). Fixes #691. |
TODO
| @@ -14,9 +14,7 @@ Next: | @@ -14,9 +14,7 @@ Next: | ||
| 14 | 14 | ||
| 15 | Pending changes: | 15 | Pending changes: |
| 16 | 16 | ||
| 17 | -* Allow users to supply a custom progress reporter for QPDFJob. If one | ||
| 18 | - is provided, use it instead of creating one. Then expose to the C | ||
| 19 | - API. Consider also exposing a way to set a new logger and to get the | 17 | +* Consider also exposing a way to set a new logger and to get the |
| 20 | logger from QPDF and QPDFJob in the C API. | 18 | logger from QPDF and QPDFJob in the C API. |
| 21 | * Check about runpath in the linux-bin distribution. I think the | 19 | * Check about runpath in the linux-bin distribution. I think the |
| 22 | appimage build specifically is setting the runpath, which is | 20 | appimage build specifically is setting the runpath, which is |
include/qpdf/qpdfjob-c.h
| @@ -125,6 +125,16 @@ extern "C" { | @@ -125,6 +125,16 @@ extern "C" { | ||
| 125 | QPDF_DLL | 125 | QPDF_DLL |
| 126 | int qpdfjob_run(qpdfjob_handle j); | 126 | int qpdfjob_run(qpdfjob_handle j); |
| 127 | 127 | ||
| 128 | + /* Allow specification of a custom progress reporter. The progress | ||
| 129 | + * reporter is only used if progress is otherwise requested (with | ||
| 130 | + * the --progress option or "progress": "" in the JSON). | ||
| 131 | + */ | ||
| 132 | + QPDF_DLL | ||
| 133 | + void qpdfjob_register_progress_reporter( | ||
| 134 | + qpdfjob_handle j, | ||
| 135 | + void (*report_progress)(int percent, void* data), | ||
| 136 | + void* data); | ||
| 137 | + | ||
| 128 | #ifdef __cplusplus | 138 | #ifdef __cplusplus |
| 129 | } | 139 | } |
| 130 | #endif | 140 | #endif |
libqpdf/qpdfjob-c.cc
| @@ -120,3 +120,12 @@ int qpdfjob_run_from_json(char const* json) | @@ -120,3 +120,12 @@ int qpdfjob_run_from_json(char const* json) | ||
| 120 | }); | 120 | }); |
| 121 | } | 121 | } |
| 122 | 122 | ||
| 123 | +void | ||
| 124 | +qpdfjob_register_progress_reporter( | ||
| 125 | + qpdfjob_handle j, | ||
| 126 | + void (*report_progress)(int percent, void* data), | ||
| 127 | + void* data) | ||
| 128 | +{ | ||
| 129 | + j->j.registerProgressReporter( | ||
| 130 | + std::bind(report_progress, std::placeholders::_1, data)); | ||
| 131 | +} |
manual/release-notes.rst
| @@ -186,8 +186,8 @@ For a detailed list of changes, please see the file | @@ -186,8 +186,8 @@ For a detailed list of changes, please see the file | ||
| 186 | - Add new ``Pipeline`` type ``Pl_String`` to append to a | 186 | - Add new ``Pipeline`` type ``Pl_String`` to append to a |
| 187 | ``std::string``. | 187 | ``std::string``. |
| 188 | 188 | ||
| 189 | - - Add methods to QUtil for converting PDF timestamps and QPDFTime | ||
| 190 | - objects to ISO-8601 timestamps. | 189 | + - Add methods to ``QUtil`` for converting PDF timestamps and |
| 190 | + ``QPDFTime`` objects to ISO-8601 timestamps. | ||
| 191 | 191 | ||
| 192 | - Enhance JSON class to better support incrementally reading and | 192 | - Enhance JSON class to better support incrementally reading and |
| 193 | writing large amounts of data without having to keep everything | 193 | writing large amounts of data without having to keep everything |
| @@ -200,6 +200,12 @@ For a detailed list of changes, please see the file | @@ -200,6 +200,12 @@ For a detailed list of changes, please see the file | ||
| 200 | interface offers more flexibility than the old interface, which | 200 | interface offers more flexibility than the old interface, which |
| 201 | remains available. | 201 | remains available. |
| 202 | 202 | ||
| 203 | + - Add ``QPDFJob::registerProgressReporter`` and | ||
| 204 | + ``qpdfjob_register_progress_reporter`` to allow a custom | ||
| 205 | + progress reporter to be used with ``QPDFJob``. The ``QPDFJob`` | ||
| 206 | + object must be configured to report progress (via command-line | ||
| 207 | + argument or otherwise) for this to be used. | ||
| 208 | + | ||
| 203 | - Other changes | 209 | - Other changes |
| 204 | 210 | ||
| 205 | - In JSON v1 mode, the ``"objects"`` key now reflects the repaired | 211 | - In JSON v1 mode, the ``"objects"`` key now reflects the repaired |
qpdf/qpdfjob-ctest.c
| @@ -21,17 +21,29 @@ wide_test() | @@ -21,17 +21,29 @@ wide_test() | ||
| 21 | #endif // QPDF_NO_WCHAR_T | 21 | #endif // QPDF_NO_WCHAR_T |
| 22 | 22 | ||
| 23 | static void | 23 | static void |
| 24 | +custom_progress(int progress, void* data) | ||
| 25 | +{ | ||
| 26 | + printf("%s: write progress: %d%%\n", (char const*)data, progress); | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +static void | ||
| 24 | run_tests() | 30 | run_tests() |
| 25 | { | 31 | { |
| 26 | /* Be sure to use a different output file for each test. */ | 32 | /* Be sure to use a different output file for each test. */ |
| 33 | + qpdfjob_handle j = NULL; | ||
| 27 | 34 | ||
| 28 | - char const* argv[5]; | 35 | + char const* argv[6]; |
| 29 | argv[0] = "qpdfjob"; | 36 | argv[0] = "qpdfjob"; |
| 30 | argv[1] = "minimal.pdf"; | 37 | argv[1] = "minimal.pdf"; |
| 31 | argv[2] = "a.pdf"; | 38 | argv[2] = "a.pdf"; |
| 32 | argv[3] = "--deterministic-id"; | 39 | argv[3] = "--deterministic-id"; |
| 33 | - argv[4] = NULL; | ||
| 34 | - assert(qpdfjob_run_from_argv(argv) == 0); | 40 | + argv[4] = "--progress"; |
| 41 | + argv[5] = NULL; | ||
| 42 | + j = qpdfjob_init(); | ||
| 43 | + qpdfjob_register_progress_reporter(j, custom_progress, (void*)"potato"); | ||
| 44 | + assert(qpdfjob_initialize_from_argv(j, argv) == 0); | ||
| 45 | + assert(qpdfjob_run(j) == 0); | ||
| 46 | + qpdfjob_cleanup(&j); | ||
| 35 | printf("argv test passed\n"); | 47 | printf("argv test passed\n"); |
| 36 | 48 | ||
| 37 | assert(qpdfjob_run_from_json("{\n\ | 49 | assert(qpdfjob_run_from_json("{\n\ |
qpdf/qtest/qpdf/qpdfjob-ctest.out
| 1 | +potato: write progress: 0% | ||
| 2 | +....other write progress.... | ||
| 3 | +potato: write progress: 100% | ||
| 1 | argv test passed | 4 | argv test passed |
| 2 | json test passed | 5 | json test passed |
| 3 | WARNING: xref-with-short-size.pdf (xref stream, offset 16227): Cross-reference stream data has the wrong size; expected = 52; actual = 56 | 6 | WARNING: xref-with-short-size.pdf (xref stream, offset 16227): Cross-reference stream data has the wrong size; expected = 52; actual = 56 |
qpdf/qtest/qpdfjob.test
| @@ -100,7 +100,8 @@ $td->runtest("json output from job", | @@ -100,7 +100,8 @@ $td->runtest("json output from job", | ||
| 100 | $td->NORMALIZE_NEWLINES); | 100 | $td->NORMALIZE_NEWLINES); |
| 101 | 101 | ||
| 102 | $td->runtest("C job API", | 102 | $td->runtest("C job API", |
| 103 | - {$td->COMMAND => "qpdfjob-ctest"}, | 103 | + {$td->COMMAND => "qpdfjob-ctest", |
| 104 | + $td->FILTER => "perl filter-progress.pl"}, | ||
| 104 | {$td->FILE => "qpdfjob-ctest.out", $td->EXIT_STATUS => 0}, | 105 | {$td->FILE => "qpdfjob-ctest.out", $td->EXIT_STATUS => 0}, |
| 105 | $td->NORMALIZE_NEWLINES); | 106 | $td->NORMALIZE_NEWLINES); |
| 106 | foreach my $i (['a.pdf', 1], ['b.pdf', 2], ['c.pdf', 3]) | 107 | foreach my $i (['a.pdf', 1], ['b.pdf', 2], ['c.pdf', 3]) |