added RepositoryInfo class, before testing

master
Phitherek 2013-04-03 18:04:02 +02:00
parent 501b2f5b5b
commit 20ab862593
3 changed files with 245 additions and 1 deletions

View File

@ -2,13 +2,14 @@ ModDescription: name, description, release, deps, repotype, repoaddr x
ModList: ModListDescription, vector<ModInfoDescription>
ModListList: vector<ModListDescription> x
LocalModDescription: ModDescription + remoteModlist (inheritance) x
LocalModList: vector<LocalModDescription>, ConfigFile
LocalModList: vector<LocalModDescription>, ConfigFile x
RepositoryModDescription: ModDescription + path (inheritance) x
RepositoryInfo: vector<RepositoryModDescription>, ConfigFile
ModListDescription: name, server, modlistPath x
ModInfoDescription: name, server, modinfoPath x
ModInfo: ModInfoDescription, vector<ModDescription>
ParameterParser: actionList, argc, argv
Action:
SyncAction
InstallAction
UpdateAction

196
RepositoryInfo.cpp Normal file
View File

@ -0,0 +1,196 @@
#include "RepositoryInfo.h"
#include "3mExceptions.h"
#include <fstream>
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<RepositoryModDescription>::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;
}

47
RepositoryInfo.h Normal file
View File

@ -0,0 +1,47 @@
#ifndef _REPOSITORYINFO_H
#define _REPOSITORYINFO_H
#include "ConfigFile.h"
#include "RepositoryModDescription.h"
#include <vector>
#include <string>
/// \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<RepositoryModDescription> _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.
};
}