forked from mooz/node-icu-charset-detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-icu-charset-detector.cpp
129 lines (104 loc) · 4.12 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
129
#include <node.h>
#include <node_buffer.h>
#include <node_object_wrap.h>
#include <unicode/ucsdet.h>
#include <cstring>
#include <iostream>
#define EXCEPTION(type, message) \
ThrowException(v8::Exception::type(v8::String::New(message)))
class CharsetMatch : public node::ObjectWrap
{
public:
static void
Initialize(const v8::Handle<v8::Object> target) {
v8::HandleScope scope;
v8::Local<v8::FunctionTemplate> constructorTemplate
= v8::FunctionTemplate::New(CharsetMatch::New);
constructorTemplate->InstanceTemplate()->SetInternalFieldCount(1);
// setup methods
NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "getName", CharsetMatch::GetName);
NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "getLanguage", CharsetMatch::GetLanguage);
NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "getConfidence", CharsetMatch::GetConfidence);
// export class
target->Set(v8::String::NewSymbol("CharsetMatch"), 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 cahrset";
}
~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 v8::Handle<v8::Value>
New(const v8::Arguments& args) {
if (args.Length() < 1 || !node::Buffer::HasInstance(args[0]))
return EXCEPTION(TypeError, "Expected Buffer for the argument");
try {
CharsetMatch::FromBuffer(args[0]->ToObject())->Wrap(args.This()); // under GC
} catch (const char* errorMessage) {
return EXCEPTION(Error, errorMessage);
}
return args.This();
}
private:
static v8::Handle<v8::Value>
GetName(const v8::Arguments& args) {
v8::HandleScope scope;
CharsetMatch* self = node::ObjectWrap::Unwrap<CharsetMatch>(args.This());
if (!self->charsetMatch_) {
return scope.Close(v8::Null());
}
UErrorCode icuError = U_ZERO_ERROR;
const char* detectedCharsetName = ucsdet_getName(self->charsetMatch_, &icuError);
return scope.Close(v8::String::New(detectedCharsetName, strlen(detectedCharsetName)));
}
static v8::Handle<v8::Value>
GetLanguage(const v8::Arguments& args) {
v8::HandleScope scope;
CharsetMatch* self = node::ObjectWrap::Unwrap<CharsetMatch>(args.This());
if (!self->charsetMatch_) {
return scope.Close(v8::Null());
}
UErrorCode icuError = U_ZERO_ERROR;
const char* detectedLanguage = ucsdet_getLanguage(self->charsetMatch_, &icuError);
return scope.Close(v8::String::New(detectedLanguage, strlen(detectedLanguage)));
}
static v8::Handle<v8::Value>
GetConfidence(const v8::Arguments& args) {
v8::HandleScope scope;
CharsetMatch* self = node::ObjectWrap::Unwrap<CharsetMatch>(args.This());
if (!self->charsetMatch_) {
return scope.Close(v8::Null());
}
UErrorCode icuError = U_ZERO_ERROR;
int32_t detectionConfidence = ucsdet_getConfidence(self->charsetMatch_, &icuError);
return scope.Close(v8::Number::New(static_cast<double>(detectionConfidence)));
}
UCharsetDetector* charsetDetector_;
const UCharsetMatch* charsetMatch_;
};
extern "C"
void
init(v8::Handle<v8::Object> target)
{
CharsetMatch::Initialize(target);
}
NODE_MODULE(node_icu_charset_detector, init)
#undef EXCEPTION