From e0c94df2a26ff977256a17b646505947023e256e Mon Sep 17 00:00:00 2001 From: Bush2021 <79072750+Bush2021@users.noreply.github.com> Date: Sun, 8 Sep 2024 21:33:11 -0400 Subject: [PATCH] chore: move utility functions to utils.h --- src/config.h | 36 +++--------------------------------- src/utils.h | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/config.h b/src/config.h index a0d3bdd..a1d4b34 100644 --- a/src/config.h +++ b/src/config.h @@ -3,64 +3,34 @@ const std::wstring kIniPath = GetAppDir() + L"\\chrome++.ini"; -std::wstring GetIniString(const std::wstring& section, - const std::wstring& key, - const std::wstring& default_value) { - std::vector buffer(100); - DWORD bytesread = 0; - do { - bytesread = ::GetPrivateProfileStringW( - section.c_str(), key.c_str(), default_value.c_str(), buffer.data(), - (DWORD)buffer.size(), kIniPath.c_str()); - if (bytesread >= buffer.size() - 1) { - buffer.resize(buffer.size() * 2); - } else { - break; - } - } while (true); - - return std::wstring(buffer.data()); -} - std::wstring GetCrCommandLine() { auto commandLine = GetIniString(L"general", L"command_line", L""); if (!commandLine.empty()) { return commandLine; } - return GetIniString(L"General", L"CommandLine", L""); // Deprecated + return GetIniString(L"General", L"CommandLine", L""); // Deprecated return L""; } -std::wstring CanonicalizePath(const std::wstring& path) { - TCHAR temp[MAX_PATH]; - ::PathCanonicalize(temp, path.data()); - return std::wstring(temp); -} - std::wstring GetDirPath(const std::wstring& dirType) { std::wstring path = CanonicalizePath(GetAppDir() + L"\\..\\" + dirType); - std::wstring DirBuffer(MAX_PATH, '\0'); ::GetPrivateProfileStringW(L"general", (dirType + L"_dir").c_str(), path.c_str(), &DirBuffer[0], MAX_PATH, kIniPath.c_str()); - // Deprecated if (DirBuffer[0] == 0) { ::GetPrivateProfileStringW(L"general", (dirType + L"dir").c_str(), path.c_str(), &DirBuffer[0], MAX_PATH, kIniPath.c_str()); } - if (DirBuffer[0] == 0) { DirBuffer = path; } std::wstring ExpandedPath = ExpandEnvironmentPath(DirBuffer); - ReplaceStringIni(ExpandedPath, L"%app%", GetAppDir()); std::wstring Dir = GetAbsolutePath(ExpandedPath); - return Dir; } @@ -85,7 +55,7 @@ std::wstring GetTranslateKey() { if (!key.empty()) { return key; } - return GetIniString(L"General", L"TranslateKey", L""); // Deprecated + return GetIniString(L"General", L"TranslateKey", L""); // Deprecated } // View password without verification @@ -161,4 +131,4 @@ std::wstring GetDisableTabName() { return GetIniString(L"tabs", L"new_tab_disable_name", L""); } -#endif // CONFIG_H_ \ No newline at end of file +#endif // CONFIG_H_ diff --git a/src/utils.h b/src/utils.h index 2fa11cc..9956333 100644 --- a/src/utils.h +++ b/src/utils.h @@ -246,6 +246,33 @@ bool isEndWith(const wchar_t* s, const wchar_t* sub) { return !_memicmp(s + len1 - len2, sub, len2 * sizeof(wchar_t)); } +// Prase the INI file. +std::wstring GetIniString(const std::wstring& section, + const std::wstring& key, + const std::wstring& default_value) { + std::vector buffer(100); + DWORD bytesread = 0; + do { + bytesread = ::GetPrivateProfileStringW( + section.c_str(), key.c_str(), default_value.c_str(), buffer.data(), + (DWORD)buffer.size(), kIniPath.c_str()); + if (bytesread >= buffer.size() - 1) { + buffer.resize(buffer.size() * 2); + } else { + break; + } + } while (true); + + return std::wstring(buffer.data()); +} + +// Canonicalize the path. +std::wstring CanonicalizePath(const std::wstring& path) { + TCHAR temp[MAX_PATH]; + ::PathCanonicalize(temp, path.data()); + return std::wstring(temp); +} + // Get the absolute path. std::wstring GetAbsolutePath(const std::wstring& path) { wchar_t buffer[MAX_PATH];