MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / LoadTextfile

Method LoadTextfile

src/textfile_gui.cpp:851–898  ·  view source on GitHub ↗

* Loads the textfile text from file and setup #lines. */ virtual */

Source from the content-addressed store, hash-verified

849 * Loads the textfile text from file and setup #lines.
850 */
851/* virtual */ void TextfileWindow::LoadTextfile(const std::string &textfile, Subdirectory dir)
852{
853 this->lines.clear();
854 this->jumplist.clear();
855
856 if (this->GetWidget<NWidgetStacked>(WID_TF_SEL_JUMPLIST)->SetDisplayedPlane(SZSP_HORIZONTAL)) this->ReInit();
857
858 if (textfile.empty()) return;
859
860 /* Get text from file */
861 size_t filesize;
862 auto handle = FioFOpenFile(textfile, "rb", dir, &filesize);
863 if (!handle.has_value()) return;
864 /* Early return on empty files. */
865 if (filesize == 0) return;
866
867 std::vector<char> buf;
868 buf.resize(filesize);
869 size_t read = fread(buf.data(), 1, buf.size(), *handle);
870
871 if (read != buf.size()) return;
872
873#if defined(WITH_ZLIB)
874 /* In-place gunzip */
875 if (textfile.ends_with(".gz")) buf = Gunzip(buf);
876#endif
877
878#if defined(WITH_LIBLZMA)
879 /* In-place xunzip */
880 if (textfile.ends_with(".xz")) buf = Xunzip(buf);
881#endif
882
883 if (buf.empty()) return;
884
885 std::string_view sv_buf(buf.data(), buf.size());
886
887 /* Check for the byte-order-mark, and skip it if needed. */
888 if (sv_buf.starts_with("\ufeff")) sv_buf.remove_prefix(3);
889
890 /* Update the filename. */
891 this->filepath = textfile;
892 this->filename = this->filepath.substr(this->filepath.find_last_of(PATHSEP) + 1);
893 /* If it's the first file being loaded, add to history. */
894 if (this->history.empty()) this->history.emplace_back(this->filepath, 0);
895
896 /* Process the loaded text into lines, and do any further parsing needed. */
897 this->LoadText(sv_buf);
898}
899
900/**
901 * Load a text into the textfile viewer.

Callers 7

NavigateHistoryMethod · 0.95
NavigateToFileMethod · 0.95
NewGRFTextfileWindowMethod · 0.80
BaseSetTextfileWindowMethod · 0.80
OnInvalidateDataMethod · 0.80
ContentTextfileWindowMethod · 0.80

Calls 12

LoadTextMethod · 0.95
FioFOpenFileFunction · 0.85
GunzipFunction · 0.85
XunzipFunction · 0.85
SetDisplayedPlaneMethod · 0.80
ReInitMethod · 0.80
resizeMethod · 0.80
substrMethod · 0.80
clearMethod · 0.45
emptyMethod · 0.45
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected