# HG changeset patch # User deba # Date 1133956650 0 # Node ID ffa7c6e96330f0a2d4c318205a1217f50e69d0ad # Parent 78b5ea23f0f1f91cda22dc4c78887960aef9ae58 Some bug fixes and improvments in the io classes diff -r 78b5ea23f0f1 -r ffa7c6e96330 lemon/bits/item_reader.h --- a/lemon/bits/item_reader.h Tue Dec 06 18:44:26 2005 +0000 +++ b/lemon/bits/item_reader.h Wed Dec 07 11:57:30 2005 +0000 @@ -180,6 +180,12 @@ public: + /// \brief Constructor for InsertReader + /// + /// Constructor for InsertReader + PushBackReader(const ItemReader& _item_reader = ItemReader()) + : item_reader(_item_reader) {} + /// \brief Reads the values into the container from the given stream. /// /// Reads the values into the container from the given stream. @@ -194,7 +200,6 @@ value.push_back(item); } if (!is) throw DataFormatError("PushBackReader format error"); - is.putback(c); } }; @@ -223,6 +228,12 @@ public: + /// \brief Constructor for InsertReader + /// + /// Constructor for InsertReader + InsertReader(const ItemReader& _item_reader = ItemReader()) + : item_reader(_item_reader) {} + /// \brief Reads the values into the container from the given stream. /// /// Reads the values into the container from the given stream. @@ -237,7 +248,6 @@ value.insert(item); } if (!is) throw DataFormatError("PushBackReader format error"); - is.putback(c); } }; @@ -310,10 +320,10 @@ /// \brief Reads the line from the given stream. /// /// Reads the line from the given stream. - void read(std::istream& is, Value& value) { + void read(std::istream& is, Value& value) const { if (skipSpaces) is >> std::ws; if (!getline(is, value)) { - throw DataFormatError("LineReader forma error"); + throw DataFormatError("LineReader format error"); } } private: @@ -321,6 +331,60 @@ }; /// \ingroup item_io + /// \brief Reader for std::pair. + /// + /// Reader for std::pair. + /// + /// \author Balazs Dezso + template , + typename _SecondReader = + DefaultReader > + class PairReader { + public: + typedef _Pair Value; + + typedef _FirstReader FirstReader; + typedef _SecondReader SecondReader; + + private: + + FirstReader first_reader; + SecondReader second_reader; + + public: + + /// \brief Constructor. + /// + /// Constructor for the PairReader. + PairReader(const FirstReader& _first_reader = FirstReader(), + const SecondReader& _second_reader = SecondReader()) + : first_reader(_first_reader), second_reader(_second_reader) {} + + /// \brief Reads the pair from the given stream. + /// + /// Reads the pair from the given stream. + void read(std::istream& is, Value& value) const { + char c; + if (!(is >> c) || c != '(') { + throw DataFormatError("PairReader format error"); + } + first_reader.read(is, value.first); + if (!(is >> c) || c != '=') { + throw DataFormatError("PairReader format error"); + } + if (!(is >> c) || c != '>') { + throw DataFormatError("PairReader format error"); + } + second_reader.read(is, value.second); + if (!(is >> c) || c != ')') { + throw DataFormatError("PairReader format error"); + } + } + }; + + /// \ingroup item_io /// /// \brief The default item reader template class. /// @@ -389,10 +453,24 @@ class DefaultReader > : public InsertReader > {}; + template + class DefaultReader > + : public InsertReader, + DefaultReader > > {}; + template class DefaultReader > : public InsertReader > {}; + template + class DefaultReader > + : public InsertReader, + DefaultReader > > {}; + + template + class DefaultReader > + : public PairReader > {}; + /// \ingroup item_io /// /// \brief The default item reader for skipping a value in the stream. diff -r 78b5ea23f0f1 -r ffa7c6e96330 lemon/bits/item_writer.h --- a/lemon/bits/item_writer.h Tue Dec 06 18:44:26 2005 +0000 +++ b/lemon/bits/item_writer.h Wed Dec 07 11:57:30 2005 +0000 @@ -53,7 +53,7 @@ /// \brief Writes a quoted string to the given stream. /// /// Writes a quoted string to the given stream. - void write(std::ostream& os, const std::string& value) { + void write(std::ostream& os, const std::string& value) const { os << "\""; if (escaped) { std::ostringstream ls; @@ -136,7 +136,7 @@ /// \brief Writes a quoted char array to the given stream. /// /// Writes a quoted char array to the given stream. - void write(std::ostream& os, const char* value) { + void write(std::ostream& os, const char* value) const { QuotedStringWriter(escaped).write(os, std::string(value)); } @@ -169,6 +169,9 @@ public: + IterableWriter(const ItemWriter& _item_writer = ItemWriter()) + : item_writer(_item_writer) {} + /// \brief Writes the values of the container to the given stream. /// /// Writes the values of the container to the given stream. @@ -185,6 +188,53 @@ }; /// \ingroup item_io + /// + /// \brief Writer for standard pairs. + /// + /// Writer for standard pairs. The representation of a pair is + /// \code ( first_value => second_value ) \endcode. + /// \author Balazs Dezso + template , + typename _SecondWriter = + DefaultWriter > + class PairWriter { + public: + + typedef _Pair Value; + + typedef _FirstWriter FirstWriter; + typedef _SecondWriter SecondWriter; + + private: + + FirstWriter first_writer; + SecondWriter second_writer; + + public: + + /// \brief Constructor. + /// + /// Constructor for the PairWriter. + PairWriter(const FirstWriter& _first_writer = FirstWriter(), + const SecondWriter& _second_writer = SecondWriter()) + : first_writer(_first_writer), second_writer(_second_writer) {} + + /// \brief Writes the pair from the given stream. + /// + /// Writes the pair from the given stream. + void write(std::ostream& os, const Value& value) const { + os << "( "; + first_writer.write(os, value.first); + os << " => "; + second_writer.write(os, value.second); + os << " )"; + } + + }; + + /// \ingroup item_io /// /// \brief The default item writer template class. /// @@ -217,6 +267,14 @@ class DefaultWriter : public QuotedCharArrayWriter {}; + template <> + class DefaultWriter + : public QuotedCharArrayWriter {}; + + template <> + class DefaultWriter + : public QuotedCharArrayWriter {}; + template class DefaultWriter > : public IterableWriter > {}; @@ -233,10 +291,22 @@ class DefaultWriter > : public IterableWriter > {}; + template + class DefaultWriter > + : public IterableWriter > {}; + template class DefaultWriter > : public IterableWriter > {}; + template + class DefaultWriter > + : public IterableWriter > {}; + + template + class DefaultWriter > + : public PairWriter > {}; + /// \ingroup item_io /// \brief Standard WriterTraits for the section writers. /// diff -r 78b5ea23f0f1 -r ffa7c6e96330 lemon/lemon_writer.h --- a/lemon/lemon_writer.h Tue Dec 06 18:44:26 2005 +0000 +++ b/lemon/lemon_writer.h Wed Dec 07 11:57:30 2005 +0000 @@ -172,7 +172,7 @@ virtual ~MapWriterBase() {} - virtual void write(std::ostream& os, const Item& item) = 0; + virtual void write(std::ostream& os, const Item& item) const = 0; }; @@ -192,7 +192,7 @@ virtual ~MapWriter() {} - virtual void write(std::ostream& os, const Item& item) { + virtual void write(std::ostream& os, const Item& item) const { Value value = map[item]; writer.write(os, value); }