forked from oceanbase/miniob
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathini.cpp
More file actions
230 lines (173 loc) · 5.64 KB
/
Copy pathini.cpp
File metadata and controls
230 lines (173 loc) · 5.64 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */
//
// Created by Longda on 2010
//
#include <errno.h>
#include <string.h>
#include <fstream>
#include "common/conf/ini.h"
#include "common/defs.h"
#include "common/lang/string.h"
#include "common/log/log.h"
namespace common {
const std::string Ini::DEFAULT_SECTION = std::string("");
const std::map<std::string, std::string> Ini::empty_map_;
Ini::Ini()
{}
Ini::~Ini()
{}
void Ini::insert_session(const std::string &session_name)
{
std::map<std::string, std::string> session_map;
std::pair<std::string, std::map<std::string, std::string>> entry =
std::pair<std::string, std::map<std::string, std::string>>(session_name, session_map);
sections_.insert(entry);
}
std::map<std::string, std::string> *Ini::switch_session(const std::string &session_name)
{
SessionsMap::iterator it = sections_.find(session_name);
if (it != sections_.end()) {
return &it->second;
}
insert_session(session_name);
it = sections_.find(session_name);
if (it != sections_.end()) {
return &it->second;
}
// should never go this
return nullptr;
}
const std::map<std::string, std::string> &Ini::get(const std::string §ion)
{
SessionsMap::iterator it = sections_.find(section);
if (it == sections_.end()) {
return empty_map_;
}
return it->second;
}
std::string Ini::get(const std::string &key, const std::string &defaultValue, const std::string §ion)
{
std::map<std::string, std::string> section_map = get(section);
std::map<std::string, std::string>::iterator it = section_map.find(key);
if (it == section_map.end()) {
return defaultValue;
}
return it->second;
}
int Ini::put(const std::string &key, const std::string &value, const std::string §ion)
{
std::map<std::string, std::string> *section_map = switch_session(section);
section_map->insert(std::pair<std::string, std::string>(key, value));
return 0;
}
int Ini::insert_entry(std::map<std::string, std::string> *session_map, const std::string &line)
{
if (session_map == nullptr) {
std::cerr << __FILE__ << __FUNCTION__ << " session map is null" << std::endl;
return -1;
}
size_t equal_pos = line.find_first_of('=');
if (equal_pos == std::string::npos) {
std::cerr << __FILE__ << __FUNCTION__ << "Invalid configuration line " << line << std::endl;
return -1;
}
std::string key = line.substr(0, equal_pos);
std::string value = line.substr(equal_pos + 1);
strip(key);
strip(value);
session_map->insert(std::pair<std::string, std::string>(key, value));
return 0;
}
int Ini::load(const std::string &file_name)
{
std::ifstream ifs;
try {
bool continue_last_line = false;
std::map<std::string, std::string> *current_session = switch_session(DEFAULT_SECTION);
char line[MAX_CFG_LINE_LEN];
std::string line_entry;
ifs.open(file_name.c_str());
while (ifs.good()) {
memset(line, 0, sizeof(line));
ifs.getline(line, sizeof(line));
char *read_buf = strip(line);
if (strlen(read_buf) == 0) {
// empty line
continue;
}
if (read_buf[0] == CFG_COMMENT_TAG) {
// comments line
continue;
}
if (read_buf[0] == CFG_SESSION_START_TAG && read_buf[strlen(read_buf) - 1] == CFG_SESSION_END_TAG) {
read_buf[strlen(read_buf) - 1] = '\0';
std::string session_name = std::string(read_buf + 1);
current_session = switch_session(session_name);
continue;
}
if (continue_last_line == false) {
// don't need continue last line
line_entry = read_buf;
} else {
line_entry += read_buf;
}
if (read_buf[strlen(read_buf) - 1] == CFG_CONTINUE_TAG) {
// this line isn't finished, need continue
continue_last_line = true;
// remove the last character
line_entry = line_entry.substr(0, line_entry.size() - 1);
continue;
} else {
continue_last_line = false;
insert_entry(current_session, line_entry);
}
}
ifs.close();
file_names_.insert(file_name);
std::cout << "Successfully load " << file_name << std::endl;
} catch (...) {
if (ifs.is_open()) {
ifs.close();
}
std::cerr << "Failed to load " << file_name << SYS_OUTPUT_ERROR << std::endl;
return -1;
}
return 0;
}
void Ini::to_string(std::string &output_str)
{
output_str.clear();
output_str += "Begin dump configuration\n";
for (SessionsMap::iterator it = sections_.begin(); it != sections_.end(); it++) {
output_str += CFG_SESSION_START_TAG;
output_str += it->first;
output_str += CFG_SESSION_END_TAG;
output_str += "\n";
std::map<std::string, std::string> §ion_map = it->second;
for (std::map<std::string, std::string>::iterator sub_it = section_map.begin(); sub_it != section_map.end();
sub_it++) {
output_str += sub_it->first;
output_str += "=";
output_str += sub_it->second;
output_str += "\n";
}
output_str += "\n";
}
output_str += "Finish dump configuration \n";
return;
}
//! Accessor function which wraps global properties object
Ini *&get_properties()
{
static Ini *properties = new Ini();
return properties;
}
} // namespace common