]> git.tdb.fi Git - libs/datafile.git/blob - tool.cpp
Fix binary format
[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         out_fn("-")
29 {
30         GetOpt getopt;
31         getopt.add_option('o', "output", out_fn, GetOpt::REQUIRED_ARG);
32         getopt.add_option('b', "binary", binary, GetOpt::NO_ARG);
33         getopt(argc, argv);
34
35         const vector<string> &args=getopt.get_args();
36         if(args.empty())
37                 throw UsageError("Must give input filename");
38
39         in_fn=args[0];
40 }
41
42 int DataTool::main()
43 {
44         ifstream in(in_fn.c_str());
45         if(!in)
46         {
47                 cerr<<"Couldn't open input file\n";
48                 return 1;
49         }
50
51         ostream *out;
52         if(out_fn=="-")
53                 out=&cout;
54         else
55                 out=new ofstream(out_fn.c_str());
56
57         DataFile::Parser parser(in, in_fn);
58         DataFile::Writer writer(*out);
59         if(binary)
60                 writer.set_binary(true);
61
62         while(parser)
63         {
64                 DataFile::Statement st=parser.parse();
65                 if(st.valid)
66                         writer.write(st);
67         }
68
69         if(out!=&cout)
70                 delete out;
71
72         return 0;
73 }
74
75 Application::RegApp<DataTool> DataTool::reg;