I am trying to come up with a way to make use of a "multilevel dynamic" container.
I am parsing a file to grab some pieces of data. Lets say the first field of data I find I push into an array. At the same time lets I wish to create 2 cascaded sublevels. So an element in Modules is a pointer to the Types vector associated with that module and each element in Types is a pointer to a vector of Data.
This concept should be similar to memory paging.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
/*
Modules Types Data
____ _____ ______
|____|------> |_____| ---------->|______|
|____| |_____| |______|
|____| |_____| |______|
|____|
|____|
|____| _____ ______
|____|------> |_____|----------->|______|
|____| |_____| |______|
*/
|
Obviously this becomes very hair quickly so it is obvious that I need to dynamically create and destroy vectors (if I do it this way). Should I just create pointers using the new operator?
An example (if possible) would be wonderful.
Here is some of my crap code if it is even worthwhile to read
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
|
class Parser
{
//storage arrays
vector <string> modules;
vector <string> terminal_type;
vector <string> tag_type;
vector <string> data;
//parsing data
string buffer;
string tmp;
vector <string> fields;
ifstream mod_file;
public:
//path of file to parse
string pathname;
Parser(){};
~Parser(){};
void parse_mod_file()
{
mod_file.open(pathname);
if(!mod_file.good())
{
//throw exception and have user re-enter the file pathname
cout << "BAD PATH" << endl;
exit(1);
}
while(!mod_file.eof() && mod_file.good())
{
getline(mod_file, buffer);
split(fields, buffer, is_any_of(" ,\t"));
for(int n=0; n < fields.size(); ++n)
{
if(fields[0].compare("<") == 0)
{
if(!finder(fields[0], &modules))
{
modules.push_back(fields[1]);
continue;
//vector <string> * "name" = new vector <string>;
//vector <string> * "name" = new vector <string>;
}
}
else
{
}
}
}
};
bool finder(string data, vector <string> *point)
{
for(int i = 0; i < point->size() ; ++i)
{
if(data.compare(point->at(i)) == 0)
{
return true;
}
}
};
void print_parsed_data()
{
for(size_t n=0; n < fields.size() ; ++n)
{
cout << "Parsed Data: " << endl;
cout << "fields[n]" << endl;
}
};
};
|
If it is more useful to know the end game here then what I am trying to do is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
$ max_logvar 1116
< arrow 0 1 8 0 3
# symb 41 -1 20
# tag arrow -30 -60
# proterm In 53 8 0 0 516 0
# proterm Out 53 -8 0 0 520 0
>
< anadelay 0 2 80 0 3
# symb 303 4 0
# tag anadelay 16 8
# interm Meas 2 0 0 0 6 0
# interm TimeDelay 2 0 0 0 6 0
# interm NoDelay 2 0 0 0 5 0
# outerm OutMeas 2 0 0 0 10 0
# outerm Error 2 0 0 0 9 0
>
|
parse this stuff and nicely organize it.
If you take the first one, "max_logvar" is a module so everything between < and > is associated with that module.
symb is unimportant for now.
then "proterm" is a "module type" so then module now needs a module type container but I may have more than one of those.
so then I break it down by "Input" and "Output" where each of those can have the integer values (just in an array where each position will be set) that are in the fields to the right.