3030#include " ../exception/illegal_argument_exception.h"
3131#include " ../util/util.h"
3232
33- const string Config::DEFAULT_SECTION = " default" ;
34- const string Config::DEFAULT_COMMENT = " #" ;
35- const string Config::DEFAULT_COMMENT_SEM = " ;" ;
36- mutex Config::mtx_lock;
33+ namespace casbin {
34+
35+ const std::string Config::DEFAULT_SECTION = " default" ;
36+ const std::string Config::DEFAULT_COMMENT = " #" ;
37+ const std::string Config::DEFAULT_COMMENT_SEM = " ;" ;
38+ std::mutex Config::mtx_lock;
3739
3840/* *
3941 * addConfig adds a new section->key:value to the configuration.
4042 */
41- bool Config :: AddConfig(string section, string option, string value) {
43+ bool Config :: AddConfig(std:: string section, std:: string option, std:: string value) {
4244 if (!section.compare (" " ))
4345 section = DEFAULT_SECTION;
4446 bool ok = data[section].find (option) != data[section].end ();
4547 data[section][option] = value;
4648 return !ok;
4749}
4850
49- void Config :: Parse(string f_name) {
51+ void Config :: Parse(std:: string f_name) {
5052 mtx_lock.lock ();
51- ifstream infile;
53+ std:: ifstream infile;
5254 try {
5355 infile.open (f_name);
54- } catch (const ifstream::failure e) {
56+ } catch (const std:: ifstream::failure e) {
5557 mtx_lock.unlock ();
5658 throw IOException (" Cannot open file." );
5759 }
@@ -60,10 +62,10 @@ void Config :: Parse(string f_name) {
6062 infile.close ();
6163}
6264
63- void Config :: ParseBuffer(istream* buf){
64- string section = " " ;
65+ void Config ::ParseBuffer (std:: istream * buf) {
66+ std:: string section = " " ;
6567 int line_num = 0 ;
66- string line;
68+ std:: string line;
6769 while (true ) {
6870 line_num++;
6971 if (getline (*buf, line, ' \n ' )){
@@ -77,17 +79,17 @@ void Config :: ParseBuffer(istream* buf){
7779 continue ;
7880 else if (line.find (DEFAULT_COMMENT_SEM)==0 )
7981 continue ;
80- else if (line.find (" [" )==0 && EndsWith (line, string (" ]" )))
82+ else if (line.find (" [" )==0 && EndsWith (line, std:: string (" ]" )))
8183 section = line.substr (1 , line.length () - 2 );
8284 else {
83- vector<string> option_val = Split (line, string (" =" ), 2 );
85+ std:: vector<std:: string> option_val = Split (line, std:: string (" =" ), 2 );
8486 if (option_val.size () != 2 ) {
8587 char * error = new char ;
8688 sprintf (error, " parse the content error : line %d , %s = ? " , line_num, option_val[0 ].c_str ());
87- throw IllegalArgumentException (string (error));
89+ throw IllegalArgumentException (std:: string (error));
8890 }
89- string option = Trim (option_val[0 ]);
90- string value = Trim (option_val[1 ]);
91+ std:: string option = Trim (option_val[0 ]);
92+ std:: string value = Trim (option_val[1 ]);
9193 AddConfig (section, option, value);
9294 }
9395 }
@@ -99,8 +101,8 @@ void Config :: ParseBuffer(istream* buf){
99101 * @param confName the path of the model file.
100102 * @return the constructor of Config.
101103 */
102- shared_ptr<Config> Config :: NewConfig(string conf_name) {
103- shared_ptr<Config> c (new Config);
104+ std:: shared_ptr<Config> Config :: NewConfig(std:: string conf_name) {
105+ std:: shared_ptr<Config> c (new Config);
104106 c->Parse (conf_name);
105107 return c;
106108}
@@ -111,50 +113,50 @@ shared_ptr<Config> Config :: NewConfig(string conf_name) {
111113 * @param text the model text.
112114 * @return the constructor of Config.
113115 */
114- shared_ptr<Config> Config :: NewConfigFromText(string text) {
115- shared_ptr<Config> c (new Config);
116- stringstream stream (text);
116+ std:: shared_ptr<Config> Config :: NewConfigFromText(std:: string text) {
117+ std:: shared_ptr<Config> c (new Config);
118+ std:: stringstream stream (text);
117119 c->ParseBuffer (&stream);
118120 return c;
119121}
120122
121- bool Config :: GetBool(string key) {
123+ bool Config :: GetBool(std:: string key) {
122124 return Get (key).compare (" true" )==0 ;
123125}
124126
125- int Config :: GetInt(string key) {
127+ int Config :: GetInt(std:: string key) {
126128 return atoi (Get (key).c_str ());
127129}
128130
129- float Config :: GetFloat(string key) {
131+ float Config :: GetFloat(std:: string key) {
130132 return float (atof (Get (key).c_str ()));
131133}
132134
133- string Config :: GetString(string key) {
135+ std:: string Config :: GetString(std:: string key) {
134136 return Get (key);
135137}
136138
137- vector<string> Config :: GetStrings(string key) {
138- string v = Get (key);
139+ std:: vector<std:: string> Config :: GetStrings(std:: string key) {
140+ std:: string v = Get (key);
139141 if (!v.compare (" " )) {
140- vector<string> empty;
142+ std:: vector<std:: string> empty;
141143 return empty;
142144 }
143- return Split (v,string (" ," ));
145+ return Split (v,std:: string (" ," ));
144146}
145147
146- void Config :: Set(string key, string value) {
148+ void Config :: Set(std:: string key, std:: string value) {
147149 mtx_lock.lock ();
148150 if (key.length () == 0 ) {
149151 mtx_lock.unlock ();
150152 throw IllegalArgumentException (" key is empty" );
151153 }
152154
153- string section = " " ;
154- string option;
155+ std:: string section = " " ;
156+ std:: string option;
155157
156158 transform (key.begin (), key.end (), key.begin (), ::tolower);
157- vector<string> keys = Split (key, string (" ::" ));
159+ std:: vector<std:: string> keys = Split (key, std:: string (" ::" ));
158160 if (keys.size () >= 2 ) {
159161 section = keys[0 ];
160162 option = keys[1 ];
@@ -166,11 +168,11 @@ void Config :: Set(string key, string value) {
166168 mtx_lock.unlock ();
167169}
168170
169- string Config :: Get(string key) {
170- string section;
171- string option;
171+ std:: string Config :: Get(std:: string key) {
172+ std:: string section;
173+ std:: string option;
172174 transform (key.begin (), key.end (), key.begin (), ::tolower);
173- vector<string> keys = Split (key, string (" ::" ));
175+ std:: vector<std:: string> keys = Split (key, std:: string (" ::" ));
174176 if (keys.size () >= 2 ) {
175177 section = keys[0 ];
176178 option = keys[1 ];
@@ -184,4 +186,6 @@ string Config :: Get(string key) {
184186 return " " ;
185187}
186188
189+ } // namespace casbin
190+
187191#endif // CONFIG_CPP
0 commit comments