]>
Dogcows Code - chaz/yoink/blob - src/moof/string.cc
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"
21 // TODO this code is ugly
23 wstring
multi_to_wide(const string
& multi
)
25 size_t length
= multi
.length();
27 if (sizeof(wchar_t) == 2)
29 wchar_t* wide
= new wchar_t[length
+ 1];
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;
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 wstring
convertedStr(wide
);
50 else if (sizeof(wchar_t) == 4)
52 wchar_t* wide
= new wchar_t[length
];
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
;
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 wstring
convertedStr(wide
);
75 throw std::runtime_error("unknown size of wide characters");
80 string
wide_to_multi(const wstring
& wide
)
82 size_t length
= wide
.length();
84 if (sizeof(wchar_t) == 2)
86 size_t multiLength
= 3 * length
+ 1;
87 char* multi
= new char[multiLength
];
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
;
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 string
convertedStr(multi
);
108 else if (sizeof(wchar_t) == 4)
110 size_t multiLength
= 4 * length
+ 1;
111 char* multi
= new char[multiLength
];
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
;
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 string
convertedStr(multi
);
134 throw std::runtime_error("unknown size of wide characters");
This page took 0.046055 seconds and 4 git commands to generate.