blob: 7aa0e654cf5a6110f7d8254a381a18c812759f12 [file] [log] [blame]
Patrick Venture64919ec2018-11-15 11:52:07 -08001#include "file_handler.hpp"
2
3#include <cstdint>
4#include <memory>
5#include <string>
6#include <vector>
7
8namespace blobs
9{
10
11bool FileHandler::open(const std::string& path)
12{
13 this->path = path;
14
15 if (file.is_open())
16 {
17 /* This wasn't properly closed somehow.
18 * TODO: Throw an error or just reset the state?
19 */
20 return false;
21 }
22
23 /* using ofstream no need to set out */
24 file.open(filename, std::ios::binary);
25 if (file.bad())
26 {
27 /* TODO: Oh no! Care about this. */
28 return false;
29 }
30
31 /* We were able to open the file for staging.
32 * TODO: We'll need to do other stuff to eventually.
33 */
34 return true;
35}
36
37void FileHandler::close()
38{
39 if (file.is_open())
40 {
41 file.close();
42 }
43 return;
44}
45
46bool FileHandler::write(std::uint32_t offset,
47 const std::vector<std::uint8_t>& data)
48{
49 if (!file.is_open())
50 {
51 return false;
52 }
53
54 /* We could track this, but if they write in a scattered method, this is
55 * easier.
56 */
57 file.seekp(offset, std::ios_base::beg);
58 if (!file.good())
59 {
60 /* the documentation wasn't super clear on fail vs bad in these cases,
61 * so let's only be happy with goodness.
62 */
63 return false;
64 }
65
66 file.write(reinterpret_cast<const char*>(data.data()), data.size());
67 if (!file.good())
68 {
69 return false;
70 }
71
72 return true;
73}
74
75} // namespace blobs