]>
Dogcows Code - chaz/yoink/blob - src/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
);
65 else if (sizeof(wchar_t) == 4)
67 wchar_t* wideStr
= new wchar_t[length
];
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
;
74 ConversionResult res
= ConvertUTF8toUTF32(&srcStart
, srcEnd
,
75 &targetStart
, targetEnd
, lenientConversion
);
76 if (res
!= conversionOK
)
79 throw std::runtime_error("bad conversion from multi to wide characters");
83 std::wstring
convertedStr(wideStr
);
89 throw std::runtime_error("unknown size of wide characters");
94 std::string
wideToMulti(const std::wstring
& wideStr
)
96 size_t length
= wideStr
.length();
98 if (sizeof(wchar_t) == 2)
100 size_t multiLength
= 3 * length
+ 1;
101 char* multiStr
= new char[multiLength
];
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
;
108 ConversionResult res
= ConvertUTF16toUTF8(&srcStart
, srcEnd
,
109 &targetStart
, targetEnd
, lenientConversion
);
110 if (res
!= conversionOK
)
113 throw std::runtime_error("bad conversion from wide to multi-characters");
117 std::string
convertedStr(multiStr
);
121 else if (sizeof(wchar_t) == 4)
123 size_t multiLength
= 4 * length
+ 1;
124 char* multiStr
= new char[multiLength
];
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
;
131 ConversionResult res
= ConvertUTF32toUTF8(&srcStart
, srcEnd
,
132 &targetStart
, targetEnd
, lenientConversion
);
133 if (res
!= conversionOK
)
136 throw std::runtime_error("bad conversion from wide to multi-characters");
140 std::string
convertedStr(multiStr
);
146 throw std::runtime_error("unknown size of wide characters");
This page took 0.045054 seconds and 5 git commands to generate.