OpenTREP Logo  0.07.18
C++ Open Travel Request Parsing Library
Loading...
Searching...
No Matches
icu_util.cpp
Go to the documentation of this file.
1// //////////////////////////////////////////////////////////////////////
2// Import section
3// //////////////////////////////////////////////////////////////////////
4// STL
5#include <cassert>
6#include <iostream>
7#include <sstream>
8// ICU
9#include <unicode/ucnv.h> // Converter
10// OpenTrep
12// Local
13#include "icu_util.hpp"
14
15namespace OPENTREP {
16
17 // //////////////////////////////////////////////////////////////////////
18 bool check (const UErrorCode& iStatus, const char* iMsg) {
19 bool oSuccessful = true;
20 if (U_FAILURE (iStatus)) {
21 oSuccessful = false;
22 OPENTREP_LOG_ERROR ("Unicode error: " << u_errorName (iStatus)
23 << " (" << iMsg << ")");
24 }
25
26 // DEBUG
27 // OPENTREP_LOG_DEBUG ("No Unicode operation error: " << iMsg);
28
29 return oSuccessful;
30 }
31
32 // Append a hex string to the target
33 // //////////////////////////////////////////////////////////////////////
34 icu::UnicodeString& appendHex (const uint32_t iNumber, int8_t ioDigits,
35 icu::UnicodeString& ioTarget) {
36 static const icu::UnicodeString DIGIT_STRING ("0123456789ABCDEF");
37 while (ioDigits > 0) {
38 ioTarget += DIGIT_STRING[(iNumber >> ((--ioDigits) * 4)) & 0xF];
39 }
40
41 return ioTarget;
42 }
43
44 // //////////////////////////////////////////////////////////////////////
45 icu::UnicodeString escape (const icu::UnicodeString& iSource) {
46 int32_t i;
47 icu::UnicodeString target ("\"");
48 for (i = 0; i < iSource.length(); ++i) {
49 UChar ch = iSource[i];
50
51 if (ch < 0x09 || (ch > 0x0A && ch < 0x20) || ch > 0x7E) {
52 target += "\\u";
53 appendHex (ch, 4, target);
54
55 } else {
56 target += ch;
57 }
58 }
59 target += "\"";
60
61 return target;
62 }
63
64 // //////////////////////////////////////////////////////////////////////
65 std::string getUTF8 (const icu::UnicodeString& iString) {
66 std::ostringstream oStr;
67
68 const int32_t len = iString.length();
69 // int32_t bufLen = iString.extract(0, len, buf); // Preflight
70 /* Preflighting seems to be broken now, so assume 1-1 conversion,
71 plus some slop. */
72 const int32_t bufLen = len + 256;
73
74 // Default codepage conversion
75 char* buf = new char[bufLen + 1];
76 const int32_t actualLen = iString.extract (0, len, buf/*, bufLen*/);
77 buf[actualLen] = '\0';
78
79 oStr << buf;
80 delete[] buf; buf = NULL;
81
82 return oStr.str();
83 }
84
85}
#define OPENTREP_LOG_ERROR(iToBeLogged)
Definition Logger.hpp:24
std::string getUTF8(const icu::UnicodeString &iString)
Definition icu_util.cpp:65
bool check(const UErrorCode &iStatus, const char *iMsg)
Definition icu_util.cpp:18
icu::UnicodeString & appendHex(const uint32_t iNumber, int8_t ioDigits, icu::UnicodeString &ioTarget)
Definition icu_util.cpp:34
icu::UnicodeString escape(const icu::UnicodeString &iSource)
Definition icu_util.cpp:45