X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=blobdiff_plain;f=tool%2Ftool.cpp;fp=tool%2Ftool.cpp;h=ca415d30a0d9dc7d996d84b62021be90bc054f9e;hp=0000000000000000000000000000000000000000;hb=db9c49893c2a9475cb5efa4a53bc481a5f66231f;hpb=11ed2b907c9b031b57c3d4fc5491fdc3460303c9 diff --git a/tool/tool.cpp b/tool/tool.cpp new file mode 100644 index 0000000..ca415d3 --- /dev/null +++ b/tool/tool.cpp @@ -0,0 +1,75 @@ +/* $Id$ + +This file is part of libmspdatafile +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#include +#include +#include +#include +#include +#include +#include +#include "tool.h" + +using namespace std; +using namespace Msp; + +DataTool::DataTool(int argc, char **argv): + in_fn("-"), + out_fn("-") +{ + GetOpt getopt; + getopt.add_option('o', "output", out_fn, GetOpt::REQUIRED_ARG); + getopt.add_option('b', "binary", binary, GetOpt::NO_ARG); + getopt(argc, argv); + + const vector &args=getopt.get_args(); + if(!args.empty()) + in_fn=args[0]; +} + +int DataTool::main() +{ + IO::Base *in; + if(in_fn=="-") + in=&IO::cin; + else + in=new IO::File(in_fn); + + IO::Base *out; + if(out_fn=="-") + out=&IO::cout; + else + out=new IO::File(out_fn, IO::M_WRITE); + + { + IO::Buffered in_buf(*in); + DataFile::Parser parser(in_buf, in_fn); + IO::Buffered out_buf(*out); + DataFile::Writer writer(out_buf); + if(binary) + writer.set_binary(true); + + while(parser) + { + DataFile::Statement st=parser.parse(); + if(st.valid) + { + writer.write(st); + out_buf.flush(); + } + } + } + + if(in!=&IO::cin) + delete in; + if(out!=&IO::cout) + delete out; + + return 0; +} + +Application::RegApp DataTool::reg;