| 137 | } // anonymous namespace |
| 138 | |
| 139 | std::vector<char> LoadSequentialFile(std::string uri) { |
| 140 | auto OpenErr = [&uri]() { |
| 141 | std::string msg; |
| 142 | msg = "Opening " + uri + " failed: "; |
| 143 | msg += error::SystemError().message(); |
| 144 | LOG(FATAL) << msg; |
| 145 | }; |
| 146 | |
| 147 | auto parsed = dmlc::io::URI(uri.c_str()); |
| 148 | CHECK((parsed.protocol == "file://" || parsed.protocol.length() == 0)) |
| 149 | << "Only local file is supported."; |
| 150 | // Read from file. |
| 151 | auto path = std::filesystem::weakly_canonical(std::filesystem::u8path(uri)); |
| 152 | std::ifstream ifs(path, std::ios_base::binary | std::ios_base::in); |
| 153 | if (!ifs) { |
| 154 | // https://stackoverflow.com/a/17338934 |
| 155 | OpenErr(); |
| 156 | } |
| 157 | |
| 158 | auto file_size = std::filesystem::file_size(path); |
| 159 | std::vector<char> buffer(file_size); |
| 160 | ifs.read(&buffer[0], file_size); |
| 161 | |
| 162 | return buffer; |
| 163 | } |
| 164 | |
| 165 | std::string FileExtension(std::string fname, bool lower) { |
| 166 | if (lower) { |