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