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