-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabase.h
More file actions
187 lines (159 loc) · 3.47 KB
/
Copy pathDatabase.h
File metadata and controls
187 lines (159 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#ifndef database_h_included
#define database_h_included
#ifndef rodb_h_included
#error "Please include rodb.h, don't include Database.h directly."
#endif
#include <fstream>
#include <vector>
namespace rodb
{
class Database
{
public:
// Doesn't throw, returns NULL on error
static Database *load(char const *filename)
{
try
{
return new Database(filename);
}
catch (std::bad_alloc const &)
{
return 0;
}
catch (std::runtime_error const &)
{
return 0;
}
}
Database(char const *filename)
{
std::ifstream in(filename, std::ios::binary);
if (in.fail())
throw std::runtime_error("Cannot open input file");
// Get file length
in.seekg(0, std::ios::end);
std::streampos size = in.tellg();
in.seekg(0, std::ios::beg);
// Read the entire file into memory
data_.resize(size);
in.read(&data_[0], size);
check_integriry();
}
Value operator [](size_t index)
{
return root()[index];
}
Value operator [](int index)
{
return root()[index];
}
Value operator [](char const *key)
{
return root()[key];
}
void dump(std::ostream &stream = std::cout) const
{
stream << *this;
}
void dump_yaml(std::ostream &stream) const
{
dump_yaml(stream, root(), 0);
}
Value root() const
{
return Value(header_ptr() + 1);
}
private:
struct Header
{
enum
{
SIGNATURE = 0x62646f72, // Should read 'rodb' when saved in little endian
VERSION = 1,
};
uint32_t signature_;
uint32_t version_;
};
Header const &header() const
{
return *header_ptr();
}
Header const *header_ptr() const
{
return reinterpret_cast<Header const *>(&data_[0]);
}
void check_integriry() const
{
if (header().signature_ != Header::SIGNATURE || header().version_ != Header::VERSION)
throw std::runtime_error("Database integrity check failed");
}
void dump_yaml(std::ostream &stream, Value const &value, size_t idndet) const
{
switch (value.type())
{
case Value::BOOL:
stream << ((bool)value ? "true" : "false");
break;
case Value::INT:
stream << (int)value;
break;
case Value::FLOAT:
stream << (float)value;
break;
case Value::STRING:
stream << "\"" << (char const *)value << "\"";
break;
case Value::ARRAY:
{
stream << "\n";
std::string padding(idndet, ' ');
for (size_t i = 0; i < value.size(); ++i)
{
stream << padding << "- ";
dump_yaml(stream, value[i], idndet + 4);
stream << "\n";
}
break;
}
case Value::MAP:
{
stream << "\n";
std::string padding(idndet, ' ');
for (size_t i = 0; i < value.size(); ++i)
{
stream << padding;
dump_yaml(stream, value.keys()[i], idndet);
stream << ": ";
dump_yaml(stream, value.values()[i], idndet + 4);
stream << "\n";
}
break;
}
}
}
std::vector<char> data_;
// Beyond private ;)
private:
Database(Database const &)
{
throw std::logic_error("Cannot copy construct Database");
}
Database &operator =(Database const &)
{
throw std::logic_error("Cannot assign Database");
}
// BFF
friend std::ostream &operator <<(std::ostream &stream, Database const&db);
};
inline std::ostream &operator <<(std::ostream &stream, Database const&db)
{
return stream
<< " Total size: " << db.data_.size() << "\n"
<< "Header size: " << sizeof(db.header()) << "\n"
<< " Data size: " << db.data_.size() - sizeof(db.header()) << "\n"
<< " Signature: " << "0x" << std::hex << db.header().signature_ << std::dec << "\n"
<< " Version: " << db.header().version_ << "\n";
}
}
#endif