diff src/win32/7zip/7z/CPP/Common/MyString.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/MyString.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,198 @@
     1.4 +// Common/MyString.cpp
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +
     1.8 +#ifdef _WIN32
     1.9 +#include "StringConvert.h"
    1.10 +#else
    1.11 +#include <ctype.h>
    1.12 +#endif
    1.13 +
    1.14 +#include "MyString.h"
    1.15 +
    1.16 +
    1.17 +#ifdef _WIN32
    1.18 +
    1.19 +#ifndef _UNICODE
    1.20 +
    1.21 +wchar_t MyCharUpper(wchar_t c)
    1.22 +{
    1.23 +  if (c == 0)
    1.24 +    return 0;
    1.25 +  wchar_t *res = CharUpperW((LPWSTR)(UINT_PTR)(unsigned int)c);
    1.26 +  if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
    1.27 +    return (wchar_t)(unsigned int)(UINT_PTR)res;
    1.28 +  const int kBufferSize = 4;
    1.29 +  char s[kBufferSize + 1];
    1.30 +  int numChars = ::WideCharToMultiByte(CP_ACP, 0, &c, 1, s, kBufferSize, 0, 0);
    1.31 +  if (numChars == 0 || numChars > kBufferSize)
    1.32 +    return c;
    1.33 +  s[numChars] = 0;
    1.34 +  ::CharUpperA(s);
    1.35 +  ::MultiByteToWideChar(CP_ACP, 0, s, numChars, &c, 1);
    1.36 +  return c;
    1.37 +}
    1.38 +
    1.39 +wchar_t MyCharLower(wchar_t c)
    1.40 +{
    1.41 +  if (c == 0)
    1.42 +    return 0;
    1.43 +  wchar_t *res = CharLowerW((LPWSTR)(UINT_PTR)(unsigned int)c);
    1.44 +  if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
    1.45 +    return (wchar_t)(unsigned int)(UINT_PTR)res;
    1.46 +  const int kBufferSize = 4;
    1.47 +  char s[kBufferSize + 1];
    1.48 +  int numChars = ::WideCharToMultiByte(CP_ACP, 0, &c, 1, s, kBufferSize, 0, 0);
    1.49 +  if (numChars == 0 || numChars > kBufferSize)
    1.50 +    return c;
    1.51 +  s[numChars] = 0;
    1.52 +  ::CharLowerA(s);
    1.53 +  ::MultiByteToWideChar(CP_ACP, 0, s, numChars, &c, 1);
    1.54 +  return c;
    1.55 +}
    1.56 +
    1.57 +wchar_t * MyStringUpper(wchar_t *s)
    1.58 +{
    1.59 +  if (s == 0)
    1.60 +    return 0;
    1.61 +  wchar_t *res = CharUpperW(s);
    1.62 +  if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
    1.63 +    return res;
    1.64 +  AString a = UnicodeStringToMultiByte(s);
    1.65 +  a.MakeUpper();
    1.66 +  return MyStringCopy(s, (const wchar_t *)MultiByteToUnicodeString(a));
    1.67 +}
    1.68 +
    1.69 +wchar_t * MyStringLower(wchar_t *s)
    1.70 +{
    1.71 +  if (s == 0)
    1.72 +    return 0;
    1.73 +  wchar_t *res = CharLowerW(s);
    1.74 +  if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
    1.75 +    return res;
    1.76 +  AString a = UnicodeStringToMultiByte(s);
    1.77 +  a.MakeLower();
    1.78 +  return MyStringCopy(s, (const wchar_t *)MultiByteToUnicodeString(a));
    1.79 +}
    1.80 +
    1.81 +#endif
    1.82 +
    1.83 +/*
    1.84 +inline int ConvertCompareResult(int r) { return r - 2; }
    1.85 +
    1.86 +int MyStringCollate(const wchar_t *s1, const wchar_t *s2)
    1.87 +{
    1.88 +  int res = CompareStringW(
    1.89 +        LOCALE_USER_DEFAULT, SORT_STRINGSORT, s1, -1, s2, -1);
    1.90 +  #ifdef _UNICODE
    1.91 +  return ConvertCompareResult(res);
    1.92 +  #else
    1.93 +  if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
    1.94 +    return ConvertCompareResult(res);
    1.95 +  return MyStringCollate(UnicodeStringToMultiByte(s1),
    1.96 +        UnicodeStringToMultiByte(s2));
    1.97 +  #endif
    1.98 +}
    1.99 +
   1.100 +#ifndef _WIN32_WCE
   1.101 +int MyStringCollate(const char *s1, const char *s2)
   1.102 +{
   1.103 +  return ConvertCompareResult(CompareStringA(
   1.104 +    LOCALE_USER_DEFAULT, SORT_STRINGSORT, s1, -1, s2, -1));
   1.105 +}
   1.106 +
   1.107 +int MyStringCollateNoCase(const char *s1, const char *s2)
   1.108 +{
   1.109 +  return ConvertCompareResult(CompareStringA(
   1.110 +    LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT, s1, -1, s2, -1));
   1.111 +}
   1.112 +#endif
   1.113 +
   1.114 +int MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2)
   1.115 +{
   1.116 +  int res = CompareStringW(
   1.117 +        LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT, s1, -1, s2, -1);
   1.118 +  #ifdef _UNICODE
   1.119 +  return ConvertCompareResult(res);
   1.120 +  #else
   1.121 +  if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
   1.122 +    return ConvertCompareResult(res);
   1.123 +  return MyStringCollateNoCase(UnicodeStringToMultiByte(s1),
   1.124 +      UnicodeStringToMultiByte(s2));
   1.125 +  #endif
   1.126 +}
   1.127 +*/
   1.128 +
   1.129 +#else
   1.130 +
   1.131 +wchar_t MyCharUpper(wchar_t c)
   1.132 +{
   1.133 +  return toupper(c);
   1.134 +}
   1.135 +
   1.136 +/*
   1.137 +int MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2)
   1.138 +{
   1.139 +  for (;;)
   1.140 +  {
   1.141 +    wchar_t c1 = *s1++;
   1.142 +    wchar_t c2 = *s2++;
   1.143 +    wchar_t u1 = MyCharUpper(c1);
   1.144 +    wchar_t u2 = MyCharUpper(c2);
   1.145 +
   1.146 +    if (u1 < u2) return -1;
   1.147 +    if (u1 > u2) return 1;
   1.148 +    if (u1 == 0) return 0;
   1.149 +  }
   1.150 +}
   1.151 +*/
   1.152 +
   1.153 +#endif
   1.154 +
   1.155 +int MyStringCompare(const char *s1, const char *s2)
   1.156 +{
   1.157 +  for (;;)
   1.158 +  {
   1.159 +    unsigned char c1 = (unsigned char)*s1++;
   1.160 +    unsigned char c2 = (unsigned char)*s2++;
   1.161 +    if (c1 < c2) return -1;
   1.162 +    if (c1 > c2) return 1;
   1.163 +    if (c1 == 0) return 0;
   1.164 +  }
   1.165 +}
   1.166 +
   1.167 +int MyStringCompare(const wchar_t *s1, const wchar_t *s2)
   1.168 +{
   1.169 +  for (;;)
   1.170 +  {
   1.171 +    wchar_t c1 = *s1++;
   1.172 +    wchar_t c2 = *s2++;
   1.173 +    if (c1 < c2) return -1;
   1.174 +    if (c1 > c2) return 1;
   1.175 +    if (c1 == 0) return 0;
   1.176 +  }
   1.177 +}
   1.178 +
   1.179 +int MyStringCompareNoCase(const wchar_t *s1, const wchar_t *s2)
   1.180 +{
   1.181 +  for (;;)
   1.182 +  {
   1.183 +    wchar_t c1 = *s1++;
   1.184 +    wchar_t c2 = *s2++;
   1.185 +    if (c1 != c2)
   1.186 +    {
   1.187 +      wchar_t u1 = MyCharUpper(c1);
   1.188 +      wchar_t u2 = MyCharUpper(c2);
   1.189 +      if (u1 < u2) return -1;
   1.190 +      if (u1 > u2) return 1;
   1.191 +    }
   1.192 +    if (c1 == 0) return 0;
   1.193 +  }
   1.194 +}
   1.195 +
   1.196 +#ifdef _WIN32
   1.197 +int MyStringCompareNoCase(const char *s1, const char *s2)
   1.198 +{
   1.199 +  return MyStringCompareNoCase(MultiByteToUnicodeString(s1), MultiByteToUnicodeString(s2));
   1.200 +}
   1.201 +#endif