From 6dd94a7fe90c6467024685fbac769067ddb74688 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 4 Feb 2008 15:00:02 +0000 Subject: [PATCH] Use libmspio instead of C++ iostreams --- Build | 1 + source/binarywriter.cpp | 2 +- source/binarywriter.h | 2 +- source/input.cpp | 2 +- source/input.h | 8 ++++---- source/loader.h | 15 +++++++-------- source/parser.cpp | 2 +- source/parser.h | 3 +-- source/textwriter.cpp | 19 ++++++++++--------- source/textwriter.h | 2 +- source/writer.cpp | 2 +- source/writer.h | 6 +++--- source/writermode.h | 6 +++--- tool.cpp | 23 +++++++++++++---------- 14 files changed, 48 insertions(+), 45 deletions(-) diff --git a/Build b/Build index b400475..9012c60 100644 --- a/Build +++ b/Build @@ -7,6 +7,7 @@ package "mspdatafile" require "mspcore"; require "mspstrings"; + require "mspio"; library "mspdatafile" { diff --git a/source/binarywriter.cpp b/source/binarywriter.cpp index 75cdded..4fc35c0 100644 --- a/source/binarywriter.cpp +++ b/source/binarywriter.cpp @@ -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) diff --git a/source/binarywriter.h b/source/binarywriter.h index 691ed70..118f66b 100644 --- a/source/binarywriter.h +++ b/source/binarywriter.h @@ -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); diff --git a/source/input.cpp b/source/input.cpp index c421f01..e03a921 100644 --- a/source/input.cpp +++ b/source/input.cpp @@ -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) diff --git a/source/input.h b/source/input.h index e03dea1..eeb1840 100644 --- a/source/input.h +++ b/source/input.h @@ -7,7 +7,7 @@ Distributed under the LGPL #ifndef MSP_DATAFILE_INPUT_H_ #define MSP_DATAFILE_INPUT_H_ -#include +#include 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 ∈ + IO::Base ∈ unsigned line; int next; }; diff --git a/source/loader.h b/source/loader.h index 749c104..a7d2dbd 100644 --- a/source/loader.h +++ b/source/loader.h @@ -8,8 +8,9 @@ Distributed under the LGPL #ifndef MSP_DATAFILE_LOADER_H_ #define MSP_DATAFILE_LOADER_H_ -#include #include +#include +#include #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 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 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); diff --git a/source/parser.cpp b/source/parser.cpp index 82ac006..06e5a8c 100644 --- a/source/parser.cpp +++ b/source/parser.cpp @@ -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), diff --git a/source/parser.h b/source/parser.h index c830c3c..814fd27 100644 --- a/source/parser.h +++ b/source/parser.h @@ -8,7 +8,6 @@ Distributed under the LGPL #ifndef MSP_DATAFILE_PARSER_H_ #define MSP_DATAFILE_PARSER_H_ -#include #include #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(); /** diff --git a/source/textwriter.cpp b/source/textwriter.cpp index def744c..890c417 100644 --- a/source/textwriter.cpp +++ b/source/textwriter.cpp @@ -5,6 +5,7 @@ Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#include #include #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<get_type()==STRING) - out<<'\"'<get_raw(), false)<<'\"'; + IO::print(out, "\"%s\"", c_escape(i->get_raw(), false)); else if(i->get_type()==BOOLEAN) - out<<(i->get() ? "true" : "false"); + out.write(i->get() ? "true" : "false"); else - out<get_raw(); + out.write(i->get_raw()); } if(!st.sub.empty()) { - out<<'\n'<::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i) write_(*i, level+1); - out< -#include +#include #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 diff --git a/source/writermode.h b/source/writermode.h index e5853ce..5014f25 100644 --- a/source/writermode.h +++ b/source/writermode.h @@ -8,7 +8,7 @@ Distributed under the LGPL #ifndef MSP_DATAFILE_WRITERMODE_H_ #define MSP_DATAFILE_WRITERMODE_H_ -#include +#include 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() { } diff --git a/tool.cpp b/tool.cpp index 8ca4f74..68c0fdc 100644 --- a/tool.cpp +++ b/tool.cpp @@ -1,8 +1,9 @@ /* $Id$ */ -#include #include #include #include +#include +#include #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; -- 2.45.2