X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Fstring.cc;fp=src%2Fmoof%2Fstring.cc;h=a2c27bb25cf3b670d9b04c66c498c55e53530f9e;hp=0000000000000000000000000000000000000000;hb=831f04d4bc19a390415ac0bbac4331c7a65509bc;hpb=299af4f2047e767e5d79501c26444473bda64c64 diff --git a/src/moof/string.cc b/src/moof/string.cc new file mode 100644 index 0000000..a2c27bb --- /dev/null +++ b/src/moof/string.cc @@ -0,0 +1,141 @@ + +/*] Copyright (c) 2009-2010, Charles McGarvey [************************** +**] All rights reserved. +* +* vi:ts=4 sw=4 tw=75 +* +* Distributable under the terms and conditions of the 2-clause BSD license; +* see the file COPYING for a complete text of the license. +* +**************************************************************************/ + +#include + +#include "ConvertUTF.h" +#include "string.hh" + + +namespace moof { + + +// TODO this code is ugly + +wstring multi_to_wide(const string& multi) +{ + size_t length = multi.length(); + + if (sizeof(wchar_t) == 2) + { + wchar_t* wide = new wchar_t[length + 1]; + + const UTF8* srcStart = reinterpret_cast(multi.c_str()); + const UTF8* srcEnd = srcStart + length; + UTF16* targetStart = reinterpret_cast(wide); + UTF16* targetEnd = targetStart + length+1; + + ConversionResult res = ConvertUTF8toUTF16(&srcStart, srcEnd, + &targetStart, targetEnd, lenientConversion); + if (res != conversionOK) + { + delete[] wide; + throw std::runtime_error("bad conversion from multi to wide characters"); + } + + *targetStart = 0; + wstring convertedStr(wide); + delete[] wide; + + return convertedStr; + } + else if (sizeof(wchar_t) == 4) + { + wchar_t* wide = new wchar_t[length]; + + const UTF8* srcStart = reinterpret_cast(multi.c_str()); + const UTF8* srcEnd = srcStart + length; + UTF32* targetStart = reinterpret_cast(wide); + UTF32* targetEnd = targetStart + length; + + ConversionResult res = ConvertUTF8toUTF32(&srcStart, srcEnd, + &targetStart, targetEnd, lenientConversion); + if (res != conversionOK) + { + delete[] wide; + throw std::runtime_error("bad conversion from multi to wide characters"); + } + + *targetStart = 0; + wstring convertedStr(wide); + delete[] wide; + + return convertedStr; + } + else + { + throw std::runtime_error("unknown size of wide characters"); + } + return L""; +} + +string wide_to_multi(const wstring& wide) +{ + size_t length = wide.length(); + + if (sizeof(wchar_t) == 2) + { + size_t multiLength = 3 * length + 1; + char* multi = new char[multiLength]; + + const UTF16* srcStart = reinterpret_cast(wide.c_str()); + const UTF16* srcEnd = srcStart + length; + UTF8* targetStart = reinterpret_cast(multi); + UTF8* targetEnd = targetStart + multiLength; + + ConversionResult res = ConvertUTF16toUTF8(&srcStart, srcEnd, + &targetStart, targetEnd, lenientConversion); + if (res != conversionOK) + { + delete[] multi; + throw std::runtime_error("bad conversion from wide to multi-characters"); + } + + *targetStart = 0; + string convertedStr(multi); + delete[] multi; + + return convertedStr; + } + else if (sizeof(wchar_t) == 4) + { + size_t multiLength = 4 * length + 1; + char* multi = new char[multiLength]; + + const UTF32* srcStart = reinterpret_cast(wide.c_str()); + const UTF32* srcEnd = srcStart + length; + UTF8* targetStart = reinterpret_cast(multi); + UTF8* targetEnd = targetStart + multiLength; + + ConversionResult res = ConvertUTF32toUTF8(&srcStart, srcEnd, + &targetStart, targetEnd, lenientConversion); + if (res != conversionOK) + { + delete[] multi; + throw std::runtime_error("bad conversion from wide to multi-characters"); + } + + *targetStart = 0; + string convertedStr(multi); + delete[] multi; + + return convertedStr; + } + else + { + throw std::runtime_error("unknown size of wide characters"); + } + return ""; +} + + +} // namespace moof +