-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUnicodeTools.cc
48 lines (42 loc) · 1.2 KB
/
UnicodeTools.cc
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
#include <xercesc/util/TransService.hpp>
#include "Error.h"
#include "XMLException.h"
#include "UnicodeTools.h"
namespace flux {
namespace xml {
UTF2ASCII::UTF2ASCII(XMLCh const * unicode)
: utf8_(0)
{
XN XMLTransService::Codes result;
XN XMLTranscoder * T = XN XMLPlatformUtils::fgTransService
->makeNewTranscoderFor(
"UTF-8", result, 16*1024
);
switch (result)
{
case XN XMLTransService::Ok: break;
case XN XMLTransService::UnsupportedEncoding:
fTHROW(XMLException,"failed to instantiate the transcoder: "
"unsupported encoding");
case XN XMLTransService::InternalFailure:
fTHROW(XMLException,"failed to instantiate the transcoder: "
"internal failure");
case XN XMLTransService::SupportFilesNotFound:
fTHROW(XMLException,"failed to instantiate the transcoder: "
"support files not found");
}
XMLSize_t length = XN XMLString::stringLen(unicode);
XMLSize_t charsEaten;
utf8_ = new char[length+1];
// Source string is in Unicode, want to transcode to UTF-8
T->transcodeTo(
unicode, length,
(XMLByte *)utf8_, length,
charsEaten,
XN XMLTranscoder::UnRep_RepChar // XMLTranscoder::UnRep_Throw
);
utf8_[length] = '\0';
delete T;
}
} // namespace flux::xml
} // namespace xml