diff --git a/src/crashlog.cpp b/src/crashlog.cpp --- a/src/crashlog.cpp +++ b/src/crashlog.cpp @@ -417,7 +417,7 @@ bool CrashLog::WriteScreenshot() std::string filename = this->CreateFileName("", false); bool res = MakeScreenshot(SC_CRASHLOG, filename); - if (res) this->screenshot_filename = _full_screenshot_name; + if (res) this->screenshot_filename = _full_screenshot_path; return res; } diff --git a/src/screenshot.cpp b/src/screenshot.cpp --- a/src/screenshot.cpp +++ b/src/screenshot.cpp @@ -40,8 +40,8 @@ static const char * const HEIGHTMAP_NAME std::string _screenshot_format_name; ///< Extension of the current screenshot format (corresponds with #_cur_screenshot_format). uint _num_screenshot_formats; ///< Number of available screenshot formats. uint _cur_screenshot_format; ///< Index of the currently selected screenshot format in #_screenshot_formats. -static char _screenshot_name[128]; ///< Filename of the screenshot file. -char _full_screenshot_name[MAX_PATH]; ///< Pathname of the screenshot file. +static std::string _screenshot_name; ///< Filename of the screenshot file. +std::string _full_screenshot_path; ///< Pathname of the screenshot file. uint _heightmap_highest_peak; ///< When saving a heightmap, this contains the highest peak on the map. /** @@ -668,43 +668,39 @@ static void LargeWorldCallback(void *use */ static const char *MakeScreenshotName(const char *default_fn, const char *ext, bool crashlog = false) { - bool generate = StrEmpty(_screenshot_name); + bool generate = _screenshot_name.empty(); if (generate) { if (_game_mode == GM_EDITOR || _game_mode == GM_MENU || _local_company == COMPANY_SPECTATOR) { - strecpy(_screenshot_name, default_fn, lastof(_screenshot_name)); + _screenshot_name = default_fn; } else { - strecpy(_screenshot_name, GenerateDefaultSaveName().c_str(), lastof(_screenshot_name)); + _screenshot_name = GenerateDefaultSaveName(); } } - size_t len = strlen(_screenshot_name); - /* Handle user-specified filenames ending in # with automatic numbering */ if (StrEndsWith(_screenshot_name, "#")) { generate = true; - len -= 1; - _screenshot_name[len] = '\0'; + _screenshot_name.pop_back(); } + size_t len = _screenshot_name.size(); /* Add extension to screenshot file */ - seprintf(&_screenshot_name[len], lastof(_screenshot_name), ".%s", ext); + _screenshot_name += fmt::format(".{}", ext); const char *screenshot_dir = crashlog ? _personal_dir.c_str() : FiosGetScreenshotDir(); for (uint serial = 1;; serial++) { - if (seprintf(_full_screenshot_name, lastof(_full_screenshot_name), "%s%s", screenshot_dir, _screenshot_name) >= (int)lengthof(_full_screenshot_name)) { - /* We need more characters than MAX_PATH -> end with error */ - _full_screenshot_name[0] = '\0'; - break; - } + _full_screenshot_path = fmt::format("{}{}", screenshot_dir, _screenshot_name); + if (!generate) break; // allow overwriting of non-automatic filenames - if (!FileExists(_full_screenshot_name)) break; + if (!FileExists(_full_screenshot_path)) break; /* If file exists try another one with same name, but just with a higher index */ - seprintf(&_screenshot_name[len], lastof(_screenshot_name) - len, "#%u.%s", serial, ext); + _screenshot_name.erase(len); + _screenshot_name += fmt::format("#{}.{}", serial, ext); } - return _full_screenshot_name; + return _full_screenshot_path.c_str(); } /** Make a screenshot of the current screen. */ @@ -922,8 +918,7 @@ static bool RealMakeScreenshot(Screensho SetScreenshotWindowVisibility(false); } - _screenshot_name[0] = '\0'; - if (!name.empty()) strecpy(_screenshot_name, name.c_str(), lastof(_screenshot_name)); + _screenshot_name = name; bool ret; switch (t) { diff --git a/src/screenshot.h b/src/screenshot.h --- a/src/screenshot.h +++ b/src/screenshot.h @@ -34,6 +34,6 @@ bool MakeMinimapWorldScreenshot(); extern std::string _screenshot_format_name; extern uint _num_screenshot_formats; extern uint _cur_screenshot_format; -extern char _full_screenshot_name[MAX_PATH]; +extern std::string _full_screenshot_path; #endif /* SCREENSHOT_H */