MCPcopy Create free account
hub / github.com/WasmEdge/WasmEdge / loadFile

Method loadFile

lib/loader/loader.cpp:23–64  ·  view source on GitHub ↗

Load data from file path. See "include/loader/loader.h".

Source from the content-addressed store, hash-verified

21
22// Load data from file path. See "include/loader/loader.h".
23Expect<std::vector<Byte>>
24Loader::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".
67Expect<std::variant<std::unique_ptr<AST::Component::Component>,

Callers

nothing calls this directly

Calls 7

InfoFileClass · 0.85
UnexpectFunction · 0.85
InfoLoadingClass · 0.85
eofMethod · 0.80
errorFunction · 0.50
readMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected