1919#ifndef CONFIG_CPP
2020#define CONFIG_CPP
2121
22-
23- #include < fstream>
24- #include < sstream>
25- #include < algorithm>
26- #include < cstdio>
27-
2822#include " ./config.h"
2923#include " ../exception/io_exception.h"
3024#include " ../exception/illegal_argument_exception.h"
@@ -40,15 +34,15 @@ std::mutex Config::mtx_lock;
4034/* *
4135 * addConfig adds a new section->key:value to the configuration.
4236 */
43- bool Config :: AddConfig(std::string section, std::string option, std::string value) {
37+ bool Config:: AddConfig (std::string section, const std::string& option, const std::string& value) {
4438 if (!section.compare (" " ))
4539 section = DEFAULT_SECTION;
4640 bool ok = data[section].find (option) != data[section].end ();
4741 data[section][option] = value;
4842 return !ok;
4943}
5044
51- void Config :: Parse(std::string f_name) {
45+ void Config:: Parse (const std::string& f_name) {
5246 mtx_lock.lock ();
5347 std::ifstream infile;
5448 try {
@@ -62,13 +56,13 @@ void Config :: Parse(std::string f_name) {
6256 infile.close ();
6357}
6458
65- void Config ::ParseBuffer (std::istream * buf) {
59+ void Config::ParseBuffer (std::istream * buf) {
6660 std::string section = " " ;
6761 int line_num = 0 ;
6862 std::string line;
6963 while (true ) {
7064 line_num++;
71- if (getline (*buf, line, ' \n ' )){
65+ if (getline (*buf, line, ' \n ' )) {
7266 if (!line.compare (" " ))
7367 continue ;
7468 }
@@ -79,10 +73,10 @@ void Config ::ParseBuffer(std::istream * buf) {
7973 continue ;
8074 else if (line.find (DEFAULT_COMMENT_SEM)==0 )
8175 continue ;
82- else if (line.find (" [" )== 0 && EndsWith (line, std::string ( " ]" ) ))
76+ else if (line.find (" [" ) == 0 && EndsWith (line, " ]" ))
8377 section = line.substr (1 , line.length () - 2 );
8478 else {
85- std::vector<std::string> option_val = Split (line, std::string ( " =" ) , 2 );
79+ std::vector<std::string> option_val = Split (line, " =" , 2 );
8680 if (option_val.size () != 2 ) {
8781 char * error = new char ;
8882 sprintf (error, " parse the content error : line %d , %s = ? " , line_num, option_val[0 ].c_str ());
@@ -101,8 +95,8 @@ void Config ::ParseBuffer(std::istream * buf) {
10195 * @param confName the path of the model file.
10296 * @return the constructor of Config.
10397 */
104- std::shared_ptr<Config> Config :: NewConfig(std::string conf_name) {
105- std::shared_ptr<Config> c ( new Config);
98+ std::shared_ptr<Config> Config:: NewConfig (const std::string& conf_name) {
99+ std::shared_ptr<Config> c = std::make_shared< Config>( );
106100 c->Parse (conf_name);
107101 return c;
108102}
@@ -113,39 +107,39 @@ std::shared_ptr<Config> Config :: NewConfig(std::string conf_name) {
113107 * @param text the model text.
114108 * @return the constructor of Config.
115109 */
116- std::shared_ptr<Config> Config :: NewConfigFromText(std::string text) {
117- std::shared_ptr<Config> c ( new Config);
110+ std::shared_ptr<Config> Config:: NewConfigFromText (const std::string& text) {
111+ std::shared_ptr<Config> c = std::make_shared< Config>( );
118112 std::stringstream stream (text);
119113 c->ParseBuffer (&stream);
120114 return c;
121115}
122116
123- bool Config :: GetBool(std::string key) {
117+ bool Config:: GetBool (std::string_view key) {
124118 return Get (key).compare (" true" )==0 ;
125119}
126120
127- int Config :: GetInt(std::string key) {
121+ int Config:: GetInt (std::string_view key) {
128122 return atoi (Get (key).c_str ());
129123}
130124
131- float Config :: GetFloat(std::string key) {
125+ float Config:: GetFloat (std::string_view key) {
132126 return float (atof (Get (key).c_str ()));
133127}
134128
135- std::string Config :: GetString(std::string key) {
129+ std::string Config:: GetString (std::string_view key) {
136130 return Get (key);
137131}
138132
139- std::vector<std::string> Config :: GetStrings(std::string key) {
133+ std::vector<std::string> Config:: GetStrings (std::string_view key) {
140134 std::string v = Get (key);
141- if (!v. compare ( " " )) {
142- std::vector<std::string> empty;
143- return empty ;
144- }
145- return Split (v,std::string ( " ," ) );
135+
136+ if (!v. compare ( " " ))
137+ return {} ;
138+
139+ return Split (v, " ," );
146140}
147141
148- void Config :: Set(std::string key, std::string value) {
142+ void Config:: Set (std::string_view key, const std::string& value) {
149143 mtx_lock.lock ();
150144 if (key.length () == 0 ) {
151145 mtx_lock.unlock ();
@@ -155,8 +149,7 @@ void Config :: Set(std::string key, std::string value) {
155149 std::string section = " " ;
156150 std::string option;
157151
158- transform (key.begin (), key.end (), key.begin (), ::tolower);
159- std::vector<std::string> keys = Split (key, std::string (" ::" ));
152+ std::vector<std::string> keys = Split (std::string (key), " ::" );
160153 if (keys.size () >= 2 ) {
161154 section = keys[0 ];
162155 option = keys[1 ];
@@ -168,19 +161,19 @@ void Config :: Set(std::string key, std::string value) {
168161 mtx_lock.unlock ();
169162}
170163
171- std::string Config :: Get(std::string key) {
164+ std::string Config:: Get (std::string_view key) {
172165 std::string section;
173166 std::string option;
174- transform (key. begin (), key. end (), key. begin (), ::tolower);
175- std::vector<std::string> keys = Split (key, std::string (" ::" ) );
167+
168+ std::vector<std::string> keys = Split (std::string (key), " ::" );
176169 if (keys.size () >= 2 ) {
177170 section = keys[0 ];
178171 option = keys[1 ];
179172 } else {
180173 section = DEFAULT_SECTION;
181174 option = keys[0 ];
182175 }
183- bool ok = data.find (section)!= data.end () && data[section].find (option) != data[section].end ();
176+ bool ok = data.find (section) != data.end () && data[section].find (option) != data[section].end ();
184177 if (ok)
185178 return data[section][option];
186179 return " " ;
0 commit comments