Changeset 2254:50cb2b90daa9 in lemon-0.x for lemon/bits/item_writer.h
- Timestamp:
- 10/18/06 17:05:12 (18 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@3005
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/bits/item_writer.h
r1956 r2254 25 25 26 26 #include <iostream> 27 #include <sstream> 27 28 #include <string> 28 29 … … 40 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 133 /// Writer class for quoted strings. It can process the escape 43 134 /// sequences in the string. 44 135 /// \author Balazs Dezso 45 136 class QuotedStringWriter { 137 friend class QuotedCharWriter; 46 138 public: 47 139 typedef std::string Value; … … 121 213 122 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 250 /// \brief Writer class for quoted char array. 124 251 /// … … 259 386 260 387 template <> 261 class DefaultWriter<std::string> 262 : public QuotedStringWriter {}; 388 class DefaultWriter<std::string> { 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 417 template <int length>
Note: See TracChangeset
for help on using the changeset viewer.