COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/bits/item_writer.h @ 1535:e667cd5c0886

Last change on this file since 1535:e667cd5c0886 was 1535:e667cd5c0886, checked in by Mihaly Barasz, 19 years ago

trivial bugfix for deba

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