lemon/bits/item_writer.h
changeset 2254 50cb2b90daa9
parent 1956 a055123339d5
child 2255 4a9cc8c800ae
     1.1 --- a/lemon/bits/item_writer.h	Tue Oct 17 11:05:23 2006 +0000
     1.2 +++ b/lemon/bits/item_writer.h	Wed Oct 18 15:05:12 2006 +0000
     1.3 @@ -24,6 +24,7 @@
     1.4  #define LEMON_BITS_ITEM_WRITER_H
     1.5  
     1.6  #include <iostream>
     1.7 +#include <sstream>
     1.8  #include <string>
     1.9  
    1.10  #include <vector>
    1.11 @@ -39,10 +40,101 @@
    1.12    /// \ingroup item_io
    1.13    /// \brief Writer class for quoted strings.
    1.14    ///
    1.15 +  /// Writer class for unformatted strings.
    1.16 +  /// \author Balazs Dezso
    1.17 +  class UnformattedWriter {
    1.18 +  public:
    1.19 +    typedef std::string Value;
    1.20 +
    1.21 +    /// \brief Constructor for the writer.
    1.22 +    ///
    1.23 +    /// Constructor for the writer.
    1.24 +    UnformattedWriter() {}
    1.25 +
    1.26 +    /// \brief Writes an unformatted string to the given stream.
    1.27 +    ///
    1.28 +    /// Writes an unformatted string to the given stream.
    1.29 +    void write(std::ostream& os, const std::string& value) const {
    1.30 +      os << value;
    1.31 +    }
    1.32 +
    1.33 +    bool readable(const std::string& value) const {
    1.34 +      std::istringstream is(value);
    1.35 +      char c;
    1.36 +      while (is.get(c) && !whiteSpace(c)) {
    1.37 +        if (!processChar(c, is)) return false;
    1.38 +      }
    1.39 +      if (is) return false;
    1.40 +      return true;
    1.41 +    }
    1.42 +
    1.43 +  private:
    1.44 +
    1.45 +    bool processChar(char c, std::istream& is) const {
    1.46 +      switch (c) {
    1.47 +      case '(':
    1.48 +        is.putback(c);
    1.49 +        if (!readableParsed('(', ')', is)) return false;
    1.50 +        break;
    1.51 +      case '[':
    1.52 +        is.putback(c);
    1.53 +        if (!readableParsed('[', ']', is)) return false;
    1.54 +        break;
    1.55 +      case '{':
    1.56 +        is.putback(c);
    1.57 +        if (!readableParsed('{', '}', is)) return false;
    1.58 +        break;
    1.59 +      case '\"':
    1.60 +        is.putback(c);
    1.61 +        if (!readableQuoted('\"', is)) return false;
    1.62 +        break;
    1.63 +      case '\'':
    1.64 +        is.putback(c);
    1.65 +        if (!readableQuoted('\'', is)) return false;
    1.66 +        break;
    1.67 +      default:
    1.68 +        break;
    1.69 +      }
    1.70 +      return true;
    1.71 +    }
    1.72 +
    1.73 +    bool readableParsed(char open, char close, std::istream& is) const {
    1.74 +      char c;
    1.75 +      if (!is.get(c) || c != open) return false;
    1.76 +      while (is.get(c) && c != close) {
    1.77 +        if (!processChar(c, is)) return false;
    1.78 +      }
    1.79 +      if (!is) return false;
    1.80 +      return true;
    1.81 +    }
    1.82 +
    1.83 +    bool readableQuoted(char quote, std::istream& is) const {
    1.84 +      char c;
    1.85 +      bool esc = false;
    1.86 +      if (!is.get(c) || c != quote) return false;
    1.87 +      while (is.get(c) && c != quote && !esc) {
    1.88 +        if (c == '\\') esc = !esc;
    1.89 +        else esc = false;
    1.90 +      }
    1.91 +      if (!is) return false;
    1.92 +      return true;
    1.93 +    }
    1.94 +
    1.95 +    static bool whiteSpace(char c) {
    1.96 +      return c == ' ' || c == '\t' || c == '\v' || 
    1.97 +        c == '\n' || c == '\r' || c == '\f'; 
    1.98 +    }
    1.99 +
   1.100 +  };
   1.101 +
   1.102 +  /// \ingroup item_io
   1.103 +  /// \brief Writer class for quoted strings.
   1.104 +  ///
   1.105    /// Writer class for quoted strings. It can process the escape
   1.106    /// sequences in the string.
   1.107    /// \author Balazs Dezso
   1.108    class QuotedStringWriter {
   1.109 +    friend class QuotedCharWriter;
   1.110    public:
   1.111      typedef std::string Value;
   1.112  
   1.113 @@ -120,6 +212,41 @@
   1.114    };
   1.115  
   1.116    /// \ingroup item_io
   1.117 +  /// \brief Writer class for quoted chars.
   1.118 +  ///
   1.119 +  /// Writer class for quoted char. It can process the escape
   1.120 +  /// sequences in the string.
   1.121 +  /// \author Balazs Dezso
   1.122 +  class QuotedCharWriter {
   1.123 +  public:
   1.124 +    typedef char Value;
   1.125 +
   1.126 +    /// \brief Constructor for the writer.
   1.127 +    ///
   1.128 +    /// Constructor for the writer. If the given parameter is true
   1.129 +    /// the writer creates escape sequences from special characters.
   1.130 +    QuotedCharWriter(bool _escaped = true) : escaped(_escaped) {}
   1.131 +
   1.132 +    /// \brief Writes a quoted char to the given stream.
   1.133 +    ///
   1.134 +    /// Writes a quoted char to the given stream.
   1.135 +    void write(std::ostream& os, const char& value) const {
   1.136 +      os << "\'";
   1.137 +      if (escaped) {
   1.138 +	std::ostringstream ls;
   1.139 +        QuotedStringWriter::writeEscape(ls, value);
   1.140 +	os << ls.str();
   1.141 +      } else {
   1.142 +	os << value;
   1.143 +      }
   1.144 +      os << "\'";
   1.145 +    }
   1.146 +
   1.147 +  private:
   1.148 +    bool escaped;
   1.149 +  };
   1.150 +
   1.151 +  /// \ingroup item_io
   1.152    /// \brief Writer class for quoted char array.
   1.153    ///
   1.154    /// Writer class for quoted char array. It can process the escape
   1.155 @@ -258,8 +385,34 @@
   1.156    };
   1.157  
   1.158    template <>
   1.159 -  class DefaultWriter<std::string> 
   1.160 -    : public QuotedStringWriter {};
   1.161 +  class DefaultWriter<std::string> {
   1.162 +  public:
   1.163 +    typedef std::string Value;
   1.164 +    
   1.165 +    void write(std::ostream& os, const Value& value) const {
   1.166 +      if (UnformattedWriter().readable(value)) {
   1.167 +        UnformattedWriter().write(os, value);
   1.168 +      } else {
   1.169 +        QuotedStringWriter().write(os, value);
   1.170 +      }
   1.171 +    }
   1.172 +      
   1.173 +  };
   1.174 +
   1.175 +  template <>
   1.176 +  class DefaultWriter<char> 
   1.177 +    : public QuotedCharWriter {};
   1.178 +
   1.179 +  template <>
   1.180 +  class DefaultWriter<bool> {
   1.181 +  public:
   1.182 +    typedef bool Value;
   1.183 +    
   1.184 +    void write(std::ostream& os, const Value& value) const {
   1.185 +      os << (value ? "1" : "0");
   1.186 +    }
   1.187 +      
   1.188 +  };
   1.189  
   1.190    template <int length>
   1.191    class DefaultWriter<char[length]>