Skip to content

Commit 99be82d

Browse files
committed
feat: revise config paths and various utilities
Use modern paths in the configuration system as well as in utilities like subprocess spawning, cloud provider info and file search.
1 parent aa64903 commit 99be82d

File tree

6 files changed

+109
-99
lines changed

6 files changed

+109
-99
lines changed

engine/core/core_config.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
class core_config_c: public core_IConfig, public conPrintHook_c, public conCmdHandler_c {
1919
public:
2020
// Interface
21-
bool LoadConfig(const char* cfgName);
22-
bool SaveConfig(const char* cfgName);
21+
bool LoadConfig(std::filesystem::path const& cfgName);
22+
bool SaveConfig(std::filesystem::path const& cfgName);
2323

2424
// Encapsulated
2525
core_config_c(sys_IMain* sysHnd);
@@ -152,19 +152,17 @@ void core_config_c::C_MemReport(IConsole* conHnd, args_c &args)
152152
// Config Files
153153
// ============
154154

155-
bool core_config_c::LoadConfig(const char* cfgName)
155+
bool core_config_c::LoadConfig(std::filesystem::path const& cfgName)
156156
{
157157
// Make sure it has .cfg extension
158-
std::string fileName = cfgName;
159-
if (fileName.find('.') == fileName.npos) {
160-
fileName += ".cfg";
161-
}
158+
auto fileName = cfgName;
159+
fileName.replace_extension(".cfg");
162160

163-
sys->con->Print(fmt::format("Executing {}\n", fileName).c_str());
161+
sys->con->Print(fmt::format("Executing {}\n", fileName.u8string()).c_str());
164162

165163
// Read the config file
166164
fileInputStream_c f;
167-
if (f.FileOpen(fileName.c_str(), true)) {
165+
if (f.FileOpen(fileName, true)) {
168166
sys->con->Warning("config file not found");
169167
return true;
170168
}
@@ -198,19 +196,17 @@ bool core_config_c::LoadConfig(const char* cfgName)
198196
return false;
199197
}
200198

201-
bool core_config_c::SaveConfig(const char* cfgName)
199+
bool core_config_c::SaveConfig(std::filesystem::path const& cfgName)
202200
{
203201
// Make sure it has .cfg extension
204-
std::string fileName = cfgName;
205-
if (fileName.find('.') == fileName.npos) {
206-
fileName += ".cfg";
207-
}
202+
auto fileName = cfgName;
203+
fileName.replace_extension(".cfg");
208204

209-
sys->con->Print(fmt::format("Saving {}\n", fileName).c_str());
205+
sys->con->Print(fmt::format("Saving {}\n", fileName.u8string()).c_str());
210206

211207
// Open the config file
212208
fileOutputStream_c f;
213-
if (f.FileOpen(fileName.c_str(), false)) {
209+
if (f.FileOpen(fileName, false)) {
214210
sys->con->Warning("couldnt write config file");
215211
return true;
216212
}
@@ -235,7 +231,7 @@ void core_config_c::ConPrintHook(const char* text)
235231
{
236232
if (con_log->intVal) {
237233
if (logOpen == false) {
238-
logFile.FileOpen(CFG_LOGFILE, false);
234+
logFile.FileOpen(std::filesystem::u8path(CFG_LOGFILE), false);
239235
logOpen = true;
240236
logFile.FilePrintf("Log opened.\n");
241237
}

engine/core/core_config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ class core_IConfig {
1414
static core_IConfig* GetHandle(sys_IMain* sysHnd);
1515
static void FreeHandle(core_IConfig* hnd);
1616

17-
virtual bool LoadConfig(const char* cfgName) = 0;
18-
virtual bool SaveConfig(const char* cfgName) = 0;
17+
virtual bool LoadConfig(std::filesystem::path const& cfgName) = 0;
18+
virtual bool SaveConfig(std::filesystem::path const& cfgName) = 0;
1919
};

engine/system/sys_main.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//
66

77
#include <filesystem>
8+
#include <optional>
89
#include <string>
910

1011
// =======
@@ -35,17 +36,17 @@ class thread_c {
3536
// File finder
3637
class find_c {
3738
public:
38-
std::string fileName;
39+
std::filesystem::path fileName;
3940
bool isDirectory = false;
4041
uintmax_t fileSize = 0;
4142
unsigned long long modified = 0;
4243

4344
find_c();
4445
~find_c();
45-
bool FindFirst(const char* fileSpec);
46+
bool FindFirst(std::filesystem::path const&& fileSpec);
4647
bool FindNext();
4748
private:
48-
std::filesystem::path glob;
49+
std::optional<std::string> globPattern; // Empty pattern accepts all files like "*" and "*.*"
4950
std::filesystem::directory_iterator iter;
5051
};
5152

@@ -72,8 +73,8 @@ class sys_IMain {
7273
virtual bool IsKeyDown(byte key) = 0;
7374
virtual void ClipboardCopy(const char* str) = 0;
7475
virtual char* ClipboardPaste() = 0;
75-
virtual void SpawnProcess(const char* cmdName, const char* argList) = 0;
7676
virtual bool SetWorkDir(std::filesystem::path const& newCwd = {}) = 0;
77+
virtual void SpawnProcess(std::filesystem::path cmdName, const char* argList) = 0;
7778
virtual void OpenURL(const char* url) = 0;
7879
virtual void Error(const char* fmt, ...) = 0;
7980
virtual void Exit(const char* msg = NULL) = 0;

engine/system/win/sys_local.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class sys_main_c: public sys_IMain {
3535
bool IsKeyDown(byte key);
3636
void ClipboardCopy(const char* str);
3737
char* ClipboardPaste();
38-
void SpawnProcess(const char* cmdName, const char* argList);
3938
bool SetWorkDir(std::filesystem::path const& newCwd = {});
39+
void SpawnProcess(std::filesystem::path cmdName, const char* argList);
4040
void OpenURL(const char* url);
4141
void Error(const char* fmt, ...);
4242
void Exit(const char* msg = NULL);

engine/system/win/sys_main.cpp

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ find_c::find_c()
136136
find_c::~find_c()
137137
{}
138138

139-
bool GlobMatch(const std::filesystem::path& glob, const std::filesystem::path& file)
139+
std::optional<std::string> BuildGlobPattern(std::filesystem::path const& glob)
140140
{
141141
using namespace std::literals::string_view_literals;
142-
auto globStr = glob.generic_u8string();
142+
auto globStr = glob.u8string();
143143

144144
// Deal with traditional "everything" wildcards.
145145
if (glob == "*" || glob == "*.*") {
146-
return true;
146+
return {};
147147
}
148148

149149
fmt::memory_buffer buf;
@@ -179,32 +179,34 @@ bool GlobMatch(const std::filesystem::path& glob, const std::filesystem::path& f
179179
}
180180
}
181181
}
182+
return to_string(buf);
183+
}
182184

185+
bool GlobMatch(std::optional<std::string> const& globPattern, std::filesystem::path const& file)
186+
{
187+
if (!globPattern.has_value()) {
188+
// Empty pattern is like "*" and "*.*".
189+
return true;
190+
}
183191
// Assume case-insensitive comparisons are desired.
184192
RE2::Options reOpts;
185193
reOpts.set_case_sensitive(false);
186-
RE2 reGlob{to_string(buf), reOpts};
194+
RE2 reGlob{globPattern.value(), reOpts};
187195

188-
auto fileStr = file.generic_u8string();
196+
auto fileStr = file.u8string();
189197
return RE2::FullMatch(fileStr, reGlob);
190198
}
191199

192-
bool find_c::FindFirst(const char* fileSpec)
200+
bool find_c::FindFirst(std::filesystem::path const&& fileSpec)
193201
{
194-
#ifdef _WIN32
195-
wchar_t* wideSpec = WidenUTF8String(fileSpec);
196-
std::filesystem::path p(wideSpec);
197-
FreeWideString(wideSpec);
198-
#else
199-
std::filesystem::path p(fileSpec);
200-
#endif
201-
glob = p.filename();
202+
auto parent = fileSpec.parent_path();
203+
globPattern = BuildGlobPattern(fileSpec.filename());
202204

203205
std::error_code ec;
204-
for (iter = std::filesystem::directory_iterator(p.parent_path(), ec); iter != std::filesystem::directory_iterator{}; ++iter) {
206+
for (iter = std::filesystem::directory_iterator(parent, ec); iter != std::filesystem::directory_iterator{}; ++iter) {
205207
auto candFilename = iter->path().filename();
206-
if (GlobMatch(glob, candFilename)) {
207-
fileName = candFilename.u8string();
208+
if (GlobMatch(globPattern, candFilename)) {
209+
fileName = candFilename;
208210
isDirectory = iter->is_directory();
209211
fileSize = iter->file_size();
210212
auto mod = iter->last_write_time();
@@ -223,8 +225,8 @@ bool find_c::FindNext()
223225

224226
for (++iter; iter != std::filesystem::directory_iterator{}; ++iter) {
225227
auto candFilename = iter->path().filename();
226-
if (GlobMatch(glob, candFilename)) {
227-
fileName = candFilename.u8string();
228+
if (GlobMatch(globPattern, candFilename)) {
229+
fileName = candFilename;
228230
isDirectory = iter->is_directory();
229231
fileSize = iter->file_size();
230232
auto mod = iter->last_write_time();
@@ -398,26 +400,27 @@ bool sys_main_c::SetWorkDir(std::filesystem::path const& newCwd)
398400
}
399401
}
400402

401-
void sys_main_c::SpawnProcess(const char* cmdName, const char* argList)
403+
void sys_main_c::SpawnProcess(std::filesystem::path cmdName, const char* argList)
402404
{
403405
#ifdef _WIN32
404-
char cmd[512];
405-
strcpy(cmd, cmdName);
406-
if ( !strchr(cmd, '.') ) {
407-
strcat(cmd, ".exe");
406+
if (!cmdName.has_extension()) {
407+
cmdName.replace_extension(".exe");
408408
}
409-
SHELLEXECUTEINFOA sinfo;
410-
memset(&sinfo, 0, sizeof(SHELLEXECUTEINFOA));
411-
sinfo.cbSize = sizeof(SHELLEXECUTEINFOA);
409+
auto fileStr = cmdName.wstring();
410+
auto wideArgs = WidenUTF8String(argList);
411+
SHELLEXECUTEINFOW sinfo;
412+
memset(&sinfo, 0, sizeof(sinfo));
413+
sinfo.cbSize = sizeof(sinfo);
412414
sinfo.fMask = SEE_MASK_NOCLOSEPROCESS;
413-
sinfo.lpFile = cmd;
414-
sinfo.lpParameters = argList;
415-
sinfo.lpVerb = "open";
415+
sinfo.lpFile = fileStr.c_str();
416+
sinfo.lpParameters = wideArgs;
417+
sinfo.lpVerb = L"open";
416418
sinfo.nShow = SW_SHOWMAXIMIZED;
417-
if ( !ShellExecuteExA(&sinfo) ) {
418-
sinfo.lpVerb = "runas";
419-
ShellExecuteExA(&sinfo);
419+
if ( !ShellExecuteExW(&sinfo) ) {
420+
sinfo.lpVerb = L"runas";
421+
ShellExecuteExW(&sinfo);
420422
}
423+
FreeWideString(wideArgs);
421424
#else
422425
#warning LV: Subprocesses not implemented on this OS.
423426
// TODO(LV): Implement subprocesses for other OSes.

0 commit comments

Comments
 (0)