config配置表读取
2019-05-12 10:43:00 # cpp

server.cfg

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
# log_level 0.trace 1.debug 2.info 3.warn 4.error 5.critical 6.off
logPattern = %C-%m-%d %T %t %^%l%$ %v
logFileLevel = 0
logConsoleLevel = 0
logFlushSec = 0

# server common
serverIdx = 24

# lua script dir
luaScriptDir = script

# gate server
port = 18888
userInitCount = 1
userMaxCount = 1000
networkThreadCount = 2

# db service
dbServiceThreadCount = 4

# game db
dbGameIp = 127.0.0.1
dbGamePort = 3306
dbGameUser = root
dbGamePasswd = 123456
dbGameName = test
dbGameCharset = utf8
dbGameConnCount = 4

config.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
#pragma once

#include <fstream>
#include <iostream>
#include <map>
#include <string_view>

#include "comm_func.h"
#include "logger.h"
#include "singleton.h"

namespace Sky {
class Config final : public Singleton<Config> {
friend class Singleton<Config>;

private:
Config() = default;

public:
~Config() = default;

bool start(std::string_view fileName);

template <typename T>
T get(std::string_view key) {
auto itr = m_config_map.find(key.data());
if (itr == m_config_map.end()) {
LOG_ERROR("Config::get not found value key:{}", key);
return {};
}

if constexpr (std::is_integral_v<T>) {
return CommFunc::stringTo<T>(itr->second);
} else if constexpr (std::is_same_v<T, bool>) {
return itr->second == "true";
} else if constexpr (std::is_same_v<T, float>) {
return CommFunc::stringTo<float>(itr->second);
} else if constexpr (std::is_same_v<T, double>) {
return CommFunc::stringTo<double>(itr->second);
} else if constexpr (std::is_same_v<T, std::time_t>) {
return CommFunc::stringTo<std::time_t>(itr->second);
} else if constexpr (std::is_same_v<T, std::string> || std::is_same_v<T, std::string_view> ||
std::is_same_v<T, const char *>) {
return itr->second;
} else {
LOG_ERROR("Config::get not found type key:{}", key);
}

return {};
}

private:
std::map<std::string, std::string> m_config_map{};
};
} // namespace Sky

config.cpp

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
#include "config.h"

#include "define.h"

namespace Sky {
bool Config::start(std::string_view fileName) {
std::string path = CommFunc::getCurrentExeDir();
path.append(SLASH_STR).append(fileName.data()).append(".cfg");

std::ifstream file_handle(path);
if (file_handle.fail()) {
LOG_ERROR("Config::start cfg open failed:{}", path);
return false;
}

std::string key, value, data;
std::string::size_type pos;
while (getline(file_handle, data)) {
pos = data.find('#');
if (pos != std::string::npos) {
continue;
}

pos = data.find('=');
if (pos == std::string::npos) {
continue;
}

key = data.substr(0, pos);
value = data.substr(pos + 1);
CommFunc::trim(key);
CommFunc::trim(value);
m_config_map.insert({key, value});
}

file_handle.close();
return true;
}
} // namespace Sky