]> git.tdb.fi Git - libs/datafile.git/blob - tool/tool.cpp
Fix some minor mistakes
[libs/datafile.git] / tool / tool.cpp
1 #include <msp/core/getopt.h>
2 #include <msp/io/buffered.h>
3 #include <msp/io/console.h>
4 #include <msp/io/file.h>
5 #include <msp/datafile/parser.h>
6 #include <msp/datafile/statement.h>
7 #include <msp/datafile/writer.h>
8 #include "compiler.h"
9 #include "tool.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 DataTool::DataTool(int argc, char **argv):
15         in_fn("-"),
16         out_fn("-"),
17         binary(false),
18         compile(false),
19         float_size(0),
20         compress(false)
21 {
22         GetOpt getopt;
23         getopt.add_option('b', "binary", binary, GetOpt::NO_ARG);
24         getopt.add_option('c', "compile", compile, GetOpt::NO_ARG);
25         getopt.add_option('f', "float-size", float_size, GetOpt::REQUIRED_ARG);
26         getopt.add_option('o', "output", out_fn, GetOpt::REQUIRED_ARG);
27         getopt.add_option('z', "compress", compress, GetOpt::NO_ARG);
28         getopt(argc, argv);
29
30         const vector<string> &args = getopt.get_args();
31         if(!args.empty())
32                 in_fn = args[0];
33 }
34
35 int DataTool::main()
36 {
37         IO::Base *in;
38         if(in_fn=="-")
39                 in = &IO::cin;
40         else
41                 in = new IO::File(in_fn);
42
43         IO::Base *out;
44         if(out_fn=="-")
45                 out = &IO::cout;
46         else
47                 out = new IO::File(out_fn, IO::M_WRITE);
48
49         {
50                 IO::Buffered in_buf(*in);
51                 DataFile::Parser parser(in_buf, in_fn);
52                 IO::Buffered out_buf(*out);
53                 DataFile::Writer writer(out_buf);
54                 if(compress)
55                         writer.set_compressed();
56                 if(binary)
57                         writer.set_binary(true);
58                 if(float_size)
59                         writer.set_float_precision(float_size);
60
61                 if(compile)
62                 {
63                         Compiler compiler(writer);
64                         compiler.load(parser);
65                 }
66                 else
67                 {
68                         while(parser)
69                         {
70                                 DataFile::Statement st = parser.parse();
71                                 if(st.valid)
72                                 {
73                                         writer.write(st);
74                                         out_buf.flush();
75                                 }
76                         }
77                 }
78         }
79
80         if(in!=&IO::cin)
81                 delete in;
82         if(out!=&IO::cout)
83                 delete out;
84
85         return 0;
86 }