Upload 2.0.2
[physicsfs] / lzma / CPP / Common / StringConvert.cpp
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
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     #ifndef _WIN32_WCE
21     if(numChars == 0)
22       throw 282228;
23     #endif
24     resultString.ReleaseBuffer(numChars);
25   }
26   return resultString;
27 }
28
29 AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage)
30 {
31   AString resultString;
32   if(!srcString.IsEmpty())
33   {
34     int numRequiredBytes = srcString.Length() * 2;
35     char defaultChar = '_';
36     int numChars = WideCharToMultiByte(codePage, 0, srcString, 
37       srcString.Length(), resultString.GetBuffer(numRequiredBytes), 
38       numRequiredBytes + 1, &defaultChar, NULL);
39     #ifndef _WIN32_WCE
40     if(numChars == 0)
41       throw 282229;
42     #endif
43     resultString.ReleaseBuffer(numChars);
44   }
45   return resultString;
46 }
47
48 #ifndef _WIN32_WCE
49 AString SystemStringToOemString(const CSysString &srcString)
50 {
51   AString result;
52   CharToOem(srcString, result.GetBuffer(srcString.Length() * 2));
53   result.ReleaseBuffer();
54   return result;
55 }
56 #endif
57
58 #else
59
60 UString MultiByteToUnicodeString(const AString &srcString, UINT codePage)
61 {
62   UString resultString;
63   for (int i = 0; i < srcString.Length(); i++)
64     resultString += wchar_t(srcString[i]);
65   /*
66   if(!srcString.IsEmpty())
67   {
68     int numChars = mbstowcs(resultString.GetBuffer(srcString.Length()), srcString, srcString.Length() + 1);
69     if (numChars < 0) throw "Your environment does not support UNICODE";
70     resultString.ReleaseBuffer(numChars);
71   }
72   */
73   return resultString;
74 }
75
76 AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage)
77 {
78   AString resultString;
79   for (int i = 0; i < srcString.Length(); i++)
80     resultString += char(srcString[i]);
81   /*
82   if(!srcString.IsEmpty())
83   {
84     int numRequiredBytes = srcString.Length() * 6 + 1;
85     int numChars = wcstombs(resultString.GetBuffer(numRequiredBytes), srcString, numRequiredBytes);
86     if (numChars < 0) throw "Your environment does not support UNICODE";
87     resultString.ReleaseBuffer(numChars);
88   }
89   */
90   return resultString;
91 }
92
93 #endif
94