Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | // Common/StringConvert.cpp |
michael@0 | 2 | |
michael@0 | 3 | #include "StdAfx.h" |
michael@0 | 4 | |
michael@0 | 5 | #include "StringConvert.h" |
michael@0 | 6 | |
michael@0 | 7 | #ifndef _WIN32 |
michael@0 | 8 | #include <stdlib.h> |
michael@0 | 9 | #endif |
michael@0 | 10 | |
michael@0 | 11 | #ifdef _WIN32 |
michael@0 | 12 | UString MultiByteToUnicodeString(const AString &srcString, UINT codePage) |
michael@0 | 13 | { |
michael@0 | 14 | UString resultString; |
michael@0 | 15 | if(!srcString.IsEmpty()) |
michael@0 | 16 | { |
michael@0 | 17 | int numChars = MultiByteToWideChar(codePage, 0, srcString, |
michael@0 | 18 | srcString.Length(), resultString.GetBuffer(srcString.Length()), |
michael@0 | 19 | srcString.Length() + 1); |
michael@0 | 20 | #ifndef _WIN32_WCE |
michael@0 | 21 | if(numChars == 0) |
michael@0 | 22 | throw 282228; |
michael@0 | 23 | #endif |
michael@0 | 24 | resultString.ReleaseBuffer(numChars); |
michael@0 | 25 | } |
michael@0 | 26 | return resultString; |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage) |
michael@0 | 30 | { |
michael@0 | 31 | AString resultString; |
michael@0 | 32 | if(!srcString.IsEmpty()) |
michael@0 | 33 | { |
michael@0 | 34 | int numRequiredBytes = srcString.Length() * 2; |
michael@0 | 35 | int numChars = WideCharToMultiByte(codePage, 0, srcString, |
michael@0 | 36 | srcString.Length(), resultString.GetBuffer(numRequiredBytes), |
michael@0 | 37 | numRequiredBytes + 1, NULL, NULL); |
michael@0 | 38 | #ifndef _WIN32_WCE |
michael@0 | 39 | if(numChars == 0) |
michael@0 | 40 | throw 282229; |
michael@0 | 41 | #endif |
michael@0 | 42 | resultString.ReleaseBuffer(numChars); |
michael@0 | 43 | } |
michael@0 | 44 | return resultString; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | #ifndef _WIN32_WCE |
michael@0 | 48 | AString SystemStringToOemString(const CSysString &srcString) |
michael@0 | 49 | { |
michael@0 | 50 | AString result; |
michael@0 | 51 | CharToOem(srcString, result.GetBuffer(srcString.Length() * 2)); |
michael@0 | 52 | result.ReleaseBuffer(); |
michael@0 | 53 | return result; |
michael@0 | 54 | } |
michael@0 | 55 | #endif |
michael@0 | 56 | |
michael@0 | 57 | #else |
michael@0 | 58 | |
michael@0 | 59 | UString MultiByteToUnicodeString(const AString &srcString, UINT codePage) |
michael@0 | 60 | { |
michael@0 | 61 | UString resultString; |
michael@0 | 62 | for (int i = 0; i < srcString.Length(); i++) |
michael@0 | 63 | resultString += wchar_t(srcString[i]); |
michael@0 | 64 | /* |
michael@0 | 65 | if(!srcString.IsEmpty()) |
michael@0 | 66 | { |
michael@0 | 67 | int numChars = mbstowcs(resultString.GetBuffer(srcString.Length()), srcString, srcString.Length() + 1); |
michael@0 | 68 | if (numChars < 0) throw "Your environment does not support UNICODE"; |
michael@0 | 69 | resultString.ReleaseBuffer(numChars); |
michael@0 | 70 | } |
michael@0 | 71 | */ |
michael@0 | 72 | return resultString; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage) |
michael@0 | 76 | { |
michael@0 | 77 | AString resultString; |
michael@0 | 78 | for (int i = 0; i < srcString.Length(); i++) |
michael@0 | 79 | resultString += char(srcString[i]); |
michael@0 | 80 | /* |
michael@0 | 81 | if(!srcString.IsEmpty()) |
michael@0 | 82 | { |
michael@0 | 83 | int numRequiredBytes = srcString.Length() * 6 + 1; |
michael@0 | 84 | int numChars = wcstombs(resultString.GetBuffer(numRequiredBytes), srcString, numRequiredBytes); |
michael@0 | 85 | if (numChars < 0) throw "Your environment does not support UNICODE"; |
michael@0 | 86 | resultString.ReleaseBuffer(numChars); |
michael@0 | 87 | } |
michael@0 | 88 | */ |
michael@0 | 89 | return resultString; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | #endif |
michael@0 | 93 |