COIN-OR::LEMON - Graph Library

Changeset 2254:50cb2b90daa9 in lemon-0.x


Ignore:
Timestamp:
10/18/06 17:05:12 (18 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

Location:
lemon/bits
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lemon/bits/item_reader.h

    r2016 r2254  
    3939  /// \ingroup item_io
    4040  ///
     41  /// \brief Reader class for unformatted strings.
     42  ///
     43  /// Reader class for unformatted strings. This class want to be
     44  /// a general reader type which can read the most
     45  ///
     46  /// \author Balazs Dezso
     47  class UnformattedReader {
     48  public:
     49    /// \brief The value type of reader.
     50    ///
     51    /// The value type of reader.
     52    typedef std::string Value;
     53   
     54    /// \brief Constructor for the reader.
     55    ///
     56    /// Constructor for the reader.
     57    UnformattedReader() {}
     58   
     59    /// \brief Reads an unformatted string from the given stream.
     60    ///
     61    /// Reads an unformatted string from the given stream.
     62    void read(std::istream& is, std::string& value) const {
     63      char c;
     64      value.clear();
     65      is >> std::ws;
     66      while (is.get(c) && !whiteSpace(c)) {
     67        processChar(c, is, value);
     68      }
     69    }
     70
     71  private:
     72
     73    void processChar(char c, std::istream& is, Value& value) const {
     74      switch (c) {
     75      case '(':
     76        is.putback(c);
     77        readParsed('(', ')', is, value);
     78        break;
     79      case '[':
     80        is.putback(c);
     81        readParsed('[', ']', is, value);
     82        break;
     83      case '{':
     84        is.putback(c);
     85        readParsed('{', '}', is, value);
     86        break;
     87      case '/':
     88        is.putback(c);
     89        readParsed('/', '/', is, value);
     90        break;
     91      case '\"':
     92        is.putback(c);
     93        readQuoted('\"', is, value);
     94        break;
     95      case '\'':
     96        is.putback(c);
     97        readQuoted('\'', is, value);
     98        break;
     99      default:
     100        value += c;
     101        break;
     102      }
     103    }
     104
     105    void readParsed(char open, char close,
     106                    std::istream& is, Value& value) const {
     107      char c;
     108      if (!is.get(c) || c != open)
     109        throw DataFormatError("Unformatted string format error");
     110      value += c;
     111      while (is.get(c) && c != close) {
     112        processChar(c, is, value);
     113      }
     114      if (!is)
     115        throw DataFormatError("Unformatted string format error");
     116      value += c;     
     117    }
     118
     119    void readQuoted(char quote, std::istream& is, Value& value) const {
     120      char c;
     121      bool esc = false;
     122      if (!is.get(c) || c != quote)
     123        throw DataFormatError("Unformatted string format error");
     124      value += c;
     125      while (is.get(c) && (c != quote || esc)) {
     126        if (c == '\\') esc = !esc;
     127        else esc = false;
     128        value += c;
     129      }
     130      if (!is)
     131        throw DataFormatError("Unformatted string format error");
     132      value += c;
     133    }
     134
     135
     136
     137    static bool whiteSpace(char c) {
     138      return c == ' ' || c == '\t' || c == '\v' ||
     139        c == '\n' || c == '\r' || c == '\f';
     140    }
     141
     142   
     143  };
     144
     145  /// \ingroup item_io
     146  ///
    41147  /// \brief Reader class for quoted strings.
    42148  ///
     
    46152  /// \author Balazs Dezso
    47153  class QuotedStringReader {
     154    friend class QuotedCharReader;
    48155  public:
    49156    /// \brief The value type of reader.
     
    67174      is >> std::ws;
    68175      if (!is.get(c) || c != '\"')
    69         throw DataFormatError("Quoted string format error");
     176        throw DataFormatError("Quoted format error");
    70177      while (is.get(c) && c != '\"') {
    71178        if (escaped && c == '\\') {
     
    75182        }
    76183      }
    77       if (!is) throw DataFormatError("Quoted string format error");
     184      if (!is) throw DataFormatError("Quoted format error");
    78185    }
    79186
     
    153260
    154261  /// \ingroup item_io
     262  ///
     263  /// \brief Reader class for quoted strings.
     264  ///
     265  /// Reader class for quoted strings. It can process the escape
     266  /// sequences in the string.
     267  ///
     268  /// \author Balazs Dezso
     269  class QuotedCharReader {
     270  public:
     271    /// \brief The value type of reader.
     272    ///
     273    /// The value type of reader.
     274    typedef char Value;
     275   
     276    /// \brief Constructor for the reader.
     277    ///
     278    /// Constructor for the reader. If the given parameter is true
     279    /// the reader processes the escape sequences.
     280    QuotedCharReader(bool _escaped = true)
     281      : escaped(_escaped) {}
     282   
     283    /// \brief Reads a quoted string from the given stream.
     284    ///
     285    /// Reads a quoted string from the given stream.
     286    void read(std::istream& is, char& value) const {
     287      char c;
     288      is >> std::ws;
     289      if (!is.get(c) || c != '\'')
     290        throw DataFormatError("Quoted format error");
     291      if (!is.get(c))
     292        throw DataFormatError("Quoted format error");
     293      if (escaped && c == '\\') {
     294        value = QuotedStringReader::readEscape(is);
     295      } else {
     296        value = c;
     297      }
     298      if (!is.get(c) || c != '\'')
     299        throw DataFormatError("Quoted format error");
     300    }
     301
     302  private:
     303    bool escaped;
     304  };
     305
     306  /// \ingroup item_io
    155307  /// \brief Reader for standard containers.
    156308  ///
     
    272424    void read(std::istream& is, Value& value) const {
    273425      char c;
     426      value.clear();
    274427      if (!(is >> c) || c != open) {
    275428        throw DataFormatError("ParsedStringReader format error");
     
    408561    void read(std::istream& is, Value& value) const {
    409562      char c;
    410       if (!(is >> std::ws >> c)) return;
     563      if (!(is >> std::ws >> c))
     564        throw DataFormatError("DefaultReader<string> format error");
    411565      is.putback(c);
    412566      switch (c) {
     
    414568        QuotedStringReader().read(is, value);
    415569        break;
    416       case '(':
    417         ParsedStringReader().read(is, value);
     570      default:
     571        UnformattedReader().read(is, value);
    418572        break;
    419       case '[':
    420         ParsedStringReader('[', ']').read(is, value);
    421         break;
    422       case '/':
    423         ParsedStringReader('/', '/').read(is, value);
     573      }
     574    }
     575   
     576  };
     577
     578  template <>
     579  class DefaultReader<char> {
     580  public:
     581    typedef char Value;
     582   
     583    void read(std::istream& is, Value& value) const {
     584      char c;
     585      if (!(is >> std::ws >> c))
     586        throw DataFormatError("DefaultReader<char> format error");
     587      is.putback(c);
     588      switch (c) {
     589      case '\'':
     590        QuotedCharReader().read(is, value);
    424591        break;
    425592      default:
    426         if (!(is >> value))
    427           throw DataFormatError("DefaultReader format error");
    428         break;
    429       }
    430     }
    431    
     593        {
     594          int temp;         
     595          if (!(is >> temp))
     596            throw DataFormatError("DefaultReader<char> format error");
     597          value = (char)temp;
     598          break;
     599        }
     600      }
     601    }   
     602  };
     603
     604  template <>
     605  class DefaultReader<bool> {
     606  public:
     607    typedef bool Value;
     608   
     609    void read(std::istream& is, Value& value) const {
     610      std::string rep;
     611      if (!(is >> rep))
     612        throw DataFormatError("DefaultReader<bool> format error");
     613      if (rep == "true" || rep == "t" || rep == "1") {
     614        value = true;
     615      } else if (rep == "false" || rep == "f" || rep == "0") {
     616        value = false;
     617      } else throw DataFormatError("DefaultReader<bool> format error");
     618    }   
    432619  };
    433620
     
    473660  ///
    474661  /// \author Balazs Dezso
    475   class DefaultSkipper : public DefaultReader<std::string> {};
     662  class DefaultSkipper : public UnformattedReader {};
    476663
    477664  /// \ingroup item_io 
  • 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.