]> git.tdb.fi Git - libs/datafile.git/blob - tool.cpp
Support const types with Collection properly
[libs/datafile.git] / tool.cpp
1 /* $Id$ */
2 #include <fstream>
3 #include <iostream>
4 #include <msp/core/application.h>
5 #include <msp/core/getopt.h>
6 #include "source/parser.h"
7 #include "source/statement.h"
8 #include "source/writer.h"
9
10 using namespace std;
11 using namespace Msp;
12
13 class DataTool: public Application
14 {
15 private:
16         string in_fn;
17         string out_fn;
18         bool binary;
19 public:
20         DataTool(int argc, char **argv);
21         int main();
22
23         static Application::RegApp<DataTool> reg;
24 };
25
26
27 DataTool::DataTool(int argc, char **argv):
28         in_fn("-"),
29         out_fn("-")
30 {
31         GetOpt getopt;
32         getopt.add_option('o', "output", out_fn, GetOpt::REQUIRED_ARG);
33         getopt.add_option('b', "binary", binary, GetOpt::NO_ARG);
34         getopt(argc, argv);
35
36         const vector<string> &args=getopt.get_args();
37         if(!args.empty())
38                 in_fn=args[0];
39 }
40
41 int DataTool::main()
42 {
43         istream *in;
44         if(in_fn=="-")
45                 in=&cin;
46         else
47                 in=new ifstream(in_fn.c_str());
48
49         ostream *out;
50         if(out_fn=="-")
51                 out=&cout;
52         else
53                 out=new ofstream(out_fn.c_str());
54
55         DataFile::Parser parser(*in, in_fn);
56         DataFile::Writer writer(*out);
57         if(binary)
58                 writer.set_binary(true);
59
60         while(parser)
61         {
62                 DataFile::Statement st=parser.parse();
63                 if(st.valid)
64                         writer.write(st);
65         }
66
67         if(out!=&cout)
68                 delete out;
69
70         return 0;
71 }
72
73 Application::RegApp<DataTool> DataTool::reg;