Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include <convert.hpp>
10 :
11 : #include <cmdline.hpp>
12 : #include <kdb.hpp>
13 : #include <modules.hpp>
14 : #include <toolexcept.hpp>
15 :
16 : #include <iostream>
17 :
18 : using namespace std;
19 : using namespace kdb;
20 : using namespace kdb::tools;
21 :
22 156 : ConvertCommand::ConvertCommand ()
23 : {
24 78 : }
25 :
26 0 : int ConvertCommand::execute (Cmdline const & cl)
27 : {
28 0 : size_t argc = cl.arguments.size ();
29 0 : if (argc != 0 && argc != 1 && argc != 2 && argc != 3 && argc != 4)
30 : {
31 0 : throw invalid_argument ("need 0 to 4 arguments");
32 : }
33 :
34 0 : string import_format = cl.format;
35 0 : if (argc > 0) import_format = cl.arguments[0];
36 :
37 0 : string export_format = cl.format;
38 0 : if (argc > 1) export_format = cl.arguments[1];
39 :
40 0 : string import_file = "/dev/stdin";
41 0 : if (argc > 2 && cl.arguments[2] != "-") import_file = cl.arguments[2];
42 :
43 0 : string export_file = "/dev/stdout";
44 0 : if (argc > 3 && cl.arguments[3] != "-") export_file = cl.arguments[3];
45 :
46 0 : if (cl.verbose)
47 : {
48 0 : cout << "converting from " << import_format << " to " << export_format << endl;
49 : }
50 :
51 0 : Modules modules;
52 0 : PluginPtr import_plugin = modules.load (import_format);
53 :
54 : // TODO: reuse import/export
55 : // to namespace dir
56 0 : PluginPtr export_plugin = modules.load (export_format);
57 :
58 0 : Key errorKey;
59 0 : KeySet keys;
60 :
61 0 : errorKey.setString (import_file);
62 0 : import_plugin->get (keys, errorKey);
63 :
64 0 : errorKey.setString (export_file);
65 0 : export_plugin->set (keys, errorKey);
66 :
67 0 : printWarnings (cerr, errorKey, cl.verbose, cl.debug);
68 0 : printError (cerr, errorKey, cl.verbose, cl.debug);
69 :
70 0 : return 0;
71 : }
72 :
73 156 : ConvertCommand::~ConvertCommand ()
74 : {
75 7242 : }
|