1
0
Fork 0

Codechange: replace char* with C++ style strings

pull/14171/head
Rubidium 2025-04-29 22:34:20 +02:00 committed by rubidium42
parent 356b0159c7
commit 708e6a512d
20 changed files with 45 additions and 57 deletions

View File

@ -1170,7 +1170,7 @@ std::optional<FileHandle> FileHandle::Open(const std::string &filename, std::str
{
#if defined(_WIN32)
/* Windows also requires mode to be wchar_t. */
auto f = _wfopen(OTTD2FS(filename).c_str(), OTTD2FS(std::string{mode}).c_str());
auto f = _wfopen(OTTD2FS(filename).c_str(), OTTD2FS(mode).c_str());
#else
auto f = fopen(filename.c_str(), std::string{mode}.c_str());
#endif /* _WIN32 */

View File

@ -113,7 +113,7 @@ void FreeTypeFontCache::SetFontSize(int pixels)
}
}
static FT_Error LoadFont(FontSize fs, FT_Face face, const char *font_name, uint size)
static FT_Error LoadFont(FontSize fs, FT_Face face, std::string_view font_name, uint size)
{
Debug(fontcache, 2, "Requested '{}', using '{} {}'", font_name, face->family_name, face->style_name);
@ -172,29 +172,28 @@ void LoadFreeTypeFont(FontSize fs)
Debug(fontcache, 2, "Initialized");
}
const char *font_name = font.c_str();
FT_Face face = nullptr;
/* If font is an absolute path to a ttf, try loading that first. */
int32_t index = 0;
if (settings->os_handle != nullptr) index = *static_cast<const int32_t *>(settings->os_handle);
FT_Error error = FT_New_Face(_ft_library, font_name, index, &face);
FT_Error error = FT_New_Face(_ft_library, font.c_str(), index, &face);
if (error != FT_Err_Ok) {
/* Check if font is a relative filename in one of our search-paths. */
std::string full_font = FioFindFullPath(BASE_DIR, font_name);
std::string full_font = FioFindFullPath(BASE_DIR, font);
if (!full_font.empty()) {
error = FT_New_Face(_ft_library, full_font.c_str(), 0, &face);
}
}
/* Try loading based on font face name (OS-wide fonts). */
if (error != FT_Err_Ok) error = GetFontByFaceName(font_name, &face);
if (error != FT_Err_Ok) error = GetFontByFaceName(font, &face);
if (error == FT_Err_Ok) {
error = LoadFont(fs, face, font_name, GetFontCacheFontSize(fs));
error = LoadFont(fs, face, font, GetFontCacheFontSize(fs));
if (error != FT_Err_Ok) {
ShowInfo("Unable to use '{}' for {} font, FreeType reported error 0x{:X}, using sprite font instead", font_name, FontSizeToName(fs), error);
ShowInfo("Unable to use '{}' for {} font, FreeType reported error 0x{:X}, using sprite font instead", font, FontSizeToName(fs), error);
}
} else {
FT_Done_Face(face);
@ -309,7 +308,7 @@ void UninitFreeType()
#if !defined(WITH_FONTCONFIG)
FT_Error GetFontByFaceName(const char *font_name, FT_Face *face) { return FT_Err_Cannot_Open_Resource; }
FT_Error GetFontByFaceName(std::string_view font_name, FT_Face *face) { return FT_Err_Cannot_Open_Resource; }
#endif /* !defined(WITH_FONTCONFIG) */

View File

@ -23,7 +23,7 @@
* @param face The face that has been found.
* @return The error we encountered.
*/
FT_Error GetFontByFaceName(const char *font_name, FT_Face *face);
FT_Error GetFontByFaceName(std::string_view font_name, FT_Face *face);
#endif /* WITH_FREETYPE */

View File

@ -134,9 +134,9 @@ struct TranslationWriter : LanguageWriter {
/* We don't write the length. */
}
void Write(const char *buffer, size_t length) override
void Write(std::string_view buffer) override
{
this->strings.emplace_back(buffer, length);
this->strings.emplace_back(buffer);
}
};

View File

