diff -r 1645f6cc9667 -r 50cb2b90daa9 lemon/bits/item_writer.h --- a/lemon/bits/item_writer.h Tue Oct 17 11:05:23 2006 +0000 +++ b/lemon/bits/item_writer.h Wed Oct 18 15:05:12 2006 +0000 @@ -24,6 +24,7 @@ #define LEMON_BITS_ITEM_WRITER_H #include +#include #include #include @@ -39,10 +40,101 @@ /// \ingroup item_io /// \brief Writer class for quoted strings. /// + /// Writer class for unformatted strings. + /// \author Balazs Dezso + class UnformattedWriter { + public: + typedef std::string Value; + + /// \brief Constructor for the writer. + /// + /// Constructor for the writer. + UnformattedWriter() {} + + /// \brief Writes an unformatted string to the given stream. + /// + /// Writes an unformatted string to the given stream. + void write(std::ostream& os, const std::string& value) const { + os << value; + } + + bool readable(const std::string& value) const { + std::istringstream is(value); + char c; + while (is.get(c) && !whiteSpace(c)) { + if (!processChar(c, is)) return false; + } + if (is) return false; + return true; + } + + private: + + bool processChar(char c, std::istream& is) const { + switch (c) { + case '(': + is.putback(c); + if (!readableParsed('(', ')', is)) return false; + break; + case '[': + is.putback(c); + if (!readableParsed('[', ']', is)) return false; + break; + case '{': + is.putback(c); + if (!readableParsed('{', '}', is)) return false; + break; + case '\"': + is.putback(c); + if (!readableQuoted('\"', is)) return false; + break; + case '\'': + is.putback(c); + if (!readableQuoted('\'', is)) return false; + break; + default: + break; + } + return true; + } + + bool readableParsed(char open, char close, std::istream& is) const { + char c; + if (!is.get(c) || c != open) return false; + while (is.get(c) && c != close) { + if (!processChar(c, is)) return false; + } + if (!is) return false; + return true; + } + + bool readableQuoted(char quote, std::istream& is) const { + char c; + bool esc = false; + if (!is.get(c) || c != quote) return false; + while (is.get(c) && c != quote && !esc) { + if (c == '\\') esc = !esc; + else esc = false; + } + if (!is) return false; + return true; + } + + static bool whiteSpace(char c) { + return c == ' ' || c == '\t' || c == '\v' || + c == '\n' || c == '\r' || c == '\f'; + } + + }; + + /// \ingroup item_io + /// \brief Writer class for quoted strings. + /// /// Writer class for quoted strings. It can process the escape /// sequences in the string. /// \author Balazs Dezso class QuotedStringWriter { + friend class QuotedCharWriter; public: typedef std::string Value; @@ -120,6 +212,41 @@ }; /// \ingroup item_io + /// \brief Writer class for quoted chars. + /// + /// Writer class for quoted char. It can process the escape + /// sequences in the string. + /// \author Balazs Dezso + class QuotedCharWriter { + public: + typedef char Value; + + /// \brief Constructor for the writer. + /// + /// Constructor for the writer. If the given parameter is true + /// the writer creates escape sequences from special characters. + QuotedCharWriter(bool _escaped = true) : escaped(_escaped) {} + + /// \brief Writes a quoted char to the given stream. + /// + /// Writes a quoted char to the given stream. + void write(std::ostream& os, const char& value) const { + os << "\'"; + if (escaped) { + std::ostringstream ls; + QuotedStringWriter::writeEscape(ls, value); + os << ls.str(); + } else { + os << value; + } + os << "\'"; + } + + private: + bool escaped; + }; + + /// \ingroup item_io /// \brief Writer class for quoted char array. /// /// Writer class for quoted char array. It can process the escape @@ -258,8 +385,34 @@ }; template <> - class DefaultWriter - : public QuotedStringWriter {}; + class DefaultWriter { + public: + typedef std::string Value; + + void write(std::ostream& os, const Value& value) const { + if (UnformattedWriter().readable(value)) { + UnformattedWriter().write(os, value); + } else { + QuotedStringWriter().write(os, value); + } + } + + }; + + template <> + class DefaultWriter + : public QuotedCharWriter {}; + + template <> + class DefaultWriter { + public: + typedef bool Value; + + void write(std::ostream& os, const Value& value) const { + os << (value ? "1" : "0"); + } + + }; template class DefaultWriter