1
0
Fork 0

Codechange: use std::string for text file name resolution

pull/10766/head
Rubidium 2023-05-05 00:04:52 +02:00 committed by rubidium42
parent 0b72297d57
commit 877349c13d
15 changed files with 56 additions and 58 deletions

View File

@ -285,7 +285,7 @@ struct AIConfigWindow : public Window {
this->SetWidgetDisabledState(WID_AIC_MOVE_DOWN, this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot + 1))); this->SetWidgetDisabledState(WID_AIC_MOVE_DOWN, this->selected_slot == INVALID_COMPANY || !IsEditable((CompanyID)(this->selected_slot + 1)));
for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) { for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
this->SetWidgetDisabledState(WID_AIC_TEXTFILE + tft, this->selected_slot == INVALID_COMPANY || (AIConfig::GetConfig(this->selected_slot)->GetTextfile(tft, this->selected_slot) == nullptr)); this->SetWidgetDisabledState(WID_AIC_TEXTFILE + tft, this->selected_slot == INVALID_COMPANY || !AIConfig::GetConfig(this->selected_slot)->GetTextfile(tft, this->selected_slot).has_value());
} }
} }
}; };

View File

@ -137,17 +137,17 @@ struct BaseSet {
/** /**
* Search a textfile file next to this base media. * Search a textfile file next to this base media.
* @param type The type of the textfile to search for. * @param type The type of the textfile to search for.
* @return The filename for the textfile, \c nullptr otherwise. * @return The filename for the textfile.
*/ */
const char *GetTextfile(TextfileType type) const std::optional<std::string> GetTextfile(TextfileType type) const
{ {
for (uint i = 0; i < NUM_FILES; i++) { for (uint i = 0; i < NUM_FILES; i++) {
const char *textfile = ::GetTextfile(type, BASESET_DIR, this->files[i].filename.c_str()); auto textfile = ::GetTextfile(type, BASESET_DIR, this->files[i].filename);
if (textfile != nullptr) { if (textfile.has_value()) {
return textfile; return textfile;
} }
} }
return nullptr; return std::nullopt;
} }
}; };

View File

@ -405,7 +405,7 @@ struct GSConfigWindow : public Window {
this->SetWidgetDisabledState(WID_GSC_CHANGE, (_game_mode == GM_NORMAL) || !IsEditable()); this->SetWidgetDisabledState(WID_GSC_CHANGE, (_game_mode == GM_NORMAL) || !IsEditable());
for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) { for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
this->SetWidgetDisabledState(WID_GSC_TEXTFILE + tft, GameConfig::GetConfig()->GetTextfile(tft, (CompanyID)OWNER_DEITY) == nullptr); this->SetWidgetDisabledState(WID_GSC_TEXTFILE + tft, !GameConfig::GetConfig()->GetTextfile(tft, (CompanyID)OWNER_DEITY).has_value());
} }
this->RebuildVisibleSettings(); this->RebuildVisibleSettings();
HideDropDownMenu(this); HideDropDownMenu(this);

View File

@ -49,11 +49,11 @@ bool ContentInfo::IsValid() const
/** /**
* Search a textfile file next to this file in the content list. * Search a textfile file next to this file in the content list.
* @param type The type of the textfile to search for. * @param type The type of the textfile to search for.
* @return The filename for the textfile, \c nullptr otherwise. * @return The filename for the textfile.
*/ */
const char *ContentInfo::GetTextfile(TextfileType type) const std::optional<std::string> ContentInfo::GetTextfile(TextfileType type) const
{ {
if (this->state == INVALID) return nullptr; if (this->state == INVALID) return std::nullopt;
const char *tmp; const char *tmp;
switch (this->type) { switch (this->type) {
default: NOT_REACHED(); default: NOT_REACHED();
@ -88,7 +88,7 @@ const char *ContentInfo::GetTextfile(TextfileType type) const
tmp = FindScenario(this, true); tmp = FindScenario(this, true);
break; break;
} }
if (tmp == nullptr) return nullptr; if (tmp == nullptr) return std::nullopt;
return ::GetTextfile(type, GetContentInfoSubDir(this->type), tmp); return ::GetTextfile(type, GetContentInfoSubDir(this->type), tmp);
} }

View File

