other-licenses/7zstub/src/Common/StringConvert.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial