This is a significant issue all by itself:
Rule rule = { target, action };
(*policies).find(policy)->second.push_back(rule);
//call Method which manipulates params in struct
// **NOTE**: Fills the local variable "rule", not the one just pushed into the list
getRuleParams(&rule);
Though I don't necessarily advise this model, you can probably address this by:
Rule rule = { target, action };
getRuleParams(&rule); // fill here, before the push
// then push.
(*policies).find(policy)->second.push_back(rule);
But to do it properly, you should push a blank rule into the list, then reference that rule object (the one in the list) for initialization:
Rule rule = { target, action };
(*policies)[policy].push_back(rule);
getRuleParams(&(*policies)[policy].back());
To be honest, the paradigm itself needs a bit of work, there is a lot of unnecessary find()
ing going on in here, and methodical use of references and iterators could clean this up quite a bit.
Rework Example:
The following has stripped all the output clauses. They were actually making it harder to see problems. Please review the following to hopefully give you some ideas:
#include <list>
#include <map>
#include <string>
#include <iostream>
using namespace std;
// basic rule with strin parameter list
typedef std::list<std::string> ParamList;
struct Rule
{
string target;
string action;
ParamList params;
};
typedef std::list<Rule> RuleList;
typedef std::map<std::string, RuleList> MapStringToRuleList;
static int getRuleParams(void *prule)
{
Rule* rule = (Rule*) (prule);
rule->params.push_back("Entry!");
return 0;
}
static int getRules(void *dbpolicies) {
string policy = "FileIO";
string target = "TARGET";
string action = "DENY";
MapStringToRuleList* policies = (MapStringToRuleList*)(dbpolicies);
// push a new rule into the list.
Rule rule = {target, action};
RuleList& rules = (*policies)[ policy ];
rules.push_back(rule);
//call Method which manipulates params in struct
getRuleParams(&rules.back());
// for each rule in our policy's rule list...
for (RuleList::const_iterator rit = rules.begin(); rit != rules.end(); rit++)
{
cout << "Rule: " << policy << endl;
// for each rule in our curent rule's params list
const ParamList& params = rit->params;
for (ParamList::const_iterator pit = params.begin(); pit != params.end(); pit++)
{
cout << " Rule Param: " << *pit << endl;
// TODO: use current param for something.
}
}
return 0;
}
static MapStringToRuleList getPolicies()
{
MapStringToRuleList policies;
getRules(&policies);
return policies;
}
int main()
{
MapStringToRuleList policies = getPolicies();
}