]> git.tdb.fi Git - libs/datafile.git/blob - tool/tool.cpp
5469106f50322b7497ea9626c5a4b2400187aa1c
[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 "compiler.h"
8 #include "tool.h"
9
10 using namespace std;
11 using namespace Msp;
12
13 DataTool::DataTool(int argc, char **argv):
14         out_fn("-"),
15         binary(false),
16         compile(false),
17         float_size(0),
18         compress(false),
19         debug(false)
20 {
21         GetOpt getopt;
22         getopt.add_option('b', "binary", binary, GetOpt::NO_ARG);
23         getopt.add_option('c', "compile", compile, GetOpt::NO_ARG);
24         getopt.add_option('f', "float-size", float_size, GetOpt::REQUIRED_ARG);
25         getopt.add_option('g', "debug", debug, GetOpt::NO_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         in_fns = getopt.get_args();
31         if(in_fns.empty())
32                 in_fns.push_back("-");
33 }
34
35 int DataTool::main()
36 {
37         if(compile)
38                 do_compile();
39         else
40                 do_transfer();
41
42         return 0;
43 }
44
45 void DataTool::do_transfer()
46 {
47         IO::Base *out = open_output(out_fn);
48         DataFile::Writer *writer = create_writer(*out);
49
50         for(vector<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
51         {
52                 IO::Base *in = open_input(*i);
53                 DataFile::Parser parser(*in, *i);
54
55                 while(parser)
56                 {
57                         DataFile::Statement st = parser.parse(true);
58                         if(st.valid && (st.keyword.compare(0, 2, "__") || st.keyword=="__src" || debug))
59                                 writer->write(st);
60                 }
61
62                 delete in;
63         }
64
65         delete writer;
66         delete out;
67 }
68
69 void DataTool::do_compile()
70 {
71         IO::Base *out = open_output(out_fn);
72         DataFile::Writer *writer = create_writer(*out);
73
74         Compiler compiler(*writer);
75         for(vector<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
76         {
77                 IO::Base *in = open_input(*i);
78                 DataFile::Parser parser(*in, *i);
79                 compiler.load(parser);
80                 delete in;
81         }
82
83         delete writer;
84         delete out;
85 }
86
87 IO::Base *DataTool::open_output(const string &fn)
88 {
89         if(fn=="-")
90                 return new IO::Buffered(IO::cout);
91         else
92                 return new IO::BufferedFile(fn, IO::M_WRITE);
93 }
94
95 IO::Base *DataTool::open_input(const string &fn)
96 {
97         if(fn=="-")
98                 return new IO::Buffered(IO::cin);
99         else
100                 return new IO::BufferedFile(fn, IO::M_READ);
101 }
102
103 DataFile::Writer *DataTool::create_writer(IO::Base &out)
104 {
105         DataFile::Writer *writer = new DataFile::Writer(out);
106         if(compress)
107                 writer->set_compressed();
108         if(binary)
109                 writer->set_binary(true);
110         if(float_size)
111                 writer->set_float_precision(float_size);
112         return writer;
113 }