rlm@1: // Common/StringConvert.cpp rlm@1: rlm@1: #include "StdAfx.h" rlm@1: rlm@1: #include "StringConvert.h" rlm@1: rlm@1: #ifndef _WIN32 rlm@1: #include rlm@1: #endif rlm@1: rlm@1: #ifdef _WIN32 rlm@1: UString MultiByteToUnicodeString(const AString &srcString, UINT codePage) rlm@1: { rlm@1: UString resultString; rlm@1: if (!srcString.IsEmpty()) rlm@1: { rlm@1: int numChars = MultiByteToWideChar(codePage, 0, srcString, rlm@1: srcString.Length(), resultString.GetBuffer(srcString.Length()), rlm@1: srcString.Length() + 1); rlm@1: #ifndef _WIN32_WCE rlm@1: if (numChars == 0) rlm@1: throw 282228; rlm@1: #endif rlm@1: resultString.ReleaseBuffer(numChars); rlm@1: } rlm@1: return resultString; rlm@1: } rlm@1: rlm@1: AString UnicodeStringToMultiByte(const UString &s, UINT codePage, char defaultChar, bool &defaultCharWasUsed) rlm@1: { rlm@1: AString dest; rlm@1: defaultCharWasUsed = false; rlm@1: if (!s.IsEmpty()) rlm@1: { rlm@1: int numRequiredBytes = s.Length() * 2; rlm@1: BOOL defUsed; rlm@1: int numChars = WideCharToMultiByte(codePage, 0, s, s.Length(), rlm@1: dest.GetBuffer(numRequiredBytes), numRequiredBytes + 1, rlm@1: &defaultChar, &defUsed); rlm@1: defaultCharWasUsed = (defUsed != FALSE); rlm@1: #ifndef _WIN32_WCE rlm@1: if (numChars == 0) rlm@1: throw 282229; rlm@1: #endif rlm@1: dest.ReleaseBuffer(numChars); rlm@1: } rlm@1: return dest; rlm@1: } rlm@1: rlm@1: AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage) rlm@1: { rlm@1: bool defaultCharWasUsed; rlm@1: return UnicodeStringToMultiByte(srcString, codePage, '_', defaultCharWasUsed); rlm@1: } rlm@1: rlm@1: #ifndef _WIN32_WCE rlm@1: AString SystemStringToOemString(const CSysString &srcString) rlm@1: { rlm@1: AString result; rlm@1: CharToOem(srcString, result.GetBuffer(srcString.Length() * 2)); rlm@1: result.ReleaseBuffer(); rlm@1: return result; rlm@1: } rlm@1: #endif rlm@1: rlm@1: #else rlm@1: rlm@1: UString MultiByteToUnicodeString(const AString &srcString, UINT codePage) rlm@1: { rlm@1: UString resultString; rlm@1: for (int i = 0; i < srcString.Length(); i++) rlm@1: resultString += wchar_t(srcString[i]); rlm@1: /* rlm@1: if (!srcString.IsEmpty()) rlm@1: { rlm@1: int numChars = mbstowcs(resultString.GetBuffer(srcString.Length()), srcString, srcString.Length() + 1); rlm@1: if (numChars < 0) throw "Your environment does not support UNICODE"; rlm@1: resultString.ReleaseBuffer(numChars); rlm@1: } rlm@1: */ rlm@1: return resultString; rlm@1: } rlm@1: rlm@1: AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage) rlm@1: { rlm@1: AString resultString; rlm@1: for (int i = 0; i < srcString.Length(); i++) rlm@1: resultString += char(srcString[i]); rlm@1: /* rlm@1: if (!srcString.IsEmpty()) rlm@1: { rlm@1: int numRequiredBytes = srcString.Length() * 6 + 1; rlm@1: int numChars = wcstombs(resultString.GetBuffer(numRequiredBytes), srcString, numRequiredBytes); rlm@1: if (numChars < 0) throw "Your environment does not support UNICODE"; rlm@1: resultString.ReleaseBuffer(numChars); rlm@1: } rlm@1: */ rlm@1: return resultString; rlm@1: } rlm@1: rlm@1: #endif rlm@1: