Home | History | Annotate | Download | only in Common
      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.Len(), resultString.GetBuffer(srcString.Len()),
     19         srcString.Len() + 1);
     20     if (numChars == 0)
     21       throw 282228;
     22     resultString.ReleaseBuffer(numChars);
     23   }
     24   return resultString;
     25 }
     26 
     27 void MultiByteToUnicodeString2(UString &dest, const AString &srcString, UINT codePage)
     28 {
     29   dest.Empty();
     30   if (!srcString.IsEmpty())
     31   {
     32     wchar_t *destBuf = dest.GetBuffer(srcString.Len());
     33     const char *sp = (const char *)srcString;
     34     unsigned i;
     35     for (i = 0;;)
     36     {
     37       char c = sp[i];
     38       if ((Byte)c >= 0x80 || c == 0)
     39         break;
     40       destBuf[i++] = (wchar_t)c;
     41     }
     42 
     43     if (i != srcString.Len())
     44     {
     45       unsigned numChars = MultiByteToWideChar(codePage, 0, sp + i,
     46           srcString.Len() - i, destBuf + i,
     47           srcString.Len() + 1 - i);
     48       if (numChars == 0)
     49         throw 282228;
     50       i += numChars;
     51     }
     52     dest.ReleaseBuffer(i);
     53   }
     54 }
     55 
     56 void UnicodeStringToMultiByte2(AString &dest, const UString &s, UINT codePage, char defaultChar, bool &defaultCharWasUsed)
     57 {
     58   dest.Empty();
     59   defaultCharWasUsed = false;
     60   if (!s.IsEmpty())
     61   {
     62     unsigned numRequiredBytes = s.Len() * 2;
     63     char *destBuf = dest.GetBuffer(numRequiredBytes);
     64     unsigned i;
     65     const wchar_t *sp = (const wchar_t *)s;
     66     for (i = 0;;)
     67     {
     68       wchar_t c = sp[i];
     69       if (c >= 0x80 || c == 0)
     70         break;
     71       destBuf[i++] = (char)c;
     72     }
     73     defaultCharWasUsed = false;
     74     if (i != s.Len())
     75     {
     76       BOOL defUsed;
     77       unsigned numChars = WideCharToMultiByte(codePage, 0, sp + i, s.Len() - i,
     78           destBuf + i, numRequiredBytes + 1 - i,
     79           &defaultChar, &defUsed);
     80       defaultCharWasUsed = (defUsed != FALSE);
     81       if (numChars == 0)
     82         throw 282229;
     83       i += numChars;
     84     }
     85     dest.ReleaseBuffer(i);
     86   }
     87 }
     88 
     89 void UnicodeStringToMultiByte2(AString &dest, const UString &srcString, UINT codePage)
     90 {
     91   bool defaultCharWasUsed;
     92   UnicodeStringToMultiByte2(dest, srcString, codePage, '_', defaultCharWasUsed);
     93 }
     94 
     95 AString UnicodeStringToMultiByte(const UString &s, UINT codePage, char defaultChar, bool &defaultCharWasUsed)
     96 {
     97   AString dest;
     98   defaultCharWasUsed = false;
     99   if (!s.IsEmpty())
    100   {
    101     unsigned numRequiredBytes = s.Len() * 2;
    102     BOOL defUsed;
    103     int numChars = WideCharToMultiByte(codePage, 0, s, s.Len(),
    104         dest.GetBuffer(numRequiredBytes), numRequiredBytes + 1,
    105         &defaultChar, &defUsed);
    106     defaultCharWasUsed = (defUsed != FALSE);
    107     if (numChars == 0)
    108       throw 282229;
    109     dest.ReleaseBuffer(numChars);
    110   }
    111   return dest;
    112 }
    113 
    114 AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage)
    115 {
    116   bool defaultCharWasUsed;
    117   return UnicodeStringToMultiByte(srcString, codePage, '_', defaultCharWasUsed);
    118 }
    119 
    120 #ifndef UNDER_CE
    121 AString SystemStringToOemString(const CSysString &srcString)
    122 {
    123   AString result;
    124   CharToOem(srcString, result.GetBuffer(srcString.Len() * 2));
    125   result.ReleaseBuffer();
    126   return result;
    127 }
    128 #endif
    129 
    130 #else
    131 
    132 UString MultiByteToUnicodeString(const AString &srcString, UINT codePage)
    133 {
    134   UString resultString;
    135   for (unsigned i = 0; i < srcString.Len(); i++)
    136     resultString += (wchar_t)srcString[i];
    137   /*
    138   if (!srcString.IsEmpty())
    139   {
    140     int numChars = mbstowcs(resultString.GetBuffer(srcString.Len()), srcString, srcString.Len() + 1);
    141     if (numChars < 0) throw "Your environment does not support UNICODE";
    142     resultString.ReleaseBuffer(numChars);
    143   }
    144   */
    145   return resultString;
    146 }
    147 
    148 AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage)
    149 {
    150   AString resultString;
    151   for (unsigned i = 0; i < srcString.Len(); i++)
    152     resultString += (char)srcString[i];
    153   /*
    154   if (!srcString.IsEmpty())
    155   {
    156     int numRequiredBytes = srcString.Len() * 6 + 1;
    157     int numChars = wcstombs(resultString.GetBuffer(numRequiredBytes), srcString, numRequiredBytes);
    158     if (numChars < 0) throw "Your environment does not support UNICODE";
    159     resultString.ReleaseBuffer(numChars);
    160   }
    161   */
    162   return resultString;
    163 }
    164 
    165 #endif
    166