-
Notifications
You must be signed in to change notification settings - Fork 2
/
ced.cpp
69 lines (55 loc) · 1.79 KB
/
ced.cpp
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
#define NAPI_VERSION 2
#include <compact_enc_det/compact_enc_det.h>
#include <node_api.h>
namespace ced {
napi_value Method(napi_env env, napi_callback_info info) {
napi_status status;
size_t argc = 1;
napi_value args[1];
status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
if (status != napi_ok) {
napi_throw_error(env, NULL, "N-API call failed");
return nullptr;
}
void* buf;
size_t buf_length;
status = napi_get_buffer_info(env, args[0], &buf, &buf_length);
if (status != napi_ok) {
napi_throw_error(env, NULL, "N-API call failed");
return nullptr;
}
const char* text = static_cast<const char*>(buf);
size_t text_length = buf_length;
bool is_reliable;
int bytes_consumed;
Encoding encoding = CompactEncDet::DetectEncoding(
text, text_length, nullptr, nullptr, nullptr, UNKNOWN_ENCODING,
UNKNOWN_LANGUAGE, CompactEncDet::QUERY_CORPUS, true, &bytes_consumed,
&is_reliable);
const char* encoding_name = EncodingName(encoding);
napi_value encodingName;
status = napi_create_string_utf8(env, encoding_name, NAPI_AUTO_LENGTH,
&encodingName);
if (status != napi_ok) {
napi_throw_error(env, NULL, "N-API call failed");
return nullptr;
}
return encodingName;
}
napi_value init(napi_env env, napi_value exports) {
napi_status status;
napi_value fn;
status = napi_create_function(env, nullptr, 0, Method, nullptr, &fn);
if (status != napi_ok) {
napi_throw_error(env, NULL, "N-API call failed");
return nullptr;
}
status = napi_set_named_property(env, exports, "detectEncoding", fn);
if (status != napi_ok) {
napi_throw_error(env, NULL, "N-API call failed");
return nullptr;
}
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, init)
} // namespace ced