aboutsummaryrefslogtreecommitdiffstats
path: root/examples/extend-c-api.c
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2023-12-16 15:46:44 +0100
committerJay Berkenbilt <ejb@ql.org>2023-12-16 16:06:34 +0100
commitd7a364b882be44c93dc4a843bcca2ae63e805c2c (patch)
tree5ead7b212c27368b8411ae0db0d072bec5747e7a /examples/extend-c-api.c
parent924ebf9f6abb1b682bfde18f537c6f336fc72c82 (diff)
downloadqpdf-d7a364b882be44c93dc4a843bcca2ae63e805c2c.tar.zst
Allow regular C++ functions to interoperate with the C API
Diffstat (limited to 'examples/extend-c-api.c')
-rw-r--r--examples/extend-c-api.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/examples/extend-c-api.c b/examples/extend-c-api.c
new file mode 100644
index 00000000..aa603845
--- /dev/null
+++ b/examples/extend-c-api.c
@@ -0,0 +1,67 @@
+/*
+ * This is an example of how to write C++ functions and make them usable with the qpdf C API. It
+ * consists of three files:
+ * - extend-c-api.h -- a plain C header file
+ * - extend-c-api.c -- a C program that calls the function
+ * - extend-c-api.cc -- a C++ file that implements the function
+ */
+
+#include "extend-c-api.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static char const* whoami = 0;
+
+static void
+usage()
+{
+ fprintf(stderr, "Usage: %s infile\n", whoami);
+ exit(2);
+}
+
+int
+main(int argc, char* argv[])
+{
+ char* infile = NULL;
+ qpdf_data qpdf = qpdf_init();
+ int warnings = 0;
+ int errors = 0;
+ char* p = NULL;
+
+ if ((p = strrchr(argv[0], '/')) != NULL) {
+ whoami = p + 1;
+ } else if ((p = strrchr(argv[0], '\\')) != NULL) {
+ whoami = p + 1;
+ } else {
+ whoami = argv[0];
+ }
+
+ if (argc != 2) {
+ usage();
+ }
+
+ infile = argv[1];
+
+ if ((qpdf_read(qpdf, infile, NULL) & QPDF_ERRORS) == 0) {
+ int npages;
+ if ((num_pages(qpdf, &npages) & QPDF_ERRORS) == 0) {
+ printf("num pages = %d\n", npages);
+ }
+ }
+ if (qpdf_more_warnings(qpdf)) {
+ warnings = 1;
+ }
+ if (qpdf_has_error(qpdf)) {
+ errors = 1;
+ printf("error: %s\n", qpdf_get_error_full_text(qpdf, qpdf_get_error(qpdf)));
+ }
+ qpdf_cleanup(&qpdf);
+ if (errors) {
+ return 2;
+ } else if (warnings) {
+ return 3;
+ }
+
+ return 0;
+}