/* $ 01.09.2000 SVS + Новый метод, для получения параметров для .Options .Options <KeyName>=<Value> */ int GetOptionsParam(api::File& SrcFile,const wchar_t *KeyName,string &strValue, UINT nCodePage) { int Length=StrLength(L".Options"); auto CurFilePos = SrcFile.GetPointer(); string ReadStr; GetFileString GetStr(SrcFile, nCodePage); while (GetStr.GetString(ReadStr)) { if (!StrCmpNI(ReadStr.data(), L".Options", Length)) { string strFullParamName = ReadStr.substr(Length); RemoveExternalSpaces(strFullParamName); size_t pos = strFullParamName.rfind(L'='); if (pos != string::npos) { strValue = strFullParamName; strValue.erase(0, pos+1); RemoveExternalSpaces(strValue); strFullParamName.resize(pos); RemoveExternalSpaces(strFullParamName); if (!StrCmpI(strFullParamName.data(),KeyName)) { SrcFile.SetPointer(CurFilePos, nullptr, FILE_BEGIN); return TRUE; } } } } SrcFile.SetPointer(CurFilePos, nullptr, FILE_BEGIN); return FALSE; }
bool OpenLangFile(api::File& LangFile, const string& Path,const string& Mask,const string& Language, string &strFileName, uintptr_t &nCodePage, bool StrongLang,string *pstrLangName) { strFileName.clear(); string strFullName, strEngFileName; api::FAR_FIND_DATA FindData; string strLangName; ScanTree ScTree(false, false); ScTree.SetFindPath(Path,Mask); while (ScTree.GetNextName(&FindData, strFullName)) { strFileName = strFullName; if (!LangFile.Open(strFileName, FILE_READ_DATA, FILE_SHARE_READ, nullptr, OPEN_EXISTING)) { strFileName.clear(); } else { GetFileFormat(LangFile, nCodePage, nullptr, false); if (GetLangParam(LangFile,L"Language",&strLangName,nullptr, nCodePage) && !StrCmpI(strLangName, Language)) break; LangFile.Close(); if (StrongLang) { strFileName.clear(); strEngFileName.clear(); break; } if (!StrCmpI(strLangName.data(),L"English")) strEngFileName = strFileName; } } if (!LangFile.Opened()) { if (!strEngFileName.empty()) strFileName = strEngFileName; if (!strFileName.empty()) { LangFile.Open(strFileName, FILE_READ_DATA, FILE_SHARE_READ, nullptr, OPEN_EXISTING); if (pstrLangName) *pstrLangName=L"English"; } } return LangFile.Opened(); }
int GetLangParam(api::File& LangFile,const string& ParamName,string *strParam1, string *strParam2, UINT nCodePage) { string strFullParamName = L"."; strFullParamName += ParamName; int Length=(int)strFullParamName.size(); /* $ 29.11.2001 DJ не поганим позицию в файле; дальше @Contents не читаем */ BOOL Found = FALSE; auto OldPos = LangFile.GetPointer(); string ReadStr; GetFileString GetStr(LangFile, nCodePage); while (GetStr.GetString(ReadStr)) { if (!StrCmpNI(ReadStr.data(), strFullParamName.data(), Length)) { size_t Pos = ReadStr.find(L'='); if (Pos != string::npos) { *strParam1 = ReadStr.substr(Pos + 1); if (strParam2) strParam2->clear(); size_t pos = strParam1->find(L','); if (pos != string::npos) { if (strParam2) { *strParam2 = *strParam1; strParam2->erase(0, pos+1); RemoveTrailingSpaces(*strParam2); } strParam1->resize(pos); } RemoveTrailingSpaces(*strParam1); Found = TRUE; break; } } else if (!StrCmpNI(ReadStr.data(), L"@Contents", 9)) break; } LangFile.SetPointer(OldPos, nullptr, FILE_BEGIN); return Found; }
bool GetFileFormat(api::File& file, uintptr_t& nCodePage, bool* pSignatureFound, bool bUseHeuristics) { DWORD dwTemp=0; bool bSignatureFound = false; bool bDetect=false; DWORD Readed = 0; if (file.Read(&dwTemp, sizeof(dwTemp), Readed) && Readed > 1 ) // minimum signature size is 2 bytes { if (LOWORD(dwTemp) == SIGN_UNICODE) { nCodePage = CP_UNICODE; file.SetPointer(2, nullptr, FILE_BEGIN); bSignatureFound = true; } else if (LOWORD(dwTemp) == SIGN_REVERSEBOM) { nCodePage = CP_REVERSEBOM; file.SetPointer(2, nullptr, FILE_BEGIN); bSignatureFound = true; } else if ((dwTemp & 0x00FFFFFF) == SIGN_UTF8) { nCodePage = CP_UTF8; file.SetPointer(3, nullptr, FILE_BEGIN); bSignatureFound = true; } else { file.SetPointer(0, nullptr, FILE_BEGIN); } } if (bSignatureFound) { bDetect = true; } else if (bUseHeuristics) { file.SetPointer(0, nullptr, FILE_BEGIN); DWORD Size=0x8000; // BUGBUG. TODO: configurable char_ptr Buffer(Size); DWORD ReadSize = 0; bool ReadResult = file.Read(Buffer.get(), Size, ReadSize); file.SetPointer(0, nullptr, FILE_BEGIN); if (ReadResult && ReadSize) { int test= IS_TEXT_UNICODE_STATISTICS| IS_TEXT_UNICODE_REVERSE_STATISTICS| IS_TEXT_UNICODE_CONTROLS| IS_TEXT_UNICODE_REVERSE_CONTROLS| IS_TEXT_UNICODE_ILLEGAL_CHARS| IS_TEXT_UNICODE_ODD_LENGTH| IS_TEXT_UNICODE_NULL_BYTES; if (IsTextUnicode(Buffer.get(), ReadSize, &test)) { if (!(test&IS_TEXT_UNICODE_ODD_LENGTH) && !(test&IS_TEXT_UNICODE_ILLEGAL_CHARS)) { if ((test&IS_TEXT_UNICODE_NULL_BYTES) || (test&IS_TEXT_UNICODE_CONTROLS) || (test&IS_TEXT_UNICODE_REVERSE_CONTROLS)) { if ((test&IS_TEXT_UNICODE_CONTROLS) || (test&IS_TEXT_UNICODE_STATISTICS)) { nCodePage=CP_UNICODE; bDetect=true; } else if ((test&IS_TEXT_UNICODE_REVERSE_CONTROLS) || (test&IS_TEXT_UNICODE_REVERSE_STATISTICS)) { nCodePage=CP_REVERSEBOM; bDetect=true; } } } } else if (IsTextUTF8(Buffer.get(), ReadSize)) { nCodePage=CP_UTF8; bDetect=true; } else { int cp = GetCpUsingUniversalDetector(Buffer.get(), ReadSize); if ( cp >= 0 ) { if (Global->Opt->strNoAutoDetectCP.Get() == L"-1") { if ( Global->Opt->CPMenuMode ) { if ( static_cast<UINT>(cp) != GetACP() && static_cast<UINT>(cp) != GetOEMCP() ) { long long selectType = Global->CodePages->GetFavorite(cp); if (0 == (selectType & CPST_FAVORITE)) cp = -1; } } } else { const auto BannedCpList = StringToList(Global->Opt->strNoAutoDetectCP, STLF_UNIQUE); if (std::find(ALL_CONST_RANGE(BannedCpList), std::to_wstring(cp)) != BannedCpList.cend()) { cp = -1; } } } if (cp != -1) { nCodePage = cp; bDetect = true; } } } } if (pSignatureFound) { *pSignatureFound = bSignatureFound; } return bDetect; }