FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TTServer/cpp/src/base/ConfigFileReader.cpp at master · codeindeep/TTServer · GitHub
codeindeep
TTServer
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
TTServer
/
cpp
/
src
/
base
/
ConfigFileReader.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
112 lines (90 loc) · 1.81 KB
Breadcrumbs
TTServer
/
cpp
/
src
/
base
/
ConfigFileReader.cpp
Copy path
File metadata and controls
112 lines (90 loc) · 1.81 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* ConfigFileReader.cpp
*
* Created on: 2013-7-2
* Author: ziteng@mogujie.com
*/
#
include
"
ConfigFileReader.h
"
CConfigFileReader::CConfigFileReader
(
const
char
* filename)
{
m_config_map =
new
map<string, string>;
_LoadFile
(filename);
}
CConfigFileReader::~CConfigFileReader
()
{
delete
m_config_map;
}
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;
}
void
CConfigFileReader::_LoadFile
(
const
char
* filename)
{
FILE
* fp =
fopen
(filename,
"
r
"
);
if
(!fp)
{
log
(
"
can not open %s
\n
"
, filename);
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
;
}
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;
}
Back
|
FazBrowse Home
|
New Git URL