]> Dogcows Code - chaz/yoink/blob - src/Moof/StringTools.cc
removed logging from script to fix compile error
[chaz/yoink] / src / Moof / StringTools.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #include <stdexcept>
13
14 #include "ConvertUTF.h"
15 #include "StringTools.hh"
16
17
18 namespace Mf {
19
20
21 // TODO this code is ugly
22
23 std::wstring multiToWide(const std::string& multiStr)
24 {
25 size_t length = multiStr.length();
26
27 if (sizeof(wchar_t) == 2)
28 {
29 wchar_t* wideStr = new wchar_t[length + 1];
30
31 const UTF8* srcStart = reinterpret_cast<const UTF8*>(multiStr.c_str());
32 const UTF8* srcEnd = srcStart + length;
33 UTF16* targetStart = reinterpret_cast<UTF16*>(wideStr);
34 UTF16* targetEnd = targetStart + length+1;
35
36 ConversionResult res = ConvertUTF8toUTF16(&srcStart, srcEnd,
37 &targetStart, targetEnd, lenientConversion);
38 if (res != conversionOK)
39 {
40 delete[] wideStr;
41 throw std::runtime_error("bad conversion from multi to wide characters");
42 }
43
44 *targetStart = 0;
45 std::wstring convertedStr(wideStr);
46 delete[] wideStr;
47
48 return convertedStr;
49 }
50 else if (sizeof(wchar_t) == 4)
51 {
52 wchar_t* wideStr = new wchar_t[length];
53
54 const UTF8* srcStart = reinterpret_cast<const UTF8*>(multiStr.c_str());
55 const UTF8* srcEnd = srcStart + length;
56 UTF32* targetStart = reinterpret_cast<UTF32*>(wideStr);
57 UTF32* targetEnd = targetStart + length;
58
59 ConversionResult res = ConvertUTF8toUTF32(&srcStart, srcEnd,
60 &targetStart, targetEnd, lenientConversion);
61 if (res != conversionOK)
62 {
63 delete[] wideStr;
64 throw std::runtime_error("bad conversion from multi to wide characters");
65 }
66
67 *targetStart = 0;
68 std::wstring convertedStr(wideStr);
69 delete[] wideStr;
70
71 return convertedStr;
72 }
73 else
74 {
75 throw std::runtime_error("unknown size of wide characters");
76 }
77 return L"";
78 }
79
80 std::string wideToMulti(const std::wstring& wideStr)
81 {
82 size_t length = wideStr.length();
83
84 if (sizeof(wchar_t) == 2)
85 {
86 size_t multiLength = 3 * length + 1;
87 char* multiStr = new char[multiLength];
88
89 const UTF16* srcStart = reinterpret_cast<const UTF16*>(wideStr.c_str());
90 const UTF16* srcEnd = srcStart + length;
91 UTF8* targetStart = reinterpret_cast<UTF8*>(multiStr);
92 UTF8* targetEnd = targetStart + multiLength;
93
94 ConversionResult res = ConvertUTF16toUTF8(&srcStart, srcEnd,
95 &targetStart, targetEnd, lenientConversion);
96 if (res != conversionOK)
97 {
98 delete[] multiStr;
99 throw std::runtime_error("bad conversion from wide to multi-characters");
100 }
101
102 *targetStart = 0;
103 std::string convertedStr(multiStr);
104 delete[] multiStr;
105
106 return convertedStr;
107 }
108 else if (sizeof(wchar_t) == 4)
109 {
110 size_t multiLength = 4 * length + 1;
111 char* multiStr = new char[multiLength];
112
113 const UTF32* srcStart = reinterpret_cast<const UTF32*>(wideStr.c_str());
114 const UTF32* srcEnd = srcStart + length;
115 UTF8* targetStart = reinterpret_cast<UTF8*>(multiStr);
116 UTF8* targetEnd = targetStart + multiLength;
117
118 ConversionResult res = ConvertUTF32toUTF8(&srcStart, srcEnd,
119 &targetStart, targetEnd, lenientConversion);
120 if (res != conversionOK)
121 {
122 delete[] multiStr;
123 throw std::runtime_error("bad conversion from wide to multi-characters");
124 }
125
126 *targetStart = 0;
127 std::string convertedStr(multiStr);
128 delete[] multiStr;
129
130 return convertedStr;
131 }
132 else
133 {
134 throw std::runtime_error("unknown size of wide characters");
135 }
136 return "";
137 }
138
139
140 } // namespace Mf
141
This page took 0.039686 seconds and 4 git commands to generate.