diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-07-28 19:33:40 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-07-28 19:33:40 +0200 |
commit | a713d2931ebd26bd2bc1b6c1cce574940736e718 (patch) | |
tree | e01bb03af80e7cde8ded58c9a45fc392c1d24362 | |
parent | d5c57ad133fe30815f83b179c08546a302baa93b (diff) | |
download | gmrf-a713d2931ebd26bd2bc1b6c1cce574940736e718.tar gmrf-a713d2931ebd26bd2bc1b6c1cce574940736e718.zip |
Clean up config_t class
-rw-r--r-- | mmss/config.cpp | 32 | ||||
-rw-r--r-- | mmss/config.hpp | 17 | ||||
-rw-r--r-- | mmss/config.y | 11 | ||||
-rw-r--r-- | mmss/context.cpp | 4 |
4 files changed, 32 insertions, 32 deletions
diff --git a/mmss/config.cpp b/mmss/config.cpp index 89f1658..8e7cea3 100644 --- a/mmss/config.cpp +++ b/mmss/config.cpp @@ -29,25 +29,23 @@ #include <config.ll.hpp> #include <config.yy.hpp> +#include <cstdio> #include <stack> #include <libgen.h> +#include <unistd.h> namespace MMSS { -namespace Config { - -void add_network(context_t *mmss, config_t *conf, const char *name) { +void config_t::add_network(const char *name) { mmss->logf(LOG_NOTICE, "adding network `%s'", name); - conf->network.push_back(std::make_shared<network_t>(name)); -} - + network.push_back(std::make_shared<network_t>(name)); } -bool read_config(context_t *mmss, config_t *conf, const char *filename) { - bool ret = true; +std::shared_ptr<const config_t> config_t::read(context_t *mmss, const char *filename) { + std::shared_ptr<config_t> conf = std::shared_ptr<config_t>(new config_t(mmss)); char *oldcwd = get_current_dir_name(); char *filename2 = NULL; char *dir = NULL; @@ -71,7 +69,7 @@ bool read_config(context_t *mmss, config_t *conf, const char *filename) { file = fopen(filename, "r"); if (!file) { mmss->logf(LOG_ERR, "can't open config file `%s': %s", filename, strerror(errno)); - ret = false; + conf.reset(); goto end_free; } } @@ -84,7 +82,7 @@ bool read_config(context_t *mmss, config_t *conf, const char *filename) { if (chdir(dir)) { mmss->logf(LOG_ERR, "change from directory `%s' to `%s' failed", oldcwd, dir); - ret = false; + conf.reset(); goto end_free; } } @@ -94,7 +92,7 @@ bool read_config(context_t *mmss, config_t *conf, const char *filename) { if (token < 0) { mmss->logf(LOG_ERR, "config error: %s at %s:%i:%i", token_val.error, filename, loc.first_line, loc.first_column); - ret = false; + conf.reset(); goto end_free; } @@ -102,11 +100,11 @@ bool read_config(context_t *mmss, config_t *conf, const char *filename) { strings.push(token_val.str); - parse_ret = mmss_config_push_parse(ps, token, &token_val, &loc, mmss, conf, filename); + parse_ret = mmss_config_push_parse(ps, token, &token_val, &loc, conf, filename); } while (parse_ret == YYPUSH_MORE); if (parse_ret) - ret = false; + conf.reset(); end_free: while (!strings.empty()) { @@ -120,13 +118,13 @@ bool read_config(context_t *mmss, config_t *conf, const char *filename) { if(chdir(oldcwd)) mmss->logf(LOG_ERR, "can't chdir to `%s': %s", oldcwd, strerror(errno)); - free(filename2); - free(oldcwd); + std::free(filename2); + std::free(oldcwd); if (filename && file) - fclose(file); + std::fclose(file); - return ret; + return conf; } } diff --git a/mmss/config.hpp b/mmss/config.hpp index 48f0634..702f142 100644 --- a/mmss/config.hpp +++ b/mmss/config.hpp @@ -35,17 +35,22 @@ namespace MMSS { class config_t : public nocopy_t { -public: +private: + context_t *mmss; + std::list<std::shared_ptr<network_t>> network; std::list<std::shared_ptr<node_t>> nodes; -}; -namespace Config { + config_t(context_t *mmss0) : mmss(mmss0) {} -void add_network(context_t *mmss, config_t *conf, const char *name); +public: + context_t* get_context() const { + return mmss; + } -} + void add_network(const char *name); -bool read_config(context_t *mmss, config_t *conf, const char *filename); + static std::shared_ptr<const config_t> read(context_t *mmss, const char *filename); +}; } diff --git a/mmss/config.y b/mmss/config.y index 452e383..2b5fb72 100644 --- a/mmss/config.y +++ b/mmss/config.y @@ -28,8 +28,7 @@ %define api.push-pull push %name-prefix "mmss_config_" %locations -%parse-param {MMSS::context_t *mmss} -%parse-param {MMSS::config_t *conf} +%parse-param {const std::shared_ptr<config_t> &conf} %parse-param {const char *filename} @@ -57,7 +56,7 @@ %code { - void mmss_config_error(YYLTYPE *loc, context_t *mmss, config_t *conf, const char *filename, const char *s); + void mmss_config_error(YYLTYPE *loc, const std::shared_ptr<config_t> &conf, const char *filename, const char *s); } @@ -76,7 +75,7 @@ statement: TOK_NETWORK network '{' network_config '}' ; network: TOK_STRING { - MMSS::Config::add_network(mmss, conf, $1); + conf->add_network($1); } ; @@ -89,6 +88,6 @@ boolean: TOK_YES { $$ = true; } %% -void mmss_config_error(YYLTYPE *loc, context_t *mmss, config_t *conf, const char *filename, const char *s) { - mmss->logf(LOG_ERR, "config error: %s at %s:%i:%i", s, filename, loc->first_line, loc->first_column); +void mmss_config_error(YYLTYPE *loc, const std::shared_ptr<config_t> &conf, const char *filename, const char *s) { + conf->get_context()->logf(LOG_ERR, "config error: %s at %s:%i:%i", s, filename, loc->first_line, loc->first_column); } diff --git a/mmss/context.cpp b/mmss/context.cpp index 4c434f7..349c6cf 100644 --- a/mmss/context.cpp +++ b/mmss/context.cpp @@ -96,9 +96,7 @@ void context_t::run(int argc, char *argv[]) { if (!proto) std::exit(1); - config_t conf = {}; - - //read_config(this, &conf, "babel_test.mmss"); + std::shared_ptr<const config_t> conf = config_t::read(this, "babel_test.mmss"); std::shared_ptr<network_t> net0 = std::make_shared<network_t>("net0"); std::shared_ptr<network_t> net1 = std::make_shared<network_t>("net1"); |