diff src/win32/7zip/7z/CPP/Common/StringConvert.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/7zip/7z/CPP/Common/StringConvert.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,102 @@
     1.4 +// Common/StringConvert.cpp
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +
     1.8 +#include "StringConvert.h"
     1.9 +
    1.10 +#ifndef _WIN32
    1.11 +#include <stdlib.h>
    1.12 +#endif
    1.13 +
    1.14 +#ifdef _WIN32
    1.15 +UString MultiByteToUnicodeString(const AString &srcString, UINT codePage)
    1.16 +{
    1.17 +  UString resultString;
    1.18 +  if (!srcString.IsEmpty())
    1.19 +  {
    1.20 +    int numChars = MultiByteToWideChar(codePage, 0, srcString,
    1.21 +      srcString.Length(), resultString.GetBuffer(srcString.Length()),
    1.22 +      srcString.Length() + 1);
    1.23 +    #ifndef _WIN32_WCE
    1.24 +    if (numChars == 0)
    1.25 +      throw 282228;
    1.26 +    #endif
    1.27 +    resultString.ReleaseBuffer(numChars);
    1.28 +  }
    1.29 +  return resultString;
    1.30 +}
    1.31 +
    1.32 +AString UnicodeStringToMultiByte(const UString &s, UINT codePage, char defaultChar, bool &defaultCharWasUsed)
    1.33 +{
    1.34 +  AString dest;
    1.35 +  defaultCharWasUsed = false;
    1.36 +  if (!s.IsEmpty())
    1.37 +  {
    1.38 +    int numRequiredBytes = s.Length() * 2;
    1.39 +    BOOL defUsed;
    1.40 +    int numChars = WideCharToMultiByte(codePage, 0, s, s.Length(),
    1.41 +        dest.GetBuffer(numRequiredBytes), numRequiredBytes + 1,
    1.42 +        &defaultChar, &defUsed);
    1.43 +    defaultCharWasUsed = (defUsed != FALSE);
    1.44 +    #ifndef _WIN32_WCE
    1.45 +    if (numChars == 0)
    1.46 +      throw 282229;
    1.47 +    #endif
    1.48 +    dest.ReleaseBuffer(numChars);
    1.49 +  }
    1.50 +  return dest;
    1.51 +}
    1.52 +
    1.53 +AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage)
    1.54 +{
    1.55 +  bool defaultCharWasUsed;
    1.56 +  return UnicodeStringToMultiByte(srcString, codePage, '_', defaultCharWasUsed);
    1.57 +}
    1.58 +
    1.59 +#ifndef _WIN32_WCE
    1.60 +AString SystemStringToOemString(const CSysString &srcString)
    1.61 +{
    1.62 +  AString result;
    1.63 +  CharToOem(srcString, result.GetBuffer(srcString.Length() * 2));
    1.64 +  result.ReleaseBuffer();
    1.65 +  return result;
    1.66 +}
    1.67 +#endif
    1.68 +
    1.69 +#else
    1.70 +
    1.71 +UString MultiByteToUnicodeString(const AString &srcString, UINT codePage)
    1.72 +{
    1.73 +  UString resultString;
    1.74 +  for (int i = 0; i < srcString.Length(); i++)
    1.75 +    resultString += wchar_t(srcString[i]);
    1.76 +  /*
    1.77 +  if (!srcString.IsEmpty())
    1.78 +  {
    1.79 +    int numChars = mbstowcs(resultString.GetBuffer(srcString.Length()), srcString, srcString.Length() + 1);
    1.80 +    if (numChars < 0) throw "Your environment does not support UNICODE";
    1.81 +    resultString.ReleaseBuffer(numChars);
    1.82 +  }
    1.83 +  */
    1.84 +  return resultString;
    1.85 +}
    1.86 +
    1.87 +AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage)
    1.88 +{
    1.89 +  AString resultString;
    1.90 +  for (int i = 0; i < srcString.Length(); i++)
    1.91 +    resultString += char(srcString[i]);
    1.92 +  /*
    1.93 +  if (!srcString.IsEmpty())
    1.94 +  {
    1.95 +    int numRequiredBytes = srcString.Length() * 6 + 1;
    1.96 +    int numChars = wcstombs(resultString.GetBuffer(numRequiredBytes), srcString, numRequiredBytes);
    1.97 +    if (numChars < 0) throw "Your environment does not support UNICODE";
    1.98 +    resultString.ReleaseBuffer(numChars);
    1.99 +  }
   1.100 +  */
   1.101 +  return resultString;
   1.102 +}
   1.103 +
   1.104 +#endif
   1.105 +