COIN-OR::LEMON - Graph Library

Changeset 2254:50cb2b90daa9 in lemon-0.x for lemon/bits/item_writer.h


Ignore:
Timestamp:
10/18/06 17:05:12 (14 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@3005
Message:

Some improvements on item readers and writers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/bits/item_writer.h

    r1956 r2254  
    2525
    2626#include <iostream>
     27#include <sstream>
    2728#include <string>
    2829
     
    4041  /// \brief Writer class for quoted strings.
    4142  ///
     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  ///
    42133  /// Writer class for quoted strings. It can process the escape
    43134  /// sequences in the string.
    44135  /// \author Balazs Dezso
    45136  class QuotedStringWriter {
     137    friend class QuotedCharWriter;
    46138  public:
    47139    typedef std::string Value;
     
    121213
    122214  /// \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
    123250  /// \brief Writer class for quoted char array.
    124251  ///
     
    259386
    260387  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  };
    263416
    264417  template <int length>
Note: See TracChangeset for help on using the changeset viewer.