]> git.tdb.fi Git - libs/datafile.git/blob - tool/tool.cpp
Move mspdatatool source to its own directory
[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 "tool.h"
16
17 using namespace std;
18 using namespace Msp;
19
20 DataTool::DataTool(int argc, char **argv):
21         in_fn("-"),
22         out_fn("-")
23 {
24         GetOpt getopt;
25         getopt.add_option('o', "output", out_fn, GetOpt::REQUIRED_ARG);
26         getopt.add_option('b', "binary", binary, GetOpt::NO_ARG);
27         getopt(argc, argv);
28
29         const vector<string> &args=getopt.get_args();
30         if(!args.empty())
31                 in_fn=args[0];
32 }
33
34 int DataTool::main()
35 {
36         IO::Base *in;
37         if(in_fn=="-")
38                 in=&IO::cin;
39         else
40                 in=new IO::File(in_fn);
41
42         IO::Base *out;
43         if(out_fn=="-")
44                 out=&IO::cout;
45         else
46                 out=new IO::File(out_fn, IO::M_WRITE);
47
48         {
49                 IO::Buffered in_buf(*in);
50                 DataFile::Parser parser(in_buf, in_fn);
51                 IO::Buffered out_buf(*out);
52                 DataFile::Writer writer(out_buf);
53                 if(binary)
54                         writer.set_binary(true);
55
56                 while(parser)
57                 {
58                         DataFile::Statement st=parser.parse();
59                         if(st.valid)
60                         {
61                                 writer.write(st);
62                                 out_buf.flush();
63                         }
64                 }
65         }
66
67         if(in!=&IO::cin)
68                 delete in;
69         if(out!=&IO::cout)
70                 delete out;
71
72         return 0;
73 }
74
75 Application::RegApp<DataTool> DataTool::reg;