"Fossies" - the Fresh Open Source Software Archive

Member "AutoHotkey_L-1.1.33.09/source/StringConv.cpp" (8 May 2021, 3178 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.cpp" see the Fossies "Dox" file reference documentation.

    1 #include "stdafx.h"
    2 #include "KuString.h"
    3 #include "StringConv.h"
    4 #include "util.h"
    5 
    6 #ifdef _WIN32
    7 LPCWSTR StringUTF8ToWChar(LPCSTR sUTF8, CStringW &sWChar, int iChars/* = -1*/)
    8 {
    9     if (!sUTF8)
   10         return NULL;
   11 
   12     sWChar.Empty();
   13     int iLen = MultiByteToWideChar(CP_UTF8, 0, sUTF8, iChars, NULL, 0);
   14     if (iLen > 0) {
   15         LPWSTR sBuf = sWChar.GetBufferSetLength(iLen);
   16         iLen = MultiByteToWideChar(CP_UTF8, 0, sUTF8, iChars, sBuf, iLen);
   17         sWChar.ReleaseBufferSetLength(sBuf[iLen - 1] ? iLen : iLen - 1);
   18         return (iLen > 0) ? sWChar.GetString() : NULL;
   19     }
   20 
   21     return *sUTF8 != 0 ? sWChar.GetString() : NULL;
   22 }
   23 
   24 LPCWSTR StringCharToWChar(LPCSTR sChar, CStringW &sWChar, int iChars/* = -1*/, UINT codepage/* = CP_ACP*/)
   25 {
   26     if (!sChar)
   27         return NULL;
   28 
   29     sWChar.Empty();
   30     int iLen = MultiByteToWideChar(codepage, 0, sChar, iChars, NULL, 0);
   31     if (iLen > 0) {
   32         LPWSTR sBuf = sWChar.GetBufferSetLength(iLen);
   33         MultiByteToWideChar(codepage, 0, sChar, iChars, sBuf, iLen);
   34         sWChar.ReleaseBufferSetLength(sBuf[iLen - 1] ? iLen : iLen - 1);
   35         return (iLen > 0) ? sWChar.GetString() : NULL;
   36     }
   37 
   38     return (*sChar != 0) ? sWChar.GetString() : NULL;
   39 }
   40 
   41 LPCSTR StringWCharToUTF8(LPCWSTR sWChar, CStringA &sUTF8, int iChars/* = -1*/)
   42 {
   43     if (!sWChar)
   44         return NULL;
   45 
   46     sUTF8.Empty();
   47     int iLen = WideCharToMultiByte(CP_UTF8, 0, sWChar, iChars, NULL, 0, NULL, NULL);
   48     if (iLen > 0) {
   49         LPSTR sBuf = sUTF8.GetBufferSetLength(iLen);
   50         WideCharToMultiByte(CP_UTF8, 0, sWChar, iChars, sBuf, iLen, NULL, NULL);
   51         sUTF8.ReleaseBufferSetLength(sBuf[iLen - 1] ? iLen : iLen - 1);
   52         return (iLen > 0) ? sUTF8.GetString() : NULL;
   53     }
   54 
   55     return (*sWChar != 0) ? sUTF8.GetString() : NULL;
   56 }
   57 
   58 LPCSTR StringCharToUTF8(LPCSTR sChar, CStringA &sUTF8, int iChars/* = -1*/, UINT codepage/* = CP_ACP*/)
   59 {
   60     return StringWCharToUTF8(CStringWCharFromChar(sChar, iChars, codepage), sUTF8);
   61 }
   62 
   63 LPCSTR StringWCharToChar(LPCWSTR sWChar, CStringA &sChar, int iChars/* = -1*/, char chDef/* = '?'*/, UINT codepage/* = CP_ACP*/)
   64 {
   65     if (!sWChar)
   66         return NULL;
   67 
   68     sChar.Empty();
   69     int iLen = WideCharToMultiByte(codepage, WC_NO_BEST_FIT_CHARS, sWChar, iChars, NULL, 0, &chDef, NULL);
   70     if (iLen > 0) {
   71         LPSTR sBuf = sChar.GetBufferSetLength(iLen);
   72         WideCharToMultiByte(codepage, WC_NO_BEST_FIT_CHARS, sWChar, iChars, sBuf, iLen, &chDef, NULL);
   73         sChar.ReleaseBufferSetLength(sBuf[iLen - 1] ? iLen : iLen - 1);
   74         return (iLen > 0) ? sChar.GetString() : NULL;
   75     }
   76 
   77     return (*sWChar != 0) ? sChar.GetString() : NULL;
   78 }
   79 
   80 LPCSTR StringUTF8ToChar(LPCSTR sUTF8, CStringA &sChar, int iChars/* = -1*/, char chDef/* = '?'*/, UINT codepage/* = CP_ACP*/)
   81 {
   82     return StringWCharToChar(CStringWCharFromUTF8(sUTF8, iChars), sChar, iChars, chDef, codepage);
   83 }
   84 #endif
   85 
   86 template <typename SRC_T, typename DEST_T>
   87 SRC_T _StringDummyConv(SRC_T sSrc, DEST_T &sDest, int iChars = -1)
   88 {
   89     if (!sSrc)
   90         return NULL;
   91     if (iChars >= 0)
   92         sDest.SetString(sSrc, iChars);
   93     else
   94         sDest = sSrc;
   95     return sDest;
   96 }
   97 
   98 LPCWSTR _StringDummyConvW(LPCWSTR sSrc, CStringW &sDest, int iChars/* = -1*/)
   99 {
  100     return _StringDummyConv(sSrc, sDest, iChars);
  101 }
  102 
  103 LPCSTR _StringDummyConvA(LPCSTR sSrc, CStringA &sDest, int iChars/* = -1*/)
  104 {
  105     return _StringDummyConv(sSrc, sDest, iChars);
  106 }