* Loads the textfile text from file and setup #lines. */ virtual */
| 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. |
no test coverage detected