Merge branch 'master' into jgrpp

# Conflicts:
#	src/company_cmd.cpp
#	src/core/overflowsafe_type.hpp
#	src/economy.cpp
#	src/engine_base.h
#	src/ground_vehicle.cpp
#	src/group_gui.cpp
#	src/industry_cmd.cpp
#	src/industry_gui.cpp
#	src/newgrf_commons.cpp
#	src/newgrf_engine.cpp
#	src/newgrf_industries.cpp
#	src/newgrf_object.cpp
#	src/newgrf_roadstop.cpp
#	src/newgrf_station.cpp
#	src/rail_gui.cpp
#	src/road_cmd.h
#	src/road_gui.cpp
#	src/saveload/afterload.cpp
#	src/script/api/script_log.cpp
#	src/script/api/script_log.hpp
#	src/settings_gui.cpp
#	src/settingsgen/settingsgen.cpp
#	src/station_cmd.cpp
#	src/station_cmd.h
#	src/station_gui.cpp
#	src/strgen/strgen.cpp
#	src/string_func.h
#	src/string_type.h
#	src/table/settings/network_private_settings.ini
#	src/tests/math_func.cpp
#	src/textfile_gui.cpp
#	src/timetable_gui.cpp
#	src/town_cmd.cpp
#	src/vehicle.cpp
#	src/waypoint_cmd.cpp
#	src/waypoint_cmd.h
#	src/widgets/dropdown.cpp
This commit is contained in:
Jonathan G Rennison
2023-06-03 19:16:42 +01:00
101 changed files with 987 additions and 964 deletions

View File

@@ -70,11 +70,6 @@ TextfileWindow::TextfileWindow(TextfileType file_type) : Window(&_textfile_desc)
this->hscroll->SetStepSize(10); // Speed up horizontal scrollbar
}
/* virtual */ TextfileWindow::~TextfileWindow()
{
free(this->text);
}
/**
* Get the total height of the content displayed in this window, if wrapping is disabled.
* @return the height in pixels
@@ -124,11 +119,11 @@ void TextfileWindow::SetupScrollbars(bool force_reflow)
if (IsWidgetLowered(WID_TF_WRAPTEXT)) {
/* Reflow is mandatory if text wrapping is on */
uint height = this->ReflowContent();
this->vscroll->SetCount(std::min<uint>(UINT16_MAX, height));
this->vscroll->SetCount(ClampTo<uint16_t>(height));
this->hscroll->SetCount(0);
} else {
uint height = force_reflow ? this->ReflowContent() : this->GetContentHeight();
this->vscroll->SetCount(std::min<uint>(UINT16_MAX, height));
this->vscroll->SetCount(ClampTo<uint16_t>(height));
this->hscroll->SetCount(this->max_length + WidgetDimensions::scaled.frametext.Horizontal());
}
@@ -199,9 +194,9 @@ void TextfileWindow::SetupScrollbars(bool force_reflow)
return FS_MONO;
}
/* virtual */ const char *TextfileWindow::NextString()
/* virtual */ std::optional<std::string_view> TextfileWindow::NextString()
{
if (this->search_iterator >= this->lines.size()) return nullptr;
if (this->search_iterator >= this->lines.size()) return std::nullopt;
return this->lines[this->search_iterator++].text;
}
@@ -344,11 +339,14 @@ static void Xunzip(byte **bufp, size_t *sizep)
FILE *handle = FioFOpenFile(textfile, "rb", dir, &filesize);
if (handle == nullptr) return;
this->text = ReallocT(this->text, filesize);
size_t read = fread(this->text, 1, filesize, handle);
char *buf = MallocT<char>(filesize);
size_t read = fread(buf, 1, filesize, handle);
fclose(handle);
if (read != filesize) return;
if (read != filesize) {
free(buf);
return;
}
#if defined(WITH_ZLIB) || defined(WITH_LIBLZMA)
const char *suffix = strrchr(textfile, '.');
@@ -357,40 +355,37 @@ static void Xunzip(byte **bufp, size_t *sizep)
#if defined(WITH_ZLIB)
/* In-place gunzip */
if (strcmp(suffix, ".gz") == 0) Gunzip((byte**)&this->text, &filesize);
if (StrEndsWith(textfile, ".gz")) Gunzip((byte**)&buf, &filesize);
#endif
#if defined(WITH_LIBLZMA)
/* In-place xunzip */
if (strcmp(suffix, ".xz") == 0) Xunzip((byte**)&this->text, &filesize);
if (StrEndsWith(textfile, ".xz")) Xunzip((byte**)&buf, &filesize);
#endif
if (!this->text) return;
if (buf == nullptr) return;
/* Add space for trailing \0 */
this->text = ReallocT(this->text, filesize + 1);
this->text[filesize] = '\0';
/* Replace tabs and line feeds with a space since StrMakeValidInPlace removes those. */
for (char *p = this->text; *p != '\0'; p++) {
if (*p == '\t' || *p == '\r') *p = ' ';
}
std::string_view sv_buf(buf, filesize);
/* Check for the byte-order-mark, and skip it if needed. */
char *p = this->text + (strncmp(u8"\ufeff", this->text, 3) == 0 ? 3 : 0);
if (StrStartsWith(sv_buf, u8"\ufeff")) sv_buf.remove_prefix(3);
/* Make sure the string is a valid UTF-8 sequence. */
StrMakeValidInPlace(p, this->text + filesize, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
/* Replace any invalid characters with a question-mark. This copies the buf in the process. */
this->text = StrMakeValid(sv_buf, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE | SVS_REPLACE_TAB_CR_NL_WITH_SPACE);
free(buf);
/* Split the string on newlines. */
std::string_view p(this->text);
int row = 0;
this->lines.emplace_back(row, p);
for (; *p != '\0'; p++) {
if (*p == '\n') {
*p = '\0';
this->lines.emplace_back(++row, p + 1);
}
auto next = p.find_first_of('\n');
while (next != std::string_view::npos) {
this->lines.emplace_back(row, p.substr(0, next));
p.remove_prefix(next + 1);
row++;
next = p.find_first_of('\n');
}
this->lines.emplace_back(row, p);
/* Calculate maximum text line length. */
uint max_length = 0;