Clean up config_t class

This commit is contained in:
Matthias Schiffer 2013-07-28 19:33:40 +02:00
parent d5c57ad133
commit a713d2931e
4 changed files with 35 additions and 35 deletions

View file

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

View file

@ -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;
config_t(context_t *mmss0) : mmss(mmss0) {}
public:
context_t* get_context() const {
return mmss;
}
void add_network(const char *name);
static std::shared_ptr<const config_t> read(context_t *mmss, const char *filename);
};
namespace Config {
void add_network(context_t *mmss, config_t *conf, const char *name);
}
bool read_config(context_t *mmss, config_t *conf, const char *filename);
}

View file

@ -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);
}

View file

@ -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");