lemon/bits/item_writer.h
author alpar
Fri, 13 Oct 2006 15:10:50 +0000
changeset 2241 37e0966e43b6
parent 1946 17eb3eaad9f8
child 2254 50cb2b90daa9
permissions -rw-r--r--
Improve build environment and scripts
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2006
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 /// \ingroup item_io
    20 /// \file
    21 /// \brief Item writer bits for lemon output.
    22 
    23 #ifndef LEMON_BITS_ITEM_WRITER_H
    24 #define LEMON_BITS_ITEM_WRITER_H
    25 
    26 #include <iostream>
    27 #include <string>
    28 
    29 #include <vector>
    30 #include <deque>
    31 #include <list>
    32 #include <set>
    33 
    34 namespace lemon {
    35   
    36   template <typename Value>
    37   class DefaultWriter;
    38 
    39   /// \ingroup item_io
    40   /// \brief Writer class for quoted strings.
    41   ///
    42   /// Writer class for quoted strings. It can process the escape
    43   /// sequences in the string.
    44   /// \author Balazs Dezso
    45   class QuotedStringWriter {
    46   public:
    47     typedef std::string Value;
    48 
    49     /// \brief Constructor for the writer.
    50     ///
    51     /// Constructor for the writer. If the given parameter is true
    52     /// the writer creates escape sequences from special characters.
    53     QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {}
    54 
    55     /// \brief Writes a quoted string to the given stream.
    56     ///
    57     /// Writes a quoted string to the given stream.
    58     void write(std::ostream& os, const std::string& value) const {
    59       os << "\"";
    60       if (escaped) {
    61 	std::ostringstream ls;
    62 	for (int i = 0; i < (int)value.size(); ++i) {
    63 	  writeEscape(ls, value[i]);
    64 	}
    65 	os << ls.str();
    66       } else {
    67 	os << value;
    68       }
    69       os << "\"";
    70     }
    71 
    72   private:
    73     
    74     static void writeEscape(std::ostream& os, char c) {
    75       switch (c) {
    76       case '\\':
    77 	os << "\\\\";
    78 	return;
    79       case '\"':
    80 	os << "\\\"";
    81 	return;
    82       case '\'':
    83 	os << "\\\'";
    84 	return;
    85       case '\?':
    86 	os << "\\\?";
    87 	return;
    88       case '\a':
    89 	os << "\\a";
    90 	return;
    91       case '\b':
    92 	os << "\\b";
    93 	return;
    94       case '\f':
    95 	os << "\\f";
    96 	return;
    97       case '\r':
    98 	os << "\\r";
    99 	return;
   100       case '\n':
   101 	os << "\\n";
   102 	return;
   103       case '\t':
   104 	os << "\\t";
   105 	return;
   106       case '\v':
   107 	os << "\\v";
   108 	return;
   109       default:
   110 	if (c < 0x20) {
   111 	  os << '\\' << std::oct << (int)c;
   112 	} else {
   113 	  os << c;
   114 	}
   115 	return;
   116       }     
   117     }
   118   private:
   119     bool escaped;
   120   };
   121 
   122   /// \ingroup item_io
   123   /// \brief Writer class for quoted char array.
   124   ///
   125   /// Writer class for quoted char array. It can process the escape
   126   /// sequences in the char array.
   127   /// \author Balazs Dezso
   128   class QuotedCharArrayWriter {
   129   public:
   130     typedef const char* Value;
   131 
   132     /// \brief Constructor for the writer.
   133     ///
   134     /// Constructor for the writer. If the given parameter is true
   135     /// the writer creates escape sequences from special characters.
   136     QuotedCharArrayWriter(bool _escaped = true) : escaped(_escaped) {}
   137 
   138     /// \brief Writes a quoted char array to the given stream.
   139     ///
   140     /// Writes a quoted char array to the given stream.
   141     void write(std::ostream& os, const char* value) const {
   142       QuotedStringWriter(escaped).write(os, std::string(value));
   143     }
   144 
   145   private:    
   146     bool escaped;
   147   };
   148 
   149 
   150   /// \ingroup item_io
   151   ///
   152   /// \brief Writer for standard containers.
   153   ///
   154   /// Writer for each iterable standard containers. The representation
   155   /// of the container is the values enumerated between an open and a
   156   /// close parse. 
   157   ///
   158   /// \author Balazs Dezso
   159   template <
   160     typename _Container, 
   161     typename _ItemWriter = DefaultWriter<typename _Container::value_type> 
   162   >
   163   class IterableWriter {
   164   public:
   165     typedef _Container Value;
   166     typedef _ItemWriter ItemWriter;
   167 
   168   private:
   169 
   170     ItemWriter item_writer;
   171 
   172   public:
   173 
   174     IterableWriter(const ItemWriter& _item_writer = ItemWriter())
   175       : item_writer(_item_writer) {}
   176 
   177     /// \brief Writes the values of the container to the given stream.
   178     ///
   179     /// Writes the values of the container to the given stream.
   180     void write(std::ostream& os, const Value& value) const {
   181       typename Value::const_iterator it;
   182       os << '(';
   183       for (it = value.begin(); it != value.end(); ++it) {
   184 	item_writer.write(os, *it);
   185 	os << ' ';
   186       }
   187       os << ')';
   188     }
   189 
   190   };
   191 
   192   /// \ingroup item_io
   193   ///
   194   /// \brief Writer for standard pairs.
   195   ///
   196   /// Writer for standard pairs. The representation of a pair is
   197   ///\code ( first_value => second_value ) \endcode.
   198   /// \author Balazs Dezso
   199   template <typename _Pair, 
   200 	    typename _FirstWriter = 
   201 	    DefaultWriter<typename _Pair::first_type>,
   202 	    typename _SecondWriter = 
   203 	    DefaultWriter<typename _Pair::second_type> >
   204   class PairWriter {
   205   public:
   206 
   207     typedef _Pair Value;
   208 
   209     typedef _FirstWriter FirstWriter;
   210     typedef _SecondWriter SecondWriter;
   211 
   212   private:
   213 
   214     FirstWriter first_writer;
   215     SecondWriter second_writer;
   216 
   217   public:
   218     
   219     /// \brief Constructor.
   220     ///
   221     /// Constructor for the PairWriter.
   222     PairWriter(const FirstWriter& _first_writer = FirstWriter(), 
   223 	       const SecondWriter& _second_writer = SecondWriter()) 
   224       : first_writer(_first_writer), second_writer(_second_writer) {}
   225     
   226     /// \brief Writes the pair from the given stream.
   227     ///
   228     /// Writes the pair from the given stream.
   229     void write(std::ostream& os, const Value& value) const {
   230       os << "( ";
   231       first_writer.write(os, value.first);
   232       os << " => ";
   233       second_writer.write(os, value.second);
   234       os << " )";
   235     }
   236 
   237   };
   238 
   239   /// \ingroup item_io
   240   /// 
   241   /// \brief The default item writer template class.
   242   ///
   243   /// The default item writer template class. If some section writer
   244   /// needs to write a value to the stream it will give the default way for it.
   245   ///
   246   /// \author Balazs Dezso
   247   template <typename _Value>
   248   class DefaultWriter {
   249   public:
   250     /// The value type.
   251     typedef _Value Value;
   252     /// \brief Writes the value to the given stream.
   253     ///
   254     /// Writes the value to the given stream.
   255     void write(std::ostream& os, const Value& value) const {
   256       os << value;
   257     }
   258   };
   259 
   260   template <>
   261   class DefaultWriter<std::string> 
   262     : public QuotedStringWriter {};
   263 
   264   template <int length>
   265   class DefaultWriter<char[length]> 
   266     : public QuotedCharArrayWriter {};
   267 
   268   template <int length>
   269   class DefaultWriter<const char[length]> 
   270     : public QuotedCharArrayWriter {};
   271 
   272   template <>
   273   class DefaultWriter<char*> 
   274     : public QuotedCharArrayWriter {};
   275 
   276   template <>
   277   class DefaultWriter<const char*> 
   278     : public QuotedCharArrayWriter {};
   279 
   280   template <typename Item>
   281   class DefaultWriter<std::vector<Item> > 
   282     : public IterableWriter<std::vector<Item> > {};
   283 
   284   template <typename Item>
   285   class DefaultWriter<std::deque<Item> > 
   286     : public IterableWriter<std::deque<Item> > {};
   287 
   288   template <typename Item>
   289   class DefaultWriter<std::list<Item> > 
   290     : public IterableWriter<std::list<Item> > {};
   291   
   292   template <typename Item>
   293   class DefaultWriter<std::set<Item> > 
   294     : public IterableWriter<std::set<Item> > {};
   295 
   296   template <typename Key, typename Value>
   297   class DefaultWriter<std::map<Key, Value> > 
   298     : public IterableWriter<std::map<Key, Value> > {};
   299 
   300   template <typename Item>
   301   class DefaultWriter<std::multiset<Item> > 
   302     : public IterableWriter<std::multiset<Item> > {};
   303 
   304   template <typename Key, typename Value>
   305   class DefaultWriter<std::multimap<Key, Value> > 
   306     : public IterableWriter<std::multimap<Key, Value> > {};
   307 
   308   template <typename First, typename Second>
   309   class DefaultWriter<std::pair<First, Second> > 
   310     : public PairWriter<std::pair<First, Second> > {};
   311 
   312   /// \ingroup item_io
   313   /// \brief Standard WriterTraits for the section writers.
   314   ///
   315   /// Standard WriterTraits for the section writers.
   316   /// It defines standard writing method for all type of value. 
   317   /// \author Balazs Dezso
   318   struct DefaultWriterTraits {
   319 
   320     template <typename _Value>
   321     struct Writer : DefaultWriter<_Value> {
   322       typedef DefaultWriter<_Value> Parent;
   323     };
   324 
   325   };
   326 
   327 }
   328 
   329 #endif