From 5aabfdc76478bae673397815d9546b04450d7b9f Mon Sep 17 00:00:00 2001 From: Phitherek Date: Sun, 7 Apr 2013 15:44:36 +0200 Subject: [PATCH] ModList after tests, finished lower-level data objects --- ModInfo.cpp | 2 +- ModList.cpp | 52 +++++++---- ModList.h | 4 + OBJECTS | 2 +- tests/Makefile | 4 +- tests/ModListTest.cpp | 209 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 252 insertions(+), 21 deletions(-) create mode 100644 tests/ModListTest.cpp diff --git a/ModInfo.cpp b/ModInfo.cpp index 4c4d036..4ba4a9a 100644 --- a/ModInfo.cpp +++ b/ModInfo.cpp @@ -354,7 +354,7 @@ void ModInfo::clear() { _localPath = ""; } -void ModInfo::setModInfoDescription(ModInfoDescripion mid) { +void ModInfo::setModInfoDescription(ModInfoDescription mid) { _desc = mid; } diff --git a/ModList.cpp b/ModList.cpp index 49d8166..86bf898 100644 --- a/ModList.cpp +++ b/ModList.cpp @@ -3,6 +3,9 @@ #include "3mExceptions.h" #include #include +#include +#include +#include using namespace mmm; ModList::ModList() { @@ -47,7 +50,7 @@ ModList::ModList(ModListDescription mld) { if(line[0] != NULL && line[0] != ' ' && line[0] != '\n' && line[0] != '\r') { if(action == "detect") { if(line[0] == '{') { - string name = ""; + std::string name = ""; for(unsigned int i = 1; line[i] != '}' && i < line.length(); i++) { name += line[i]; } @@ -79,8 +82,8 @@ ModList::ModList(ModListDescription mld) { throw ParseException(_desc.getServer() + _desc.getPath(), msg); } } else if(line[0] == '[') { - string tmpact = ""; - for(int i = 1; line[i] != ']' && i < line.length(); i++) { + std::string tmpact = ""; + for(unsigned int i = 1; line[i] != ']' && i < line.length(); i++) { tmpact += line[i]; } if(tmpact == "server" || tmpact == "modinfo") { @@ -126,18 +129,20 @@ ModList::ModList(ModListDescription mld) { } } } -cout << "Got all modinfo descriptions from " << _desc.getName() << ", downloading and parsing modinfos..." << endl; +std::cout << "Got all modinfo descriptions from " << _desc.getName() << ", downloading and parsing modinfos..." << std::endl; for(unsigned int i = 0; i < _modinfos.size(); i++) { try { ModInfo mi(_modinfos[i].getModInfoDescription()); - cout << "Successfully downloaded and parsed: " << mi.getName() << endl; + std::cout << "Successfully downloaded and parsed: " << mi.getName() << std::endl; _modinfos[i] = mi; } catch(NetSocketPP::NetworkException &exc) { - cerr << "NetworkException occured in NetSocket++: " << exc.what() << endl; + std::cerr << "NetworkException occured in NetSocket++: " << exc.what() << std::endl; } catch(NetSocketPP::SocketException &exc) { - cerr << "SocketException occured in NetSocket++: " << exc.what() << endl; + std::cerr << "SocketException occured in NetSocket++: " << exc.what() << std::endl; } catch(ParseException &exc) { - cerr << "ParseException occured: " << exc.what() << endl; + std::cerr << "ParseException occured: " << exc.what() << std::endl; + } catch(BadResponseException &exc) { + std::cerr << "BadResponseException occured: " << exc.what() << std::endl; } } } @@ -150,7 +155,7 @@ ModList::ModList(std::string path) { _localPath = path; _modinfosIterator = -1; _modinfosAtEnd = false; - ifstream modlist(_localPath.c_str()); + std::ifstream modlist(_localPath.c_str()); if(!modlist) { throw FileException(_localPath, "reading", "Could not open file!"); } @@ -169,7 +174,7 @@ ModList::ModList(std::string path) { if(line[0] != NULL && line[0] != ' ' && line[0] != '\n' && line[0] != '\r') { if(action == "detect") { if(line[0] == '{') { - string name = ""; + std::string name = ""; for(unsigned int i = 1; line[i] != '}' && i < line.length(); i++) { name += line[i]; } @@ -201,8 +206,8 @@ ModList::ModList(std::string path) { throw ParseException(_localPath, msg); } } else if(line[0] == '[') { - string tmpact = ""; - for(int i = 1; line[i] != ']' && i < line.length(); i++) { + std::string tmpact = ""; + for(unsigned int i = 1; line[i] != ']' && i < line.length(); i++) { tmpact += line[i]; } if(tmpact == "server" || tmpact == "modinfo") { @@ -286,7 +291,7 @@ ModInfo ModList::getNextModInfo() { ModInfo ModList::getModInfoByName(std::string name) { for(unsigned int i = 0; i < _modinfos.size(); i++) { if(_modinfos[i].getModInfoDescription().getName() == name) { - return modinfos[i]; + return _modinfos[i]; } } static ModInfo emptymi; @@ -303,9 +308,11 @@ void ModList::insertModInfoDescription(ModInfoDescription mid) { } void ModList::deleteModInfo(std::string name) { - for(std::vector::iterator i = _modinfos.begin; i != _modinfos.end(); i++) { - if((i -> getModInfoDescription()).getName() == name) { + for(std::vector::iterator i = _modinfos.begin(); i != _modinfos.end(); i++) { + mmm::ModInfoDescription mid = i -> getModInfoDescription(); + if(mid.getName() == name) { _modinfos.erase(i); + return; } } } @@ -325,13 +332,22 @@ void ModList::write() { if(!_edit) { throw NonEditableException("Tried to write back remotely obtained modlist file!"); } - ofstream modlist(_localPath.c_str()); - if(!ofstream) { + std::ofstream modlist(_localPath.c_str()); + if(!modlist) { 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 << "{" << mid.getName() << "}" << std::endl << "[server]" << std::endl << mid.getServer() << std::endl << "[modinfo]" << std::endl << mid.getPath() << std::endl << "{end}" << std::endl; } modlist.close(); } + +ModListDescription ModList::getModListDescription() { + return _desc; +} + +void ModList::setPath(std::string path) { + _localPath = path; + _edit = true; +} diff --git a/ModList.h b/ModList.h index d06e9ab..1082f5d 100644 --- a/ModList.h +++ b/ModList.h @@ -47,6 +47,10 @@ public: ///< \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. + ModListDescription getModListDescription(); ///< \brief A function that gets a ModListDescription object. + ///< \return A ModListDescription object. + void setPath(std::string path); ///< \brief A function that sets path to local modlist file. + ///< \param path A path to local modlist file. }; } #endif diff --git a/OBJECTS b/OBJECTS index 7067f2f..b31f6d2 100644 --- a/OBJECTS +++ b/OBJECTS @@ -1,5 +1,5 @@ ModDescription: name, description, release, deps, repotype, repoaddr x -ModList: ModListDescription, vector t +ModList: ModListDescription, vector x ModListList: vector x LocalModDescription: ModDescription + remoteModlist (inheritance) x LocalModList: vector, ConfigFile x diff --git a/tests/Makefile b/tests/Makefile index 7be59cc..14cde16 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -7,6 +7,7 @@ all: ${CXX} ${CXXFLAGS} -o LocalModListTest LocalModListTest.cpp ../LocalModList.cpp ../LocalModDescription.cpp ../ModDescription.cpp ../3mExceptions.cpp ../ConfigFile.cpp ${CXX} ${CXXFLAGS} -o RepositoryInfoTest RepositoryInfoTest.cpp ../RepositoryModDescription.cpp ../ModDescription.cpp ../3mExceptions.cpp ../ConfigFile.cpp ../RepositoryInfo.cpp ${CXX} ${CXXFLAGS} -o ModInfoTest ModInfoTest.cpp ../ModInfo.cpp ../ModInfoDescription.cpp ../ModDescription.cpp ../3mExceptions.cpp -lnetsocketpp + ${CXX} ${CXXFLAGS} -o ModListTest ModListTest.cpp ../ModList.cpp ../ModListDescription.cpp ../ModInfo.cpp ../ModInfoDescription.cpp ../ModDescription.cpp ../3mExceptions.cpp -lnetsocketpp debug: ${CXX} ${CXXFLAGS} -o ConfigFileTest ConfigFileTest.cpp ../ConfigFile.cpp ../3mExceptions.cpp -g ${CXX} ${CXXFLAGS} -o ModDescriptionTest ModDescriptionTest.cpp ../ModDescription.cpp -g @@ -15,5 +16,6 @@ debug: ${CXX} ${CXXFLAGS} -o LocalModListTest LocalModListTest.cpp ../LocalModList.cpp ../LocalModDescription.cpp ../ModDescription.cpp ../3mExceptions.cpp ../ConfigFile.cpp -g ${CXX} ${CXXFLAGS} -o RepositoryInfoTest RepositoryInfoTest.cpp ../RepositoryModDescription.cpp ../ModDescription.cpp ../3mExceptions.cpp ../ConfigFile.cpp ../RepositoryInfo.cpp -g ${CXX} ${CXXFLAGS} -o ModInfoTest ModInfoTest.cpp ../ModInfo.cpp ../ModInfoDescription.cpp ../ModDescription.cpp ../3mExceptions.cpp -lnetsocketpp -g + ${CXX} ${CXXFLAGS} -o ModListTest ModListTest.cpp ../ModList.cpp ../ModListDescription.cpp ../ModInfo.cpp ../ModInfoDescription.cpp ../ModDescription.cpp ../3mExceptions.cpp -lnetsocketpp -g clean: - rm -rf ConfigFileTest ModDescriptionTest LocalModDescriptionTest ModListListTest LocalModListTest RepositoryInfoTest ModInfoTest + rm -rf ConfigFileTest ModDescriptionTest LocalModDescriptionTest ModListListTest LocalModListTest RepositoryInfoTest ModInfoTest ModListTest diff --git a/tests/ModListTest.cpp b/tests/ModListTest.cpp new file mode 100644 index 0000000..8e1328e --- /dev/null +++ b/tests/ModListTest.cpp @@ -0,0 +1,209 @@ +#include "../ModList.h" +#include "../ModInfo.h" +#include "NetSocket++/NetSocketPP.h" +#include "../ModListDescription.h" +#include "../ModInfoDescription.h" +#include "../ModDescription.h" +#include +#include +using namespace std; + +int main() { +char mode; +cout << "Select mode: Download, parse and list (r)emote modlist file, Open and parse (l)ocal modlist file, Create (n)ew local modlist file: "; +cin >> mode; +if(mode == 'r') { + string server; + string path; + cout << "Enter server of remote modlist: "; + cin >> server; + cout << "Enter path to the remote modlist on the server: "; + cin >> path; + try { + mmm::ModListDescription mld("test", server, path); + mmm::ModList ml(mld); + mld = ml.getModListDescription(); + cout << "Got modlist: " << mld.getName() << "!" << endl << "server: " << mld.getServer() << endl << "path: " << mld.getPath() << endl << "Content: " << endl; + ml.resetModInfoIterator(); + while(!ml.modinfosEnd()) { + mmm::ModInfo mi = ml.getNextModInfo(); + if(mi.getModInfoDescription().getName() != "" && mi.getModInfoDescription().getServer() != "" && mi.getModInfoDescription().getPath() != "") { + cout << "Modinfo: " << endl << "name: " << mi.getModInfoDescription().getName() << endl << "server: " << mi.getModInfoDescription().getServer() << endl << "path: " << mi.getModInfoDescription().getPath() << endl; + if(mi.getName() != "" && mi.getDescription() != "" && mi.getReleaseNr() > 0 && mi.getRepositoryType() != "" && mi.getRepositoryAddress() != "") { + cout << "Mod description: " << endl << "name: " << mi.getName() << endl << "description: " << mi.getDescription() << endl << "release: " << mi.getReleaseNr() << endl << "dependencies:" << endl; + mi.resetDependencyIterator(); + while(!mi.dependenciesEnd()) { + string dep = mi.getNextDependency(); + if(dep != "") { + cout << dep << endl; + } + } + cout << "repository type: " << mi.getRepositoryType() << endl << "repository address: " << mi.getRepositoryAddress() << endl; + } else { + cout << "No mod information!" << endl; + } + } + cout << endl; + } + } catch(NetSocketPP::NetworkException &exc) { + cerr << "NetworkException occured in NetSocket++: " << exc.what() << "! Exiting..." << endl; + return EXIT_FAILURE; + } catch(NetSocketPP::SocketException &exc) { + cerr << "SocketException occured in NetSocket++: " << exc.what() << "! Exiting..." << endl; + return EXIT_FAILURE; + } catch(mmm::ParseException &exc) { + cerr << "ParseException occured: " << exc.what() << "! That probably means you have badly formatted modlist file! Exiting..." << endl; + return EXIT_FAILURE; + } catch(mmm::BadResponseException &exc) { + cerr << "BadResponseException occured: " << exc.what() << "! That probably means you have given wrong path to the file on the server! Exiting..." << endl; + } + cout << "All OK!" << endl; +} else if(mode == 'l') { + string path; + cout << "Enter path: "; + cin >> path; + try { + mmm::ModList ml(path); + char action; + do { + cout << "Select action: (l)ist modlist, (f)ind by name and list, (a)dd modinfo description, (d)elete modinfo description, (s)ave and exit, (q)uit without saving: "; + cin >> action; + if(action == 'l') { + mmm::ModListDescription mld = ml.getModListDescription(); + cout << "Modlist name: " << mld.getName() << endl << "server: " << mld.getServer() << endl << "path: " << mld.getPath() << endl << "Content: " << endl; + ml.resetModInfoIterator(); + while(!ml.modinfosEnd()) { + mmm::ModInfo mi = ml.getNextModInfo(); + if(mi.getModInfoDescription().getName() != "" && mi.getModInfoDescription().getServer() != "" && mi.getModInfoDescription().getPath() != "") { + cout << "Modinfo: " << endl << "name: " << mi.getModInfoDescription().getName() << endl << "server: " << mi.getModInfoDescription().getServer() << endl << "path: " << mi.getModInfoDescription().getPath() << endl; + } + } + } else if(action == 'f') { + string name; + cout << "Modinfo name: "; + cin >> name; + mmm::ModInfo mi = ml.getModInfoByName(name); + if(mi.getModInfoDescription().getName() != "" && mi.getModInfoDescription().getServer() != "" && mi.getModInfoDescription().getPath() != "") { + cout << "name: " << mi.getModInfoDescription().getName() << endl << "server: " << mi.getModInfoDescription().getServer() << endl << "path: " << mi.getModInfoDescription().getPath() << endl; + } else { + cout << name << " not found!" << endl; + } + } else if(action == 'a') { + string name, server, path; + cout << "name: "; + cin >> name; + cout << "server: "; + cin >> server; + cout << "path: "; + cin >> path; + if(name != "" && server != "" && path != "") { + mmm::ModInfoDescription mid(name, server, path); + ml.insertModInfoDescription(mid); + } else { + cerr << "Cannot insert empty field!" << endl; + } + } else if(action == 'd') { + string name; + cout << "Modinfo name: "; + cin >> name; + mmm::ModInfo mi = ml.getModInfoByName(name); + if(mi.getModInfoDescription().getName() != "" && mi.getModInfoDescription().getServer() != "" && mi.getModInfoDescription().getPath() != "") { + ml.deleteModInfo(name); + } else { + cout << name << " not found!" << endl; + } + } else if(action != 's' && action != 'q') { + cerr << "Unknown action!" << endl; + } + } while(action != 's' && action != 'q'); + if(action == 's') { + ml.write(); + } + cout << "All OK!" << endl; + } catch(mmm::FileException &exc) { + cerr << "FileException occured: " << exc.what() << "! That probably means you entered wrong path! Exiting..." << endl; + return EXIT_FAILURE; + } catch(mmm::ParseException &exc) { + cerr << "ParseException occured: " << exc.what() << "! That probably means your modinfo file is badly formatted! Exiting..." << endl; + return EXIT_FAILURE; + } catch(mmm::NonEditableException &exc) { + cerr << "NonEditableException occured: " << exc.what() << "! Exiting..." << endl; + } +} else if(mode == 'n') { + string path; + cout << "Enter path: "; + cin >> path; + try { + mmm::ModList ml; + ml.setPath(path); + char action; + do { + cout << "Select action: (l)ist modlist, (f)ind by name and list, (a)dd modinfo description, (d)elete modinfo description, (s)ave and exit, (q)uit without saving: "; + cin >> action; + if(action == 'l') { + mmm::ModListDescription mld = ml.getModListDescription(); + cout << "Modlist name: " << mld.getName() << endl << "server: " << mld.getServer() << endl << "path: " << mld.getPath() << endl << "Content: " << endl; + ml.resetModInfoIterator(); + while(!ml.modinfosEnd()) { + mmm::ModInfo mi = ml.getNextModInfo(); + if(mi.getModInfoDescription().getName() != "" && mi.getModInfoDescription().getServer() != "" && mi.getModInfoDescription().getPath() != "") { + cout << "Modinfo: " << endl << "name: " << mi.getModInfoDescription().getName() << endl << "server: " << mi.getModInfoDescription().getServer() << endl << "path: " << mi.getModInfoDescription().getPath() << endl; + } + } + } else if(action == 'f') { + string name; + cout << "Modinfo name: "; + cin >> name; + mmm::ModInfo mi = ml.getModInfoByName(name); + if(mi.getModInfoDescription().getName() != "" && mi.getModInfoDescription().getServer() != "" && mi.getModInfoDescription().getPath() != "") { + cout << "name: " << mi.getModInfoDescription().getName() << endl << "server: " << mi.getModInfoDescription().getServer() << endl << "path: " << mi.getModInfoDescription().getPath() << endl; + } else { + cout << name << " not found!" << endl; + } + } else if(action == 'a') { + string name, server, path; + cout << "name: "; + cin >> name; + cout << "server: "; + cin >> server; + cout << "path: "; + cin >> path; + if(name != "" && server != "" && path != "") { + mmm::ModInfoDescription mid(name, server, path); + ml.insertModInfoDescription(mid); + } else { + cerr << "Cannot insert empty field!" << endl; + } + } else if(action == 'd') { + string name; + cout << "Modinfo name: "; + cin >> name; + mmm::ModInfo mi = ml.getModInfoByName(name); + if(mi.getModInfoDescription().getName() != "" && mi.getModInfoDescription().getServer() != "" && mi.getModInfoDescription().getPath() != "") { + ml.deleteModInfo(name); + } else { + cout << name << "not found!" << endl; + } + } else if(action != 's' && action != 'q') { + cerr << "Unknown action!" << endl; + } + } while(action != 's' && action != 'q'); + if(action == 's') { + ml.write(); + } + cout << "All OK!" << endl; + } catch(mmm::FileException &exc) { + cerr << "FileException occured: " << exc.what() << "! That probably means you entered wrong path! Exiting..." << endl; + return EXIT_FAILURE; + } catch(mmm::ParseException &exc) { + cerr << "ParseException occured: " << exc.what() << "! That probably means your modinfo file is badly formatted! Exiting..." << endl; + return EXIT_FAILURE; + } catch(mmm::NonEditableException &exc) { + cerr << "NonEditableException occured: " << exc.what() << "! Exiting..." << endl; + } +} else { + cerr << "Unknown mode: " << mode << "! Exiting..." << endl; + return EXIT_FAILURE; +} +return EXIT_SUCCESS; +}