diff --git a/src/openrct2-ui/windows/EditorObjectSelection.cpp b/src/openrct2-ui/windows/EditorObjectSelection.cpp index a766157493..f78617e643 100644 --- a/src/openrct2-ui/windows/EditorObjectSelection.cpp +++ b/src/openrct2-ui/windows/EditorObjectSelection.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -1236,12 +1237,12 @@ private: DrawTextBasic(dpi, screenPos, stringId, {}, { COLOUR_WHITE, TextAlignment::RIGHT }); screenPos.y += LIST_ROW_HEIGHT; - // Draw object dat name + // Draw object filename { - const char* path = path_get_filename(listItem->repositoryItem->Path.c_str()); + auto path = Path::GetFileName(listItem->repositoryItem->Path); auto ft = Formatter(); ft.Add(STR_STRING); - ft.Add(path); + ft.Add(path.c_str()); DrawTextBasic( dpi, { windowPos.x + this->width - 5, screenPos.y }, STR_WINDOW_COLOUR_2_STRINGID, ft, { COLOUR_BLACK, TextAlignment::RIGHT }); diff --git a/src/openrct2-ui/windows/LoadSave.cpp b/src/openrct2-ui/windows/LoadSave.cpp index ebcd19d187..659939f68e 100644 --- a/src/openrct2-ui/windows/LoadSave.cpp +++ b/src/openrct2-ui/windows/LoadSave.cpp @@ -966,15 +966,12 @@ static void SetAndSaveConfigPath(u8string& config_str, u8string_view path) static bool IsValidPath(const char* path) { - char filename[MAX_PATH]; - safe_strcpy(filename, path_get_filename(path), sizeof(filename)); - // HACK This is needed because tracks get passed through with td? // I am sure this will change eventually to use the new FileScanner // which handles multiple patterns - path_remove_extension(filename); + auto filename = Path::GetFileNameWithoutExtension(path); - return filename_valid_characters(filename); + return filename_valid_characters(filename.c_str()); } static void WindowLoadsaveSelect(rct_window* w, const char* path) diff --git a/src/openrct2-ui/windows/TitleCommandEditor.cpp b/src/openrct2-ui/windows/TitleCommandEditor.cpp index e58f588862..2d8e223bf9 100644 --- a/src/openrct2-ui/windows/TitleCommandEditor.cpp +++ b/src/openrct2-ui/windows/TitleCommandEditor.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -143,7 +144,7 @@ static void ScenarioSelectCallback(const utf8* path) { if (_command.Type == TitleScript::LoadSc) { - const utf8* fileName = path_get_filename(path); + const auto fileName = Path::GetFileName(path); auto scenario = GetScenarioRepository()->GetByFilename(fileName); safe_strcpy(_command.Scenario, scenario->internal_name, sizeof(_command.Scenario)); } diff --git a/src/openrct2-ui/windows/TitleEditor.cpp b/src/openrct2-ui/windows/TitleEditor.cpp index e5dcdd1f5c..53a76b5aa0 100644 --- a/src/openrct2-ui/windows/TitleEditor.cpp +++ b/src/openrct2-ui/windows/TitleEditor.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -182,7 +183,7 @@ static bool _isSequenceReadOnly; static std::unique_ptr _editingTitleSequence; static const utf8* _sequenceName; -static utf8* _renameSavePath = nullptr; +static u8string _renameSavePath = u8string(); static int16_t _window_title_editor_highlighted_index; @@ -247,7 +248,7 @@ static void WindowTitleEditorClose(rct_window* w) _editingTitleSequence = nullptr; _sequenceName = nullptr; - SafeFree(_renameSavePath); + _renameSavePath.clear(); } static void WindowTitleEditorMouseup(rct_window* w, rct_widgetindex widgetIndex) @@ -1120,19 +1121,18 @@ static void WindowTitleEditorAddParkCallback(int32_t result, const utf8* path) if (extension != FileExtension::SV4 && extension != FileExtension::SV6 && extension != FileExtension::PARK) return; - const utf8* filename = path_get_filename(path); - if (SaveFilenameExists(filename)) + const auto filename = Path::GetFileName(path); + if (SaveFilenameExists(filename.c_str())) { - free(_renameSavePath); - _renameSavePath = _strdup(filename); + _renameSavePath = filename; rct_window* w = window_find_by_class(WC_TITLE_EDITOR); WindowTextInputOpen( w, WIDX_TITLE_EDITOR_RENAME_SAVE, STR_FILEBROWSER_RENAME_SAVE_TITLE, STR_ERROR_EXISTING_NAME, {}, STR_STRING, - reinterpret_cast(_renameSavePath), 52 - 1); + reinterpret_cast(_renameSavePath.c_str()), 52 - 1); return; } - TitleSequenceAddPark(*_editingTitleSequence, path, filename); + TitleSequenceAddPark(*_editingTitleSequence, path, filename.c_str()); } static void WindowTitleEditorRenamePark(size_t index, const utf8* name) diff --git a/src/openrct2/Game.cpp b/src/openrct2/Game.cpp index 5880f2aae1..0b9098bdd5 100644 --- a/src/openrct2/Game.cpp +++ b/src/openrct2/Game.cpp @@ -578,13 +578,11 @@ void save_game_with_name(u8string_view name) void* create_save_game_as_intent() { - char name[MAX_PATH]; - safe_strcpy(name, path_get_filename(gScenarioSavePath.c_str()), MAX_PATH); - path_remove_extension(name); + auto name = Path::GetFileNameWithoutExtension(gScenarioSavePath); Intent* intent = new Intent(WC_LOADSAVE); intent->putExtra(INTENT_EXTRA_LOADSAVE_TYPE, LOADSAVETYPE_SAVE | LOADSAVETYPE_GAME); - intent->putExtra(INTENT_EXTRA_PATH, std::string{ name }); + intent->putExtra(INTENT_EXTRA_PATH, name); return intent; } diff --git a/src/openrct2/interface/Screenshot.cpp b/src/openrct2/interface/Screenshot.cpp index e3f49ac55e..d01e431238 100644 --- a/src/openrct2/interface/Screenshot.cpp +++ b/src/openrct2/interface/Screenshot.cpp @@ -20,6 +20,7 @@ #include "../core/Console.hpp" #include "../core/File.h" #include "../core/Imaging.h" +#include "../core/Path.hpp" #include "../drawing/Drawing.h" #include "../drawing/X8DrawingEngine.h" #include "../localisation/Formatter.h" @@ -415,9 +416,10 @@ void screenshot_giant() WriteDpiToFile(path.value(), &dpi, gPalette); // Show user that screenshot saved successfully + const auto filename = Path::GetFileName(path.value()); Formatter ft; ft.Add(STR_STRING); - ft.Add(path_get_filename(path->c_str())); + ft.Add(filename.c_str()); context_show_error(STR_SCREENSHOT_SAVED_AS, STR_NONE, ft); } catch (const std::exception& e) diff --git a/src/openrct2/rct1/S4Importer.cpp b/src/openrct2/rct1/S4Importer.cpp index eb6748a685..24543e0fd1 100644 --- a/src/openrct2/rct1/S4Importer.cpp +++ b/src/openrct2/rct1/S4Importer.cpp @@ -340,7 +340,7 @@ namespace RCT1 return ""; } - return path_get_filename(scenarioEntry->path); + return Path::GetFileName(scenarioEntry->path); } void InitialiseEntryMaps() diff --git a/src/openrct2/scenario/ScenarioRepository.cpp b/src/openrct2/scenario/ScenarioRepository.cpp index 54dd575e06..d79a6d6513 100644 --- a/src/openrct2/scenario/ScenarioRepository.cpp +++ b/src/openrct2/scenario/ScenarioRepository.cpp @@ -383,7 +383,7 @@ public: return result; } - const scenario_index_entry* GetByFilename(const utf8* filename) const override + const scenario_index_entry* GetByFilename(u8string_view filename) const override { for (const auto& scenario : _scenarios) { diff --git a/src/openrct2/scenario/ScenarioRepository.h b/src/openrct2/scenario/ScenarioRepository.h index c9ff1e12c7..9266471d7d 100644 --- a/src/openrct2/scenario/ScenarioRepository.h +++ b/src/openrct2/scenario/ScenarioRepository.h @@ -75,7 +75,7 @@ struct IScenarioRepository virtual size_t GetCount() const abstract; virtual const scenario_index_entry* GetByIndex(size_t index) const abstract; - virtual const scenario_index_entry* GetByFilename(const utf8* filename) const abstract; + virtual const scenario_index_entry* GetByFilename(u8string_view filename) const abstract; /** * Does not return custom scenarios due to the fact that they may have the same name. */ diff --git a/src/openrct2/util/Util.cpp b/src/openrct2/util/Util.cpp index 861033004c..bbebe2170e 100644 --- a/src/openrct2/util/Util.cpp +++ b/src/openrct2/util/Util.cpp @@ -81,7 +81,7 @@ utf8* path_get_directory(const utf8* path) return directory; } -const char* path_get_filename(const utf8* path) +static const char* path_get_filename(const utf8* path) { // Find last slash or backslash in the path char* filename = const_cast(strrchr(path, *PATH_SEPARATOR)); diff --git a/src/openrct2/util/Util.h b/src/openrct2/util/Util.h index 3e70ac10d8..e62f285baf 100644 --- a/src/openrct2/util/Util.h +++ b/src/openrct2/util/Util.h @@ -26,7 +26,6 @@ int32_t mph_to_dmps(int32_t mph); bool filename_valid_characters(const utf8* filename); char* path_get_directory(const utf8* path); -const char* path_get_filename(const utf8* path); void path_set_extension(utf8* path, const utf8* newExtension, size_t size); void path_append_extension(utf8* path, const utf8* newExtension, size_t size); void path_remove_extension(utf8* path);