lemon/bits/item_writer.h
changeset 2254 50cb2b90daa9
parent 1956 a055123339d5
child 2255 4a9cc8c800ae
equal deleted inserted replaced
7:2b72e0381f54 8:f3df706d4c42
    22 
    22 
    23 #ifndef LEMON_BITS_ITEM_WRITER_H
    23 #ifndef LEMON_BITS_ITEM_WRITER_H
    24 #define LEMON_BITS_ITEM_WRITER_H
    24 #define LEMON_BITS_ITEM_WRITER_H
    25 
    25 
    26 #include <iostream>
    26 #include <iostream>
       
    27 #include <sstream>
    27 #include <string>
    28 #include <string>
    28 
    29 
    29 #include <vector>
    30 #include <vector>
    30 #include <deque>
    31 #include <deque>
    31 #include <list>
    32 #include <list>
    37   class DefaultWriter;
    38   class DefaultWriter;
    38 
    39 
    39   /// \ingroup item_io
    40   /// \ingroup item_io
    40   /// \brief Writer class for quoted strings.
    41   /// \brief Writer class for quoted strings.
    41   ///
    42   ///
       
    43   /// Writer class for unformatted strings.
       
    44   /// \author Balazs Dezso
       
    45   class UnformattedWriter {
       
    46   public:
       
    47     typedef std::string Value;
       
    48 
       
    49     /// \brief Constructor for the writer.
       
    50     ///
       
    51     /// Constructor for the writer.
       
    52     UnformattedWriter() {}
       
    53 
       
    54     /// \brief Writes an unformatted string to the given stream.
       
    55     ///
       
    56     /// Writes an unformatted string to the given stream.
       
    57     void write(std::ostream& os, const std::string& value) const {
       
    58       os << value;
       
    59     }
       
    60 
       
    61     bool readable(const std::string& value) const {
       
    62       std::istringstream is(value);
       
    63       char c;
       
    64       while (is.get(c) && !whiteSpace(c)) {
       
    65         if (!processChar(c, is)) return false;
       
    66       }
       
    67       if (is) return false;
       
    68       return true;
       
    69     }
       
    70 
       
    71   private:
       
    72 
       
    73     bool processChar(char c, std::istream& is) const {
       
    74       switch (c) {
       
    75       case '(':
       
    76         is.putback(c);
       
    77         if (!readableParsed('(', ')', is)) return false;
       
    78         break;
       
    79       case '[':
       
    80         is.putback(c);
       
    81         if (!readableParsed('[', ']', is)) return false;
       
    82         break;
       
    83       case '{':
       
    84         is.putback(c);
       
    85         if (!readableParsed('{', '}', is)) return false;
       
    86         break;
       
    87       case '\"':
       
    88         is.putback(c);
       
    89         if (!readableQuoted('\"', is)) return false;
       
    90         break;
       
    91       case '\'':
       
    92         is.putback(c);
       
    93         if (!readableQuoted('\'', is)) return false;
       
    94         break;
       
    95       default:
       
    96         break;
       
    97       }
       
    98       return true;
       
    99     }
       
   100 
       
   101     bool readableParsed(char open, char close, std::istream& is) const {
       
   102       char c;
       
   103       if (!is.get(c) || c != open) return false;
       
   104       while (is.get(c) && c != close) {
       
   105         if (!processChar(c, is)) return false;
       
   106       }
       
   107       if (!is) return false;
       
   108       return true;
       
   109     }
       
   110 
       
   111     bool readableQuoted(char quote, std::istream& is) const {
       
   112       char c;
       
   113       bool esc = false;
       
   114       if (!is.get(c) || c != quote) return false;
       
   115       while (is.get(c) && c != quote && !esc) {
       
   116         if (c == '\\') esc = !esc;
       
   117         else esc = false;
       
   118       }
       
   119       if (!is) return false;
       
   120       return true;
       
   121     }
       
   122 
       
   123     static bool whiteSpace(char c) {
       
   124       return c == ' ' || c == '\t' || c == '\v' || 
       
   125         c == '\n' || c == '\r' || c == '\f'; 
       
   126     }
       
   127 
       
   128   };
       
   129 
       
   130   /// \ingroup item_io
       
   131   /// \brief Writer class for quoted strings.
       
   132   ///
    42   /// Writer class for quoted strings. It can process the escape
   133   /// Writer class for quoted strings. It can process the escape
    43   /// sequences in the string.
   134   /// sequences in the string.
    44   /// \author Balazs Dezso
   135   /// \author Balazs Dezso
    45   class QuotedStringWriter {
   136   class QuotedStringWriter {
       
   137     friend class QuotedCharWriter;
    46   public:
   138   public:
    47     typedef std::string Value;
   139     typedef std::string Value;
    48 
   140 
    49     /// \brief Constructor for the writer.
   141     /// \brief Constructor for the writer.
    50     ///
   142     ///
   118   private:
   210   private:
   119     bool escaped;
   211     bool escaped;
   120   };
   212   };
   121 
   213 
   122   /// \ingroup item_io
   214   /// \ingroup item_io
       
   215   /// \brief Writer class for quoted chars.
       
   216   ///
       
   217   /// Writer class for quoted char. It can process the escape
       
   218   /// sequences in the string.
       
   219   /// \author Balazs Dezso
       
   220   class QuotedCharWriter {
       
   221   public:
       
   222     typedef char Value;
       
   223 
       
   224     /// \brief Constructor for the writer.
       
   225     ///
       
   226     /// Constructor for the writer. If the given parameter is true
       
   227     /// the writer creates escape sequences from special characters.
       
   228     QuotedCharWriter(bool _escaped = true) : escaped(_escaped) {}
       
   229 
       
   230     /// \brief Writes a quoted char to the given stream.
       
   231     ///
       
   232     /// Writes a quoted char to the given stream.
       
   233     void write(std::ostream& os, const char& value) const {
       
   234       os << "\'";
       
   235       if (escaped) {
       
   236 	std::ostringstream ls;
       
   237         QuotedStringWriter::writeEscape(ls, value);
       
   238 	os << ls.str();
       
   239       } else {
       
   240 	os << value;
       
   241       }
       
   242       os << "\'";
       
   243     }
       
   244 
       
   245   private:
       
   246     bool escaped;
       
   247   };
       
   248 
       
   249   /// \ingroup item_io
   123   /// \brief Writer class for quoted char array.
   250   /// \brief Writer class for quoted char array.
   124   ///
   251   ///
   125   /// Writer class for quoted char array. It can process the escape
   252   /// Writer class for quoted char array. It can process the escape
   126   /// sequences in the char array.
   253   /// sequences in the char array.
   127   /// \author Balazs Dezso
   254   /// \author Balazs Dezso
   256       os << value;
   383       os << value;
   257     }
   384     }
   258   };
   385   };
   259 
   386 
   260   template <>
   387   template <>
   261   class DefaultWriter<std::string> 
   388   class DefaultWriter<std::string> {
   262     : public QuotedStringWriter {};
   389   public:
       
   390     typedef std::string Value;
       
   391     
       
   392     void write(std::ostream& os, const Value& value) const {
       
   393       if (UnformattedWriter().readable(value)) {
       
   394         UnformattedWriter().write(os, value);
       
   395       } else {
       
   396         QuotedStringWriter().write(os, value);
       
   397       }
       
   398     }
       
   399       
       
   400   };
       
   401 
       
   402   template <>
       
   403   class DefaultWriter<char> 
       
   404     : public QuotedCharWriter {};
       
   405 
       
   406   template <>
       
   407   class DefaultWriter<bool> {
       
   408   public:
       
   409     typedef bool Value;
       
   410     
       
   411     void write(std::ostream& os, const Value& value) const {
       
   412       os << (value ? "1" : "0");
       
   413     }
       
   414       
       
   415   };
   263 
   416 
   264   template <int length>
   417   template <int length>
   265   class DefaultWriter<char[length]> 
   418   class DefaultWriter<char[length]> 
   266     : public QuotedCharArrayWriter {};
   419     : public QuotedCharArrayWriter {};
   267 
   420