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