aboutsummaryrefslogtreecommitdiffstats
path: root/src/cnki.c
diff options
context:
space:
mode:
authoryzrh <yzrh@tuta.io>2020-08-14 22:04:26 +0000
committeryzrh <yzrh@tuta.io>2020-08-14 22:04:26 +0000
commit12ecdd71592eccf7bdb6214edbc7318246469c1c (patch)
treefda27e41c37a2345702ad3e90480154d975e426f /src/cnki.c
downloadmelon-12ecdd71592eccf7bdb6214edbc7318246469c1c.tar.gz
melon-12ecdd71592eccf7bdb6214edbc7318246469c1c.tar.zst
Initial commit.
Diffstat (limited to 'src/cnki.c')
-rw-r--r--src/cnki.c168
1 files changed, 168 insertions, 0 deletions
diff --git a/src/cnki.c b/src/cnki.c
new file mode 100644
index 0000000..001be54
--- /dev/null
+++ b/src/cnki.c
@@ -0,0 +1,168 @@
+/*
+ * Copyright (c) 2020, yzrh <yzrh@tuta.io>
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "cnki.h"
+
+int
+cnki_create(cnki_t **param)
+{
+ if (*param != NULL)
+ return 1;
+
+ *param = malloc(sizeof(cnki_t));
+
+ if (*param == NULL)
+ return 1;
+
+ (*param)->stat = 0;
+ (*param)->size_buf = 524288;
+ (*param)->fp_i = NULL;
+ (*param)->fp_o = NULL;
+
+ (*param)->file_stat = malloc(sizeof(file_stat_t));
+
+ if ((*param)->file_stat== NULL)
+ return 1;
+
+ memset((*param)->file_stat, 0, sizeof(file_stat_t));
+
+ (*param)->object_outline = NULL;
+ (*param)->object_nh = NULL;
+
+ return 0;
+}
+
+void
+cnki_destroy(cnki_t **param)
+{
+ if (*param != NULL) {
+ if ((*param)->file_stat != NULL)
+ free((*param)->file_stat);
+ if ((*param)->object_outline != NULL)
+ free((*param)->object_outline);
+ if ((*param)->object_nh != NULL)
+ free((*param)->object_nh);
+ free(*param);
+ }
+}
+
+int
+cnki_info(cnki_t **param)
+{
+ if (*param == NULL)
+ return 1;
+
+ if ((*param)->stat > 1)
+ printf("Reading file header at %x\n", ADDRESS_HEAD);
+
+ int addr[2];
+
+ fseek((*param)->fp_i, ADDRESS_HEAD, SEEK_SET);
+ fread((*param)->file_stat->type, 4, 1, (*param)->fp_i);
+
+ if ((*param)->stat > 0)
+ printf("File type is '%s'\n", (*param)->file_stat->type);
+
+ if (strcmp((*param)->file_stat->type, "%PDF") == 0) {
+ return 0;
+ } else if (strcmp((*param)->file_stat->type, "CAJ") == 0) {
+ addr[0] = ADDRESS_CAJ_PAGE;
+ addr[1] = ADDRESS_CAJ_OUTLINE;
+ } else if (strcmp((*param)->file_stat->type, "HN") == 0) {
+ addr[0] = ADDRESS_HN_PAGE;
+ addr[1] = ADDRESS_HN_OUTLINE;
+ } else if (strcmp((*param)->file_stat->type, "KDH ") == 0) {
+ return 0;
+ } else {
+ return 1;
+ }
+
+ if ((*param)->stat > 1)
+ printf("Reading page count at %x\n", addr[0]);
+
+ fseek((*param)->fp_i, addr[0], SEEK_SET);
+ fread(&(*param)->file_stat->page, 4, 1, (*param)->fp_i);
+
+ if ((*param)->stat > 0)
+ printf("Advised %d page(s)\n",
+ (*param)->file_stat->page);
+
+ if ((*param)->stat > 1)
+ printf("Reading outline count at %x\n", addr[1]);
+
+ fseek((*param)->fp_i, addr[1], SEEK_SET);
+ fread(&(*param)->file_stat->outline, 4, 1, (*param)->fp_i);
+
+ if ((*param)->stat > 0)
+ printf("Advised %d outline(s)\n",
+ (*param)->file_stat->outline);
+
+ if ((*param)->file_stat->outline > 0) {
+ if ((*param)->stat > 1) {
+ printf("Loading outline(s)\n");
+ printf("\t%16s\t%-24s\t%12s\t%12s\t%5s\n",
+ "title",
+ "hierarchy",
+ "page",
+ "text",
+ "depth");
+ }
+
+ (*param)->object_outline = malloc(sizeof(object_outline_t));
+
+ if ((*param)->object_outline == NULL)
+ return 1;
+
+ object_outline_t *ptr = (*param)->object_outline;
+ for (int i = 0; i < (*param)->file_stat->outline; i++) {
+ fread(ptr->title, 256, 1, (*param)->fp_i);
+ fread(ptr->hierarchy, 24, 1, (*param)->fp_i);
+ fread(ptr->page, 12, 1, (*param)->fp_i);
+ fread(ptr->text, 12, 1, (*param)->fp_i);
+ fread(&ptr->depth, 4, 1, (*param)->fp_i);
+
+ ptr->next = NULL;
+
+ if ((*param)->stat > 1) {
+ printf("\t");
+ for (int j = 1; j <= 256; j++) {
+ printf("%02x", (unsigned char) ptr->title[j - 1]);
+
+ if (j % 8 == 0 && ptr->title[j] == '\0')
+ break;
+
+ if (j % 8 == 0)
+ printf("\n\t");
+ else if (j % 2 == 0)
+ printf(" ");
+ }
+ printf("\t%-24s\t%12s\t%12s\t%5d\n",
+ ptr->hierarchy,
+ ptr->page,
+ ptr->text,
+ ptr->depth);
+ }
+
+ if (i < (*param)->file_stat->outline - 1) {
+ ptr->next = malloc(sizeof(object_outline_t));
+
+ if (ptr->next == NULL)
+ return 1;
+ }
+
+ ptr = ptr->next;
+ }
+
+ if ((*param)->stat > 0)
+ printf("Loaded %d outline(s)\n",
+ (*param)->file_stat->outline);
+ }
+
+ return 0;
+}