From a32ac518bb2b415f24b1a0d006ad8a56b6e58b38 Mon Sep 17 00:00:00 2001 From: Phitherek Date: Thu, 13 Sep 2012 00:40:06 +0200 Subject: [PATCH] written remote modlist parser and checker - modlistparser.cpp --- filetesters/modlistparser.cpp | 122 ++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 filetesters/modlistparser.cpp diff --git a/filetesters/modlistparser.cpp b/filetesters/modlistparser.cpp new file mode 100644 index 0000000..93cdbcf --- /dev/null +++ b/filetesters/modlistparser.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +struct rmodlistdata { +string name; +string server; +string modinfo; +}; + +int parsermodlist(vector *rmodlist, string mfn) { +ifstream modlist(mfn.c_str()); +vector tmpv = *rmodlist; +if(!modlist) { + cerr << "Modlist parse error: Cannot open modlist file for reading." << endl; +} +string action = "detect"; +rmodlistdata tmprmld; +while(!modlist.eof()) { + string line; + modlist >> line; + if(modlist) { + if(action == "detect") { + if(line[0] == '{') { + string name = ""; + for(int i = 1; line[i] != '}' && i < line.length(); i++) { + name += line[i]; + } + tmprmld.name = name; + action = "parse"; + } else { + cerr << "Modlist parse error: Found " << line[0] << " although { was expected." << endl; + modlist.close(); + return 1; + } + } else if(action == "parse") { + if(line[0] == '{') { + if(line[1] == 'e' && line[2] == 'n' && line[3] == 'd' && line[4] == '}') { + if(tmprmld.name != "" && tmprmld.server != "" && tmprmld.modinfo != "") { + tmpv.push_back(tmprmld); + } else { + cerr << "Modlist parse error: Data error." << endl; + modlist.close(); + return 1; + } + action = "detect"; + } else { + cerr << "Modlist parse error: Found " << line << " although {end} or action in [] was expected." << endl; + modlist.close(); + return 1; + } + } else if(line[0] == '[') { + string tmpact = ""; + for(int i = 1; line[i] != ']' && i < line.length(); i++) { + tmpact += line[i]; + } + if(tmpact == "server" || tmpact == "modinfo") { + action = tmpact; + } else { + cerr << "Modlist parse error: Found " << tmpact << " although server/modinfo was expected." << endl; + modlist.close(); + return 1; + } + } else { + cerr << "Modlist parse error: Found " << line << " although {end} or action in [] was expected." << endl; + modlist.close(); + return 1; + } + } else if(action == "server") { + if(line[0] == '[' || line[0] == '{') { + cerr << "Modlist parse error: Found " << line[0] << " although string was expected." << endl; + modlist.close(); + return 1; + } else { + tmprmld.server = line; + action = "parse"; + } + } else if(action == "modinfo") { + if(line[0] == '[' || line[0] == '{') { + cerr << "Modlist parse error: Found " << line[0] << " although string was expected." << endl; + modlist.close(); + return 1; + } else { + tmprmld.modinfo = line; + action = "parse"; + } + } else { + cerr << "Modlist parse error: The program should not reach this place!" << endl; + modlist.close(); + return 1; + } +} +} +*rmodlist = tmpv; +return 0; +} + +int main(int argc, char** argv) { +string path; +if(argc != 2) { +cout << "Enter path to your remote modlist to parse: " << endl; +cin >> path; +} else { +path = argv[1]; +} +vector mods; +int pr; +pr = parsermodlist(&mods, path); +if(pr == 1) { + cerr << "Parse error! Your modlist is in the wrong format!" << endl; + return EXIT_FAILURE; +} +cout << "Modlist parsed successfully! Read contents: " << endl; +for(int i = 0; i < mods.size(); i++) { + cout << "mods[" << i << "] = {name: " << mods[i].name << ", server: " << mods[i].server << ", modinfo: " << mods[i].modinfo << "}" << endl; +} +return EXIT_SUCCESS; +}