-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new API for wstring conversion on windows and wrap get/set env. (#…
- Loading branch information
Showing
7 changed files
with
198 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2025~2025 CSSlayer <[email protected]> | ||
* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* | ||
*/ | ||
|
||
#include "environ.h" | ||
#include <optional> | ||
#include <string> | ||
#include <string_view> | ||
#include "fcitx-utils/utf8.h" | ||
#include "stringutils.h" | ||
#include "utf8.h" | ||
|
||
#ifdef _WIN32 | ||
#include <windows.h> | ||
#endif | ||
|
||
namespace fcitx { | ||
|
||
void setEnvironment(const char *variable, const char *value) { | ||
#ifdef _WIN32 | ||
if (!variable || !value) { | ||
return; | ||
} | ||
|
||
auto wname = utf8::UTF8ToUTF16(variable); | ||
auto wvalue = utf8::UTF8ToUTF16(value); | ||
|
||
auto assign = stringutils::concat(variable, "=", value); | ||
auto wassign = utf8::UTF8ToUTF16(assign); | ||
|
||
_wputenv(wassign.data()); | ||
|
||
SetEnvironmentVariableW(wname.data(), wvalue.data()); | ||
#else | ||
setenv(variable, value, 1); | ||
#endif | ||
} | ||
|
||
std::optional<std::string> getEnvironment(const char *variable) { | ||
#ifdef _WIN32 | ||
if (!variable) { | ||
return std::nullopt; | ||
} | ||
auto wname = utf8::UTF8ToUTF16(variable); | ||
DWORD len = GetEnvironmentVariableW(wname.data(), nullptr, 0); | ||
|
||
if (GetLastError() == ERROR_ENVVAR_NOT_FOUND) { | ||
return std::nullopt; | ||
} | ||
std::vector<wchar_t> wdata(len); | ||
if (GetEnvironmentVariableW(wname.data(), wdata.data(), len) != len - 1) { | ||
return std::nullopt; | ||
} | ||
std::wstring wvalue(wdata.data()); | ||
|
||
if (wvalue.find(L"%") != std::wstring::npos) { | ||
len = ExpandEnvironmentStringsW(wvalue.data(), nullptr, 0); | ||
|
||
if (len > 0) { | ||
wdata.resize(len); | ||
if (ExpandEnvironmentStringsW(wvalue.data(), wdata.data(), len) == | ||
len) { | ||
wvalue = wdata.data(); | ||
} | ||
} | ||
} | ||
|
||
return utf8::UTF16ToUTF8(wvalue); | ||
#else | ||
const char *value = getenv(variable); | ||
if (value) { | ||
return std::string(value); | ||
} | ||
return std::nullopt; | ||
#endif | ||
} | ||
|
||
} // namespace fcitx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2025~2025 CSSlayer <[email protected]> | ||
* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* | ||
*/ | ||
#ifndef _FCITX_UTILS_SHIM_WIN32_H_ | ||
#define _FCITX_UTILS_SHIM_WIN32_H_ | ||
|
||
#include <optional> | ||
#include <string> | ||
#include "fcitx-utils/fcitxutils_export.h" | ||
|
||
namespace fcitx { | ||
|
||
/** | ||
* Set environment variable | ||
* | ||
* @param variable variable name | ||
* @return value | ||
* | ||
* @since 5.1.13 | ||
*/ | ||
FCITXUTILS_EXPORT void setEnvironment(const char *variable, const char *value); | ||
|
||
/** | ||
* Get environment variable value. | ||
* | ||
* @param variable variable name | ||
* @return value | ||
* | ||
* @since 5.1.13 | ||
*/ | ||
FCITXUTILS_EXPORT std::optional<std::string> | ||
getEnvironment(const char *variable); | ||
|
||
} // namespace fcitx | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2025~2025 CSSlayer <[email protected]> | ||
* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* | ||
*/ | ||
|
||
#include <optional> | ||
#include "fcitx-utils/environ.h" | ||
#include "fcitx-utils/log.h" | ||
|
||
int main() { | ||
fcitx::setEnvironment("TEST_VAR", "TEST_VALUE"); | ||
FCITX_ASSERT(fcitx::getEnvironment("TEST_VAR") == "TEST_VALUE"); | ||
FCITX_ASSERT(fcitx::getEnvironment("__BAD_VAR") == std::nullopt); | ||
|
||
return 0; | ||
} |