]> Dogcows Code - chaz/yoink/blob - src/moof/string.cc
begin cleaning up resource management
[chaz/yoink] / src / moof / string.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 "string.hh"
16
17
18 namespace moof {
19
20
21 // TODO this code is ugly
22
23 wstring multi_to_wide(const string& multi)
24 {
25 size_t length = multi.length();
26
27 if (sizeof(wchar_t) == 2)
28 {
29 wchar_t* wide = new wchar_t[length + 1];
30
31 const UTF8* srcStart = reinterpret_cast<const UTF8*>(multi.c_str());
32 const UTF8* srcEnd = srcStart + length;
33 UTF16* targetStart = reinterpret_cast<UTF16*>(wide);
34 UTF16* targetEnd = targetStart + length+1;
35
36 ConversionResult res = ConvertUTF8toUTF16(&srcStart, srcEnd,
37 &targetStart, targetEnd, lenientConversion);
38 if (res != conversionOK)
39 {
40 delete[] wide;
41 throw std::runtime_error("bad conversion from multi to wide characters");
42 }
43
44 *targetStart = 0;
45 wstring convertedStr(wide);
46 delete[] wide;
47
48 return convertedStr;
49 }
50 else if (sizeof(wchar_t) == 4)
51 {
52 wchar_t* wide = new wchar_t[length];
53
54 const UTF8* srcStart = reinterpret_cast<const UTF8*>(multi.c_str());
55 const UTF8* srcEnd = srcStart + length;
56 UTF32* targetStart = reinterpret_cast<UTF32*>(wide);
57 UTF32* targetEnd = targetStart + length;
58
59 ConversionResult res = ConvertUTF8toUTF32(&srcStart, srcEnd,
60 &targetStart, targetEnd, lenientConversion);
61 if (res != conversionOK)
62 {
63 delete[] wide;
64 throw std::runtime_error("bad conversion from multi to wide characters");
65 }
66
67 *targetStart = 0;
68 wstring convertedStr(wide);
69 delete[] wide;
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 string wide_to_multi(const wstring& wide)
81 {
82 size_t length = wide.length();
83
84 if (sizeof(wchar_t) == 2)
85 {
86 size_t multiLength = 3 * length + 1;
87 char* multi = new char[multiLength];
88
89 const UTF16* srcStart = reinterpret_cast<const UTF16*>(wide.c_str());
90 const UTF16* srcEnd = srcStart + length;
91 UTF8* targetStart = reinterpret_cast<UTF8*>(multi);
92 UTF8* targetEnd = targetStart + multiLength;
93
94 ConversionResult res = ConvertUTF16toUTF8(&srcStart, srcEnd,
95 &targetStart, targetEnd, lenientConversion);
96 if (res != conversionOK)
97 {
98 delete[] multi;
99 throw std::runtime_error("bad conversion from wide to multi-characters");
100 }
101
102 *targetStart = 0;
103 string convertedStr(multi);
104 delete[] multi;
105
106 return convertedStr;
107 }
108 else if (sizeof(wchar_t) == 4)
109 {
110 size_t multiLength = 4 * length + 1;
111 char* multi = new char[multiLength];
112
113 const UTF32* srcStart = reinterpret_cast<const UTF32*>(wide.c_str());
114 const UTF32* srcEnd = srcStart + length;
115 UTF8* targetStart = reinterpret_cast<UTF8*>(multi);
116 UTF8* targetEnd = targetStart + multiLength;
117
118 ConversionResult res = ConvertUTF32toUTF8(&srcStart, srcEnd,
119 &targetStart, targetEnd, lenientConversion);
120 if (res != conversionOK)
121 {
122 delete[] multi;
123 throw std::runtime_error("bad conversion from wide to multi-characters");
124 }
125
126 *targetStart = 0;
127 string convertedStr(multi);
128 delete[] multi;
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 moof
141
This page took 0.034427 seconds and 4 git commands to generate.