@ -12,6 +12,8 @@
#ifndef NETWORK_CORE_TCP_CONTENT_TYPE_H #ifndef NETWORK_CORE_TCP_CONTENT_TYPE_H
#define NETWORK_CORE_TCP_CONTENT_TYPE_H #define NETWORK_CORE_TCP_CONTENT_TYPE_H
#include <optional>
/** The values in the enum are important; they are used as database 'keys' */ /** The values in the enum are important; they are used as database 'keys' */
enum ContentType { enum ContentType {
CONTENT_TYPE_BEGIN = 1, ///< Helper to mark the begin of the types CONTENT_TYPE_BEGIN = 1, ///< Helper to mark the begin of the types
@ -75,7 +77,7 @@ struct ContentInfo {
bool IsSelected() const; bool IsSelected() const;
bool IsValid() const; bool IsValid() const;
const char *GetTextfile(TextfileType type) const; std::optional<std::string> GetTextfile(TextfileType type) const;
}; };
#endif /* NETWORK_CORE_TCP_CONTENT_TYPE_H */ #endif /* NETWORK_CORE_TCP_CONTENT_TYPE_H */

View File

@ -43,8 +43,8 @@ struct ContentTextfileWindow : public TextfileWindow {
ContentTextfileWindow(TextfileType file_type, const ContentInfo *ci) : TextfileWindow(file_type), ci(ci) ContentTextfileWindow(TextfileType file_type, const ContentInfo *ci) : TextfileWindow(file_type), ci(ci)
{ {
const char *textfile = this->ci->GetTextfile(file_type); auto textfile = this->ci->GetTextfile(file_type);
this->LoadTextfile(textfile, GetContentInfoSubDir(this->ci->type)); this->LoadTextfile(textfile.value(), GetContentInfoSubDir(this->ci->type));
} }
StringID GetTypeString() const StringID GetTypeString() const
@ -998,7 +998,7 @@ public:
this->SetWidgetDisabledState(WID_NCL_SELECT_UPDATE, !show_select_upgrade); this->SetWidgetDisabledState(WID_NCL_SELECT_UPDATE, !show_select_upgrade);
this->SetWidgetDisabledState(WID_NCL_OPEN_URL, this->selected == nullptr || this->selected->url.empty()); this->SetWidgetDisabledState(WID_NCL_OPEN_URL, this->selected == nullptr || this->selected->url.empty());
for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) { for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
this->SetWidgetDisabledState(WID_NCL_TEXTFILE + tft, this->selected == nullptr || this->selected->state != ContentInfo::ALREADY_HERE || this->selected->GetTextfile(tft) == nullptr); this->SetWidgetDisabledState(WID_NCL_TEXTFILE + tft, this->selected == nullptr || this->selected->state != ContentInfo::ALREADY_HERE || !this->selected->GetTextfile(tft).has_value());
} }
this->GetWidget<NWidgetCore>(WID_NCL_CANCEL)->widget_data = this->filesize_sum == 0 ? STR_AI_SETTINGS_CLOSE : STR_AI_LIST_CANCEL; this->GetWidget<NWidgetCore>(WID_NCL_CANCEL)->widget_data = this->filesize_sum == 0 ? STR_AI_SETTINGS_CLOSE : STR_AI_LIST_CANCEL;

View File

@ -785,9 +785,9 @@ char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
/** /**
* Search a textfile file next to this NewGRF. * Search a textfile file next to this NewGRF.
* @param type The type of the textfile to search for. * @param type The type of the textfile to search for.
* @return The filename for the textfile, \c nullptr otherwise. * @return The filename for the textfile.
*/ */
const char *GRFConfig::GetTextfile(TextfileType type) const std::optional<std::string> GRFConfig::GetTextfile(TextfileType type) const
{ {
return ::GetTextfile(type, NEWGRF_DIR, this->filename.c_str()); return ::GetTextfile(type, NEWGRF_DIR, this->filename);
} }

View File

