ModList before tests

master
Phitherek 2013-04-07 00:38:01 +02:00
parent 239b262b8f
commit 062f6feebd
5 changed files with 309 additions and 5 deletions

View File

@ -357,3 +357,7 @@ void ModInfo::clear() {
void ModInfo::setModInfoDescription(ModInfoDescripion mid) {
_desc = mid;
}
ModInfoDescription ModInfo::getModInfoDescription() {
return _desc;
}

View File

@ -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.

View File

@ -1,6 +1,8 @@
#include "ModList.h"
#include "NetSocket++/NetSocketPP.h"
#include "3mExceptions.h"
#include <vector>
#include <fstream>
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<std::string> 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<ModInfo>::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();
}

View File

@ -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

View File

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