2017-03-27 12:38:19 +02:00
|
|
|
/** Convert old style config to new JSON format.
|
|
|
|
*
|
2022-12-14 17:41:58 +01:00
|
|
|
* @author Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
|
2022-07-04 18:20:03 +02:00
|
|
|
* @license Apache 2.0
|
2017-03-27 12:38:19 +02:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2017-11-22 15:53:19 +01:00
|
|
|
#include <libgen.h>
|
2018-07-03 21:04:28 +02:00
|
|
|
#include <iostream>
|
2017-03-27 12:38:19 +02:00
|
|
|
#include <jansson.h>
|
|
|
|
#include <libconfig.h>
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/config.hpp>
|
2019-03-26 07:01:10 +01:00
|
|
|
#include <villas/config_helper.hpp>
|
2018-10-20 14:22:43 +02:00
|
|
|
#include <villas/utils.hpp>
|
2019-04-19 14:52:52 +02:00
|
|
|
#include <villas/tool.hpp>
|
2017-03-27 12:38:19 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
namespace tools {
|
2017-03-27 12:38:19 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
class Config2Json : public Tool {
|
2017-03-27 12:38:19 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
public:
|
|
|
|
Config2Json(int argc, char *argv[]) :
|
|
|
|
Tool(argc, argv, "conf2json")
|
|
|
|
{ }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
void usage()
|
|
|
|
{
|
|
|
|
std::cout << "Usage: conf2json input.conf > output.json" << std::endl << std::endl;
|
|
|
|
|
|
|
|
printCopyright();
|
2017-03-27 12:38:19 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
config_t cfg;
|
|
|
|
config_setting_t *cfg_root;
|
|
|
|
json_t *json;
|
|
|
|
|
|
|
|
if (argc != 2) {
|
|
|
|
usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-11-22 15:53:19 +01:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
FILE *f = fopen(argv[1], "r");
|
2021-05-10 00:12:30 +02:00
|
|
|
if (f == nullptr)
|
2019-04-19 14:52:52 +02:00
|
|
|
return -1;
|
2017-11-22 15:53:19 +01:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
const char *confdir = dirname(argv[1]);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
config_init(&cfg);
|
2017-11-22 15:53:19 +01:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
config_set_include_dir(&cfg, confdir);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
ret = config_read(&cfg, f);
|
|
|
|
if (ret != CONFIG_TRUE)
|
|
|
|
return -2;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
cfg_root = config_root_setting(&cfg);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
json = config_to_json(cfg_root);
|
|
|
|
if (!json)
|
|
|
|
return -3;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
ret = json_dumpf(json, stdout, JSON_INDENT(2)); fflush(stdout);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
json_decref(json);
|
|
|
|
config_destroy(&cfg);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-15 22:16:38 +02:00
|
|
|
} /* namespace tools */
|
|
|
|
} /* namespace node */
|
|
|
|
} /* namespace villas */
|
2019-04-19 14:52:52 +02:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2019-06-18 17:52:54 +02:00
|
|
|
villas::node::tools::Config2Json t(argc, argv);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
return t.run();
|
2017-04-15 20:38:58 +02:00
|
|
|
}
|