]> git.tdb.fi Git - libs/datafile.git/blobdiff - tool/tool.cpp
Use custom encoding for floats in binary format
[libs/datafile.git] / tool / tool.cpp
index ca415d30a0d9dc7d996d84b62021be90bc054f9e..5f9febeedda26d87a749551de4035d7ee6e07a30 100644 (file)
@@ -1,10 +1,3 @@
-/* $Id$
-
-This file is part of libmspdatafile
-Copyright © 2008  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #include <msp/core/getopt.h>
 #include <msp/io/buffered.h>
 #include <msp/io/console.h>
@@ -12,6 +5,7 @@ Distributed under the LGPL
 #include <msp/datafile/parser.h>
 #include <msp/datafile/statement.h>
 #include <msp/datafile/writer.h>
+#include "compiler.h"
 #include "tool.h"
 
 using namespace std;
@@ -19,31 +13,36 @@ using namespace Msp;
 
 DataTool::DataTool(int argc, char **argv):
        in_fn("-"),
-       out_fn("-")
+       out_fn("-"),
+       binary(false),
+       compile(false),
+       float_size(0)
 {
        GetOpt getopt;
-       getopt.add_option('o', "output", out_fn, GetOpt::REQUIRED_ARG);
        getopt.add_option('b', "binary", binary, GetOpt::NO_ARG);
+       getopt.add_option('c', "compile", compile, GetOpt::NO_ARG);
+       getopt.add_option('f', "float-size", float_size, GetOpt::REQUIRED_ARG);
+       getopt.add_option('o', "output", out_fn, GetOpt::REQUIRED_ARG);
        getopt(argc, argv);
 
-       const vector<string> &args=getopt.get_args();
+       const vector<string> &args = getopt.get_args();
        if(!args.empty())
-               in_fn=args[0];
+               in_fn = args[0];
 }
 
 int DataTool::main()
 {
        IO::Base *in;
        if(in_fn=="-")
-               in=&IO::cin;
+               in = &IO::cin;
        else
-               in=new IO::File(in_fn);
+               in = new IO::File(in_fn);
 
        IO::Base *out;
        if(out_fn=="-")
-               out=&IO::cout;
+               out = &IO::cout;
        else
-               out=new IO::File(out_fn, IO::M_WRITE);
+               out = new IO::File(out_fn, IO::M_WRITE);
 
        {
                IO::Buffered in_buf(*in);
@@ -52,14 +51,24 @@ int DataTool::main()
                DataFile::Writer writer(out_buf);
                if(binary)
                        writer.set_binary(true);
+               if(float_size)
+                       writer.set_float_precision(float_size);
 
-               while(parser)
+               if(compile)
                {
-                       DataFile::Statement st=parser.parse();
-                       if(st.valid)
+                       Compiler compiler(writer);
+                       compiler.load(parser);
+               }
+               else
+               {
+                       while(parser)
                        {
-                               writer.write(st);
-                               out_buf.flush();
+                               DataFile::Statement st = parser.parse();
+                               if(st.valid)
+                               {
+                                       writer.write(st);
+                                       out_buf.flush();
+                               }
                        }
                }
        }
@@ -71,5 +80,3 @@ int DataTool::main()
 
        return 0;
 }
-
-Application::RegApp<DataTool> DataTool::reg;