written ConfigFile class and two exceptions - ParseException and FileException
This commit is contained in:
parent
899fc23801
commit
e69101fd9f
42
3mExceptions.cpp
Normal file
42
3mExceptions.cpp
Normal file
@ -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();
|
||||
}
|
45
3mExceptions.h
Normal file
45
3mExceptions.h
Normal file
@ -0,0 +1,45 @@
|
||||
#ifndef _3M_EXCEPTIONS_H
|
||||
#define _3M_EXCEPTIONS_H
|
||||
#include <string>
|
||||
#include <exception>
|
||||
/// \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
|
106
ConfigFile.cpp
Normal file
106
ConfigFile.cpp
Normal file
@ -0,0 +1,106 @@
|
||||
#include "ConfigFile.h"
|
||||
#include "3mExceptions.h"
|
||||
#include <fstream>
|
||||
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();
|
||||
}
|
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user