From 062f6feebde1b150a2752b6f6beb88c339f851e6 Mon Sep 17 00:00:00 2001 From: Phitherek Date: Sun, 7 Apr 2013 00:38:01 +0200 Subject: [PATCH] ModList before tests --- ModInfo.cpp | 4 + ModInfo.h | 2 + ModList.cpp | 301 +++++++++++++++++++++++++++++++++++++++++++++++++++- ModList.h | 3 +- OBJECTS | 4 +- 5 files changed, 309 insertions(+), 5 deletions(-) diff --git a/ModInfo.cpp b/ModInfo.cpp index d62a519..4c4d036 100644 --- a/ModInfo.cpp +++ b/ModInfo.cpp @@ -357,3 +357,7 @@ void ModInfo::clear() { void ModInfo::setModInfoDescription(ModInfoDescripion mid) { _desc = mid; } + +ModInfoDescription ModInfo::getModInfoDescription() { + return _desc; +} diff --git a/ModInfo.h b/ModInfo.h index e25c28d..f4e1001 100644 --- a/ModInfo.h +++ b/ModInfo.h @@ -30,6 +30,8 @@ void setPath(std::string path); ///< \brief A function that sets local modinfo f ///< \param path A path to local modinfo file. void setModInfoDescription(ModInfoDescription mid); ///< \brief A function that sets ModInfoDescription in the object. ///< \param mid A ModInfoDescription object. +ModInfoDescription getModInfoDescription(); ///< \brief A function that returns ModInfoDescription object. +///< \return ModInfoDescription object. void write(); ///< A function that writes the changes to local modinfo file. void releaseInc(); ///< A function that increases modinfo release. ~ModInfo(); ///< A destructor. diff --git a/ModList.cpp b/ModList.cpp index 8fe879d..49d8166 100644 --- a/ModList.cpp +++ b/ModList.cpp @@ -1,6 +1,8 @@ #include "ModList.h" #include "NetSocket++/NetSocketPP.h" #include "3mExceptions.h" +#include +#include using namespace mmm; ModList::ModList() { @@ -18,7 +20,7 @@ ModList::ModList(ModListDescription mld) { _modinfos.clear(); _edit = false; _localPath = ""; - _modinforIterator = -1; + _modinfosIterator = -1; _modinfosAtEnd = false; NetSocketPP::HTTPReply data; try { @@ -36,5 +38,300 @@ ModList::ModList(ModListDescription mld) { throw BadResponseException(data.getResponse()); } std::string content = data.getContent(); - + std::vector modlist = strtovec(content); + std::string action = "detect"; + ModInfoDescription mid; + for(unsigned int i = 0; i < modlist.size(); i++) { + std::string line = ""; + line = modlist[i]; + if(line[0] != NULL && line[0] != ' ' && line[0] != '\n' && line[0] != '\r') { + if(action == "detect") { + if(line[0] == '{') { + string name = ""; + for(unsigned int i = 1; line[i] != '}' && i < line.length(); i++) { + name += line[i]; + } + mid.setName(name); + action = "parse"; + } else { + std::string msg = ""; + msg += "Found "; + msg += line[0]; + msg += " although { was expected."; + throw ParseException(_desc.getServer() + _desc.getPath(), msg); + } + } else if(action == "parse") { + if(line[0] == '{') { + if(line[1] == 'e' && line[2] == 'n' && line[3] == 'd' && line[4] == '}') { + if(mid.getName() != "" && mid.getServer() != "" && mid.getPath() != "") { + ModInfo mi; + mi.setModInfoDescription(mid); + _modinfos.push_back(mi); + } else { + throw ParseException(_desc.getServer() + _desc.getPath(), "Data error."); + } + action = "detect"; + } else { + std::string msg = ""; + msg += "Found "; + msg += line; + msg += " although {end} or action in [] was expected."; + throw ParseException(_desc.getServer() + _desc.getPath(), msg); + } + } 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 { + std::string msg = ""; + msg += "Found "; + msg += tmpact; + msg += " although server/modinfo was expected."; + throw ParseException(_desc.getServer() + _desc.getPath(), msg); + } + } else { + std::string msg = ""; + msg += "Found "; + msg += line; + msg += " although {end} or action in [] was expected."; + throw ParseException(_desc.getServer() + _desc.getPath(), msg); + } + } else if(action == "server") { + if(line[0] == '[' || line[0] == '{') { + std::string msg = ""; + msg += "Found "; + msg += line[0]; + msg += " although string was expected."; + throw ParseException(_desc.getServer() + _desc.getPath(), msg); + } else { + mid.setServer(line); + action = "parse"; + } + } else if(action == "modinfo") { + if(line[0] == '[' || line[0] == '{') { + std::string msg = ""; + msg += "Found "; + msg += line[0]; + msg += " although string was expected."; + throw ParseException(_desc.getServer() + _desc.getPath(), msg); + } else { + mid.setPath(line); + action = "parse"; + } + } else { + throw ParseException(_desc.getServer() + _desc.getPath(), "The program should not reach this place!"); + } +} +} +cout << "Got all modinfo descriptions from " << _desc.getName() << ", downloading and parsing modinfos..." << endl; +for(unsigned int i = 0; i < _modinfos.size(); i++) { + try { + ModInfo mi(_modinfos[i].getModInfoDescription()); + cout << "Successfully downloaded and parsed: " << mi.getName() << endl; + _modinfos[i] = mi; + } catch(NetSocketPP::NetworkException &exc) { + cerr << "NetworkException occured in NetSocket++: " << exc.what() << endl; + } catch(NetSocketPP::SocketException &exc) { + cerr << "SocketException occured in NetSocket++: " << exc.what() << endl; + } catch(ParseException &exc) { + cerr << "ParseException occured: " << exc.what() << endl; + } +} +} + +ModList::ModList(std::string path) { + ModListDescription emptymld; + _desc = emptymld; + _modinfos.clear(); + _edit = true; + _localPath = path; + _modinfosIterator = -1; + _modinfosAtEnd = false; + ifstream modlist(_localPath.c_str()); + if(!modlist) { + throw FileException(_localPath, "reading", "Could not open file!"); + } + std::string action = "detect"; + ModInfoDescription mid; + while(!modlist.eof()) { + std::string line = ""; + char c; + do { + modlist.get(c); + if(c != '\n'); + line += c; + } while(c != '\n'); + line = strip_endl(line); + if(modlist) { + if(line[0] != NULL && line[0] != ' ' && line[0] != '\n' && line[0] != '\r') { + if(action == "detect") { + if(line[0] == '{') { + string name = ""; + for(unsigned int i = 1; line[i] != '}' && i < line.length(); i++) { + name += line[i]; + } + mid.setName(name); + action = "parse"; + } else { + std::string msg = ""; + msg += "Found "; + msg += line[0]; + msg += " although { was expected."; + throw ParseException(_localPath, msg); + } + } else if(action == "parse") { + if(line[0] == '{') { + if(line[1] == 'e' && line[2] == 'n' && line[3] == 'd' && line[4] == '}') { + if(mid.getName() != "" && mid.getServer() != "" && mid.getPath() != "") { + ModInfo mi; + mi.setModInfoDescription(mid); + _modinfos.push_back(mi); + } else { + throw ParseException(_localPath, "Data error."); + } + action = "detect"; + } else { + std::string msg = ""; + msg += "Found "; + msg += line; + msg += " although {end} or action in [] was expected."; + throw ParseException(_localPath, msg); + } + } 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 { + std::string msg = ""; + msg += "Found "; + msg += tmpact; + msg += " although server/modinfo was expected."; + throw ParseException(_localPath, msg); + } + } else { + std::string msg = ""; + msg += "Found "; + msg += line; + msg += " although {end} or action in [] was expected."; + throw ParseException(_localPath, msg); + } + } else if(action == "server") { + if(line[0] == '[' || line[0] == '{') { + std::string msg = ""; + msg += "Found "; + msg += line[0]; + msg += " although string was expected."; + throw ParseException(_localPath, msg); + } else { + mid.setServer(line); + action = "parse"; + } + } else if(action == "modinfo") { + if(line[0] == '[' || line[0] == '{') { + std::string msg = ""; + msg += "Found "; + msg += line[0]; + msg += " although string was expected."; + throw ParseException(_localPath, msg); + } else { + mid.setPath(line); + action = "parse"; + } + } else { + throw ParseException(_localPath, "The program should not reach this place!"); + } +} +} +} +modlist.close(); +} + +ModList::~ModList() { + ModListDescription emptymld; + _desc = emptymld; + _modinfos.clear(); + _edit = false; + _localPath = ""; + _modinfosIterator = -1; + _modinfosAtEnd = true; +} + +void ModList::clear() { + ModListDescription emptymld; + _desc = emptymld; + _modinfos.clear(); + _edit = false; + _localPath = ""; + _modinfosIterator = -1; + _modinfosAtEnd = true; +} + +ModInfo ModList::getNextModInfo() { + if(_modinfosIterator+1 < _modinfos.size()) { + _modinfosIterator++; + return _modinfos[_modinfosIterator]; + } else { + static ModInfo emptymi; + _modinfosAtEnd = true; + return emptymi; + } +} + +ModInfo ModList::getModInfoByName(std::string name) { + for(unsigned int i = 0; i < _modinfos.size(); i++) { + if(_modinfos[i].getModInfoDescription().getName() == name) { + return modinfos[i]; + } + } + static ModInfo emptymi; + return emptymi; +} + +void ModList::insertModInfoDescription(ModInfoDescription mid) { + ModInfo mi; + mi.setModInfoDescription(mid); + _modinfos.push_back(mi); + if(_modinfosAtEnd) { + _modinfosAtEnd = false; + } +} + +void ModList::deleteModInfo(std::string name) { + for(std::vector::iterator i = _modinfos.begin; i != _modinfos.end(); i++) { + if((i -> getModInfoDescription()).getName() == name) { + _modinfos.erase(i); + } + } +} + +void ModList::resetModInfoIterator() { + _modinfosIterator = -1; + if(_modinfosAtEnd) { + _modinfosAtEnd = false; + } +} + +bool ModList::modinfosEnd() { + return _modinfosAtEnd; +} + +void ModList::write() { + if(!_edit) { + throw NonEditableException("Tried to write back remotely obtained modlist file!"); + } + ofstream modlist(_localPath.c_str()); + if(!ofstream) { + throw FileException(_localPath, "writing", "Could not open file!"); + } + for(unsigned int i = 0; i < _modinfos.size(); i++) { + ModInfoDescription mid = _modinfos[i].getModInfoDescription(); + modlist << "{" << mid.getName() << "}" << endl << "[server]" << endl << mid.getServer() << endl << "[modinfo]" << endl << mid.getPath() << endl << "{end}" << endl; + } + modlist.close(); } diff --git a/ModList.h b/ModList.h index 88ff097..d06e9ab 100644 --- a/ModList.h +++ b/ModList.h @@ -40,12 +40,13 @@ public: ///< \return ModInfo or empty ModInfo object on failure. void insertModInfoDescription(ModInfoDescription mid); ///< \brief A function that inserts a ModInfoDescription to the modlist. ///< \param mid A ModInfoDescription to be inserted. - void deleteModInfoDescription(std::string name); ///< \brief A function that deletes ModInfoDescription of given name from the modlist. + void deleteModInfo(std::string name); ///< \brief A function that deletes ModInfo of given name from the modlist. ///< \param name Modinfo name. void resetModInfoIterator(); ///< A function that resets modlist iterator. bool modinfosEnd(); ///< \brief A function, that returns if modlist iterator reached its end. ///< \return True if modlist iterator reached its end, false otherwise. void write(); ///< A function that writes modlist to local file. + void clear(); ///< A function that clears the object. }; } #endif diff --git a/OBJECTS b/OBJECTS index e784f8b..7067f2f 100644 --- a/OBJECTS +++ b/OBJECTS @@ -1,5 +1,5 @@ ModDescription: name, description, release, deps, repotype, repoaddr x -ModList: ModListDescription, vector +ModList: ModListDescription, vector t ModListList: vector x LocalModDescription: ModDescription + remoteModlist (inheritance) x LocalModList: vector, ConfigFile x @@ -7,7 +7,7 @@ RepositoryModDescription: ModDescription + path (inheritance) x RepositoryInfo: vector, ConfigFile x ModListDescription: name, server, modlistPath x ModInfoDescription: name, server, modinfoPath x -ModInfo: ModInfoDescription, vector +ModInfo: ModInfoDescription, vector x ParameterParser: actionList, argc, argv Action: SyncAction