1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-23 00:00:01 +01:00
VILLASnode/src/villas-convert.cpp

177 lines
4 KiB
C++
Raw Normal View History

/** Convert between samples IO formats
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2020-01-20 17:17:00 +01:00
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* 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, either version 3 of the License, or
* any later version.
*
* 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @addtogroup tools Test and debug tools
* @{
*********************************************************************************/
2018-07-03 21:04:28 +02:00
#include <iostream>
2021-05-10 00:12:30 +02:00
#include <unistd.h>
2018-07-03 21:04:28 +02:00
#include <villas/tool.hpp>
2018-10-20 14:20:51 +02:00
#include <villas/utils.hpp>
#include <villas/log.hpp>
2021-05-10 00:12:30 +02:00
#include <villas/format.hpp>
#include <villas/sample.h>
2018-10-20 14:20:51 +02:00
#include <villas/exceptions.hpp>
2021-06-21 16:11:42 -04:00
#include <villas/node/config.h>
#include <villas/memory.h>
2018-10-20 14:20:51 +02:00
using namespace villas;
namespace villas {
namespace node {
namespace tools {
class Convert : public Tool {
2019-04-17 18:46:18 +02:00
public:
Convert(int argc, char *argv[]) :
Tool(argc, argv, "convert"),
dtypes("64f")
{
2019-04-17 18:46:18 +02:00
int ret;
ret = memory_init(DEFAULT_NR_HUGEPAGES);
if (ret)
throw RuntimeError("Failed to initialize memory");
for (unsigned i = 0; i < ARRAY_LEN(dirs); i++) {
2020-01-09 09:38:20 +01:00
dirs[i].name = i == 0 ? "in" : "out";
dirs[i].format = "villas.human";
}
}
protected:
std::string dtypes;
struct {
std::string name;
std::string format;
2021-05-10 00:12:30 +02:00
Format *formatter;
} dirs[2];
void usage()
{
std::cout << "Usage: villas-convert [OPTIONS]" << std::endl
<< " OPTIONS are:" << std::endl
<< " -i FMT set the input format" << std::endl
<< " -o FMT set the output format" << std::endl
<< " -t DT the data-type format string" << std::endl
<< " -d LVL set debug log level to LVL" << std::endl
<< " -h show this usage information" << std::endl
<< " -V show the version of the tool" << std::endl << std::endl;
printCopyright();
}
void parse()
{
2019-04-17 18:46:18 +02:00
/* Parse optional command line arguments */
int c;
while ((c = getopt(argc, argv, "Vhd:i:o:t:")) != -1) {
switch (c) {
case 'V':
printVersion();
2019-04-17 18:46:18 +02:00
exit(EXIT_SUCCESS);
case 'i':
dirs[0].format = optarg;
2019-04-17 18:46:18 +02:00
break;
case 'o':
dirs[1].format = optarg;
2019-04-17 18:46:18 +02:00
break;
case 't':
dtypes = optarg;
break;
case 'd':
logging.setLevel(optarg);
break;
case 'h':
case '?':
usage();
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
}
}
2018-08-27 11:23:21 +02:00
2019-04-17 18:46:18 +02:00
if (argc != optind) {
usage();
exit(EXIT_FAILURE);
}
}
2018-08-27 11:23:21 +02:00
int main()
{
int ret;
2019-04-17 18:46:18 +02:00
for (unsigned i = 0; i < ARRAY_LEN(dirs); i++) {
2021-05-10 00:12:30 +02:00
json_t *json_format;
json_error_t err;
std::string format = dirs[i].format;
/* Try parsing format config as JSON */
json_format = json_loads(format.c_str(), 0, &err);
dirs[i].formatter = json_format
? FormatFactory::make(json_format)
: FormatFactory::make(format);
if (!dirs[i].formatter)
throw RuntimeError("Failed to initialize format: {}", dirs[i].name);
dirs[i].formatter->start(dtypes);
2019-04-17 18:46:18 +02:00
}
2019-04-17 18:46:18 +02:00
struct sample *smp = sample_alloc_mem(DEFAULT_SAMPLE_LENGTH);
2021-05-10 00:12:30 +02:00
while (true) {
ret = dirs[0].formatter->scan(stdin, smp);
2019-04-17 18:46:18 +02:00
if (ret == 0)
continue;
2021-05-10 00:12:30 +02:00
else if (ret < 0)
2018-10-20 14:20:51 +02:00
break;
2021-05-10 00:12:30 +02:00
dirs[1].formatter->print(stdout, smp);
}
2021-05-10 00:12:30 +02:00
for (unsigned i = 0; i < ARRAY_LEN(dirs); i++)
delete dirs[i].formatter;
2019-04-17 18:46:18 +02:00
return 0;
}
};
2020-06-15 22:16:38 +02:00
} /* namespace tools */
} /* namespace node */
} /* namespace villas */
int main(int argc, char *argv[])
{
2019-06-18 17:52:54 +02:00
villas::node::tools::Convert t(argc, argv);
return t.run();
}
/** @} */