"Fossies" - the Fresh Open Source Software Archive

Member "AutoHotkey_L-1.1.33.09/source/StringConv.h" (8 May 2021, 5014 Bytes) of package /windows/misc/AutoHotkey_L-1.1.33.09.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "StringConv.h" see the Fossies "Dox" file reference documentation.

    1 #pragma once
    2 
    3 #ifdef _WIN32
    4 
    5 #define IsValidUTF8(str, cch) MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, (str), (cch), NULL, 0)
    6 #define IsValidACP(str, cch) MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, (str), (cch), NULL, 0)
    7 
    8 LPCWSTR StringUTF8ToWChar(LPCSTR sUTF8, CStringW &sWChar, int iChars = -1);
    9 LPCWSTR StringCharToWChar(LPCSTR sChar, CStringW &sWChar, int iChars = -1, UINT codepage = CP_ACP);
   10 LPCSTR StringWCharToUTF8(LPCWSTR sWChar, CStringA &sUTF8, int iChars = -1);
   11 LPCSTR StringCharToUTF8(LPCSTR sChar, CStringA &sUTF8, int iChars = -1, UINT codepage = CP_ACP);
   12 LPCSTR StringWCharToChar(LPCWSTR sWChar, CStringA &sChar, int iChars = -1, char chDef = '?', UINT codepage = CP_ACP);
   13 LPCSTR StringUTF8ToChar(LPCSTR sUTF8, CStringA &sChar, int iChars = -1, char chDef = '?', UINT codepage = CP_ACP);
   14 
   15 LPCWSTR _StringDummyConvW(LPCWSTR sSrc, CStringW &sDest, int iChars = -1);
   16 LPCSTR _StringDummyConvA(LPCSTR sSrc, CStringA &sDest, int iChars = -1);
   17 
   18 #ifdef UNICODE
   19 
   20 #define StringTCharToWChar  _StringDummyConvW
   21 #define StringTCharToUTF8   StringWCharToUTF8
   22 #define StringTCharToChar   StringWCharToChar
   23 inline LPCWSTR StringWCharToTChar(LPCWSTR sWChar, CStringW &sTChar, int iChars = -1, char chDef = '?')
   24 { return _StringDummyConvW(sWChar, sTChar, iChars); }
   25 inline LPCWSTR StringUTF8ToTChar(LPCSTR sUTF8, CStringW &sTChar, int iChars = -1, char chDef = '?')
   26 { return StringUTF8ToWChar(sUTF8, sTChar, iChars); }
   27 #define StringCharToTChar   StringCharToWChar
   28 
   29 #else
   30 // ! _UNICODE
   31 #define StringTCharToWChar  StringCharToWChar
   32 #define StringTCharToUTF8   StringCharToUTF8
   33 #define StringTCharToChar   _StringDummyConvA
   34 #define StringWCharToTChar  StringWCharToChar
   35 #define StringUTF8ToTChar   StringUTF8ToChar
   36 #define StringCharToTChar   _StringDummyConvA
   37 
   38 #endif // _UNICODE
   39 
   40 class CStringWCharFromUTF8 : public CStringW
   41 {
   42 public:
   43     CStringWCharFromUTF8(LPCSTR sUTF8, int iChars = -1)
   44     { StringUTF8ToWChar(sUTF8, *this, iChars); }
   45 };
   46 
   47 class CStringWCharFromChar : public CStringW
   48 {
   49 public:
   50     CStringWCharFromChar(LPCSTR sChar, int iChars = -1, UINT codepage = CP_ACP)
   51     { StringCharToWChar(sChar, *this, iChars, codepage); }
   52 };
   53 
   54 class CStringUTF8FromWChar : public CStringA
   55 {
   56 public:
   57     CStringUTF8FromWChar(LPCWSTR sWChar, int iChars = -1)
   58     { StringWCharToUTF8(sWChar, *this, iChars); }
   59 };
   60 
   61 class CStringUTF8FromChar : public CStringA
   62 {
   63 public:
   64     CStringUTF8FromChar(LPCSTR sChar, int iChars = -1, UINT codepage = CP_ACP)
   65     { StringCharToUTF8(sChar, *this, iChars, codepage); }
   66 };
   67 
   68 class CStringCharFromWChar : public CStringA
   69 {
   70 public:
   71     CStringCharFromWChar(LPCWSTR sWChar, int iChars = -1, char chDef = '?', UINT codepage = CP_ACP)
   72     { StringWCharToChar(sWChar, *this, iChars, chDef, codepage); }
   73 };
   74 
   75 class CStringCharFromUTF8 : public CStringA
   76 {
   77 public:
   78     CStringCharFromUTF8(LPCSTR sUTF8, int iChars = -1, char chDef = '?', UINT codepage = CP_ACP)
   79     { StringUTF8ToChar(sUTF8, *this, iChars, chDef, codepage); }
   80 };
   81 
   82 #ifdef UNICODE
   83 #define CStringTCharFromWCharIfNeeded(s, ...) (s)
   84 #define CStringWCharFromTCharIfNeeded(s, ...) (s)
   85 #define CStringTCharFromCharIfNeeded(s, ...) CStringTCharFromChar((s), __VA_ARGS__)
   86 #define CStringCharFromTCharIfNeeded(s, ...) CStringCharFromTChar((s), __VA_ARGS__)
   87 
   88 class CStringTCharFromWChar : public CStringW
   89 {
   90 public:
   91     CStringTCharFromWChar(LPCWSTR sWChar, int iChars = -1, char chDef = '?')
   92     { _StringDummyConvW(sWChar, *this, iChars); }
   93 };
   94 class CStringTCharFromUTF8 : public CStringW
   95 {
   96 public:
   97     CStringTCharFromUTF8(LPCSTR sUTF8, int iChars = -1, char chDef = '?')
   98     { StringUTF8ToWChar(sUTF8, *this, iChars); }
   99 };
  100 typedef CStringWCharFromChar    CStringTCharFromChar;
  101 typedef CStringW                CStringWCharFromTChar;
  102 typedef CStringUTF8FromWChar    CStringUTF8FromTChar;
  103 typedef CStringCharFromWChar    CStringCharFromTChar;
  104 
  105 #else
  106 // ! _UNICODE
  107 #define CStringTCharFromWCharIfNeeded(s, ...) CStringTCharFromWChar((s), __VA_ARGS__)
  108 #define CStringWCharFromTCharIfNeeded(s, ...) CStringWCharFromTChar((s), __VA_ARGS__)
  109 #define CStringTCharFromCharIfNeeded(s, ...) (s)
  110 #define CStringCharFromTCharIfNeeded(s, ...) (s)
  111 
  112 typedef CStringCharFromWChar    CStringTCharFromWChar;
  113 typedef CStringCharFromUTF8     CStringTCharFromUTF8;
  114 typedef CStringA                CStringTCharFromChar;
  115 typedef CStringWCharFromChar    CStringWCharFromTChar;
  116 typedef CStringUTF8FromChar     CStringUTF8FromTChar;
  117 typedef CStringA                CStringCharFromTChar;
  118 #endif // _UNICODE
  119 
  120 typedef CStringTCharFromWChar   CStringFromWChar;
  121 typedef CStringTCharFromUTF8    CStringFromUTF8;
  122 typedef CStringTCharFromChar    CStringFromChar;
  123 
  124 #endif // _WIN32
  125 
  126 template <typename CHAR_T>
  127 void CharConvEndian(CHAR_T *pChar)
  128 {
  129     BYTE *pCh = (BYTE *) pChar, chTemp;
  130     int i, j;
  131     for (i = 0, j = sizeof(CHAR_T) - 1;i < (sizeof(CHAR_T) >> 1);i++, j--) {
  132         chTemp = pCh[i];
  133         pCh[i] = pCh[j];
  134         pCh[j] = chTemp;
  135     }
  136 }
  137 
  138 // uChars = 0 ==> treat 'sSrc' as NULL-terminal
  139 template <typename CHAR_T>
  140 unsigned long StringConvEndian(CHAR_T *sSrc, ULONG_PTR uChars = 0)
  141 {
  142     ULONG_PTR i;
  143     for (i = 0;sSrc[i] && (uChars == 0 || i < uChars);i++)
  144         CharConvEndian(sSrc + i);
  145     return i;
  146 }