@@ -24,11 +24,12 @@ vector<string> Model :: required_sections{"r","p","e","m"};
2424void Model :: LoadModelFromConfig(ConfigInterface *cfg) {
2525 for (unordered_map <string, string> :: iterator it = section_name_map.begin () ; it != section_name_map.end () ; it++)
2626 LoadSection (this , cfg, it->first );
27+
2728 vector<string> ms;
28- for (vector<string> :: iterator it = required_sections. begin () ; it != required_sections.end () ; it ++){
29- if (!this ->HasSection (*it))
30- ms.push_back (section_name_map[*it ]);
31- }
29+ for (int i= 0 ; i < required_sections.size () ; i ++)
30+ if (!this ->HasSection (required_sections[i]))
31+ ms.push_back (section_name_map[required_sections[i] ]);
32+
3233 if (ms.size () > 0 )
3334 throw MissingRequiredSections (" missing required sections: " + Join (ms, " ," ));
3435}
@@ -66,20 +67,22 @@ bool Model :: LoadAssertion(Model* model, ConfigInterface* cfg, string sec, stri
6667bool Model :: AddDef(string sec, string key, string value) {
6768 if (value == " " )
6869 return false ;
69- Assertion ast;
70- ast.key = key;
71- ast.value = value;
70+
71+ Assertion* ast = new Assertion;
72+ ast->key = key;
73+ ast->value = value;
7274 if (sec == " r" || sec == " p" ) {
73- ast. tokens = Split (ast. value , " ," );
74- for (int i = 0 ; i < ast. tokens .size () ; i++)
75- ast. tokens [i] = key + " _" + Trim (ast. tokens [i]);
75+ ast-> tokens = Split (ast-> value , " ," );
76+ for (int i = 0 ; i < ast-> tokens .size () ; i++)
77+ ast-> tokens [i] = key + " _" + Trim (ast-> tokens [i]);
7678 }
7779 else
78- ast. value = RemoveComments (EscapeAssertion (ast. value ));
80+ ast-> value = RemoveComments (EscapeAssertion (ast-> value ));
7981
8082 if (m.find (sec) != m.end ())
8183 m[sec] = AssertionMap ();
82- m[sec].assertion_map [key] = *
84+ ast->policy = vector<vector<string>>{};
85+ m[sec].assertion_map [key] = ast;
8386
8487 return true ;
8588}
@@ -167,13 +170,13 @@ void Model :: PrintPolicy() {
167170
168171// ClearPolicy clears all current policy.
169172void Model :: ClearPolicy() {
170- for (unordered_map<string, Assertion*> :: iterator it = this ->m [" p" ].assertion_map .begin () ; it != this ->m [" p" ].assertion_map .end () ; it++) {
171- ( it->second )->policy .clear ();
172- }
173+ for (unordered_map<string, Assertion*> :: iterator it = this ->m [" p" ].assertion_map .begin () ; it != this ->m [" p" ].assertion_map .end () ; it++)
174+ if (( it->second )->policy .size () > 0 )
175+ (it-> second )-> policy . clear ();
173176
174- for (unordered_map<string, Assertion*> :: iterator it = this ->m [" g" ].assertion_map .begin () ; it != this ->m [" g" ].assertion_map .end () ; it++) {
175- ( it->second )->policy .clear ();
176- }
177+ for (unordered_map<string, Assertion*> :: iterator it = this ->m [" g" ].assertion_map .begin () ; it != this ->m [" g" ].assertion_map .end () ; it++)
178+ if (( it->second )->policy .size () > 0 )
179+ (it-> second )-> policy . clear ();
177180}
178181
179182// GetPolicy gets all rules in a policy.
@@ -184,30 +187,28 @@ vector<vector<string>> Model :: GetPolicy(string sec, string p_type) {
184187// GetFilteredPolicy gets rules based on field filters from a policy.
185188vector<vector<string>> Model :: GetFilteredPolicy(string sec, string p_type, int field_index, vector<string> field_values) {
186189 vector<vector<string>> res;
187-
188- for (vector<vector<string>> :: iterator it = m[sec]. assertion_map [p_type]-> policy . begin () ; it != m[sec]. assertion_map [p_type]-> policy .end () ; it ++){
190+ vector<vector<string>> policy (m[sec]. assertion_map [p_type]-> policy );
191+ for ( int i = 0 ; i < policy.size () ; i ++){
189192 bool matched = true ;
190193 for (int i = 0 ; i < field_values.size () ; i++){
191- if (field_values[i] != " " && (*it )[field_index + i] != field_values[i] ){
194+ if (field_values[i] != " " && (policy[i] )[field_index + i] != field_values[i] ){
192195 matched = false ;
193196 break ;
194197 }
195198 }
196- if (matched) {
197- res.push_back (*it);
198- }
199+ if (matched)
200+ res.push_back (policy[i]);
199201 }
200202
201203 return res;
202204}
203205
204206// HasPolicy determines whether a model has the specified policy rule.
205207bool Model :: HasPolicy(string sec, string p_type, vector<string> rule) {
206- for (vector<vector<string>> :: iterator it = m[sec].assertion_map [p_type]->policy .begin () ; it != m[sec].assertion_map [p_type]->policy .end () ; it++) {
207- if (ArrayEquals (rule, *it)) {
208+ vector<vector<string>> policy (m[sec].assertion_map [p_type]->policy );
209+ for (int i=0 ; i < policy.size () ; i++)
210+ if (ArrayEquals (rule, policy[i]))
208211 return true ;
209- }
210- }
211212
212213 return false ;
213214}
@@ -267,21 +268,22 @@ bool Model :: RemovePolicies(string sec, string p_type, vector<vector<string>> r
267268pair<bool , vector<vector<string>>> Model :: RemoveFilteredPolicy(string sec, string p_type, int field_index, vector<string> field_values) {
268269 vector<vector<string>> tmp;
269270 vector<vector<string>> effects;
271+ vector<vector<string>> policy (m[sec].assertion_map [p_type]->policy );
270272 bool res = false ;
271- for (vector<vector< string>> :: iterator it = m[sec]. assertion_map [p_type]-> policy . begin () ; it != m[sec]. assertion_map [p_type]-> policy .end () ; it ++) {
273+ for ( int i = 0 ; i < policy.size () ; i ++){
272274 bool matched = true ;
273275 for (int i = 0 ; i < field_values.size () ; i++) {
274- if (field_values[i] != " " && (*it )[field_index+i] != field_values[i]) {
276+ if (field_values[i] != " " && (policy[i] )[field_index+i] != field_values[i]) {
275277 matched = false ;
276278 break ;
277279 }
278280 }
279281 if (matched){
280- effects.push_back (*it );
282+ effects.push_back (policy[i] );
281283 res = true ;
282284 }
283285 else
284- tmp.push_back (*it );
286+ tmp.push_back (policy[i] );
285287 }
286288
287289 m[sec].assertion_map [p_type]->policy = tmp;
@@ -292,10 +294,9 @@ pair<bool, vector<vector<string>>> Model :: RemoveFilteredPolicy(string sec, str
292294// GetValuesForFieldInPolicy gets all values for a field for all rules in a policy, duplicated values are removed.
293295vector<string> Model :: GetValuesForFieldInPolicy(string sec, string p_type, int field_index) {
294296 vector<string> values;
295-
296- for (vector<vector<string>> :: iterator it = m[sec].assertion_map [p_type]->policy .begin () ; it != m[sec].assertion_map [p_type]->policy .end () ; it++){
297- values.push_back ((*it)[field_index]);
298- }
297+ vector<vector<string>> policy (m[sec].assertion_map [p_type]->policy );
298+ for (int i = 0 ; i < policy.size () ; i++)
299+ values.push_back ((policy[i])[field_index]);
299300
300301 ArrayRemoveDuplicates (values);
301302
@@ -307,9 +308,9 @@ vector<string> Model :: GetValuesForFieldInPolicyAllTypes(string sec, int field_
307308 vector<string> values;
308309
309310 for (unordered_map<string, Assertion*> :: iterator it = m[sec].assertion_map .begin () ; it != m[sec].assertion_map .end () ; it++) {
310- for ( vector<string> :: iterator it1 = this -> GetValuesForFieldInPolicy (sec, it-> first , field_index). begin () ; it1 != this ->GetValuesForFieldInPolicy (sec, it->first , field_index). end () ; it1++) {
311- values. push_back (*it1);
312- }
311+ vector<string> values_for_field ( this ->GetValuesForFieldInPolicy (sec, it->first , field_index));
312+ for ( int i = 0 ; i < values_for_field. size () ; i++)
313+ values. push_back (values_for_field[i]);
313314 }
314315
315316 ArrayRemoveDuplicates (values);
0 commit comments