Annotation of elwix/tools/oldlzma/SRC/Common/String.cpp, revision 1.1.1.1

1.1       misho       1: // Common/String.cpp
                      2: 
                      3: #include "StdAfx.h"
                      4: 
                      5: #ifdef _WIN32
                      6: #include "StringConvert.h"
                      7: #else
                      8: #include <ctype.h>
                      9: #endif
                     10: 
                     11: #include "Common/String.h"
                     12: 
                     13: 
                     14: #ifdef _WIN32
                     15: 
                     16: #ifndef _UNICODE
                     17: 
                     18: wchar_t MyCharUpper(wchar_t c)
                     19: {
                     20:   if (c == 0)
                     21:     return 0;
                     22:   wchar_t *res = CharUpperW((LPWSTR)(unsigned int)c);
                     23:   if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
                     24:     return (wchar_t)(unsigned int)res;
                     25:   const int kBufferSize = 4;
                     26:   char s[kBufferSize];
                     27:   int numChars = ::WideCharToMultiByte(CP_ACP, 0, &c, 1, s, kBufferSize, 0, 0);
                     28:   ::CharUpperA(s);
                     29:   ::MultiByteToWideChar(CP_ACP, 0, s, numChars, &c, 1);
                     30:   return c;
                     31: }
                     32: 
                     33: wchar_t MyCharLower(wchar_t c)
                     34: {
                     35:   if (c == 0)
                     36:     return 0;
                     37:   wchar_t *res = CharLowerW((LPWSTR)(unsigned int)c);
                     38:   if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
                     39:     return (wchar_t)(unsigned int)res;
                     40:   const int kBufferSize = 4;
                     41:   char s[kBufferSize];
                     42:   int numChars = ::WideCharToMultiByte(CP_ACP, 0, &c, 1, s, kBufferSize, 0, 0);
                     43:   ::CharLowerA(s);
                     44:   ::MultiByteToWideChar(CP_ACP, 0, s, numChars, &c, 1);
                     45:   return c;
                     46: }
                     47: 
                     48: wchar_t * MyStringUpper(wchar_t *s)
                     49: {
                     50:   if (s == 0)
                     51:     return 0;
                     52:   wchar_t *res = CharUpperW(s);
                     53:   if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
                     54:     return res;
                     55:   AString a = UnicodeStringToMultiByte(s);
                     56:   a.MakeUpper();
                     57:   return MyStringCopy(s, (const wchar_t *)MultiByteToUnicodeString(a));
                     58: }
                     59: 
                     60: wchar_t * MyStringLower(wchar_t *s)
                     61: { 
                     62:   if (s == 0)
                     63:     return 0;
                     64:   wchar_t *res = CharLowerW(s);
                     65:   if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
                     66:     return res;
                     67:   AString a = UnicodeStringToMultiByte(s);
                     68:   a.MakeLower();
                     69:   return MyStringCopy(s, (const wchar_t *)MultiByteToUnicodeString(a));
                     70: }
                     71: 
                     72: #endif
                     73: 
                     74: inline int ConvertCompareResult(int r) { return r - 2; }
                     75: 
                     76: int MyStringCollate(const wchar_t *s1, const wchar_t *s2)
                     77: { 
                     78:   int res = CompareStringW(
                     79:         LOCALE_USER_DEFAULT, SORT_STRINGSORT, s1, -1, s2, -1); 
                     80:   #ifdef _UNICODE
                     81:   return ConvertCompareResult(res);
                     82:   #else
                     83:   if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
                     84:     return ConvertCompareResult(res);
                     85:   return MyStringCollate(UnicodeStringToMultiByte(s1), 
                     86:         UnicodeStringToMultiByte(s2));
                     87:   #endif
                     88: }
                     89: 
                     90: #ifndef _WIN32_WCE
                     91: int MyStringCollate(const char *s1, const char *s2)
                     92: { 
                     93:   return ConvertCompareResult(CompareStringA(
                     94:     LOCALE_USER_DEFAULT, SORT_STRINGSORT, s1, -1, s2, -1)); 
                     95: }
                     96: 
                     97: int MyStringCollateNoCase(const char *s1, const char *s2)
                     98: { 
                     99:   return ConvertCompareResult(CompareStringA(
                    100:     LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT, s1, -1, s2, -1)); 
                    101: }
                    102: #endif
                    103: 
                    104: int MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2)
                    105: { 
                    106:   int res = CompareStringW(
                    107:         LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT, s1, -1, s2, -1); 
                    108:   #ifdef _UNICODE
                    109:   return ConvertCompareResult(res);
                    110:   #else
                    111:   if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
                    112:     return ConvertCompareResult(res);
                    113:   return MyStringCollateNoCase(UnicodeStringToMultiByte(s1), 
                    114:       UnicodeStringToMultiByte(s2));
                    115:   #endif
                    116: }
                    117: 
                    118: #else
                    119: 
                    120: inline int NormalizeCompareResult(int res)
                    121: {
                    122:   if (res < 0) return -1;
                    123:   if (res > 0) return 1;
                    124:   return 0;
                    125: }
                    126: 
                    127: /*
                    128: inline wchar_t MyCharUpper(wchar_t c)
                    129:   { return towupper(c); }
                    130: */
                    131: wchar_t MyCharUpper(wchar_t c)
                    132: {
                    133:   return toupper(c);
                    134: }
                    135: 
                    136: int MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2)
                    137: { 
                    138:   while (true)
                    139:   {
                    140:     wchar_t c1 = *s1++;
                    141:     wchar_t c2 = *s2++;
                    142:     wchar_t u1 = MyCharUpper(c1);
                    143:     wchar_t u2 = MyCharUpper(c2);
                    144: 
                    145:     if (u1 < u2) return -1;
                    146:     if (u1 > u2) return 1;
                    147:     if (u1 == 0) return 0;
                    148:   }
                    149: }
                    150: 
                    151: #endif
                    152: 
                    153: int MyStringCompare(const char *s1, const char *s2)
                    154: { 
                    155:   while (true)
                    156:   {
                    157:     unsigned char c1 = (unsigned char)*s1++;
                    158:     unsigned char c2 = (unsigned char)*s2++;
                    159:     if (c1 < c2) return -1;
                    160:     if (c1 > c2) return 1;
                    161:     if (c1 == 0) return 0;
                    162:   }
                    163: }
                    164: 
                    165: int MyStringCompare(const wchar_t *s1, const wchar_t *s2)
                    166: { 
                    167:   while (true)
                    168:   {
                    169:     wchar_t c1 = *s1++;
                    170:     wchar_t c2 = *s2++;
                    171:     if (c1 < c2) return -1;
                    172:     if (c1 > c2) return 1;
                    173:     if (c1 == 0) return 0;
                    174:   }
                    175: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>