diff --git a/3mExceptions.cpp b/3mExceptions.cpp new file mode 100644 index 0000000..2df0538 --- /dev/null +++ b/3mExceptions.cpp @@ -0,0 +1,42 @@ +#include "3mExceptions.h" +using namespace mmm; + +FileException::FileException(std::string filename, std::string mode, std::string err) { + _filename = filename; + _mode = mode; + _err = err; +} + +FileException::~FileException() throw() { + _filename = ""; + _mode = ""; + _err = ""; +} + +const char* FileException::what() const throw() { + std::string msg = ""; + msg += _filename; + msg += " ("; + msg += _mode; + msg += "): "; + msg += _err; + return msg.c_str(); +} + +ParseException::ParseException(std::string filename, std::string err) { + _filename = filename; + _err = err; +} + +ParseException::~ParseException() throw() { + _filename = ""; + _err = ""; +} + +const char* ParseException::what() const throw() { + std::string msg = ""; + msg += _filename; + msg += ": "; + msg += _err; + return msg.c_str(); +} diff --git a/3mExceptions.h b/3mExceptions.h new file mode 100644 index 0000000..e7eea1b --- /dev/null +++ b/3mExceptions.h @@ -0,0 +1,45 @@ +#ifndef _3M_EXCEPTIONS_H +#define _3M_EXCEPTIONS_H +#include +#include +/// \file 3mExceptions.h +/// \brief A header file for all the exception classes for 3m +/// \author Phitherek_ +/// \date 2013 +/// \version 0.1 + +/// \namespace mmm +/// \brief A global namespace for 3m +namespace mmm { +/// \class FileException +/// \brief An exception to be thrown when file operation fails. +class FileException: public std::exception { +private: + std::string _filename; + std::string _mode; + std::string _err; +public: + FileException(std::string filename, std::string mode, std::string err); ///< \brief A constructor with parameters + ///< \param filename Name of the file that caused an error. + ///< \param mode A mode (reading/writing etc.) that the file was opened in. + ///< \param err Error message. + ~FileException() throw(); /// \brief A destructor, as needed by std::exception. + const char* what() const throw(); ///< \brief A function, that returns the whole error. + ///< \return Whole error message. +}; +/// \class ParseException +/// \brief An exception to be throw when parse error occurs. +class ParseException { +private: + std::string _filename; + std::string _err; +public: + ParseException(std::string filename, std::string err); ///< \brief A constructor with parameters + ///< \param filename Name of the file that caused an error. + ///< \param err Error message. + ~ParseException() throw(); /// \brief A destructor, as needed by std::exception. + const char* what() const throw(); ///< \brief A function, that returns the whole error. + ///< \return Whole error message. +}; +} +#endif diff --git a/ConfigFile.cpp b/ConfigFile.cpp new file mode 100644 index 0000000..9362ef0 --- /dev/null +++ b/ConfigFile.cpp @@ -0,0 +1,106 @@ +#include "ConfigFile.h" +#include "3mExceptions.h" +#include +using namespace mmm; + +ConfigFile::ConfigFile(std::string path) { +std::ifstream conf(path.c_str()); +if(!conf) { +conf.close(); +throw FileException(path, "reading", "Could not open file!"); +} +_path = path; +string action = "parse"; +while(!conf.eof()) { +std::string line = ""; +conf >> line; +if(conf) { +if(action == "parse") { +if(line[0] == '[') { +int i = 1; +string sa = ""; +while(line[i] != ']') { +if(i >= line.length()-1 && line[i] != ']') { +std::string err = ""; +err += "Found "; +err += line[i]; +err += " although ] was expected."; +conf.close(); +throw ParseException(_path, err); +} +sa += line[i]; +i++; +} +if(sa == "localpath") { +action = "localpath"; +} else if(sa == "modlist") { +action = "modlist"; +} else if(sa == "repoinfo") { +action = "repoinfo"; +} else if(sa == "end") { +action = "end"; +} else { +conf.close(); +std::string err = ""; +err += "Found "; +err += sa; +err += " although localpath, modlist or repoinfo was expected."; +throw ParseException(_path, err); +} +} else { +conf.close(); +std::string err; +err += "Found "; +err += line[0]; +err += " although [ was expected."; +throw ParseException(_path, err); +} +} else if(action == "localpath") { +if(line[0] == '[') { +conf.close(); +throw ParseException(_path, "Found [ although string and not option was expected."); +} +_localpath = line; +action = "parse"; +} else if(action == "modlist") { +if(line[0] == '[') { +conf.close(); +throw ParseException(_path, "Found [ although string and not option was expected."); +} +_modlist = line; +action = "parse"; +} else if(action == "repoinfo") { +if(line[0] == '[') { +conf.close(); +throw ParseException(_path, "Found [ although string and not option was expected."); +} +_repoinfo = line; +action = "parse"; +} else if(action == "end") { +conf.close(); +} +} +} +} + +std::string& ConfigFile::getLocalPath() { + return _localpath; +} + +std::string& ConfigFile::getModList() { + return _modlist; +} + +std::string& ConfigFile::getRepoInfo() { + return _repoinfo; +} + +void ConfigFile::write() { + std::ofstream conf(_path.c_str()); + if(!conf) { + conf.close(); + throw FileException(_path, "writing", "Could not open file!"); + } + conf << "[localpath]" << std::endl << _localpath << std::endl << "[modlist]" << std::endl << _modlist << std::endl << "[repoinfo]" << std::endl << _repoinfo << std::endl << "[end]" << std::endl; + conf.close(); +} diff --git a/ConfigFile.h b/ConfigFile.h index 00d034d..62da438 100644 --- a/ConfigFile.h +++ b/ConfigFile.h @@ -7,8 +7,9 @@ /// \date 2013 /// \version 0.1-pre -/// \namespace 3m +/// \namespace mmm /// \brief A global namespace for 3m +namespace mmm { /// \class ConfigFile /// \brief A class that parses and stores 3m configuration.