]>
Dogcows Code - chaz/yoink/blob - src/Moof/StringTools.cc
34d2f61a72addb98bd2252a7f3f8a06e4bbb76ab
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
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.
10 **************************************************************************/
14 #include "ConvertUTF.h"
15 #include "StringTools.hh"
21 // TODO this code is ugly
23 std::wstring
multiToWide(const std::string
& multiStr
)
25 size_t length
= multiStr
.length();
27 if (sizeof(wchar_t) == 2)
29 wchar_t* wideStr
= new wchar_t[length
+ 1];
31 const UTF8
* srcStart
= reinterpret_cast<const UTF8
*>(multiStr
.c_str());
32 const UTF8
* srcEnd
= srcStart
+ length
;
33 UTF16
* targetStart
= reinterpret_cast<UTF16
*>(wideStr
);
34 UTF16
* targetEnd
= targetStart
+ length
+1;
36 ConversionResult res
= ConvertUTF8toUTF16(&srcStart
, srcEnd
,
37 &targetStart
, targetEnd
, lenientConversion
);
38 if (res
!= conversionOK
)
41 throw std::runtime_error("bad conversion from multi to wide characters");
45 std::wstring
convertedStr(wideStr
);
50 else if (sizeof(wchar_t) == 4)
52 wchar_t* wideStr
= new wchar_t[length
];
54 const UTF8
* srcStart
= reinterpret_cast<const UTF8
*>(multiStr
.c_str());
55 const UTF8
* srcEnd
= srcStart
+ length
;
56 UTF32
* targetStart
= reinterpret_cast<UTF32
*>(wideStr
);
57 UTF32
* targetEnd
= targetStart
+ length
;
59 ConversionResult res
= ConvertUTF8toUTF32(&srcStart
, srcEnd
,
60 &targetStart
, targetEnd
, lenientConversion
);
61 if (res
!= conversionOK
)
64 throw std::runtime_error("bad conversion from multi to wide characters");
68 std::wstring
convertedStr(wideStr
);
75 throw std::runtime_error("unknown size of wide characters");
80 std::string
wideToMulti(const std::wstring
& wideStr
)
82 size_t length
= wideStr
.length();
84 if (sizeof(wchar_t) == 2)
86 size_t multiLength
= 3 * length
+ 1;
87 char* multiStr
= new char[multiLength
];
89 const UTF16
* srcStart
= reinterpret_cast<const UTF16
*>(wideStr
.c_str());
90 const UTF16
* srcEnd
= srcStart
+ length
;
91 UTF8
* targetStart
= reinterpret_cast<UTF8
*>(multiStr
);
92 UTF8
* targetEnd
= targetStart
+ multiLength
;
94 ConversionResult res
= ConvertUTF16toUTF8(&srcStart
, srcEnd
,
95 &targetStart
, targetEnd
, lenientConversion
);
96 if (res
!= conversionOK
)
99 throw std::runtime_error("bad conversion from wide to multi-characters");
103 std::string
convertedStr(multiStr
);
108 else if (sizeof(wchar_t) == 4)
110 size_t multiLength
= 4 * length
+ 1;
111 char* multiStr
= new char[multiLength
];
113 const UTF32
* srcStart
= reinterpret_cast<const UTF32
*>(wideStr
.c_str());
114 const UTF32
* srcEnd
= srcStart
+ length
;
115 UTF8
* targetStart
= reinterpret_cast<UTF8
*>(multiStr
);
116 UTF8
* targetEnd
= targetStart
+ multiLength
;
118 ConversionResult res
= ConvertUTF32toUTF8(&srcStart
, srcEnd
,
119 &targetStart
, targetEnd
, lenientConversion
);
120 if (res
!= conversionOK
)
123 throw std::runtime_error("bad conversion from wide to multi-characters");
127 std::string
convertedStr(multiStr
);
134 throw std::runtime_error("unknown size of wide characters");
This page took 0.037207 seconds and 4 git commands to generate.