Use cwstring_view in all System functions and remove obsolete overloads
MODIFY
include/vcpkg_System.h
include/vcpkg_System.h
namespace vcpkg::System
{
- optional<std::wstring> get_registry_string(HKEY base, const wchar_t* subkey, const wchar_t* valuename);
-
fs::path get_exe_path_of_current_process();
struct exit_code_and_output
@@ -17,30 +15,13 @@ namespace vcpkg::System
std::string output;
};
- int cmd_execute_clean(const wchar_t* cmd_line);
-
- inline int cmd_execute_clean(const std::wstring& cmd_line)
- {
- return cmd_execute_clean(cmd_line.c_str());
- }
-
- int cmd_execute(const wchar_t* cmd_line);
+ int cmd_execute_clean(const cwstring_view cmd_line);
- inline int cmd_execute(const std::wstring& cmd_line)
- {
- return cmd_execute(cmd_line.c_str());
- }
-
- exit_code_and_output cmd_execute_and_capture_output(const wchar_t* cmd_line);
-
- inline exit_code_and_output cmd_execute_and_capture_output(const std::wstring& cmd_line)
- {
- return cmd_execute_and_capture_output(cmd_line.c_str());
- }
+ int cmd_execute(const cwstring_view cmd_line);
- std::wstring create_powershell_script_cmd(const fs::path& script_path);
+ exit_code_and_output cmd_execute_and_capture_output(const cwstring_view cmd_line);
- std::wstring create_powershell_script_cmd(const fs::path& script_path, const std::wstring& args);
+ std::wstring create_powershell_script_cmd(const fs::path& script_path, const cwstring_view args = L"");
enum class color
{
@@ -49,56 +30,38 @@ namespace vcpkg::System
warning = 14,
};
- void print(const char* message);
- void println(const char* message);
- void print(const color c, const char* message);
- void println(const color c, const char* message);
+ void print(const cstring_view message);
+ void println(const cstring_view message);
+ void print(const color c, const cstring_view message);
+ void println(const color c, const cstring_view message);
- inline void print(const std::string& message)
+ template <class Arg1, class...Args>
+ void print(const char* messageTemplate, const Arg1& arg1, const Args&... messageArgs)
{
- return print(message.c_str());
+ return print(Strings::format(messageTemplate, arg1, messageArgs...).c_str());
}
- inline void println(const std::string& message)
+ template <class Arg1, class...Args>
+ void print(const color c, const Arg1& arg1, const char* messageTemplate, const Args&... messageArgs)
{
- return println(message.c_str());
+ return print(c, Strings::format(messageTemplate, arg1, messageArgs...).c_str());
}
- inline void print(const color c, const std::string& message)
+ template <class Arg1, class...Args>
+ void println(const char* messageTemplate, const Arg1& arg1, const Args&... messageArgs)
{
- return print(c, message.c_str());
+ return println(Strings::format(messageTemplate, arg1, messageArgs...).c_str());
}
- inline void println(const color c, const std::string& message)
+ template <class Arg1, class...Args>
+ void println(const color c, const char* messageTemplate, const Arg1& arg1, const Args&... messageArgs)
{
- return println(c, message.c_str());
+ return println(c, Strings::format(messageTemplate, arg1, messageArgs...).c_str());
}
- template <class...Args>
- void print(const char* messageTemplate, const Args&... messageArgs)
- {
- return print(Strings::format(messageTemplate, messageArgs...).c_str());
- }
-
- template <class...Args>
- void print(const color c, const char* messageTemplate, const Args&... messageArgs)
- {
- return print(c, Strings::format(messageTemplate, messageArgs...).c_str());
- }
-
- template <class...Args>
- void println(const char* messageTemplate, const Args&... messageArgs)
- {
- return println(Strings::format(messageTemplate, messageArgs...).c_str());
- }
-
- template <class...Args>
- void println(const color c, const char* messageTemplate, const Args&... messageArgs)
- {
- return println(c, Strings::format(messageTemplate, messageArgs...).c_str());
- }
+ optional<std::wstring> get_environmental_variable(const cwstring_view varname) noexcept;
- optional<std::wstring> get_environmental_variable(const wchar_t* varname) noexcept;
+ void set_environmental_variable(const cwstring_view varname, const cwstring_view varvalue) noexcept;
- void set_environmental_variable(const wchar_t* varname, const wchar_t* varvalue) noexcept;
+ optional<std::wstring> get_registry_string(HKEY base, const cwstring_view subkey, const cwstring_view valuename);
}