@ -17,6 +17,7 @@
#include "fileio_type.h" #include "fileio_type.h"
#include "textfile_type.h" #include "textfile_type.h"
#include "newgrf_text.h" #include "newgrf_text.h"
#include <optional>
/** GRF config bit flags */ /** GRF config bit flags */
enum GCF_Flags { enum GCF_Flags {
@ -184,7 +185,7 @@ struct GRFConfig : ZeroedMemoryAllocator {
void CopyParams(const GRFConfig &src); void CopyParams(const GRFConfig &src);
const char *GetTextfile(TextfileType type) const; std::optional<std::string> GetTextfile(TextfileType type) const;
const char *GetName() const; const char *GetName() const;
const char *GetDescription() const; const char *GetDescription() const;
const char *GetURL() const; const char *GetURL() const;

View File

@ -545,8 +545,8 @@ struct NewGRFTextfileWindow : public TextfileWindow {
NewGRFTextfileWindow(TextfileType file_type, const GRFConfig *c) : TextfileWindow(file_type), grf_config(c) NewGRFTextfileWindow(TextfileType file_type, const GRFConfig *c) : TextfileWindow(file_type), grf_config(c)
{ {
const char *textfile = this->grf_config->GetTextfile(file_type); auto textfile = this->grf_config->GetTextfile(file_type);
this->LoadTextfile(textfile, NEWGRF_DIR); this->LoadTextfile(textfile.value(), NEWGRF_DIR);
} }
void SetStringParameters(int widget) const override void SetStringParameters(int widget) const override
@ -1285,7 +1285,7 @@ struct NewGRFWindow : public Window, NewGRFScanCallback {
const GRFConfig *selected_config = (this->avail_sel == nullptr) ? this->active_sel : this->avail_sel; const GRFConfig *selected_config = (this->avail_sel == nullptr) ? this->active_sel : this->avail_sel;
for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) { for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
this->SetWidgetDisabledState(WID_NS_NEWGRF_TEXTFILE + tft, selected_config == nullptr || selected_config->GetTextfile(tft) == nullptr); this->SetWidgetDisabledState(WID_NS_NEWGRF_TEXTFILE + tft, selected_config == nullptr || !selected_config->GetTextfile(tft).has_value());
} }
this->SetWidgetDisabledState(WID_NS_OPEN_URL, selected_config == nullptr || StrEmpty(selected_config->GetURL())); this->SetWidgetDisabledState(WID_NS_OPEN_URL, selected_config == nullptr || StrEmpty(selected_config->GetURL()));

View File

@ -234,9 +234,9 @@ std::string ScriptConfig::SettingsToString() const
return string; return string;
} }
const char *ScriptConfig::GetTextfile(TextfileType type, CompanyID slot) const std::optional<std::string> ScriptConfig::GetTextfile(TextfileType type, CompanyID slot) const
{ {
if (slot == INVALID_COMPANY || this->GetInfo() == nullptr) return nullptr; if (slot == INVALID_COMPANY || this->GetInfo() == nullptr) return std::nullopt;
return ::GetTextfile(type, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR, this->GetInfo()->GetMainScript()); return ::GetTextfile(type, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR, this->GetInfo()->GetMainScript());
} }

View File

@ -184,9 +184,9 @@ public:
* Search a textfile file next to this script. * Search a textfile file next to this script.
* @param type The type of the textfile to search for. * @param type The type of the textfile to search for.
* @param slot #CompanyID to check status of. * @param slot #CompanyID to check status of.
* @return The filename for the textfile, \c nullptr otherwise. * @return The filename for the textfile.
*/ */
const char *GetTextfile(TextfileType type, CompanyID slot) const; std::optional<std::string> GetTextfile(TextfileType type, CompanyID slot) const;
void SetToLoadData(ScriptInstance::ScriptData *data); void SetToLoadData(ScriptInstance::ScriptData *data);
ScriptInstance::ScriptData *GetToLoadData(); ScriptInstance::ScriptData *GetToLoadData();

View File

@ -643,11 +643,11 @@ struct ScriptTextfileWindow : public TextfileWindow {
void OnInvalidateData(int data = 0, bool gui_scope = true) override void OnInvalidateData(int data = 0, bool gui_scope = true) override
{ {
const char *textfile = GetConfig(slot)->GetTextfile(file_type, slot); auto textfile = GetConfig(slot)->GetTextfile(file_type, slot);
if (textfile == nullptr) { if (!textfile.has_value()) {
this->Close(); this->Close();
} else { } else {
this->LoadTextfile(textfile, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR); this->LoadTextfile(textfile.value(), (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR);
} }
} }
}; };

View File

@ -101,8 +101,8 @@ struct BaseSetTextfileWindow : public TextfileWindow {
BaseSetTextfileWindow(TextfileType file_type, const TBaseSet* baseset, StringID content_type) : TextfileWindow(file_type), baseset(baseset), content_type(content_type) BaseSetTextfileWindow(TextfileType file_type, const TBaseSet* baseset, StringID content_type) : TextfileWindow(file_type), baseset(baseset), content_type(content_type)
{ {
const char *textfile = this->baseset->GetTextfile(file_type); auto textfile = this->baseset->GetTextfile(file_type);
this->LoadTextfile(textfile, BASESET_DIR); this->LoadTextfile(textfile.value(), BASESET_DIR);
} }
void SetStringParameters(int widget) const override void SetStringParameters(int widget) const override
@ -704,9 +704,9 @@ struct GameOptionsWindow : Window {
this->GetWidget<NWidgetCore>(WID_GO_BASE_GRF_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_GRF_STATUS, STR_NULL); this->GetWidget<NWidgetCore>(WID_GO_BASE_GRF_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_GRF_STATUS, STR_NULL);
for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) { for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
this->SetWidgetDisabledState(WID_GO_BASE_GRF_TEXTFILE + tft, BaseGraphics::GetUsedSet() == nullptr || BaseGraphics::GetUsedSet()->GetTextfile(tft) == nullptr); this->SetWidgetDisabledState(WID_GO_BASE_GRF_TEXTFILE + tft, BaseGraphics::GetUsedSet() == nullptr || !BaseGraphics::GetUsedSet()->GetTextfile(tft).has_value());
this->SetWidgetDisabledState(WID_GO_BASE_SFX_TEXTFILE + tft, BaseSounds::GetUsedSet() == nullptr || BaseSounds::GetUsedSet()->GetTextfile(tft) == nullptr); this->SetWidgetDisabledState(WID_GO_BASE_SFX_TEXTFILE + tft, BaseSounds::GetUsedSet() == nullptr || !BaseSounds::GetUsedSet()->GetTextfile(tft).has_value());
this->SetWidgetDisabledState(WID_GO_BASE_MUSIC_TEXTFILE + tft, BaseMusic::GetUsedSet() == nullptr || BaseMusic::GetUsedSet()->GetTextfile(tft) == nullptr); this->SetWidgetDisabledState(WID_GO_BASE_MUSIC_TEXTFILE + tft, BaseMusic::GetUsedSet() == nullptr || !BaseMusic::GetUsedSet()->GetTextfile(tft).has_value());
} }
missing_files = BaseMusic::GetUsedSet()->GetNumInvalid() == 0; missing_files = BaseMusic::GetUsedSet()->GetNumInvalid() == 0;

View File

@ -333,9 +333,9 @@ static void Xunzip(byte **bufp, size_t *sizep)
/** /**
* Loads the textfile text from file and setup #lines. * Loads the textfile text from file and setup #lines.
*/ */
/* virtual */ void TextfileWindow::LoadTextfile(const char *textfile, Subdirectory dir) /* virtual */ void TextfileWindow::LoadTextfile(const std::string &textfile, Subdirectory dir)
{ {
if (textfile == nullptr) return; if (textfile.empty()) return;
this->lines.clear(); this->lines.clear();
@ -350,19 +350,14 @@ static void Xunzip(byte **bufp, size_t *sizep)
if (read != filesize) return; if (read != filesize) return;
#if defined(WITH_ZLIB) || defined(WITH_LIBLZMA)
const char *suffix = strrchr(textfile, '.');
if (suffix == nullptr) return;
#endif
#if defined(WITH_ZLIB) #if defined(WITH_ZLIB)
/* In-place gunzip */ /* In-place gunzip */
if (strcmp(suffix, ".gz") == 0) Gunzip((byte**)&this->text, &filesize); if (StrEndsWith(textfile, ".gz")) Gunzip((byte**)&this->text, &filesize);
#endif #endif
#if defined(WITH_LIBLZMA) #if defined(WITH_LIBLZMA)
/* In-place xunzip */ /* In-place xunzip */
if (strcmp(suffix, ".xz") == 0) Xunzip((byte**)&this->text, &filesize); if (StrEndsWith(textfile, ".xz")) Xunzip((byte**)&this->text, &filesize);
#endif #endif
if (!this->text) return; if (!this->text) return;
@ -409,7 +404,7 @@ static void Xunzip(byte **bufp, size_t *sizep)
* @param filename The filename of the content to look for. * @param filename The filename of the content to look for.
* @return The path to the textfile, \c nullptr otherwise. * @return The path to the textfile, \c nullptr otherwise.
*/ */
const char *GetTextfile(TextfileType type, Subdirectory dir, const char *filename) std::optional<std::string> GetTextfile(TextfileType type, Subdirectory dir, const std::string &filename)
{ {
static const char * const prefixes[] = { static const char * const prefixes[] = {
"readme", "readme",
@ -418,17 +413,16 @@ const char *GetTextfile(TextfileType type, Subdirectory dir, const char *filenam
}; };
static_assert(lengthof(prefixes) == TFT_END); static_assert(lengthof(prefixes) == TFT_END);
const char *prefix = prefixes[type]; std::string_view prefix = prefixes[type];
if (filename == nullptr) return nullptr; if (filename.empty()) return std::nullopt;
static char file_path[MAX_PATH]; auto slash = filename.find_last_of(PATHSEPCHAR);
strecpy(file_path, filename, lastof(file_path)); if (slash == std::string::npos) return std::nullopt;
char *slash = strrchr(file_path, PATHSEPCHAR); std::string_view base_path(filename.data(), slash + 1);
if (slash == nullptr) return nullptr;
static const char * const exts[] = { static const std::initializer_list<std::string_view> extensions{
"txt", "txt",
#if defined(WITH_ZLIB) #if defined(WITH_ZLIB)
"txt.gz", "txt.gz",
@ -438,15 +432,15 @@ const char *GetTextfile(TextfileType type, Subdirectory dir, const char *filenam
#endif #endif
}; };
for (size_t i = 0; i < lengthof(exts); i++) { for (auto &extension : extensions) {
seprintf(slash + 1, lastof(file_path), "%s_%s.%s", prefix, GetCurrentLanguageIsoCode(), exts[i]); std::string file_path = fmt::format("{}{}_{}.{}", base_path, prefix, GetCurrentLanguageIsoCode(), extension);
if (FioCheckFileExists(file_path, dir)) return file_path; if (FioCheckFileExists(file_path, dir)) return file_path;
seprintf(slash + 1, lastof(file_path), "%s_%.2s.%s", prefix, GetCurrentLanguageIsoCode(), exts[i]); file_path = fmt::format("{}{}_{:.2s}.{}", base_path, prefix, GetCurrentLanguageIsoCode(), extension);
if (FioCheckFileExists(file_path, dir)) return file_path; if (FioCheckFileExists(file_path, dir)) return file_path;
seprintf(slash + 1, lastof(file_path), "%s.%s", prefix, exts[i]); file_path = fmt::format("{}{}.{}", base_path, prefix, extension);
if (FioCheckFileExists(file_path, dir)) return file_path; if (FioCheckFileExists(file_path, dir)) return file_path;
} }
return nullptr; return std::nullopt;
} }

View File

@ -14,8 +14,9 @@
#include "strings_func.h" #include "strings_func.h"
#include "textfile_type.h" #include "textfile_type.h"
#include "window_gui.h" #include "window_gui.h"
#include <optional>
const char *GetTextfile(TextfileType type, Subdirectory dir, const char *filename); std::optional<std::string> GetTextfile(TextfileType type, Subdirectory dir, const std::string &filename);
/** Window for displaying a textfile */ /** Window for displaying a textfile */
struct TextfileWindow : public Window, MissingGlyphSearcher { struct TextfileWindow : public Window, MissingGlyphSearcher {
@ -51,7 +52,7 @@ struct TextfileWindow : public Window, MissingGlyphSearcher {
bool Monospace() override; bool Monospace() override;
void SetFontNames(FontCacheSettings *settings, const char *font_name, const void *os_data) override; void SetFontNames(FontCacheSettings *settings, const char *font_name, const void *os_data) override;
virtual void LoadTextfile(const char *textfile, Subdirectory dir); virtual void LoadTextfile(const std::string &textfile, Subdirectory dir);
private: private:
uint ReflowContent(); uint ReflowContent();