| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | #include <util/file.hh> |
| 25 | |
| 26 | #include <iostream> |
| 27 | #include <fstream> |
| 28 | |
| 29 | vector<u8> read_file(const string &filename, bool binary) { |
| 30 | std::ios::openmode flags; |
| 31 | if (binary) { |
| 32 | flags = std::ios::binary | std::ios::ate; |
| 33 | } else { |
| 34 | flags = std::ios::in; |
| 35 | } |
| 36 | |
| 37 | std::ifstream file(filename, flags); |
| 38 | if (!file) { |
| 39 | throw std::runtime_error("ファイルを開くに失敗: " + filename); |
| 40 | } |
| 41 | |
| 42 | const auto size = file.tellg(); |
| 43 | file.seekg(0); |
| 44 | vector<u8> data(static_cast<size_t>(size)); |
| 45 | if (!file.read(reinterpret_cast<char *>(data.data()), size)) { |
| 46 | throw std::runtime_error("ファイルの読み込みに失敗。"); |
| 47 | } |
| 48 | |
| 49 | file.close(); |
| 50 | |
| 51 | return data; |
| 52 | } |
| 53 | |
| 54 | bool write_file(const string &filename, vector<string> contents, bool binary) { |
| 55 | std::ios::openmode flags; |
| 56 | if (binary) { |
| 57 | flags = std::ios::binary | std::ios::ate; |
| 58 | } else { |
| 59 | flags = std::ios::out | std::ios::trunc; |
| 60 | } |
| 61 | |
| 62 | std::ofstream file(filename, flags); |
| 63 | if (!file) { |
| 64 | throw std::runtime_error("ファイルを開くに失敗: " + filename); |
| 65 | } |
| 66 | |
| 67 | for (const auto &c : contents) { |
| 68 | file << c; |
| 69 | file << "\n"; |
| 70 | } |
| 71 | |
| 72 | file.close(); |
| 73 | |
| 74 | return true; |
| 75 | } |