MCPcopy Create free account
hub / github.com/bgrimstad/splinter / loadFromFile

Method loadFromFile

src/serializer.cpp:41–72  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

39}
40
41void Serializer::loadFromFile(const std::string &fileName)
42{
43 // Open the file in binary mode at the end
44 std::ifstream ifs(fileName, std::ios::binary | std::ios::ate);
45
46 if (!ifs.is_open()) {
47 std::string error_message("Serializer::loadFromFile: Unable to open file \"");
48 error_message.append(fileName);
49 error_message.append("\" for deserializing.");
50 throw Exception(error_message);
51 }
52
53 // Because we opened the file at the end, tellg() will give us the size of the file
54 std::ifstream::pos_type pos = ifs.tellg();
55
56 std::vector<char> result(pos);
57
58 ifs.seekg(0, std::ios::beg);
59
60 // http://www.cplusplus.com/reference/vector/vector/data/
61 // Elements of the vector are guaranteed to be stored in a contiguous array,
62 // *result.data() can therefore be treated as an array of the same type as the vector
63 ifs.read(result.data(), pos);
64 //assert(ifs);
65
66 stream.clear();
67 // Convert from char to uint_8 vector
68 for (const char& byte : result)
69 stream.push_back((uint8_t)byte);
70
71 read = stream.cbegin();
72}
73
74/*
75 * get_size implementations

Callers

nothing calls this directly

Calls 2

ExceptionClass · 0.85
cbeginMethod · 0.80

Tested by

no test coverage detected