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