]> Dogcows Code - chaz/yoink/blob - src/stringtools.cc
91d2a6cb805f90454e5506459bb06d9d987b16e6
[chaz/yoink] / src / 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 dc {
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 return convertedStr;
64 }
65 else if (sizeof(wchar_t) == 4)
66 {
67 wchar_t* wideStr = new wchar_t[length];
68
69 const UTF8* srcStart = reinterpret_cast<const UTF8*>(multiStr.c_str());
70 const UTF8* srcEnd = srcStart + length;
71 UTF32* targetStart = reinterpret_cast<UTF32*>(wideStr);
72 UTF32* targetEnd = targetStart + length;
73
74 ConversionResult res = ConvertUTF8toUTF32(&srcStart, srcEnd,
75 &targetStart, targetEnd, lenientConversion);
76 if (res != conversionOK)
77 {
78 delete[] wideStr;
79 throw std::runtime_error("bad conversion from multi to wide characters");
80 }
81
82 *targetStart = 0;
83 std::wstring convertedStr(wideStr);
84 delete[] wideStr;
85 return convertedStr;
86 }
87 else
88 {
89 throw std::runtime_error("unknown size of wide characters");
90 }
91 return L"";
92 }
93
94 std::string wideToMulti(const std::wstring& wideStr)
95 {
96 size_t length = wideStr.length();
97
98 if (sizeof(wchar_t) == 2)
99 {
100 size_t multiLength = 3 * length + 1;
101 char* multiStr = new char[multiLength];
102
103 const UTF16* srcStart = reinterpret_cast<const UTF16*>(wideStr.c_str());
104 const UTF16* srcEnd = srcStart + length;
105 UTF8* targetStart = reinterpret_cast<UTF8*>(multiStr);
106 UTF8* targetEnd = targetStart + multiLength;
107
108 ConversionResult res = ConvertUTF16toUTF8(&srcStart, srcEnd,
109 &targetStart, targetEnd, lenientConversion);
110 if (res != conversionOK)
111 {
112 delete[] multiStr;
113 throw std::runtime_error("bad conversion from wide to multi-characters");
114 }
115
116 *targetStart = 0;
117 std::string convertedStr(multiStr);
118 delete[] multiStr;
119 return convertedStr;
120 }
121 else if (sizeof(wchar_t) == 4)
122 {
123 size_t multiLength = 4 * length + 1;
124 char* multiStr = new char[multiLength];
125
126 const UTF32* srcStart = reinterpret_cast<const UTF32*>(wideStr.c_str());
127 const UTF32* srcEnd = srcStart + length;
128 UTF8* targetStart = reinterpret_cast<UTF8*>(multiStr);
129 UTF8* targetEnd = targetStart + multiLength;
130
131 ConversionResult res = ConvertUTF32toUTF8(&srcStart, srcEnd,
132 &targetStart, targetEnd, lenientConversion);
133 if (res != conversionOK)
134 {
135 delete[] multiStr;
136 throw std::runtime_error("bad conversion from wide to multi-characters");
137 }
138
139 *targetStart = 0;
140 std::string convertedStr(multiStr);
141 delete[] multiStr;
142 return convertedStr;
143 }
144 else
145 {
146 throw std::runtime_error("unknown size of wide characters");
147 }
148 return "";
149 }
150
151
152 } // namespace dc
153
154 /** vim: set ts=4 sw=4 tw=80: *************************************************/
155
This page took 0.039285 seconds and 3 git commands to generate.