From 7f5d78c2d15565dd8a2357268f187045eb3ebc27 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Fri, 23 Oct 2009 15:27:30 +0000 Subject: improve C error handling interface git-svn-id: svn+q:///qpdf/trunk@884 71b93d88-0707-0410-a8cf-f5a4172ac649 --- examples/pdf-linearize.c | 7 +++---- include/qpdf/Constants.h | 3 +-- include/qpdf/qpdf-c.h | 16 +++++++++++++--- libqpdf/qpdf-c.cc | 8 +++++++- qpdf/qpdf-ctest.c | 4 ++-- qpdf/qpdf.testcov | 3 ++- 6 files changed, 28 insertions(+), 13 deletions(-) diff --git a/examples/pdf-linearize.c b/examples/pdf-linearize.c index d9da913e..78e2483b 100644 --- a/examples/pdf-linearize.c +++ b/examples/pdf-linearize.c @@ -24,7 +24,6 @@ int main(int argc, char* argv[]) int warnings = 0; int errors = 0; char* p = 0; - qpdf_error e = 0; if ((p = strrchr(argv[0], '/')) != NULL) { @@ -57,11 +56,11 @@ int main(int argc, char* argv[]) printf("warning: %s\n", qpdf_get_error_full_text(qpdf, qpdf_next_warning(qpdf))); } - e = qpdf_get_error(qpdf); - if (e) + if (qpdf_has_error(qpdf)) { errors = 1; - printf("error: %s\n", qpdf_get_error_full_text(qpdf, e)); + printf("error: %s\n", + qpdf_get_error_full_text(qpdf, qpdf_get_error(qpdf))); } qpdf_cleanup(&qpdf); if (errors) diff --git a/include/qpdf/Constants.h b/include/qpdf/Constants.h index eb872788..d74cace3 100644 --- a/include/qpdf/Constants.h +++ b/include/qpdf/Constants.h @@ -17,8 +17,7 @@ enum qpdf_error_code_e { - qpdf_e_success = 0, - qpdf_e_internal, /* logic/programming error -- indicates bug */ + qpdf_e_internal = 1, /* logic/programming error -- indicates bug */ qpdf_e_system, /* I/O error, memory error, etc. */ qpdf_e_unsupported, /* PDF feature not (yet) supported by qpdf */ qpdf_e_password, /* incorrect password for encrypted file */ diff --git a/include/qpdf/qpdf-c.h b/include/qpdf/qpdf-c.h index 0980f1f7..492a72ba 100644 --- a/include/qpdf/qpdf-c.h +++ b/include/qpdf/qpdf-c.h @@ -106,9 +106,18 @@ extern "C" { /* ERROR REPORTING */ + /* Returns 1 if there is an error condition. The error condition + * can be retrieved by a single call to qpdf_get_error. + */ + QPDF_DLL + QPDF_BOOL qpdf_has_error(qpdf_data qpdf); + /* Returns the error condition, if any. The return value is a - * pointer to data that will become invalid the next time an error - * occurs or after this function is called gain. + * pointer to data that will become invalid after the next call to + * this function, qpdf_next_warning, or qpdf_destroy. After this + * function is called, qpdf_has_error will return QPDF_FALSE until + * the next error condition occurs. If there is no error + * condition, this function returns a null pointer. */ QPDF_DLL qpdf_error qpdf_get_error(qpdf_data qpdf); @@ -133,7 +142,8 @@ extern "C" { char const* qpdf_get_error_full_text(qpdf_data q, qpdf_error e); /* Use these functions to extract individual fields from the - * error; see QPDFExc.hh for details. */ + * error; see QPDFExc.hh for details. It is invalid for e to be a + * null pointer for any of these calls. */ QPDF_DLL enum qpdf_error_code_e qpdf_get_error_code(qpdf_data q, qpdf_error e); QPDF_DLL diff --git a/libqpdf/qpdf-c.cc b/libqpdf/qpdf-c.cc index 1d1ac735..d0d824b6 100644 --- a/libqpdf/qpdf-c.cc +++ b/libqpdf/qpdf-c.cc @@ -128,13 +128,19 @@ QPDF_BOOL qpdf_more_warnings(qpdf_data qpdf) } } +QPDF_BOOL qpdf_has_error(qpdf_data qpdf) +{ + QTC::TC("qpdf", "qpdf-c called qpdf_has_error"); + return (qpdf->error.getPointer() ? QPDF_TRUE : QPDF_FALSE); +} + qpdf_error qpdf_get_error(qpdf_data qpdf) { if (qpdf->error.getPointer()) { qpdf->tmp_error.exc = qpdf->error; qpdf->error = 0; - QTC::TC("qpdf", "qpdf-c qpdf_next_error returned error"); + QTC::TC("qpdf", "qpdf-c qpdf_get_error returned error"); return &qpdf->tmp_error; } else diff --git a/qpdf/qpdf-ctest.c b/qpdf/qpdf-ctest.c index 422aff15..08663cde 100644 --- a/qpdf/qpdf-ctest.c +++ b/qpdf/qpdf-ctest.c @@ -18,9 +18,9 @@ static void report_errors() printf(" pos : %ld\n", qpdf_get_error_file_position(qpdf, e)); printf(" text: %s\n", qpdf_get_error_message_detail(qpdf, e)); } - e = qpdf_get_error(qpdf); - if (e) + if (qpdf_has_error(qpdf)) { + e = qpdf_get_error(qpdf); printf("error: %s\n", qpdf_get_error_full_text(qpdf, e)); printf(" code: %d\n", qpdf_get_error_code(qpdf, e)); printf(" file: %s\n", qpdf_get_error_filename(qpdf, e)); diff --git a/qpdf/qpdf.testcov b/qpdf/qpdf.testcov index 811bddff..4d4c537a 100644 --- a/qpdf/qpdf.testcov +++ b/qpdf/qpdf.testcov @@ -121,7 +121,7 @@ QPDF_Stream ignore non-dictionary DecodeParms 0 qpdf-c called qpdf_init 0 qpdf-c called qpdf_cleanup 0 qpdf-c called qpdf_more_warnings 0 -qpdf-c qpdf_next_error returned error 0 +qpdf-c qpdf_get_error returned error 0 qpdf-c qpdf_next_warning returned warning 0 qpdf-c called qpdf_set_suppress_warnings 0 qpdf-c called qpdf_set_ignore_xref_streams 0 @@ -172,3 +172,4 @@ qpdf-c called qpdf_set_static_aes_IV 0 QPDF_encryption stream crypt filter 0 QPDF ERR object stream with wrong type 0 QPDF object gone after xref reconstruction 0 +qpdf-c called qpdf_has_error 0 -- cgit v1.2.3-70-g09d2