diff --git a/Source/DiabloUI/fonts.cpp b/Source/DiabloUI/fonts.cpp index 0281c62a1..1724acc1d 100644 --- a/Source/DiabloUI/fonts.cpp +++ b/Source/DiabloUI/fonts.cpp @@ -66,10 +66,10 @@ void LoadTtfFont() was_fonts_init = true; } - std::string ttfFontPath = GetTtfPath() + GetTtfName(); + std::string ttfFontPath = paths::TtfPath() + paths::TtfName(); #ifdef __linux__ if (!FileExists(ttfFontPath.c_str())) { - ttfFontPath = "/usr/share/fonts/truetype/" + GetTtfName(); + ttfFontPath = "/usr/share/fonts/truetype/" + paths::TtfName(); } #endif font = TTF_OpenFont(ttfFontPath.c_str(), 17); diff --git a/Source/capture.cpp b/Source/capture.cpp index 9f4e86a34..bea591dac 100644 --- a/Source/capture.cpp +++ b/Source/capture.cpp @@ -137,7 +137,7 @@ static std::ofstream *CaptureFile(std::string *dstPath) char filename[sizeof("screen00.PCX") / sizeof(char)]; for (int i = 0; i <= 99; ++i) { snprintf(filename, sizeof(filename) / sizeof(char), "screen%02d.PCX", i); - *dstPath = GetPrefPath() + filename; + *dstPath = paths::PrefPath() + filename; if (!FileExists(dstPath->c_str())) { return new std::ofstream(*dstPath, std::ios::binary | std::ios::trunc); } diff --git a/Source/diablo.cpp b/Source/diablo.cpp index 8600be2df..a9c3f3284 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -186,17 +186,17 @@ static void diablo_parse_flags(int argc, char **argv) printInConsole("%s v%s\n", PROJECT_NAME, PROJECT_VERSION); diablo_quit(0); } else if (strcasecmp("--data-dir", argv[i]) == 0) { - SetBasePath(argv[++i]); + paths::SetBasePath(argv[++i]); } else if (strcasecmp("--save-dir", argv[i]) == 0) { - SetPrefPath(argv[++i]); + paths::SetPrefPath(argv[++i]); } else if (strcasecmp("--config-dir", argv[i]) == 0) { - SetConfigPath(argv[++i]); + paths::SetConfigPath(argv[++i]); } else if (strcasecmp("--lang-dir", argv[i]) == 0) { - SetLangPath(argv[++i]); + paths::SetLangPath(argv[++i]); } else if (strcasecmp("--ttf-dir", argv[i]) == 0) { - SetTtfPath(argv[++i]); + paths::SetTtfPath(argv[++i]); } else if (strcasecmp("--ttf-name", argv[i]) == 0) { - SetTtfName(argv[++i]); + paths::SetTtfName(argv[++i]); } else if (strcasecmp("-n", argv[i]) == 0) { gbShowIntro = false; } else if (strcasecmp("-f", argv[i]) == 0) { diff --git a/Source/dvlnet/zerotier_native.cpp b/Source/dvlnet/zerotier_native.cpp index 4a35599f6..cbb944528 100644 --- a/Source/dvlnet/zerotier_native.cpp +++ b/Source/dvlnet/zerotier_native.cpp @@ -63,7 +63,7 @@ void zerotier_network_start() { if (zt_started) return; - zts_start(GetPrefPath().c_str(), (void (*)(void *))Callback, 0); + zts_start(paths::PrefPath().c_str(), (void (*)(void *))Callback, 0); std::atexit(zerotier_network_stop); } diff --git a/Source/init.cpp b/Source/init.cpp index dfcd4dc21..9a7418d64 100644 --- a/Source/init.cpp +++ b/Source/init.cpp @@ -152,8 +152,8 @@ void init_archives() std::vector paths; paths.reserve(5); - paths.push_back(GetBasePath()); - paths.push_back(GetPrefPath()); + paths.push_back(paths::BasePath()); + paths.push_back(paths::PrefPath()); if (paths[0] == paths[1]) paths.pop_back(); diff --git a/Source/pfile.cpp b/Source/pfile.cpp index 833eac7b3..55f6adff0 100644 --- a/Source/pfile.cpp +++ b/Source/pfile.cpp @@ -29,7 +29,7 @@ namespace { std::string GetSavePath(DWORD save_num) { - std::string path = GetPrefPath(); + std::string path = paths::PrefPath(); const char *ext = ".sv"; if (gbIsHellfire) ext = ".hsv"; diff --git a/Source/restrict.cpp b/Source/restrict.cpp index 8dbd7dd1e..448dcc56d 100644 --- a/Source/restrict.cpp +++ b/Source/restrict.cpp @@ -15,10 +15,10 @@ namespace devilution { */ void ReadOnlyTest() { - const std::string path = GetPrefPath() + "Diablo1ReadOnlyTest.foo"; + const std::string path = paths::PrefPath() + "Diablo1ReadOnlyTest.foo"; FILE *f = fopen(path.c_str(), "wt"); if (f == nullptr) { - DirErrorDlg(GetPrefPath().c_str()); + DirErrorDlg(paths::PrefPath().c_str()); } fclose(f); diff --git a/Source/storm/storm.cpp b/Source/storm/storm.cpp index 245021532..358a17216 100644 --- a/Source/storm/storm.cpp +++ b/Source/storm/storm.cpp @@ -37,7 +37,7 @@ std::string *SBasePath = nullptr; radon::File &getIni() { - static radon::File ini(GetConfigPath() + "diablo.ini"); + static radon::File ini(paths::ConfigPath() + "diablo.ini"); return ini; } diff --git a/Source/utils/language.cpp b/Source/utils/language.cpp index 7e80bea70..c1d4ed43f 100644 --- a/Source/utils/language.cpp +++ b/Source/utils/language.cpp @@ -137,9 +137,9 @@ void LanguageInitialize() FILE *fp; bool utf8; - auto path = GetLangPath() + "./" + sgOptions.Language.szCode + ".gmo"; + auto path = paths::LangPath() + "./" + sgOptions.Language.szCode + ".gmo"; if (!(fp = fopen(path.c_str(), "rb"))) { - path = GetLangPath() + "./" + sgOptions.Language.szCode + ".mo"; + path = paths::LangPath() + "./" + sgOptions.Language.szCode + ".mo"; if (!(fp = fopen(path.c_str(), "rb"))) { perror(path.c_str()); return; diff --git a/Source/utils/paths.cpp b/Source/utils/paths.cpp index 22f2196e3..6ed7c78eb 100644 --- a/Source/utils/paths.cpp +++ b/Source/utils/paths.cpp @@ -23,29 +23,31 @@ namespace devilution { +namespace paths { + namespace { -std::string *basePath = nullptr; -std::string *prefPath = nullptr; -std::string *configPath = nullptr; -std::string *langPath = nullptr; -std::string *ttfPath = nullptr; -std::string *ttfName = nullptr; +std::optional basePath; +std::optional prefPath; +std::optional configPath; +std::optional langPath; +std::optional ttfPath; +std::optional ttfName; -void AddTrailingSlash(std::string *path) +void AddTrailingSlash(std::string &path) { #ifdef _WIN32 - if (!path->empty() && path->back() != '\\') - *path += '\\'; + if (!path.empty() && path.back() != '\\') + path += '\\'; #else - if (!path->empty() && path->back() != '/') - *path += '/'; + if (!path.empty() && path.back() != '/') + path += '/'; #endif } -std::string *FromSDL(char *s) +std::string FromSDL(char *s) { - auto *result = new std::string(s != nullptr ? s : ""); + std::string result = (s != nullptr ? s : ""); if (s != nullptr) { SDL_free(s); } else { @@ -57,98 +59,88 @@ std::string *FromSDL(char *s) } // namespace -const std::string &GetBasePath() +const std::string &BasePath() { + if (!basePath) { #ifdef __vita__ - if (basePath == NULL) - basePath = new std::string(GetPrefPath()); + basePath = PrefPath(); #else - if (basePath == nullptr) basePath = FromSDL(SDL_GetBasePath()); #endif + } return *basePath; } -const std::string &GetPrefPath() +const std::string &PrefPath() { - if (prefPath == nullptr) + if (!prefPath) prefPath = FromSDL(SDL_GetPrefPath("diasurgical", "devilution")); return *prefPath; } -const std::string &GetConfigPath() +const std::string &ConfigPath() { - if (configPath == nullptr) + if (!configPath) configPath = FromSDL(SDL_GetPrefPath("diasurgical", "devilution")); return *configPath; } -const std::string &GetTtfPath() +const std::string &LangPath() { - if (ttfPath == nullptr) - ttfPath = new std::string(TTF_FONT_DIR); - return *ttfPath; + if (!langPath) + langPath = MO_LANG_DIR; + return *langPath; } -const std::string &GetLangPath() +const std::string &TtfPath() { - if (langPath == NULL) - langPath = new std::string(MO_LANG_DIR); - return *langPath; + if (!ttfPath) + ttfPath = TTF_FONT_DIR; + return *ttfPath; } -const std::string &GetTtfName() +const std::string &TtfName() { - if (ttfName == nullptr) - ttfName = new std::string(TTF_FONT_NAME); + if (!ttfName) + ttfName = TTF_FONT_NAME; return *ttfName; } -void SetBasePath(const char *path) +void SetBasePath(const std::string &path) { - if (basePath == nullptr) - basePath = new std::string; - *basePath = path; - AddTrailingSlash(basePath); + basePath = path; + AddTrailingSlash(*basePath); } -void SetPrefPath(const char *path) +void SetPrefPath(const std::string &path) { - if (prefPath == nullptr) - prefPath = new std::string; - *prefPath = path; - AddTrailingSlash(prefPath); + prefPath = path; + AddTrailingSlash(*prefPath); } -void SetConfigPath(const char *path) +void SetConfigPath(const std::string &path) { - if (configPath == nullptr) - configPath = new std::string; - *configPath = path; - AddTrailingSlash(configPath); + configPath = path; + AddTrailingSlash(*configPath); } -void SetLangPath(const char *path) +void SetLangPath(const std::string &path) { - if (langPath == NULL) - langPath = new std::string; - *langPath = path; - AddTrailingSlash(langPath); + langPath = path; + AddTrailingSlash(*langPath); } -void SetTtfPath(const char *path) +void SetTtfPath(const std::string &path) { - if (ttfPath == nullptr) - ttfPath = new std::string; - *ttfPath = path; - AddTrailingSlash(ttfPath); + ttfPath = path; + AddTrailingSlash(*ttfPath); } -void SetTtfName(const char *path) +void SetTtfName(const std::string &name) { - if (ttfName == nullptr) - ttfName = new std::string; - *ttfName = path; + ttfName = name; } +} // namespace paths + } // namespace devilution diff --git a/Source/utils/paths.h b/Source/utils/paths.h index 7c286bbc4..02bd2fc44 100644 --- a/Source/utils/paths.h +++ b/Source/utils/paths.h @@ -4,18 +4,22 @@ namespace devilution { -const std::string &GetBasePath(); -const std::string &GetPrefPath(); -const std::string &GetConfigPath(); -const std::string &GetLangPath(); -const std::string &GetTtfPath(); -const std::string &GetTtfName(); - -void SetBasePath(const char *path); -void SetPrefPath(const char *path); -void SetConfigPath(const char *path); -void SetLangPath(const char *path); -void SetTtfPath(const char *path); -void SetTtfName(const char *path); +namespace paths { + +const std::string &BasePath(); +const std::string &PrefPath(); +const std::string &ConfigPath(); +const std::string &LangPath(); +const std::string &TtfPath(); +const std::string &TtfName(); + +void SetBasePath(const std::string &path); +void SetPrefPath(const std::string &path); +void SetConfigPath(const std::string &path); +void SetLangPath(const std::string &path); +void SetTtfPath(const std::string &path); +void SetTtfName(const std::string &name); + +} // namespace paths } // namespace devilution diff --git a/test/writehero_test.cpp b/test/writehero_test.cpp index 642bb6552..b78d83ff8 100644 --- a/test/writehero_test.cpp +++ b/test/writehero_test.cpp @@ -361,7 +361,7 @@ static void AssertPlayer(PlayerStruct *pPlayer) TEST(Writehero, pfile_write_hero) { - SetPrefPath("."); + paths::SetPrefPath("."); std::remove("multi_0.sv"); gbVanilla = true;