lemon/bits/item_writer.h
author ladanyi
Thu, 26 May 2005 13:21:47 +0000
changeset 1437 2a3f3448ced1
parent 1434 d8475431bbbb
child 1533 43c7b3085212
permissions -rw-r--r--
- test both lp implementations
- commented out the checking of LpCplex because it fails
     1 /* -*- C++ -*-
     2  * lemon/bits/item_reader.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     6  *
     7  * Permission to use, modify and distribute this software is granted
     8  * provided that this copyright notice appears in all copies. For
     9  * precise terms see the accompanying LICENSE file.
    10  *
    11  * This software is provided "AS IS" with no warranty of any kind,
    12  * express or implied, and with no claim as to its suitability for any
    13  * purpose.
    14  *
    15  */
    16 
    17 /// \ingroup item_io
    18 /// \file
    19 /// \brief Item writer bits for lemon output.
    20 
    21 #ifndef LEMON_BITS_ITEM_WRITER_H
    22 #define LEMON_BITS_ITEM_WRITER_H
    23 
    24 #include <iostream>
    25 #include <string>
    26 
    27 #include <vector>
    28 #include <deque>
    29 #include <list>
    30 #include <set>
    31 
    32 namespace lemon {
    33   
    34   template <typename Value>
    35   class DefaultWriter;
    36 
    37   /// \ingroup item_io
    38   /// \brief Writer class for quoted strings.
    39   ///
    40   /// Writer class for quoted strings. It can process the escape
    41   /// sequences in the string.
    42   /// \author Balazs Dezso
    43   class QuotedStringWriter {
    44   public:
    45     typedef std::string Value;
    46 
    47     /// \brief Constructor for the writer.
    48     ///
    49     /// Constructor for the writer. If the given parameter is true
    50     /// the writer creates escape sequences from special characters.
    51     QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {}
    52 
    53     /// \brief Writes a quoted string to the given stream.
    54     ///
    55     /// Writes a quoted string to the given stream.
    56     void write(std::ostream& os, const std::string& value) {
    57       os << "\"";
    58       if (escaped) {
    59 	std::ostringstream ls;
    60 	for (int i = 0; i < (int)value.size(); ++i) {
    61 	  writeEscape(ls, value[i]);
    62 	}
    63 	os << ls.str();
    64       } else {
    65 	os << value;
    66       }
    67       os << "\"";
    68     }
    69 
    70   private:
    71     
    72     static void writeEscape(std::ostream& os, char c) {
    73       switch (c) {
    74       case '\\':
    75 	os << "\\\\";
    76 	return;
    77       case '\"':
    78 	os << "\\\"";
    79 	return;
    80       case '\'':
    81 	os << "\\\'";
    82 	return;
    83       case '\?':
    84 	os << "\\\?";
    85 	return;
    86       case '\a':
    87 	os << "\\a";
    88 	return;
    89       case '\b':
    90 	os << "\\b";
    91 	return;
    92       case '\f':
    93 	os << "\\f";
    94 	return;
    95       case '\r':
    96 	os << "\\r";
    97 	return;
    98       case '\n':
    99 	os << "\\n";
   100 	return;
   101       case '\t':
   102 	os << "\\t";
   103 	return;
   104       case '\v':
   105 	os << "\\v";
   106 	return;
   107       default:
   108 	if (c < 0x20) {
   109 	  os << '\\' << std::oct << (int)c;
   110 	} else {
   111 	  os << c;
   112 	}
   113 	return;
   114       }     
   115     }
   116   private:
   117     bool escaped;
   118   };
   119 
   120   /// \ingroup item_io
   121   ///
   122   /// \brief Writer for standard containers.
   123   ///
   124   /// Writer for each iterable standard containers. The representation
   125   /// of the container is the values enumerated between an open and a
   126   /// close parse. 
   127   ///
   128   /// \author Balazs Dezso
   129   template <
   130     typename _Container, 
   131     typename _ItemWriter = DefaultWriter<typename _Container::value_type> 
   132   >
   133   class IterableWriter {
   134   public:
   135     typedef _Container Value;
   136     typedef _ItemWriter ItemWriter;
   137 
   138   private:
   139 
   140     ItemWriter item_writer;
   141 
   142   public:
   143 
   144     /// \brief Writes the values of the container to the given stream.
   145     ///
   146     /// Writes the values of the container to the given stream.
   147     void write(std::ostream& os, const Value& value) const {
   148       typename Value::const_iterator it;
   149       os << '(';
   150       for (it = value.begin(); it != value.end(); ++it) {
   151 	item_writer.write(os, *it);
   152 	os << ' ';
   153       }
   154       os << ')';
   155     }
   156 
   157   };
   158 
   159   /// \ingroup item_io
   160   /// 
   161   /// \brief The default item writer template class.
   162   ///
   163   /// The default item writer template class. If some section writer
   164   /// needs to write a value to the stream it will give the default way for it.
   165   ///
   166   /// \author Balazs Dezso
   167   template <typename _Value>
   168   class DefaultWriter {
   169   public:
   170     /// The value type.
   171     typedef _Value Value;
   172     /// \brief Writes the value to the given stream.
   173     ///
   174     /// Writes the value to the given stream.
   175     void write(std::ostream& os, const Value& value) const {
   176       os << value;
   177     }
   178   };
   179 
   180   template <>
   181   class DefaultWriter<std::string> 
   182     : public QuotedStringWriter {};
   183 
   184   template <typename Item>
   185   class DefaultWriter<std::vector<Item> > 
   186     : public IterableWriter<std::vector<Item> > {};
   187 
   188   template <typename Item>
   189   class DefaultWriter<std::deque<Item> > 
   190     : public IterableWriter<std::deque<Item> > {};
   191 
   192   template <typename Item>
   193   class DefaultWriter<std::list<Item> > 
   194     : public IterableWriter<std::list<Item> > {};
   195   
   196   template <typename Item>
   197   class DefaultWriter<std::set<Item> > 
   198     : public IterableWriter<std::set<Item> > {};
   199 
   200   template <typename Item>
   201   class DefaultWriter<std::multiset<Item> > 
   202     : public IterableWriter<std::multiset<Item> > {};
   203 
   204   /// \ingroup item_io
   205   /// \brief Standard WriterTraits for the section writers.
   206   ///
   207   /// Standard WriterTraits for the section writers.
   208   /// It defines standard writing method for all type of value. 
   209   /// \author Balazs Dezso
   210   struct DefaultWriterTraits {
   211 
   212     template <typename _Value>
   213     struct Writer : DefaultWriter<_Value> {};
   214 
   215   };
   216 
   217 }
   218 
   219 #endif