]> git.tdb.fi Git - libs/datafile.git/commitdiff
Use libmspio instead of C++ iostreams
authorMikko Rasa <tdb@tdb.fi>
Mon, 4 Feb 2008 15:00:02 +0000 (15:00 +0000)
committerMikko Rasa <tdb@tdb.fi>
Mon, 4 Feb 2008 15:00:02 +0000 (15:00 +0000)
14 files changed:
Build
source/binarywriter.cpp
source/binarywriter.h
source/input.cpp
source/input.h
source/loader.h
source/parser.cpp
source/parser.h
source/textwriter.cpp
source/textwriter.h
source/writer.cpp
source/writer.h
source/writermode.h
tool.cpp

diff --git a/Build b/Build
index b400475a0c3f58136111ede50a854bfa613181ec..9012c602fc3facaae6f66570468c9aad1b4fdb30 100644 (file)
--- a/Build
+++ b/Build
@@ -7,6 +7,7 @@ package "mspdatafile"
 
        require "mspcore";
        require "mspstrings";
+       require "mspio";
 
        library "mspdatafile"
        {
index 75cdded3c4de0bb9ce8f6a776be3a13c36aa48c5..4fc35c00449e380b7562fe33ef66dd4cca4e20a5 100644 (file)
@@ -13,7 +13,7 @@ using namespace std;
 namespace Msp {
 namespace DataFile {
 
-BinaryWriter::BinaryWriter(ostream &o):
+BinaryWriter::BinaryWriter(IO::Base &o):
        WriterMode(o),
        next_st_id(3),
        next_enum_id(1)
index 691ed701d3bcaa4be9c5641aeebddfb0391fabb9..118f66b0d6cebdf75ea1d985a60e2ca66d9c6d50 100644 (file)
@@ -30,7 +30,7 @@ private:
        unsigned next_enum_id;
 
 public:
-       BinaryWriter(std::ostream &o);
+       BinaryWriter(IO::Base &o);
        void write(const Statement &st);
 private:
        void write_(const Statement &st);
index c421f01b977c36797d95c41d60f3787f4bdf3ac4..e03a92126944703234a8d6fa330c4eba66cacb5e 100644 (file)
@@ -9,7 +9,7 @@ Distributed under the LGPL
 namespace Msp {
 namespace DataFile {
 
-Input::Input(std::istream &i):
+Input::Input(IO::Base &i):
        in(i),
        line(1),
        next(-1)
index e03dea1e839714e23416b8fed7fea7cb68f8ed97..eeb18407ec2848927cdef93507180987c71d4cbe 100644 (file)
@@ -7,7 +7,7 @@ Distributed under the LGPL
 #ifndef MSP_DATAFILE_INPUT_H_
 #define MSP_DATAFILE_INPUT_H_
 
-#include <istream>
+#include <msp/io/base.h>
 
 namespace Msp {
 namespace DataFile {
@@ -15,13 +15,13 @@ namespace DataFile {
 class Input
 {   
 public:
-       Input(std::istream &); 
+       Input(IO::Base &); 
        int get();
        int peek();
        unsigned get_line_number() const { return line; }
-       operator bool() const { return in; }
+       operator bool() const { return !in.eof(); }
 private:
-       std::istream &in;
+       IO::Base &in;
        unsigned line;
        int next;
 };  
index 749c1047cd1a762084adb91d37956671fe8d6745..a7d2dbdfbc9eb70f191e9e9c979f355a55174315 100644 (file)
@@ -8,8 +8,9 @@ Distributed under the LGPL
 #ifndef MSP_DATAFILE_LOADER_H_
 #define MSP_DATAFILE_LOADER_H_
 
-#include <fstream>
 #include <map>
+#include <msp/io/buffered.h>
+#include <msp/io/file.h>
 #include "except.h"
 #include "parser.h"
 #include "statement.h"
@@ -361,11 +362,10 @@ Loads an object from a file.  The object must have a public Loader class.
 template<typename T>
 void load(T &obj, const std::string &fn)
 {
-       std::ifstream in(fn.c_str());
-       if(!in)
-               throw Exception("Couldn't open "+fn);
+       IO::File in(fn);
+       IO::Buffered buf(in);
 
-       Parser parser(in, fn);
+       Parser parser(buf, fn);
        typename T::Loader loader(obj);
        loader.load(parser);
 }
@@ -373,9 +373,8 @@ void load(T &obj, const std::string &fn)
 template<typename T, typename U>
 void load(T &obj, const std::string &fn, U arg)
 {
-       std::ifstream in(fn.c_str());
-       if(!in)
-               throw Exception("Couldn't open "+fn);
+       IO::File in(fn);
+       IO::Buffered buf(in);
 
        Parser parser(in, fn);
        typename T::Loader loader(obj, arg);
index 82ac0069b95d4f22e3d0de1ab0c0b66b04c76423..06e5a8c33745e78e3855a32f4148e939e93e390f 100644 (file)
@@ -16,7 +16,7 @@ using namespace std;
 namespace Msp {
 namespace DataFile {
 
-Parser::Parser(istream &i, const string &s):
+Parser::Parser(IO::Base &i, const string &s):
        in(i),
        src(s),
        good(true),
index c830c3c8367f5c082788ad864f23eed84266439a..814fd2771b1240ccb14a86c4c8d5576f26c99d4b 100644 (file)
@@ -8,7 +8,6 @@ Distributed under the LGPL
 #ifndef MSP_DATAFILE_PARSER_H_
 #define MSP_DATAFILE_PARSER_H_
 
-#include <istream>
 #include <string>
 #include "input.h"
 
@@ -33,7 +32,7 @@ private:
        ParserMode  *mode;
 
 public:
-       Parser(std::istream &i, const std::string &s);
+       Parser(IO::Base &i, const std::string &s);
        ~Parser();
 
        /**
index def744c33b06ff945c4cfccbe28025bb43402872..890c4172162ea17631d8bc69fad7be8ce3a28788 100644 (file)
@@ -5,6 +5,7 @@ Copyright © 2006  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
+#include <msp/io/print.h>
 #include <msp/strings/utils.h>
 #include "statement.h"
 #include "textwriter.h"
@@ -14,7 +15,7 @@ using namespace std;
 namespace Msp {
 namespace DataFile {
 
-TextWriter::TextWriter(ostream &o):
+TextWriter::TextWriter(IO::Base &o):
        WriterMode(o)
 { }
 
@@ -27,25 +28,25 @@ void TextWriter::write_(const Statement &st, unsigned level)
 {
        string indent(level, '\t');
 
-       out<<indent<<st.keyword;
+       IO::print(out, "%s%s", indent, st.keyword);
        for(ValueArray::const_iterator i=st.args.begin(); i!=st.args.end(); ++i)
        {
-               out<<' ';
+               out.put(' ');
                if(i->get_type()==STRING)
-                       out<<'\"'<<c_escape(i->get_raw(), false)<<'\"';
+                       IO::print(out, "\"%s\"", c_escape(i->get_raw(), false));
                else if(i->get_type()==BOOLEAN)
-                       out<<(i->get<bool>() ? "true" : "false");
+                       out.write(i->get<bool>() ? "true" : "false");
                else
-                       out<<i->get_raw();
+                       out.write(i->get_raw());
        }
        if(!st.sub.empty())
        {
-               out<<'\n'<<indent<<"{\n";
+               IO::print(out, "\n%s{\n", indent);
                for(list<Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
                        write_(*i, level+1);
-               out<<indent<<'}';
+               IO::print(out, "%s}", indent);
        }
-       out<<";\n";
+       out.write(";\n", 2);
 }
 
 } // namespace DataFile
index 5c83b7a59fa9386e389c4e527989fe6e1c1f746b..de6de457af516e36757ae14f16de3085e79d06a2 100644 (file)
@@ -16,7 +16,7 @@ namespace DataFile {
 class TextWriter: public WriterMode
 {
 public:
-       TextWriter(std::ostream &o);
+       TextWriter(IO::Base &o);
        void write(const Statement &st);
 private:
        void write_(const Statement &st, unsigned);
index cdd78b62e18e5e636994f0bf674da9aba39d3676..0da77bc0304dc471f24a368399c86cfbcabff391 100644 (file)
@@ -15,7 +15,7 @@ using namespace std;
 namespace Msp {
 namespace DataFile {
 
-Writer::Writer(ostream &o):
+Writer::Writer(IO::Base &o):
        out(o),
        mode(new TextWriter(out)),
        binary(false)
index 698bade8f650cf45198ee92ac147f18a10540b02..e62e8bce2851313d6a9cb10e4df86531920dbd4e 100644 (file)
@@ -9,7 +9,7 @@ Distributed under the LGPL
 #define MSP_DATAFILE_WRITER_H_
 
 #include <map>
-#include <ostream>
+#include <msp/io/base.h>
 #include "binarydict.h"
 
 namespace Msp {
@@ -24,12 +24,12 @@ Frontend for writing data.
 class Writer
 {
 private:
-       std::ostream &out;
+       IO::Base &out;
        WriterMode *mode;
        bool binary;
 
 public:
-       Writer(std::ostream &o);
+       Writer(IO::Base &o);
 
        /**
        Writes a statement to the output.  This function always writes a complete
index e5853ceafc88796f2446a1318af9567d149b34d8..5014f25cd1801398aa23f3fdc89190a0f3a9c9e9 100644 (file)
@@ -8,7 +8,7 @@ Distributed under the LGPL
 #ifndef MSP_DATAFILE_WRITERMODE_H_
 #define MSP_DATAFILE_WRITERMODE_H_
 
-#include <ostream>
+#include <msp/io/base.h>
 
 namespace Msp {
 namespace DataFile {
@@ -18,9 +18,9 @@ class Statement;
 class WriterMode
 {
 protected:
-       std::ostream &out;
+       IO::Base &out;
 
-       WriterMode(std::ostream &o): out(o) { }
+       WriterMode(IO::Base &o): out(o) { }
 public:
        virtual ~WriterMode() { }
 
index 8ca4f746017a5466140fb292b57cd54644a0e31e..68c0fdccff40fada878072df672d55a212db0238 100644 (file)
--- a/tool.cpp
+++ b/tool.cpp
@@ -1,8 +1,9 @@
 /* $Id$ */
-#include <fstream>
 #include <iostream>
 #include <msp/core/application.h>
 #include <msp/core/getopt.h>
+#include <msp/io/buffered.h>
+#include <msp/io/file.h>
 #include "source/parser.h"
 #include "source/statement.h"
 #include "source/writer.h"
@@ -40,20 +41,22 @@ DataTool::DataTool(int argc, char **argv):
 
 int DataTool::main()
 {
-       istream *in;
+       IO::Base *in;
        if(in_fn=="-")
-               in=&cin;
+               throw Exception("stdin/out not supported at the moment");
        else
-               in=new ifstream(in_fn.c_str());
+               in=new IO::File(in_fn);
 
-       ostream *out;
+       IO::Base *out;
        if(out_fn=="-")
-               out=&cout;
+               throw Exception("stdin/out not supported at the moment");
        else
-               out=new ofstream(out_fn.c_str());
+               out=new IO::File(out_fn, IO::M_WRITE);
 
-       DataFile::Parser parser(*in, in_fn);
-       DataFile::Writer writer(*out);
+       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);
 
@@ -64,7 +67,7 @@ int DataTool::main()
                        writer.write(st);
        }
 
-       if(out!=&cout)
+       //if(out!=&cout)
                delete out;
 
        return 0;