Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <list>
- #include <map>
- #include <string>
- #include <sstream>
- const std::string BEGIN = "desc";
- const std::string END = "csed";
- const std::string EQUALS = "=";
- struct Block
- {
- std::string action;
- std::list<std::string> arguments;
- };
- std::ostream& operator<<(std::ostream& stream, const Block& block)
- {
- stream << block.action << ' ';
- for(std::list<std::string>::const_iterator it = block.arguments.begin(); it != block.arguments.end(); it++)
- {
- stream << *it << ' ';
- }
- return stream;
- }
- std::map<int, Block> parse()
- {
- std::ifstream input("f.txt");
- std::map<int, Block> blocks;
- std::string line, equals, action, arg;
- int id;
- bool flag = true;
- // input.open(name);
- getline(input, line);
- if(line.compare(BEGIN) != 0)
- {
- // throw std::invalid_argument("Exception incorrect data");
- }
- while(flag)
- {
- getline(input, line);
- if(line.compare(END) == 0)
- {
- flag = false;
- }
- if(flag)
- {
- std::stringstream stream(line);
- stream >> id >> equals;
- if(equals.compare(EQUALS) == 0)
- {
- Block token;
- stream >> action;
- token.action = action;
- while(!stream.eof())
- {
- stream >> arg;
- token.arguments.push_back(arg);
- }
- blocks[id] = token;
- }
- else
- {
- // throw std::invalid_argument("Exception incorrect data");
- }
- }
- }
- return blocks;
- }
- std::list<int> transporter()
- {
- std::ifstream input("f.txt");
- std::list<int> transporter;
- std::string line, next;
- int id;
- bool flag = true;
- getline(input, line);
- if(line.compare(BEGIN) != 0)
- {
- // throw std::invalid_argument("Exception incorrect data");
- }
- while(flag)
- {
- getline(input, line);
- if(line.compare(END) == 0)
- {
- flag = false;
- }
- }
- getline(input, line);
- std::stringstream stream(line);
- while(!stream.eof())
- {
- stream >> id >> next;
- transporter.push_back(id);
- }
- return transporter;
- }
- int main()
- {
- std::map<int, Block> lol;
- std::list<int> what;
- lol = parse();
- what = transporter();
- for(std::map<int, Block>::const_iterator it = lol.begin(); it != lol.end(); it++)
- {
- std::cout << it->first << "=" << it->second << std::endl;
- }
- for(std::list<int>::const_iterator it = what.begin(); it != what.end(); it++)
- {
- std::cout << *it << ' ';
- }
- getchar();
- return 0;
- }
Add Comment
Please, Sign In to add comment