lemon/bits/item_writer.h
author deba
Mon, 17 Oct 2005 10:28:48 +0000
changeset 1729 06f939455cb1
parent 1533 43c7b3085212
child 1852 ffa7c6e96330
permissions -rw-r--r--
Removing signal/commit Change from alteration notifier

It makes slower the change Target/Source functions
and used only by the In/Out DegMap
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) {
klao@1535
    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@1533
   121
  /// \brief Writer class for quoted char array.
deba@1533
   122
  ///
deba@1533
   123
  /// Writer class for quoted char array. It can process the escape
deba@1533
   124
  /// sequences in the char array.
deba@1533
   125
  /// \author Balazs Dezso
deba@1533
   126
  class QuotedCharArrayWriter {
deba@1533
   127
  public:
deba@1533
   128
    typedef const char* Value;
deba@1533
   129
deba@1533
   130
    /// \brief Constructor for the writer.
deba@1533
   131
    ///
deba@1533
   132
    /// Constructor for the writer. If the given parameter is true
deba@1533
   133
    /// the writer creates escape sequences from special characters.
deba@1533
   134
    QuotedCharArrayWriter(bool _escaped = true) : escaped(_escaped) {}
deba@1533
   135
deba@1533
   136
    /// \brief Writes a quoted char array to the given stream.
deba@1533
   137
    ///
deba@1533
   138
    /// Writes a quoted char array to the given stream.
deba@1533
   139
    void write(std::ostream& os, const char* value) {
deba@1533
   140
      QuotedStringWriter(escaped).write(os, std::string(value));
deba@1533
   141
    }
deba@1533
   142
deba@1533
   143
  private:    
deba@1533
   144
    bool escaped;
deba@1533
   145
  };
deba@1533
   146
deba@1533
   147
deba@1533
   148
  /// \ingroup item_io
deba@1409
   149
  ///
deba@1409
   150
  /// \brief Writer for standard containers.
deba@1409
   151
  ///
deba@1409
   152
  /// Writer for each iterable standard containers. The representation
deba@1409
   153
  /// of the container is the values enumerated between an open and a
deba@1409
   154
  /// close parse. 
deba@1409
   155
  ///
deba@1409
   156
  /// \author Balazs Dezso
deba@1409
   157
  template <
deba@1409
   158
    typename _Container, 
deba@1409
   159
    typename _ItemWriter = DefaultWriter<typename _Container::value_type> 
deba@1409
   160
  >
deba@1409
   161
  class IterableWriter {
deba@1409
   162
  public:
deba@1409
   163
    typedef _Container Value;
deba@1409
   164
    typedef _ItemWriter ItemWriter;
deba@1409
   165
deba@1409
   166
  private:
deba@1409
   167
deba@1409
   168
    ItemWriter item_writer;
deba@1409
   169
deba@1409
   170
  public:
deba@1409
   171
deba@1409
   172
    /// \brief Writes the values of the container to the given stream.
deba@1409
   173
    ///
deba@1409
   174
    /// Writes the values of the container to the given stream.
deba@1409
   175
    void write(std::ostream& os, const Value& value) const {
deba@1409
   176
      typename Value::const_iterator it;
deba@1409
   177
      os << '(';
deba@1409
   178
      for (it = value.begin(); it != value.end(); ++it) {
deba@1409
   179
	item_writer.write(os, *it);
deba@1409
   180
	os << ' ';
deba@1409
   181
      }
deba@1409
   182
      os << ')';
deba@1409
   183
    }
deba@1409
   184
deba@1409
   185
  };
deba@1409
   186
deba@1415
   187
  /// \ingroup item_io
deba@1409
   188
  /// 
deba@1409
   189
  /// \brief The default item writer template class.
deba@1409
   190
  ///
deba@1409
   191
  /// The default item writer template class. If some section writer
deba@1409
   192
  /// needs to write a value to the stream it will give the default way for it.
deba@1409
   193
  ///
deba@1409
   194
  /// \author Balazs Dezso
deba@1409
   195
  template <typename _Value>
deba@1409
   196
  class DefaultWriter {
deba@1409
   197
  public:
deba@1409
   198
    /// The value type.
deba@1409
   199
    typedef _Value Value;
deba@1409
   200
    /// \brief Writes the value to the given stream.
deba@1409
   201
    ///
deba@1409
   202
    /// Writes the value to the given stream.
deba@1409
   203
    void write(std::ostream& os, const Value& value) const {
deba@1409
   204
      os << value;
deba@1409
   205
    }
deba@1409
   206
  };
deba@1409
   207
deba@1429
   208
  template <>
deba@1429
   209
  class DefaultWriter<std::string> 
ladanyi@1434
   210
    : public QuotedStringWriter {};
deba@1429
   211
deba@1533
   212
  template <int length>
deba@1533
   213
  class DefaultWriter<char[length]> 
deba@1533
   214
    : public QuotedCharArrayWriter {};
deba@1533
   215
deba@1533
   216
  template <int length>
deba@1533
   217
  class DefaultWriter<const char[length]> 
deba@1533
   218
    : public QuotedCharArrayWriter {};
deba@1533
   219
deba@1409
   220
  template <typename Item>
deba@1409
   221
  class DefaultWriter<std::vector<Item> > 
deba@1409
   222
    : public IterableWriter<std::vector<Item> > {};
deba@1409
   223
deba@1409
   224
  template <typename Item>
deba@1409
   225
  class DefaultWriter<std::deque<Item> > 
deba@1409
   226
    : public IterableWriter<std::deque<Item> > {};
deba@1409
   227
deba@1409
   228
  template <typename Item>
deba@1409
   229
  class DefaultWriter<std::list<Item> > 
deba@1409
   230
    : public IterableWriter<std::list<Item> > {};
deba@1409
   231
  
deba@1409
   232
  template <typename Item>
deba@1409
   233
  class DefaultWriter<std::set<Item> > 
deba@1409
   234
    : public IterableWriter<std::set<Item> > {};
deba@1409
   235
deba@1409
   236
  template <typename Item>
deba@1409
   237
  class DefaultWriter<std::multiset<Item> > 
deba@1409
   238
    : public IterableWriter<std::multiset<Item> > {};
deba@1409
   239
deba@1415
   240
  /// \ingroup item_io
deba@1409
   241
  /// \brief Standard WriterTraits for the section writers.
deba@1409
   242
  ///
deba@1409
   243
  /// Standard WriterTraits for the section writers.
deba@1409
   244
  /// It defines standard writing method for all type of value. 
deba@1409
   245
  /// \author Balazs Dezso
deba@1409
   246
  struct DefaultWriterTraits {
deba@1409
   247
deba@1409
   248
    template <typename _Value>
deba@1533
   249
    struct Writer : DefaultWriter<_Value> {
deba@1533
   250
      typedef DefaultWriter<_Value> Parent;
deba@1533
   251
    };
deba@1409
   252
deba@1409
   253
  };
deba@1409
   254
deba@1409
   255
}
deba@1409
   256
deba@1409
   257
#endif