Load data from file path. See "include/loader/loader.h".
| 21 | |
| 22 | // Load data from file path. See "include/loader/loader.h". |
| 23 | Expect<std::vector<Byte>> |
| 24 | Loader::loadFile(const std::filesystem::path &FilePath) { |
| 25 | std::error_code EC; |
| 26 | size_t FileSize = std::filesystem::file_size(FilePath, EC); |
| 27 | if (EC) { |
| 28 | spdlog::error(ErrCode::Value::IllegalPath); |
| 29 | spdlog::error(ErrInfo::InfoFile(FilePath)); |
| 30 | return Unexpect(ErrCode::Value::IllegalPath); |
| 31 | } |
| 32 | |
| 33 | std::ifstream Fin(FilePath, std::ios::in | std::ios::binary); |
| 34 | if (!Fin) { |
| 35 | spdlog::error(ErrCode::Value::IllegalPath); |
| 36 | spdlog::error(ErrInfo::InfoFile(FilePath)); |
| 37 | return Unexpect(ErrCode::Value::IllegalPath); |
| 38 | } |
| 39 | |
| 40 | std::vector<Byte> Buf(FileSize); |
| 41 | size_t Index = 0; |
| 42 | while (FileSize > 0) { |
| 43 | const uint32_t BlockSize = static_cast<uint32_t>( |
| 44 | std::min<size_t>(FileSize, std::numeric_limits<uint32_t>::max())); |
| 45 | Fin.read(reinterpret_cast<char *>(Buf.data()) + Index, BlockSize); |
| 46 | const uint32_t ReadCount = static_cast<uint32_t>(Fin.gcount()); |
| 47 | if (ReadCount != BlockSize) { |
| 48 | if (Fin.eof()) { |
| 49 | spdlog::error(ErrCode::Value::UnexpectedEnd); |
| 50 | spdlog::error(ErrInfo::InfoLoading(ReadCount)); |
| 51 | spdlog::error(ErrInfo::InfoFile(FilePath)); |
| 52 | return Unexpect(ErrCode::Value::UnexpectedEnd); |
| 53 | } else { |
| 54 | spdlog::error(ErrCode::Value::ReadError); |
| 55 | spdlog::error(ErrInfo::InfoLoading(ReadCount)); |
| 56 | spdlog::error(ErrInfo::InfoFile(FilePath)); |
| 57 | return Unexpect(ErrCode::Value::ReadError); |
| 58 | } |
| 59 | } |
| 60 | Index += static_cast<size_t>(BlockSize); |
| 61 | FileSize -= static_cast<size_t>(BlockSize); |
| 62 | } |
| 63 | return Buf; |
| 64 | } |
| 65 | |
| 66 | // Parse module or component from file path. See "include/loader/loader.h". |
| 67 | Expect<std::variant<std::unique_ptr<AST::Component::Component>, |