MODIFY
src/vcpkg_System.cpp
src/vcpkg_System.cpp
@@ -13,7 +13,7 @@ namespace vcpkg::System
return fs::path(buf, buf + bytes);
}
- int cmd_execute_clean(const wchar_t* cmd_line)
+ int cmd_execute_clean(const cwstring_view cmd_line)
{
static const std::wstring system_root = *get_environmental_variable(L"SystemRoot");
static const std::wstring system_32 = system_root + LR"(\system32)";
@@ -87,7 +87,7 @@ namespace vcpkg::System
return static_cast<int>(exit_code);
}
- int cmd_execute(const wchar_t* cmd_line)
+ int cmd_execute(const cwstring_view cmd_line)
{
// Flush stdout before launching external process
_flushall();
@@ -98,7 +98,7 @@ namespace vcpkg::System
return exit_code;
}
- exit_code_and_output cmd_execute_and_capture_output(const wchar_t* cmd_line)
+ exit_code_and_output cmd_execute_and_capture_output(const cwstring_view cmd_line)
{
// Flush stdout before launching external process
fflush(stdout);
@@ -124,29 +124,24 @@ namespace vcpkg::System
return { ec, output };
}
- std::wstring create_powershell_script_cmd(const fs::path& script_path)
- {
- return create_powershell_script_cmd(script_path, L"");
- }
-
- std::wstring create_powershell_script_cmd(const fs::path& script_path, const std::wstring& args)
+ std::wstring create_powershell_script_cmd(const fs::path& script_path, const cwstring_view args)
{
// TODO: switch out ExecutionPolicy Bypass with "Remove Mark Of The Web" code and restore RemoteSigned
return Strings::wformat(LR"(powershell -NoProfile -ExecutionPolicy Bypass -Command "& {& '%s' %s}")", script_path.native(), args);
}
- void print(const char* message)
+ void print(const cstring_view message)
{
fputs(message, stdout);
}
- void println(const char* message)
+ void println(const cstring_view message)
{
print(message);
putchar('\n');
}
- void print(const color c, const char* message)
+ void print(const color c, const cstring_view message)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
@@ -159,13 +154,13 @@ namespace vcpkg::System
SetConsoleTextAttribute(hConsole, original_color);
}
- void println(const color c, const char* message)
+ void println(const color c, const cstring_view message)
{
print(c, message);
putchar('\n');
}
- optional<std::wstring> get_environmental_variable(const wchar_t* varname) noexcept
+ optional<std::wstring> get_environmental_variable(const cwstring_view varname) noexcept
{
auto sz = GetEnvironmentVariableW(varname, nullptr, 0);
if (sz == 0)
@@ -179,7 +174,7 @@ namespace vcpkg::System
return ret;
}
- void set_environmental_variable(const wchar_t* varname, const wchar_t* varvalue) noexcept
+ void set_environmental_variable(const cwstring_view varname, const cwstring_view varvalue) noexcept
{
_wputenv_s(varname, varvalue);
}
@@ -189,7 +184,7 @@ namespace vcpkg::System
return hkey_type == REG_SZ || hkey_type == REG_MULTI_SZ || hkey_type == REG_EXPAND_SZ;
}
- optional<std::wstring> get_registry_string(HKEY base, const wchar_t* subKey, const wchar_t* valuename)
+ optional<std::wstring> get_registry_string(HKEY base, const cwstring_view subKey, const cwstring_view valuename)
{
HKEY k = nullptr;
LSTATUS ec = RegOpenKeyExW(base, subKey, NULL, KEY_READ, &k);

Use cwstring_view in all System functions and remove obsolete overloads