Commit 7f5d78c2d15565dd8a2357268f187045eb3ebc27
1 parent
75ea1971
improve C error handling interface
git-svn-id: svn+q:///qpdf/trunk@884 71b93d88-0707-0410-a8cf-f5a4172ac649
Showing
6 changed files
with
28 additions
and
13 deletions
examples/pdf-linearize.c
| ... | ... | @@ -24,7 +24,6 @@ int main(int argc, char* argv[]) |
| 24 | 24 | int warnings = 0; |
| 25 | 25 | int errors = 0; |
| 26 | 26 | char* p = 0; |
| 27 | - qpdf_error e = 0; | |
| 28 | 27 | |
| 29 | 28 | if ((p = strrchr(argv[0], '/')) != NULL) |
| 30 | 29 | { |
| ... | ... | @@ -57,11 +56,11 @@ int main(int argc, char* argv[]) |
| 57 | 56 | printf("warning: %s\n", |
| 58 | 57 | qpdf_get_error_full_text(qpdf, qpdf_next_warning(qpdf))); |
| 59 | 58 | } |
| 60 | - e = qpdf_get_error(qpdf); | |
| 61 | - if (e) | |
| 59 | + if (qpdf_has_error(qpdf)) | |
| 62 | 60 | { |
| 63 | 61 | errors = 1; |
| 64 | - printf("error: %s\n", qpdf_get_error_full_text(qpdf, e)); | |
| 62 | + printf("error: %s\n", | |
| 63 | + qpdf_get_error_full_text(qpdf, qpdf_get_error(qpdf))); | |
| 65 | 64 | } |
| 66 | 65 | qpdf_cleanup(&qpdf); |
| 67 | 66 | if (errors) | ... | ... |
include/qpdf/Constants.h
| ... | ... | @@ -17,8 +17,7 @@ |
| 17 | 17 | |
| 18 | 18 | enum qpdf_error_code_e |
| 19 | 19 | { |
| 20 | - qpdf_e_success = 0, | |
| 21 | - qpdf_e_internal, /* logic/programming error -- indicates bug */ | |
| 20 | + qpdf_e_internal = 1, /* logic/programming error -- indicates bug */ | |
| 22 | 21 | qpdf_e_system, /* I/O error, memory error, etc. */ |
| 23 | 22 | qpdf_e_unsupported, /* PDF feature not (yet) supported by qpdf */ |
| 24 | 23 | qpdf_e_password, /* incorrect password for encrypted file */ | ... | ... |
include/qpdf/qpdf-c.h
| ... | ... | @@ -106,9 +106,18 @@ extern "C" { |
| 106 | 106 | |
| 107 | 107 | /* ERROR REPORTING */ |
| 108 | 108 | |
| 109 | + /* Returns 1 if there is an error condition. The error condition | |
| 110 | + * can be retrieved by a single call to qpdf_get_error. | |
| 111 | + */ | |
| 112 | + QPDF_DLL | |
| 113 | + QPDF_BOOL qpdf_has_error(qpdf_data qpdf); | |
| 114 | + | |
| 109 | 115 | /* Returns the error condition, if any. The return value is a |
| 110 | - * pointer to data that will become invalid the next time an error | |
| 111 | - * occurs or after this function is called gain. | |
| 116 | + * pointer to data that will become invalid after the next call to | |
| 117 | + * this function, qpdf_next_warning, or qpdf_destroy. After this | |
| 118 | + * function is called, qpdf_has_error will return QPDF_FALSE until | |
| 119 | + * the next error condition occurs. If there is no error | |
| 120 | + * condition, this function returns a null pointer. | |
| 112 | 121 | */ |
| 113 | 122 | QPDF_DLL |
| 114 | 123 | qpdf_error qpdf_get_error(qpdf_data qpdf); |
| ... | ... | @@ -133,7 +142,8 @@ extern "C" { |
| 133 | 142 | char const* qpdf_get_error_full_text(qpdf_data q, qpdf_error e); |
| 134 | 143 | |
| 135 | 144 | /* Use these functions to extract individual fields from the |
| 136 | - * error; see QPDFExc.hh for details. */ | |
| 145 | + * error; see QPDFExc.hh for details. It is invalid for e to be a | |
| 146 | + * null pointer for any of these calls. */ | |
| 137 | 147 | QPDF_DLL |
| 138 | 148 | enum qpdf_error_code_e qpdf_get_error_code(qpdf_data q, qpdf_error e); |
| 139 | 149 | QPDF_DLL | ... | ... |
libqpdf/qpdf-c.cc
| ... | ... | @@ -128,13 +128,19 @@ QPDF_BOOL qpdf_more_warnings(qpdf_data qpdf) |
| 128 | 128 | } |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | +QPDF_BOOL qpdf_has_error(qpdf_data qpdf) | |
| 132 | +{ | |
| 133 | + QTC::TC("qpdf", "qpdf-c called qpdf_has_error"); | |
| 134 | + return (qpdf->error.getPointer() ? QPDF_TRUE : QPDF_FALSE); | |
| 135 | +} | |
| 136 | + | |
| 131 | 137 | qpdf_error qpdf_get_error(qpdf_data qpdf) |
| 132 | 138 | { |
| 133 | 139 | if (qpdf->error.getPointer()) |
| 134 | 140 | { |
| 135 | 141 | qpdf->tmp_error.exc = qpdf->error; |
| 136 | 142 | qpdf->error = 0; |
| 137 | - QTC::TC("qpdf", "qpdf-c qpdf_next_error returned error"); | |
| 143 | + QTC::TC("qpdf", "qpdf-c qpdf_get_error returned error"); | |
| 138 | 144 | return &qpdf->tmp_error; |
| 139 | 145 | } |
| 140 | 146 | else | ... | ... |
qpdf/qpdf-ctest.c
| ... | ... | @@ -18,9 +18,9 @@ static void report_errors() |
| 18 | 18 | printf(" pos : %ld\n", qpdf_get_error_file_position(qpdf, e)); |
| 19 | 19 | printf(" text: %s\n", qpdf_get_error_message_detail(qpdf, e)); |
| 20 | 20 | } |
| 21 | - e = qpdf_get_error(qpdf); | |
| 22 | - if (e) | |
| 21 | + if (qpdf_has_error(qpdf)) | |
| 23 | 22 | { |
| 23 | + e = qpdf_get_error(qpdf); | |
| 24 | 24 | printf("error: %s\n", qpdf_get_error_full_text(qpdf, e)); |
| 25 | 25 | printf(" code: %d\n", qpdf_get_error_code(qpdf, e)); |
| 26 | 26 | printf(" file: %s\n", qpdf_get_error_filename(qpdf, e)); | ... | ... |
qpdf/qpdf.testcov
| ... | ... | @@ -121,7 +121,7 @@ QPDF_Stream ignore non-dictionary DecodeParms 0 |
| 121 | 121 | qpdf-c called qpdf_init 0 |
| 122 | 122 | qpdf-c called qpdf_cleanup 0 |
| 123 | 123 | qpdf-c called qpdf_more_warnings 0 |
| 124 | -qpdf-c qpdf_next_error returned error 0 | |
| 124 | +qpdf-c qpdf_get_error returned error 0 | |
| 125 | 125 | qpdf-c qpdf_next_warning returned warning 0 |
| 126 | 126 | qpdf-c called qpdf_set_suppress_warnings 0 |
| 127 | 127 | qpdf-c called qpdf_set_ignore_xref_streams 0 |
| ... | ... | @@ -172,3 +172,4 @@ qpdf-c called qpdf_set_static_aes_IV 0 |
| 172 | 172 | QPDF_encryption stream crypt filter 0 |
| 173 | 173 | QPDF ERR object stream with wrong type 0 |
| 174 | 174 | QPDF object gone after xref reconstruction 0 |
| 175 | +qpdf-c called qpdf_has_error 0 | ... | ... |