annotate src/win32/7zip/7z/CPP/Common/StringConvert.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
rev   line source
rlm@1 1 // Common/StringConvert.cpp
rlm@1 2
rlm@1 3 #include "StdAfx.h"
rlm@1 4
rlm@1 5 #include "StringConvert.h"
rlm@1 6
rlm@1 7 #ifndef _WIN32
rlm@1 8 #include <stdlib.h>
rlm@1 9 #endif
rlm@1 10
rlm@1 11 #ifdef _WIN32
rlm@1 12 UString MultiByteToUnicodeString(const AString &srcString, UINT codePage)
rlm@1 13 {
rlm@1 14 UString resultString;
rlm@1 15 if (!srcString.IsEmpty())
rlm@1 16 {
rlm@1 17 int numChars = MultiByteToWideChar(codePage, 0, srcString,
rlm@1 18 srcString.Length(), resultString.GetBuffer(srcString.Length()),
rlm@1 19 srcString.Length() + 1);
rlm@1 20 #ifndef _WIN32_WCE
rlm@1 21 if (numChars == 0)
rlm@1 22 throw 282228;
rlm@1 23 #endif
rlm@1 24 resultString.ReleaseBuffer(numChars);
rlm@1 25 }
rlm@1 26 return resultString;
rlm@1 27 }
rlm@1 28
rlm@1 29 AString UnicodeStringToMultiByte(const UString &s, UINT codePage, char defaultChar, bool &defaultCharWasUsed)
rlm@1 30 {
rlm@1 31 AString dest;
rlm@1 32 defaultCharWasUsed = false;
rlm@1 33 if (!s.IsEmpty())
rlm@1 34 {
rlm@1 35 int numRequiredBytes = s.Length() * 2;
rlm@1 36 BOOL defUsed;
rlm@1 37 int numChars = WideCharToMultiByte(codePage, 0, s, s.Length(),
rlm@1 38 dest.GetBuffer(numRequiredBytes), numRequiredBytes + 1,
rlm@1 39 &defaultChar, &defUsed);
rlm@1 40 defaultCharWasUsed = (defUsed != FALSE);
rlm@1 41 #ifndef _WIN32_WCE
rlm@1 42 if (numChars == 0)
rlm@1 43 throw 282229;
rlm@1 44 #endif
rlm@1 45 dest.ReleaseBuffer(numChars);
rlm@1 46 }
rlm@1 47 return dest;
rlm@1 48 }
rlm@1 49
rlm@1 50 AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage)
rlm@1 51 {
rlm@1 52 bool defaultCharWasUsed;
rlm@1 53 return UnicodeStringToMultiByte(srcString, codePage, '_', defaultCharWasUsed);
rlm@1 54 }
rlm@1 55
rlm@1 56 #ifndef _WIN32_WCE
rlm@1 57 AString SystemStringToOemString(const CSysString &srcString)
rlm@1 58 {
rlm@1 59 AString result;
rlm@1 60 CharToOem(srcString, result.GetBuffer(srcString.Length() * 2));
rlm@1 61 result.ReleaseBuffer();
rlm@1 62 return result;
rlm@1 63 }
rlm@1 64 #endif
rlm@1 65
rlm@1 66 #else
rlm@1 67
rlm@1 68 UString MultiByteToUnicodeString(const AString &srcString, UINT codePage)
rlm@1 69 {
rlm@1 70 UString resultString;
rlm@1 71 for (int i = 0; i < srcString.Length(); i++)
rlm@1 72 resultString += wchar_t(srcString[i]);
rlm@1 73 /*
rlm@1 74 if (!srcString.IsEmpty())
rlm@1 75 {
rlm@1 76 int numChars = mbstowcs(resultString.GetBuffer(srcString.Length()), srcString, srcString.Length() + 1);
rlm@1 77 if (numChars < 0) throw "Your environment does not support UNICODE";
rlm@1 78 resultString.ReleaseBuffer(numChars);
rlm@1 79 }
rlm@1 80 */
rlm@1 81 return resultString;
rlm@1 82 }
rlm@1 83
rlm@1 84 AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage)
rlm@1 85 {
rlm@1 86 AString resultString;
rlm@1 87 for (int i = 0; i < srcString.Length(); i++)
rlm@1 88 resultString += char(srcString[i]);
rlm@1 89 /*
rlm@1 90 if (!srcString.IsEmpty())
rlm@1 91 {
rlm@1 92 int numRequiredBytes = srcString.Length() * 6 + 1;
rlm@1 93 int numChars = wcstombs(resultString.GetBuffer(numRequiredBytes), srcString, numRequiredBytes);
rlm@1 94 if (numChars < 0) throw "Your environment does not support UNICODE";
rlm@1 95 resultString.ReleaseBuffer(numChars);
rlm@1 96 }
rlm@1 97 */
rlm@1 98 return resultString;
rlm@1 99 }
rlm@1 100
rlm@1 101 #endif
rlm@1 102