예제 #1
0
파일: dizlist.cpp 프로젝트: alexlav/conemu
int DizList::GetDizPosEx(const string& Name, const string& ShortName, int *TextPos)
{
	int DizPos=GetDizPos(Name,TextPos);

	if (DizPos==-1)
		DizPos=GetDizPos(ShortName,TextPos);

	//если файл описаний был в OEM/ANSI то имена файлов могут не совпадать с юникодными
	if (DizPos==-1 && !IsUnicodeOrUtfCodePage(OrigCodePage) && OrigCodePage!=CP_DEFAULT)
	{
		size_t len = Name.GetLength();
		char *tmp = (char *)xf_realloc_nomove(AnsiBuf, len+1);

		if (!tmp)
			return -1;

		AnsiBuf = tmp;
		WideCharToMultiByte(OrigCodePage, 0, Name, static_cast<int>(len), AnsiBuf, static_cast<int>(len), nullptr, nullptr);
		AnsiBuf[len]=0;
		string strRecoded(AnsiBuf, OrigCodePage);

		if (strRecoded==Name)
			return -1;

		return GetDizPos(strRecoded,TextPos);
	}

	return DizPos;
}
예제 #2
0
int OldGetFileString::GetString(wchar_t **DestStr, int nCodePage, int &Length)
{
    int nExitCode;

    if (nCodePage == CP_UNICODE) //utf-16
        nExitCode = GetUnicodeString(DestStr, Length, false);
    else if (nCodePage == CP_REVERSEBOM)
        nExitCode = GetUnicodeString(DestStr, Length, true);
    else
    {
        char *Str;
        nExitCode = GetAnsiString(&Str, Length);

        if (nExitCode == 1)
        {
            DWORD ret = ERROR_SUCCESS;
            int nResultLength = 0;
            bool bGet = false;
            *wStr = L'\0';

            if (!SomeDataLost)
            {
                // при CP_UTF7 dwFlags должен быть 0, см. MSDN
                nResultLength = MultiByteToWideChar(
                                    nCodePage,
                                    (SomeDataLost || nCodePage==CP_UTF7) ? 0 : MB_ERR_INVALID_CHARS,
                                    Str,
                                    Length,
                                    wStr,
                                    m_nwStrLength - 1
                                );

                if (!nResultLength)
                {
                    ret = GetLastError();

                    if (ERROR_NO_UNICODE_TRANSLATION == ret)
                    {
                        SomeDataLost = true;
                        bGet = true;
                    }
                }
            }
            else
                bGet = true;

            if (bGet)
            {
                nResultLength = MultiByteToWideChar(nCodePage, 0, Str, Length, wStr, m_nwStrLength - 1);

                if (!nResultLength)
                    ret = GetLastError();
            }

            if (ERROR_INSUFFICIENT_BUFFER == ret)
            {
                nResultLength = MultiByteToWideChar(nCodePage, 0, Str, Length, nullptr, 0);
                wStr = (wchar_t*)xf_realloc_nomove(wStr, (nResultLength + 1) * sizeof(wchar_t));
                *wStr = L'\0';
                m_nwStrLength = nResultLength+1;
                nResultLength = MultiByteToWideChar(nCodePage, 0, Str, Length, wStr, nResultLength);
            }

            if (nResultLength)
                wStr[nResultLength] = L'\0';

            Length = nResultLength;
            *DestStr = wStr;
        }
    }

    return nExitCode;
}
예제 #3
0
int GetFileString::GetString(LPWSTR* DestStr, UINT nCodePage, int& Length)
{
    if(Peek)
    {
        Peek = false;
        DestStr = LastString;
        Length = LastLength;
        return LastResult;
    }
    int nExitCode;

    if (nCodePage == CP_UNICODE) //utf-16
    {
        nExitCode = GetUnicodeString(DestStr, Length, false);
    }
    else if (nCodePage == CP_REVERSEBOM)
    {
        nExitCode = GetUnicodeString(DestStr, Length, true);
    }
    else
    {
        char *Str;
        nExitCode = GetAnsiString(&Str, Length);

        if (nExitCode == 1)
        {
            DWORD Result = ERROR_SUCCESS;
            int nResultLength = 0;
            bool bGet = false;
            *wStr = L'\0';

            if (!SomeDataLost)
            {
                // при CP_UTF7 dwFlags должен быть 0, см. MSDN
                nResultLength = MultiByteToWideChar(nCodePage, (SomeDataLost || nCodePage==CP_UTF7) ? 0 : MB_ERR_INVALID_CHARS, Str, Length, wStr, m_nwStrLength - 1);

                if (!nResultLength)
                {
                    Result = GetLastError();
                    if (Result == ERROR_NO_UNICODE_TRANSLATION)
                    {
                        SomeDataLost = true;
                        bGet = true;
                    }
                }
            }
            else
            {
                bGet = true;
            }
            if (bGet)
            {
                nResultLength = MultiByteToWideChar(nCodePage, 0, Str, Length, wStr, m_nwStrLength - 1);
                if (!nResultLength)
                {
                    Result = GetLastError();
                }
            }
            if (Result == ERROR_INSUFFICIENT_BUFFER)
            {
                nResultLength = MultiByteToWideChar(nCodePage, 0, Str, Length, nullptr, 0);
                wStr = static_cast<LPWSTR>(xf_realloc_nomove(wStr, (nResultLength + 1) * sizeof(wchar_t)));
                *wStr = L'\0';
                m_nwStrLength = nResultLength+1;
                nResultLength = MultiByteToWideChar(nCodePage, 0, Str, Length, wStr, nResultLength);
            }
            if (nResultLength)
            {
                wStr[nResultLength] = L'\0';
            }
            Length = nResultLength;
            *DestStr = wStr;
        }
    }
    return nExitCode;
}