]>
Dogcows Code - chaz/yoink/blob - src/Moof/StringTools.cc
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
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.
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.
27 *******************************************************************************/
31 #include "ConvertUTF.h"
33 #include "StringTools.hh"
39 std::wstring
multiToWide(const std::string
& multiStr
)
41 size_t length
= multiStr
.length();
43 if (sizeof(wchar_t) == 2)
45 wchar_t* wideStr
= new wchar_t[length
+ 1];
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;
52 ConversionResult res
= ConvertUTF8toUTF16(&srcStart
, srcEnd
,
53 &targetStart
, targetEnd
, lenientConversion
);
54 if (res
!= conversionOK
)
57 throw std::runtime_error("bad conversion from multi to wide characters");
61 std::wstring
convertedStr(wideStr
);
66 else if (sizeof(wchar_t) == 4)
68 wchar_t* wideStr
= new wchar_t[length
];
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
;
75 ConversionResult res
= ConvertUTF8toUTF32(&srcStart
, srcEnd
,
76 &targetStart
, targetEnd
, lenientConversion
);
77 if (res
!= conversionOK
)
80 throw std::runtime_error("bad conversion from multi to wide characters");
84 std::wstring
convertedStr(wideStr
);
91 throw std::runtime_error("unknown size of wide characters");
96 std::string
wideToMulti(const std::wstring
& wideStr
)
98 size_t length
= wideStr
.length();
100 if (sizeof(wchar_t) == 2)
102 size_t multiLength
= 3 * length
+ 1;
103 char* multiStr
= new char[multiLength
];
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
;
110 ConversionResult res
= ConvertUTF16toUTF8(&srcStart
, srcEnd
,
111 &targetStart
, targetEnd
, lenientConversion
);
112 if (res
!= conversionOK
)
115 throw std::runtime_error("bad conversion from wide to multi-characters");
119 std::string
convertedStr(multiStr
);
124 else if (sizeof(wchar_t) == 4)
126 size_t multiLength
= 4 * length
+ 1;
127 char* multiStr
= new char[multiLength
];
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
;
134 ConversionResult res
= ConvertUTF32toUTF8(&srcStart
, srcEnd
,
135 &targetStart
, targetEnd
, lenientConversion
);
136 if (res
!= conversionOK
)
139 throw std::runtime_error("bad conversion from wide to multi-characters");
143 std::string
convertedStr(multiStr
);
150 throw std::runtime_error("unknown size of wide characters");
158 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.038283 seconds and 4 git commands to generate.