-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathINIReaderBin.h
96 lines (89 loc) · 2.98 KB
/
INIReaderBin.h
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
#pragma once
#include "filefunc.h"
#include "INIReader.h"
struct INIReaderBin
{
private:
INIReaderNormal ini_;
const char* head_ = "CFG_BIN INI";
const int head_size_ = 32;
// The head is "CFG_BIN INI", 32 bytes is kept
// The next 8 bytes is the length of the txt information
// Next is the txt information. One value has two integers which are the differ from the beginning of content and the length
// Next is the binary content
// The beginning of the binary is 40+length of txt
public:
int parse(const std::string& str)
{
if (str.size() > head_size_ + sizeof(uint64_t) && str.substr(0, 11) == head_)
{
uint64_t size_ini = 0;
if (str.size() >= sizeof(uint64_t))
{
size_ini = *(uint64_t*)(str.data() + head_size_);
}
uint64_t begin = head_size_ + sizeof(uint64_t) + size_ini;
INIReaderNormal assist;
if (str.size() < begin)
{
return -1;
}
assist.loadString(str.substr(head_size_ + sizeof(uint64_t), size_ini));
for (auto& section : assist.getAllSections())
{
for (auto& key : assist.getAllKeys(section))
{
auto vec = assist.getVector<int>(section, key);
if (vec.size() == 2)
{
if (begin + vec[0] + vec[1] > str.size())
{
return -1;
}
ini_.setKey(section, key, str.substr(begin + vec[0], vec[1]));
}
}
}
return 0;
}
return -1;
}
// The result may be not a text string.
std::string to_string()
{
std::string str_content;
INIReaderNormal assist;
for (auto& section : ini_.getAllSections())
{
for (auto& key : ini_.getAllKeys(section))
{
std::string str1 = ini_.getString(section, key);
assist.setKey(section, key, std::to_string(str_content.size()) + "," + std::to_string(str1.size()));
str_content += str1;
}
}
auto str_ini = assist.toString();
uint64_t l = str_ini.size();
std::string result = head_;
result.resize(head_size_ + sizeof(uint64_t));
memcpy(&result[head_size_], &l, sizeof(uint64_t));
result = result + str_ini + str_content;
return result;
}
int save(const std::string& filename)
{
return filefunc::writeStringToFile(to_string(), filename);
}
int load(const std::string& filename)
{
return parse(filefunc::readFileToString(filename));
}
std::string get_value(const std::string& key)
{
return ini_.getString("", key);
}
void set_value(const std::string& key, const std::string& value)
{
ini_.setKey("", key, value);
}
};