require "mspcore";
require "mspstrings";
+ require "mspio";
library "mspdatafile"
{
namespace Msp {
namespace DataFile {
-BinaryWriter::BinaryWriter(ostream &o):
+BinaryWriter::BinaryWriter(IO::Base &o):
WriterMode(o),
next_st_id(3),
next_enum_id(1)
unsigned next_enum_id;
public:
- BinaryWriter(std::ostream &o);
+ BinaryWriter(IO::Base &o);
void write(const Statement &st);
private:
void write_(const Statement &st);
namespace Msp {
namespace DataFile {
-Input::Input(std::istream &i):
+Input::Input(IO::Base &i):
in(i),
line(1),
next(-1)
#ifndef MSP_DATAFILE_INPUT_H_
#define MSP_DATAFILE_INPUT_H_
-#include <istream>
+#include <msp/io/base.h>
namespace Msp {
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;
};
#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"
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);
}
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);
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),
#ifndef MSP_DATAFILE_PARSER_H_
#define MSP_DATAFILE_PARSER_H_
-#include <istream>
#include <string>
#include "input.h"
ParserMode *mode;
public:
- Parser(std::istream &i, const std::string &s);
+ Parser(IO::Base &i, const std::string &s);
~Parser();
/**
Distributed under the LGPL
*/
+#include <msp/io/print.h>
#include <msp/strings/utils.h>
#include "statement.h"
#include "textwriter.h"
namespace Msp {
namespace DataFile {
-TextWriter::TextWriter(ostream &o):
+TextWriter::TextWriter(IO::Base &o):
WriterMode(o)
{ }
{
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
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);
namespace Msp {
namespace DataFile {
-Writer::Writer(ostream &o):
+Writer::Writer(IO::Base &o):
out(o),
mode(new TextWriter(out)),
binary(false)
#define MSP_DATAFILE_WRITER_H_
#include <map>
-#include <ostream>
+#include <msp/io/base.h>
#include "binarydict.h"
namespace Msp {
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
#ifndef MSP_DATAFILE_WRITERMODE_H_
#define MSP_DATAFILE_WRITERMODE_H_
-#include <ostream>
+#include <msp/io/base.h>
namespace Msp {
namespace DataFile {
class WriterMode
{
protected:
- std::ostream &out;
+ IO::Base &out;
- WriterMode(std::ostream &o): out(o) { }
+ WriterMode(IO::Base &o): out(o) { }
public:
virtual ~WriterMode() { }
/* $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"
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);
writer.write(st);
}
- if(out!=&cout)
+ //if(out!=&cout)
delete out;
return 0;