-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
138 lines (129 loc) · 3.41 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "hdtd.h"
//================ patch unicode to utf8 ==================
static int to_utf8(char *buf, hd_wchar_t c, int buf_size) {
int size = 0;
//0x0000-0x007f
if (c < 0x80) {
size = 1;
if (buf_size < size) {
return -1;
}
buf[0] = c;
}
else if (c < 0x0800) {
size = 1;
if (buf_size < size) {
return -1;
}
//0x0080-0x07ff
//110X XXXX
buf[0] = 0b11000000 + (c >> 6);
//10XX XXXX
buf[1] = 0b10000000 + (c & 0b00111111);
size = 2;
}
else if (c < 0x10000) {
size = 1;
if (buf_size < size) {
return -1;
}
//1110 XXXX
buf[0] = 0b11100000 + (c >> 12);
//10XX XXXX
buf[1] = 0b10000000 + ((c >> 6) & 0b111111);
//10XX XXXX
buf[2] = 0b10000000 + (c & 0b111111);
size = 3;
}
else {
size = 1;
if (buf_size < size) {
return -1;
}
//1111 0XXX
//10XX XXXX
buf[0] = 0b11110000 + (c >> 18);
//10XX XXXX
buf[1] = 0b10000000 + ((c >> 12) & 0b111111);
//10XX XXXX
buf[2] = 0b10000000 + ((c >> 6) & 0b111111);
//10XX XXXX
buf[3] = 0b10000000 + (c & 0b111111);
size = 4;
}
return size;
}
static int unicode_to_utf8(char *buf, hd_wchar_t *unicode, int buf_size) {
char *p = buf;
int total_size = 0;
//Used to store'\0'
buf_size -= 1;
int i;
for (i = 0; unicode[i] != 0 && i < buf_size; i++) {
int size = to_utf8(p, unicode[i], buf_size);
if (size < 0) {
break;
}
p += size;
buf_size -= size;
total_size += size;
}
buf[total_size] = '\0';
return total_size;
}
int main() {
hd_context *ctx;
hd_document *doc;
ctx = hd_new_context(NULL, HD_STORE_DEFAULT);
if (!ctx)
{
fprintf(stderr, "cannot create hdContents context\n");
return EXIT_FAILURE;
}
/* Register the default file types to handle. */
hd_try(ctx)
hd_register_document_handlers(ctx);
hd_catch(ctx)
{
fprintf(stderr, "cannot register document handlers: %s\n", hd_caught_message(ctx));
hd_drop_context(ctx);
return EXIT_FAILURE;
}
/* Open the document. */
hd_try(ctx)
doc = hd_open_document(ctx, "/Users/sjw/Documents/debugfile/f0711656.pdf");
hd_catch(ctx) {
fprintf(stderr, "cannot open document: %s\n", hd_caught_message(ctx));
hd_drop_context(ctx);
return EXIT_FAILURE;
}
hd_page *page = hd_load_page(ctx, doc, 0);
char buf[512] = {0};
uint32_t extract_len = 0;
hd_try(ctx)
{
hd_run_page_contents(ctx, page, buf, &extract_len);
unsigned char filenameUtf8[128];
memset(filenameUtf8, 0, 128);
unsigned int len;
len = (extract_len > 32) ? 32 : extract_len;
if (len > 0)
{
unicode_to_utf8(filenameUtf8, (hd_wchar_t* )buf, len);
FILE *fp;
fp=fopen("/Users/sjw/Documents/debugfile/test.txt","a+");
fprintf(fp,"%s",filenameUtf8);
fclose(fp);
}
}
hd_catch(ctx)
{
fprintf(stderr, "cannot run page contents: %s\n", hd_caught_message(ctx));
hd_drop_context(ctx);
hd_drop_document(ctx, doc);
return EXIT_FAILURE;
}
hd_drop_document(ctx, doc);
hd_drop_context(ctx);
printf("hd_new_context is end\n");
}