view src/win32/7zip/7z/CPP/Common/StringConvert.cpp @ 3:b05d00f19d80

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