BlinkenArea - GitList
Repositories
Blog
Wiki
libetherpix
Code
Commits
Branches
Tags
Search
Tree:
680076d
Branches
Tags
master
libetherpix
simulator
config.cpp
begin of config file parsing
Stefan Schuermans
commited
680076d
at 2017-06-08 19:55:08
config.cpp
Blame
History
Raw
/* * EtherPix simulator * * Copyright 2017 Stefan Schuermans <stefan schuermans info> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3 of the License. * * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <algorithm> #include <cctype> #include <functional> #include <fstream> #include <stdexcept> #include <sstream> #include <string> #include "config.h" /** * @brief constructor * @param[in] configFile name of config file */ Config::Config(std::string const &configFile) { readFile(configFile); } /** * @brief remove whitespace at beging and end of string * @param[in,out] str string to process */ void Config::trim(std::string &str) { // remove leading whitespace str.erase(str.begin(), std::find_if(str.begin(), str.end(), std::not1(std::ptr_fun<int, int>(std::isspace)))); // remove trailing whitespace str.erase(std::find_if(str.rbegin(), str.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), str.end()); } /** * @brief split string at sequences of whitespace * @param[in] str string to split * @param[out] fields fields of string */ void Config::split(std::string const &str, std::vector<std::string> &fields) { std::stringstream strm(str); fields.clear(); while (! strm.eof()) { std::string field; strm >> field; if (! field.empty()) { fields.push_back(field); } } } /** * @brief read config file * @param[in] configFile name of config file * @throws std::exception in case of error */ void Config::readFile(std::string const &configFile) { // open file std::ifstream cf(configFile.c_str(), std::ios::in); if (! cf.is_open()) { std::stringstream msg; msg << "cannot open config file \"" << configFile << "\" for reading"; throw std::runtime_error(msg.str()); } // read lines and process them std::string line; size_t no; for (no = 1; std::getline(cf, line); ++no) { // process line try { procLine(line); } catch (std::exception &ex) { std::stringstream msg; msg << "error in line " << no << " of config file \"" << configFile << "\": " << ex.what(); throw std::runtime_error(msg.str()); } } // close file cf.close(); } /** * @brief process line from config file * @param[in] line line to process * @throws std::exception in case of error */ void Config::procLine(std::string line) { // remove comment ("# ...") size_t commentStart = line.find("#"); if (commentStart != std::string::npos) { line.erase(commentStart); } trim(line); // ignore empty lines if (line.empty()) { return; } // split line at equal sign size_t equalSign = line.find("="); if (equalSign == std::string::npos) { std::stringstream msg; msg << "no equal sign found in \"" << line << "\""; throw std::runtime_error(msg.str()); } std::string setting = line.substr(0, equalSign); std::string value = line.substr(equalSign + 1); trim(setting); trim(value); procSetting(setting, value); } /** * @brief process setting from config file * @param[in] setting name of the setting * @param[in] value value for setting * @throws std::exception in case of error */ void Config::procSetting(std::string const &setting, std::string const &value) { std::vector<std::string> setFields, valFields; split(setting, setFields); split(value, valFields); // empty setting if (setFields.size() < 1) { throw std::runtime_error("empty setting name"); } // distributor if (setFields[0] == "distributor") { // TODO } // distributor address else if (setFields[0] == "distributorAddr") { // TODO } // mapping else if (setFields[0] == "mapping") { // TODO } // output else if (setFields[0] == "output") { // TODO } // unknown setting else { std::stringstream msg; msg << "unknown setting \"" << setting << "\""; throw std::runtime_error(msg.str()); } }