]> Dogcows Code - chaz/yoink/blob - src/Moof/StringTools.cc
dispatcher alias methods
[chaz/yoink] / src / Moof / StringTools.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #include <stdexcept>
30
31 #include "ConvertUTF.h"
32
33 #include "StringTools.hh"
34
35
36 namespace Mf {
37
38
39 std::wstring multiToWide(const std::string& multiStr)
40 {
41 size_t length = multiStr.length();
42
43 if (sizeof(wchar_t) == 2)
44 {
45 wchar_t* wideStr = new wchar_t[length + 1];
46
47 const UTF8* srcStart = reinterpret_cast<const UTF8*>(multiStr.c_str());
48 const UTF8* srcEnd = srcStart + length;
49 UTF16* targetStart = reinterpret_cast<UTF16*>(wideStr);
50 UTF16* targetEnd = targetStart + length+1;
51
52 ConversionResult res = ConvertUTF8toUTF16(&srcStart, srcEnd,
53 &targetStart, targetEnd, lenientConversion);
54 if (res != conversionOK)
55 {
56 delete[] wideStr;
57 throw std::runtime_error("bad conversion from multi to wide characters");
58 }
59
60 *targetStart = 0;
61 std::wstring convertedStr(wideStr);
62 delete[] wideStr;
63
64 return convertedStr;
65 }
66 else if (sizeof(wchar_t) == 4)
67 {
68 wchar_t* wideStr = new wchar_t[length];
69
70 const UTF8* srcStart = reinterpret_cast<const UTF8*>(multiStr.c_str());
71 const UTF8* srcEnd = srcStart + length;
72 UTF32* targetStart = reinterpret_cast<UTF32*>(wideStr);
73 UTF32* targetEnd = targetStart + length;
74
75 ConversionResult res = ConvertUTF8toUTF32(&srcStart, srcEnd,
76 &targetStart, targetEnd, lenientConversion);
77 if (res != conversionOK)
78 {
79 delete[] wideStr;
80 throw std::runtime_error("bad conversion from multi to wide characters");
81 }
82
83 *targetStart = 0;
84 std::wstring convertedStr(wideStr);
85 delete[] wideStr;
86
87 return convertedStr;
88 }
89 else
90 {
91 throw std::runtime_error("unknown size of wide characters");
92 }
93 return L"";
94 }
95
96 std::string wideToMulti(const std::wstring& wideStr)
97 {
98 size_t length = wideStr.length();
99
100 if (sizeof(wchar_t) == 2)
101 {
102 size_t multiLength = 3 * length + 1;
103 char* multiStr = new char[multiLength];
104
105 const UTF16* srcStart = reinterpret_cast<const UTF16*>(wideStr.c_str());
106 const UTF16* srcEnd = srcStart + length;
107 UTF8* targetStart = reinterpret_cast<UTF8*>(multiStr);
108 UTF8* targetEnd = targetStart + multiLength;
109
110 ConversionResult res = ConvertUTF16toUTF8(&srcStart, srcEnd,
111 &targetStart, targetEnd, lenientConversion);
112 if (res != conversionOK)
113 {
114 delete[] multiStr;
115 throw std::runtime_error("bad conversion from wide to multi-characters");
116 }
117
118 *targetStart = 0;
119 std::string convertedStr(multiStr);
120 delete[] multiStr;
121
122 return convertedStr;
123 }
124 else if (sizeof(wchar_t) == 4)
125 {
126 size_t multiLength = 4 * length + 1;
127 char* multiStr = new char[multiLength];
128
129 const UTF32* srcStart = reinterpret_cast<const UTF32*>(wideStr.c_str());
130 const UTF32* srcEnd = srcStart + length;
131 UTF8* targetStart = reinterpret_cast<UTF8*>(multiStr);
132 UTF8* targetEnd = targetStart + multiLength;
133
134 ConversionResult res = ConvertUTF32toUTF8(&srcStart, srcEnd,
135 &targetStart, targetEnd, lenientConversion);
136 if (res != conversionOK)
137 {
138 delete[] multiStr;
139 throw std::runtime_error("bad conversion from wide to multi-characters");
140 }
141
142 *targetStart = 0;
143 std::string convertedStr(multiStr);
144 delete[] multiStr;
145
146 return convertedStr;
147 }
148 else
149 {
150 throw std::runtime_error("unknown size of wide characters");
151 }
152 return "";
153 }
154
155
156 } // namespace Mf
157
158 /** vim: set ts=4 sw=4 tw=80: *************************************************/
159
This page took 0.035624 seconds and 4 git commands to generate.