@ -128,7 +128,7 @@ static void AddGrfInfo(std::back_insert_iterator<std::string> &output_iterator,
/** Text messages for various logged actions */
static const char * const la_text[] = {
static const std::string_view la_text[] = {
"new game started",
"game loaded",
"GRF config changed",

View File

@ -117,8 +117,8 @@ struct SelectGameWindow : public Window {
intro_viewport_commands.clear();
/* Regular expression matching the commands: T, spaces, integer, spaces, flags, spaces, integer */
const char *sign_langauge = "^T\\s*([0-9]+)\\s*([-+A-Z0-9]+)\\s*([0-9]+)";
std::regex re(sign_langauge, std::regex_constants::icase);
static const std::string sign_language = "^T\\s*([0-9]+)\\s*([-+A-Z0-9]+)\\s*([0-9]+)";
std::regex re(sign_language, std::regex_constants::icase);
/* List of signs successfully parsed to delete afterwards. */
std::vector<SignID> signs_to_delete;

View File

@ -590,7 +590,7 @@ static void FillGrfidMap(const GRFConfigList &lst, GrfIdMap &grfid_map)
}
static void NewGRFConfirmationCallback(Window *w, bool confirmed);
static void ShowSavePresetWindow(const char *initial_text);
static void ShowSavePresetWindow(std::string_view initial_text);
/**
* Window for showing NewGRF files
@ -958,13 +958,13 @@ struct NewGRFWindow : public Window, NewGRFScanCallback {
}
case WID_NS_PRESET_SAVE:
ShowSavePresetWindow((this->preset == -1) ? nullptr : this->grf_presets[this->preset].c_str());
ShowSavePresetWindow((this->preset == -1) ? std::string_view{} : this->grf_presets[this->preset]);
break;
case WID_NS_PRESET_DELETE:
if (this->preset == -1) return;
DeleteGRFPresetFromConfig(this->grf_presets[this->preset].c_str());
DeleteGRFPresetFromConfig(this->grf_presets[this->preset]);
this->grf_presets = GetGRFPresetList();
this->preset = -1;
this->InvalidateData();
@ -2020,12 +2020,12 @@ struct SavePresetWindow : public Window {
/**
* Constructor of the save preset window.
* @param initial_text Initial text to display in the edit box, or \c nullptr.
* @param initial_text Initial text to display in the edit box.
*/
SavePresetWindow(const char *initial_text) : Window(_save_preset_desc), presetname_editbox(32)
SavePresetWindow(std::string_view initial_text) : Window(_save_preset_desc), presetname_editbox(32)
{
this->presets = GetGRFPresetList();
if (initial_text != nullptr) {
if (!initial_text.empty()) {
for (uint i = 0; i < this->presets.size(); i++) {
if (this->presets[i] == initial_text) {
this->selected = i;
@ -2044,7 +2044,7 @@ struct SavePresetWindow : public Window {
this->vscroll->SetCount(this->presets.size());
this->SetFocusedWidget(WID_SVP_EDITBOX);
if (initial_text != nullptr) this->presetname_editbox.text.Assign(initial_text);
this->presetname_editbox.text.Assign(initial_text);
}
~SavePresetWindow()
@ -2132,7 +2132,7 @@ struct SavePresetWindow : public Window {
* Open the window for saving a preset.
* @param initial_text Initial text to display in the edit box, or \c nullptr.
*/
static void ShowSavePresetWindow(const char *initial_text)
static void ShowSavePresetWindow(std::string_view initial_text)
{
CloseWindowByClass(WC_SAVE_PRESET);
new SavePresetWindow(initial_text);

View File

@ -104,7 +104,7 @@ bool HandleBootstrap();
extern void CheckCaches();
extern Company *DoStartupNewCompany(bool is_ai, CompanyID company = CompanyID::Invalid());
extern void OSOpenBrowser(const std::string &url);
extern void ShowOSErrorBox(const char *buf, bool system);
extern void ShowOSErrorBox(std::string_view buf, bool system);
extern std::string _config_file;
bool _save_config = false;
@ -118,7 +118,7 @@ NewGRFScanCallback *_request_newgrf_scan_callback = nullptr;
*/
void UserErrorI(const std::string &str)
{
ShowOSErrorBox(str.c_str(), false);
ShowOSErrorBox(str, false);
if (VideoDriver::GetInstance() != nullptr) VideoDriver::GetInstance()->Stop();
#ifdef __EMSCRIPTEN__
@ -140,7 +140,7 @@ void UserErrorI(const std::string &str)
void FatalErrorI(const std::string &str)
{
if (VideoDriver::GetInstance() == nullptr || VideoDriver::GetInstance()->HasGUI()) {
ShowOSErrorBox(str.c_str(), true);
ShowOSErrorBox(str, true);
}
/* Set the error message for the crash log and then invoke it. */

View File

@ -130,7 +130,7 @@ public:
"{}\n{}\n{}\n{}",
this->crashlog_filename, this->crashdump_filename, this->savegame_filename, this->screenshot_filename);
ShowMacDialog(crash_title, message.c_str(), "Quit");
ShowMacDialog(crash_title, message, "Quit");
}
/** Buffer to track the long jump set setup. */

View File

@ -133,7 +133,7 @@ void ShowMacDialog(std::string_view title, std::string_view message, std::string
* @param buf error message text.
* @param system message text originates from OS.
*/
void ShowOSErrorBox(const char *buf, bool system)
void ShowOSErrorBox(std::string_view buf, bool system)
{
/* Display the error in the best way possible. */
if (system) {

View File

@ -39,7 +39,7 @@ static std::tuple<std::string, std::string> SplitFontFamilyAndStyle(std::string_
return { std::string(font_name.substr(0, separator)), std::string(font_name.substr(begin)) };
}
FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
FT_Error GetFontByFaceName(std::string_view font_name, FT_Face *face)
{
FT_Error err = FT_Err_Cannot_Open_Resource;

View File

@ -190,7 +190,7 @@ void ShowInfoI(std::string_view str)
}
#if !defined(__APPLE__)
void ShowOSErrorBox(const char *buf, bool)
void ShowOSErrorBox(std::string_view buf, bool)
{
/* All unix systems, except OSX. Only use escape codes on a TTY. */
if (isatty(fileno(stderr))) {

View File

@ -47,7 +47,7 @@ bool MyShowCursor(bool show, bool toggle)
return !show;
}
void ShowOSErrorBox(const char *buf, bool)
void ShowOSErrorBox(std::string_view buf, bool)
{
MyShowCursor(true);
MessageBox(GetActiveWindow(), OTTD2FS(buf).c_str(), L"Error!", MB_ICONSTOP | MB_TASKMODAL);

View File

@ -33,8 +33,8 @@
#include "safeguards.h"
static const char * const SCREENSHOT_NAME = "screenshot"; ///< Default filename of a saved screenshot.
static const char * const HEIGHTMAP_NAME = "heightmap"; ///< Default filename of a saved heightmap.
static const std::string_view SCREENSHOT_NAME = "screenshot"; ///< Default filename of a saved screenshot.
static const std::string_view HEIGHTMAP_NAME = "heightmap"; ///< Default filename of a saved heightmap.
std::string _screenshot_format_name; ///< Extension of the current screenshot format.
static std::string _screenshot_name; ///< Filename of the screenshot file.

View File

@ -124,16 +124,6 @@ uint BaseSettingEntry::Draw(GameSettings *settings_ptr, int left, int right, int
/* == SettingEntry methods == */
/**
* Constructor for a single setting in the 'advanced settings' window
* @param name Name of the setting in the setting table
*/
SettingEntry::SettingEntry(const char *name)
{
this->name = name;
this->setting = nullptr;
}
/**
* Initialization of a setting entry
* @param level Page nesting level of this entry

View File

@ -92,10 +92,10 @@ protected:
/** Standard setting */
struct SettingEntry : BaseSettingEntry {
const char *name; ///< Name of the setting
const IntSettingDesc *setting; ///< Setting description of the setting
const std::string_view name; ///< Name of the setting
const IntSettingDesc *setting = nullptr; ///< Setting description of the setting
SettingEntry(const char *name);
SettingEntry(std::string_view name) : name(name) {}
void Init(uint8_t level = 0) override;
void ResetAll() override;

View File

@ -52,9 +52,9 @@ static const TrackdirBits _enterdir_to_trackdirbits[DIAGDIR_END] = {
template <typename Tdir, uint items>
struct SmallSet {
private:
uint n; // actual number of units
bool overflowed; // did we try to overflow the set?
const char *name; // name, used for debugging purposes...
uint n = 0; // actual number of units
bool overflowed = false; // did we try to overflow the set?
const std::string_view name; // name, used for debugging purposes...
/** Element of set */
struct SSdata {
@ -64,7 +64,7 @@ private:
public:
/** Constructor - just set default values and 'name' */
SmallSet(const char *name) : n(0), overflowed(false), name(name) { }
SmallSet(std::string_view name) : name(name) { }
/** Reset variables to default values */
void Reset()

View File

@ -289,7 +289,7 @@ struct LanguageFileWriter : LanguageWriter, FileWriter {
void WriteHeader(const LanguagePackHeader *header) override
{
this->Write(reinterpret_cast<const char *>(header), sizeof(*header));
this->Write({reinterpret_cast<const char *>(header), sizeof(*header)});
}
void Finalise() override
@ -298,9 +298,9 @@ struct LanguageFileWriter : LanguageWriter, FileWriter {
this->FileWriter::Finalise();
}
void Write(const char *buffer, size_t length) override
void Write(std::string_view buffer) override
{
this->output_stream.write(buffer, length);
this->output_stream.write(buffer.data(), buffer.size());
}
};

View File

@ -117,9 +117,8 @@ struct LanguageWriter {
/**
* Write a number of bytes.
* @param buffer The buffer to write.
* @param length The amount of byte to write.
*/
virtual void Write(const char *buffer, size_t length) = 0;
virtual void Write(std::string_view buffer) = 0;
/**
* Finalise writing the file.

View File

@ -718,7 +718,7 @@ void LanguageWriter::WriteLength(size_t length)
buffer[offs++] = static_cast<char>(static_cast<uint8_t>((length >> 8) | 0xC0));
}
buffer[offs++] = static_cast<char>(static_cast<uint8_t>(length & 0xFF));
this->Write(buffer, offs);
this->Write({buffer, offs});
}
/**
@ -803,7 +803,7 @@ void LanguageWriter::WriteLang(const StringData &data)
builder.Put(def_str);
this->WriteLength(output.size());
this->Write(output.data(), output.size());
this->Write(output);
}
}
}