-
Notifications
You must be signed in to change notification settings - Fork 22
/
node-icu-charset-detector.cpp
128 lines (101 loc) · 4.08 KB
/
node-icu-charset-detector.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
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
#include <node.h>
#include <nan.h>
#include <node_buffer.h>
#include <node_object_wrap.h>
#include <unicode/ucsdet.h>
#include <cstring>
#include <iostream>
class CharsetMatch : public Nan::ObjectWrap
{
public:
static void
RegisterClass(const v8::Handle<v8::Object> target) {
Nan::HandleScope scope;
const char* ClassName = "CharsetMatch";
v8::Local<v8::FunctionTemplate> constructorTemplate
= Nan::New<v8::FunctionTemplate>(CharsetMatch::New);
constructorTemplate->SetClassName(Nan::New(ClassName).ToLocalChecked());
constructorTemplate->InstanceTemplate()->SetInternalFieldCount(1);
// setup methods
Nan::SetPrototypeMethod(constructorTemplate, "getName", CharsetMatch::GetName);
Nan::SetPrototypeMethod(constructorTemplate, "getLanguage", CharsetMatch::GetLanguage);
Nan::SetPrototypeMethod(constructorTemplate, "getConfidence", CharsetMatch::GetConfidence);
// export class
target->Set(Nan::New(ClassName).ToLocalChecked(), constructorTemplate->GetFunction());
}
CharsetMatch(const char* bufferData, size_t bufferLength) {
UErrorCode icuError = U_ZERO_ERROR;
charsetDetector_ = ucsdet_open(&icuError);
if (U_FAILURE(icuError))
throw "Failed to open detector";
// send text
ucsdet_setText(charsetDetector_, bufferData, bufferLength, &icuError);
if (U_FAILURE(icuError))
throw "Failed to set text";
// detect language
charsetMatch_ = ucsdet_detect(charsetDetector_, &icuError);
if (U_FAILURE(icuError))
throw "Failed to detect charset";
}
~CharsetMatch () {
ucsdet_close(charsetDetector_);
}
// Internal API
static CharsetMatch*
FromBuffer(v8::Handle<v8::Object> bufferObject) {
return new CharsetMatch(node::Buffer::Data(bufferObject),
node::Buffer::Length(bufferObject));
}
// JS Constructor
static
NAN_METHOD(New) {
if (info.Length() < 1)
Nan::ThrowError("Not enough arguments");
v8::Handle<v8::Object> buffer = info[0]->ToObject();
if (!node::Buffer::HasInstance(buffer))
Nan::ThrowTypeError("Expected Buffer for the argument");
try {
CharsetMatch::FromBuffer(buffer)->Wrap(info.This()); // under GC
} catch (const char* errorMessage) {
Nan::ThrowError(errorMessage);
}
info.GetReturnValue().Set(info.This());
}
private:
static
NAN_METHOD(GetName) {
CharsetMatch* self = Nan::ObjectWrap::Unwrap<CharsetMatch>(info.This());
if (!self->charsetMatch_) {
info.GetReturnValue().SetNull();
}
UErrorCode icuError = U_ZERO_ERROR;
const char* detectedCharsetName = ucsdet_getName(self->charsetMatch_, &icuError);
info.GetReturnValue().Set(Nan::New<v8::String>(detectedCharsetName, strlen(detectedCharsetName)).ToLocalChecked());
}
static
NAN_METHOD(GetLanguage) {
CharsetMatch* self = Nan::ObjectWrap::Unwrap<CharsetMatch>(info.This());
if (!self->charsetMatch_) {
info.GetReturnValue().SetNull();
}
UErrorCode icuError = U_ZERO_ERROR;
const char* detectedLanguage = ucsdet_getLanguage(self->charsetMatch_, &icuError);
info.GetReturnValue().Set(Nan::New<v8::String>(detectedLanguage, strlen(detectedLanguage)).ToLocalChecked());
}
static
NAN_METHOD(GetConfidence) {
CharsetMatch* self = Nan::ObjectWrap::Unwrap<CharsetMatch>(info.This());
if (!self->charsetMatch_) {
info.GetReturnValue().SetNull();
}
UErrorCode icuError = U_ZERO_ERROR;
int32_t detectionConfidence = ucsdet_getConfidence(self->charsetMatch_, &icuError);
info.GetReturnValue().Set(Nan::New(static_cast<double>(detectionConfidence)));
}
UCharsetDetector* charsetDetector_;
const UCharsetMatch* charsetMatch_;
};
NAN_MODULE_INIT(RegisterModule) {
CharsetMatch::RegisterClass(target);
}
NODE_MODULE(node_icu_charset_detector, RegisterModule);