Skip to content

Commit

Permalink
chore: move utility functions to utils.h
Browse files Browse the repository at this point in the history
  • Loading branch information
Bush2021 committed Sep 9, 2024
1 parent e30cee8 commit e0c94df
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
36 changes: 3 additions & 33 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<TCHAR> 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;
}

Expand All @@ -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
Expand Down Expand Up @@ -161,4 +131,4 @@ std::wstring GetDisableTabName() {
return GetIniString(L"tabs", L"new_tab_disable_name", L"");
}

#endif // CONFIG_H_
#endif // CONFIG_H_
27 changes: 27 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<TCHAR> 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];
Expand Down

0 comments on commit e0c94df

Please sign in to comment.