summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2017-12-30 00:38:07 +0000
committerVincent Sanders <vince@kyllikki.org>2017-12-30 00:38:07 +0000
commit31b1f792826f51e9271475d124c3a1df4aa5116b (patch)
tree3e0c1083064ba66398dee4fa0aa1c464be3b5325 /test
parent5422dd50a49fe1a282271f22cd324f815e592e07 (diff)
downloadlibnspdf-31b1f792826f51e9271475d124c3a1df4aa5116b.tar.gz
libnspdf-31b1f792826f51e9271475d124c3a1df4aa5116b.tar.bz2
make an actual library
Diffstat (limited to 'test')
-rw-r--r--test/Makefile3
-rw-r--r--test/parsepdf.c94
-rwxr-xr-xtest/runtest.sh4
3 files changed, 101 insertions, 0 deletions
diff --git a/test/Makefile b/test/Makefile
new file mode 100644
index 0000000..e3a2929
--- /dev/null
+++ b/test/Makefile
@@ -0,0 +1,3 @@
+DIR_TEST_ITEMS := parsepdf:parsepdf.c
+
+include $(NSBUILD)/Makefile.subdir
diff --git a/test/parsepdf.c b/test/parsepdf.c
new file mode 100644
index 0000000..3482af5
--- /dev/null
+++ b/test/parsepdf.c
@@ -0,0 +1,94 @@
+/*
+ * Copyright 2018 Vincent Sanders <vince@netsurf-browser.org>
+ *
+ * This file is part of libnspdf.
+ *
+ * Licensed under the MIT License,
+ * http://www.opensource.org/licenses/mit-license.php
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+
+#include <nspdf/document.h>
+
+static nspdferror
+read_whole_pdf(const char *fname, uint8_t **buffer, uint64_t *buffer_length)
+{
+ FILE *f;
+ off_t len;
+ uint8_t *buf;
+ size_t rd;
+
+ f = fopen(fname, "r");
+ if (f == NULL) {
+ perror("pdf open");
+ return NSPDFERROR_NOTFOUND;
+ }
+
+ fseek(f, 0, SEEK_END);
+ len = ftello(f);
+
+ buf = malloc(len);
+ fseek(f, 0, SEEK_SET);
+
+ rd = fread(buf, len, 1, f);
+ if (rd != 1) {
+ perror("pdf read");
+ free(buf);
+ return 1;
+ }
+
+ fclose(f);
+
+ *buffer = buf;
+ *buffer_length = len;
+
+ return NSPDFERROR_OK;
+}
+
+
+int main(int argc, char **argv)
+{
+ uint8_t *buffer;
+ uint64_t buffer_length;
+ struct nspdf_doc *doc;
+ nspdferror res;
+
+ if (argc < 2) {
+ fprintf(stderr, "Usage %s <filename>\n", argv[0]);
+ return 1;
+ }
+
+ res = read_whole_pdf(argv[1], &buffer, &buffer_length);
+ if (res != 0) {
+ printf("failed to read file\n");
+ return res;
+ }
+
+ res = nspdf_document_create(&doc);
+ if (res != NSPDFERROR_OK) {
+ printf("failed to create a document\n");
+ return res;
+ }
+
+ res = nspdf_document_parse(doc, buffer, buffer_length);
+ if (res != NSPDFERROR_OK) {
+ printf("document parse failed (%d)\n", res);
+ return res;
+ }
+
+ res = nspdf_document_destroy(doc);
+ if (res != NSPDFERROR_OK) {
+ printf("failed to destroy document (%d)\n", res);
+ return res;
+ }
+
+ free(buffer);
+
+ return 0;
+}
diff --git a/test/runtest.sh b/test/runtest.sh
new file mode 100755
index 0000000..1aa83c7
--- /dev/null
+++ b/test/runtest.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+TEST_PATH=$1
+
+${TEST_PATH}/test_parsepdf ~/Downloads/HiKey_User_Guide_Rev0.2.pdf