-
Notifications
You must be signed in to change notification settings - Fork 0
/
IO.c
193 lines (164 loc) · 4.39 KB
/
IO.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdio.h>
#include <libxml/xmlIO.h>
#include "napi.h"
#include "IO.h"
napi_status
IO_init(napi_env env, napi_value exports)
{
napi_status status;
napi_value fn;
NAPI_EXPORT_FN(status, env, xmlAllocOutputBuffer, fn, exports);
NAPI_EXPORT_FN(status, env, xmlOutputBufferFlush, fn, exports);
NAPI_EXPORT_FN(status, env, xmlOutputBufferGetContent, fn, exports);
NAPI_EXPORT_FN(status, env, xmlOutputBufferGetSize, fn, exports);
return napi_ok;
}
napi_value
_xmlAllocOutputBuffer(napi_env env, napi_callback_info info)
{
napi_status status;
size_t argc = 1;
napi_value argv[1], export;
napi_valuetype vt;
xmlCharEncodingHandlerPtr encp;
xmlOutputBufferPtr bufp;
// Get arguments
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read arguments");
return NULL;
}
// Get xmlCharEncodingHandlerPtr
status = napi_typeof(env, argv[0], &vt);
if (vt == napi_null || vt == napi_undefined) {
encp = NULL;
} else {
status = napi_get_value_external(env, argv[0], (void **)&encp);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read "
"xmlCharEncodingHandlerPtr at arg 0");
return NULL;
}
}
bufp = xmlAllocOutputBuffer(encp);
if (bufp == NULL) {
napi_throw_error(env, NULL, "Can't allocate xmlOutputBuffer");
return NULL;
}
// create the external
status = napi_create_external(env, (void *)bufp,
_xmlAllocOutputBufferFree, NULL, &export);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't create external");
return NULL;
}
return export;
}
void
_xmlAllocOutputBufferFree(napi_env env, void *finalize_data,
void *finalize_hint)
{
//
if (xmlOutputBufferClose(finalize_data) == -1) {
napi_throw_error(env, NULL, "Can't close xmlOutputBuffer");
}
}
napi_value
_xmlOutputBufferFlush(napi_env env, napi_callback_info info)
{
napi_status status;
size_t argc = 1;
napi_value argv[1], export;
xmlOutputBufferPtr out;
int ret;
// Get arguments
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read arguments");
return NULL;
}
// Get xmlOutputBufferPtr
status = napi_get_value_external(env, argv[0], (void **)&out);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read xmlOutputBufferPtr "
"at arg 0");
return NULL;
}
// Exec
ret = xmlOutputBufferFlush(out);
// create the external
status = napi_create_int64(env, (int64_t)ret, &export);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't create external");
return NULL;
}
return export;
}
napi_value
_xmlOutputBufferGetContent(napi_env env, napi_callback_info info)
{
napi_status status;
size_t argc = 1;
napi_value argv[1], export;
xmlOutputBufferPtr out;
const xmlChar *ret;
// Get arguments
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read arguments");
return NULL;
}
// Get xmlOutputBufferPtr
status = napi_get_value_external(env, argv[0], (void **)&out);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read xmlOutputBufferPtr "
"at arg 0");
return NULL;
}
// Exec
ret = xmlOutputBufferGetContent(out);
if (ret == NULL) {
napi_throw_error(env, NULL, "Exucution error "
"xmlOutputBufferGetContent");
return NULL;
}
// create the external
status = napi_create_string_utf8(env, (char *)ret, NAPI_AUTO_LENGTH,
&export);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't create external");
return NULL;
}
return export;
}
napi_value
_xmlOutputBufferGetSize(napi_env env, napi_callback_info info)
{
napi_status status;
size_t argc = 1;
napi_value argv[1], export;
xmlOutputBufferPtr out;
size_t ret;
// Get arguments
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read arguments");
return NULL;
}
// Get xmlOutputBufferPtr
status = napi_get_value_external(env, argv[0], (void **)&out);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't read xmlOutputBufferPtr "
"at arg 0");
return NULL;
}
// Exec
ret = xmlOutputBufferGetSize(out);
// create the external
status = napi_create_int64(env, (int64_t)ret, &export);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Can't create external");
return NULL;
}
return export;
}