From 20ab862593b1433e7a2a00a63b4dd1c9625f15fa Mon Sep 17 00:00:00 2001 From: Phitherek Date: Wed, 3 Apr 2013 18:04:02 +0200 Subject: [PATCH] added RepositoryInfo class, before testing --- OBJECTS | 3 +- RepositoryInfo.cpp | 196 +++++++++++++++++++++++++++++++++++++++++++++ RepositoryInfo.h | 47 +++++++++++ 3 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 RepositoryInfo.cpp create mode 100644 RepositoryInfo.h diff --git a/OBJECTS b/OBJECTS index 72ecfc7..0f0a380 100644 --- a/OBJECTS +++ b/OBJECTS @@ -2,13 +2,14 @@ ModDescription: name, description, release, deps, repotype, repoaddr x ModList: ModListDescription, vector ModListList: vector x LocalModDescription: ModDescription + remoteModlist (inheritance) x -LocalModList: vector, ConfigFile +LocalModList: vector, ConfigFile x RepositoryModDescription: ModDescription + path (inheritance) x RepositoryInfo: vector, ConfigFile ModListDescription: name, server, modlistPath x ModInfoDescription: name, server, modinfoPath x ModInfo: ModInfoDescription, vector ParameterParser: actionList, argc, argv +Action: SyncAction InstallAction UpdateAction diff --git a/RepositoryInfo.cpp b/RepositoryInfo.cpp new file mode 100644 index 0000000..c9d4e5f --- /dev/null +++ b/RepositoryInfo.cpp @@ -0,0 +1,196 @@ +#include "RepositoryInfo.h" +#include "3mExceptions.h" +#include +using namespace mmm; + +RepositoryInfo::RepositoryInfo() { + ConfigFile emptyconf; + _conf = emptyconf; + _repoinfo.clear(); + _repoinfoIterator = -1; + _repoinfoAtEnd = true; +} + +RepositoryInfo::RepositoryInfo(ConfigFile conf) { +_conf = conf; +std::string rifn = _conf.getRepoInfo(); +std::ifstream rifile(rifn.c_str()); +if(!rifile) { + throw FileException(rifn, "reading", "Could not open file!"); +} +std::string action = "detect"; +RepositoryModDescription tmprid; +while(!rifile.eof()) { + std::string line; + rifile >> line; + if(rifile) { + if(action == "detect") { + if(line[0] == '{') { + std::string name = ""; + for(int i = 1; line[i] != '}' && i < line.length(); i++) { + name += line[i]; + } + tmprid.setName(name); + action = "parse"; + } else { + std::string msg = ""; + msg += "Found "; + msg += line[0]; + msg += " although { was expected."; + rifile.close(); + throw ParseException(rifn, msg); + } + } else if(action == "parse") { + if(line[0] == '{') { + if(line[1] == 'e' && line[2] == 'n' && line[3] == 'd' && line[4] == '}') { + if(tmprid.getName() != "" && tmprid.getPath() != "") { + _repoinfo.push_back(tmprid); + tmprid.clear(); + } else { + rifile.close(); + throw ParseException(rifn, "Data error."); + } + action = "detect"; + } else { + std::string msg = ""; + msg += "Found "; + msg += line; + msg += " although {end} or action in [] was expected."; + rifile.close(); + throw ParseException(rifn, msg); + } + } else if(line[0] == '[') { + std::string tmpact = ""; + for(int i = 1; line[i] != ']' && i < line.length(); i++) { + tmpact += line[i]; + } + if(tmpact == "release" || tmpact == "path") { + action = tmpact; + } else { + std::string msg = ""; + msg += "Found "; + msg += tmpact; + msg += " although release/path was expected."; + rifile.close(); + throw ParseException(rifn, msg); + } + } else { + std::string msg = ""; + msg += "Found "; + msg += line; + msg += " although {end} or action in [] was expected."; + rifile.close(); + throw ParseException(rifn, msg); + } + } else if(action == "release") { + if(line[0] == '[' || line[0] == '{') { + std::string msg = ""; + msg += "Found "; + msg += line[0]; + msg += " although string was expected."; + rifile.close(); + throw ParseException(rifn, msg); + } else { + tmprid.setReleaseNr(line.c_str()); + action = "parse"; + } + } else if(action == "path") { + if(line[0] == '[' || line[0] == '{') { + std::string msg = ""; + msg += "Found "; + msg += line[0]; + msg += " although string was expected."; + rifile.close(); + throw ParseException(rifn, msg); + } else { + tmprid.setPath(line); + action = "parse"; + } + } else { + rifile.close(); + throw ParseException(rifn, "The program should not reach this place!"); + } +} +} +_repoinfoIterator = -1; +_repoinfoAtEnd = false; +} + +RepositoryInfo::~RepositoryInfo() { + ConfigFile emptyconf; + _conf = emptyconf; + _repoinfo.clear(); + _repoinfoIterator = -1; + _repoinfoAtEnd = true; +} + +RepositoryModDescription RepositoryInfo::getNextModDescription() { + if(_repoinfoIterator+1 < _repoinfo.size()) { + _repoinfoIterator++; + return _repoinfo[_repoinfoIterator]; + } else { + static RepositoryModDescription emptyrmd; + _repoinfoAtEnd = true; + return emptyrmd; + } +} + +RepositoryModDescription RepositoryInfo::getModDescriptionByName(std::string name) { + for(unsigned int i = 0; i < _repoinfo.size(); i++) { + if(_repoinfo[i].getName() == name) { + return _repoinfo[i]; + } + } + static RepositoryModDescription emptyrmd; + return emptyrmd; +} + +void RepositoryInfo::insertModDescription(RepositoryModDescription rmd) { + _repoinfo.push_back(rmd); + if(_repoinfoAtEnd) { + _repoinfoAtEnd = false; + } +} + +void RepositoryInfo::deleteModDescription(std::string name) { + for(std::vector::iterator i = _repoinfo.begin(); i < _repoinfo.end(); i++) { + if(i -> getName() == name) { + _repoinfo.erase(i); + } + } +} + +void RepositoryInfo::resetModDescriptionIterator() { + _repoinfoIterator = -1; + if(_repoinfoAtEnd) { + _repoinfoAtEnd = false; + } +} + +bool RepositoryInfo::modDescriptionsAtEnd() { + return _repoinfoAtEnd; +} + +void RepositoryInfo::setConfigFile(ConfigFile conf) { + _conf = conf; +} + +void RepositoryInfo::write() { + std::string rifn = _conf.getRepoInfo(); +std::ofstream rifile(rifn.c_str()); +if(!rifile) { + throw FileException(rifn, "writing", "Could not open file!"); +} +for(unsigned int i = 0; i < _repoinfo.size(); i++) { + rifile << "{" << _repoinfo[i].getName() << "}" << std::endl << "[release]" << std::endl << _repoinfo[i].getReleaseNr() << std::endl << "[path]" << std::endl << _repoinfo[i].getPath() << std::endl << "{end}" << std::endl; +} +rifile.close(); +} + +void RepositoryInfo::clear() { + ConfigFile emptyconf; + _conf = emptyconf; + _repoinfo.clear(); + _repoinfoIterator = -1; + _repoinfoAtEnd = false; +} diff --git a/RepositoryInfo.h b/RepositoryInfo.h new file mode 100644 index 0000000..6f43430 --- /dev/null +++ b/RepositoryInfo.h @@ -0,0 +1,47 @@ +#ifndef _REPOSITORYINFO_H +#define _REPOSITORYINFO_H +#include "ConfigFile.h" +#include "RepositoryModDescription.h" +#include +#include +/// \file RepositoryInfo.h +/// \brief A class representing a local repository info file. +/// \author Phitherek_ +/// \date 2013 +/// \version 0.1-pre + +/// \namespace mmm +/// \brief A global namespace for 3m. +namespace mmm { +/// \class RepositoryInfo +/// \brief A class representing a local repository info file. +class RepositoryInfo { +private: + ConfigFile _conf; + std::vector _repoinfo; + int _repoinfoIterator; + bool _repoinfoAtEnd; +public: + RepositoryInfo(); ///< A constructor. + RepositoryInfo(ConfigFile conf); ///< \brief A constructor with parameter. + ///< Tries to open and parse local repository info file from given ConfigFile. + ///< \param conf A ConfigFile object. + ~RepositoryInfo(); ///< A destructor. + RepositoryModDescription getNextModDescription(); ///< \brief A function that returns next RepositoryModDescription from the list. + ///< \return Next RepositoryModDescription from the list or empty RepositoryModDescription object if at end. + RepositoryModDescription getModDescriptionByName(std::string name); ///< \brief A function that searches for a mod name and returns its RepositoryModDescription. + ///< \param name A mod name. + ///< \return A RepositoryModDescription of the mod or empty RepositoryModDescription object on failure. + void insertModDescription(RepositoryModDescription rmd); ///< \brief A function that inserts a RepositoryModDescription to the repository info file. + ///< \param rmd A RepositoryModDescription to insert. + void deleteModDescription(std::string name); ///< \brief A function that removes a RepositoryModDescription from local repository info file. + ///< \param name A name of a mod to be removed. + void resetModDescriptionIterator(); ///< A function that resets repository info iterator. + bool modDescriptionsAtEnd(); ///< \brief A function that returns if repository info iterator is at its end. + ///< \return True if iterator is at end, false otherwise. + void setConfigFile(ConfigFile conf); ///< \brief A function that sets ConfigFile in the object. + ///< \param conf A ConfigFile object. + void write(); ///< A function that writes back the repository info file. + void clear(); ///< A function that clears the object. +}; +}