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