-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigFileReader.cpp
157 lines (135 loc) · 2.85 KB
/
ConfigFileReader.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
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
/*
* ConfigFileReader.cpp
*
* Created on: 2013-7-2
* Author: ziteng@mogujie.com
*/
#include "ConfigFileReader.h"
CConfigFileReader::CConfigFileReader(const char* filename)
{
_LoadFile(filename);
}
CConfigFileReader::~CConfigFileReader()
{
}
char* CConfigFileReader::GetConfigName(const char* name)
{
if (!m_load_ok)
return NULL;
char* value = NULL;
map<string, string>::iterator it = m_config_map.find(name);
if (it != m_config_map.end()) {
value = (char*)it->second.c_str();
}
return value;
}
int CConfigFileReader::SetConfigValue(const char* name, const char* value)
{
if(!m_load_ok)
return -1;
map<string, string>::iterator it = m_config_map.find(name);
if(it != m_config_map.end())
{
it->second = value;
}
else
{
m_config_map.insert(make_pair(name, value));
}
return _WriteFIle();
}
void CConfigFileReader::_LoadFile(const char* filename)
{
m_config_file.clear();
m_config_file.append(filename);
FILE* fp = fopen(filename, "r");
if (!fp)
{
log("can not open %s,errno = %d", filename,errno);
return;
}
char buf[256];
for (;;)
{
char* p = fgets(buf, 256, fp);
if (!p)
break;
size_t len = strlen(buf);
if (buf[len - 1] == '\n')
buf[len - 1] = 0; // remove \n at the end
char* ch = strchr(buf, '#'); // remove string start with #
if (ch)
*ch = 0;
if (strlen(buf) == 0)
continue;
_ParseLine(buf);
}
fclose(fp);
m_load_ok = true;
}
int CConfigFileReader::_WriteFIle(const char* filename)
{
FILE* fp = NULL;
if(filename == NULL)
{
fp = fopen(m_config_file.c_str(), "w");
}
else
{
fp = fopen(filename, "w");
}
if(fp == NULL)
{
return -1;
}
char szPaire[128];
map<string, string>::iterator it = m_config_map.begin();
for (; it != m_config_map.end(); it++)
{
memset(szPaire, 0, sizeof(szPaire));
snprintf(szPaire, sizeof(szPaire), "%s=%s\n", it->first.c_str(), it->second.c_str());
uint32_t ret = fwrite(szPaire, strlen(szPaire),1,fp);
if(ret != 1)
{
fclose(fp);
return -1;
}
}
fclose(fp);
return 0;
}
void CConfigFileReader::_ParseLine(char* line)
{
char* p = strchr(line, '=');
if (p == NULL)
return;
*p = 0;
char* key = _TrimSpace(line);
char* value = _TrimSpace(p + 1);
if (key && value)
{
m_config_map.insert(make_pair(key, value));
}
}
char* CConfigFileReader::_TrimSpace(char* name)
{
// remove starting space or tab
char* start_pos = name;
while ( (*start_pos == ' ') || (*start_pos == '\t') )
{
start_pos++;
}
if (strlen(start_pos) == 0)
return NULL;
// remove ending space or tab
char* end_pos = name + strlen(name) - 1;
while ( (*end_pos == ' ') || (*end_pos == '\t') )
{
*end_pos = 0;
end_pos--;
}
int len = (int)(end_pos - start_pos) + 1;
if (len <= 0)
return NULL;
return start_pos;
}