deba@1409
|
1 |
/* -*- C++ -*-
|
ladanyi@1435
|
2 |
* lemon/bits/item_reader.h - Part of LEMON, a generic C++ optimization library
|
deba@1409
|
3 |
*
|
alpar@1875
|
4 |
* Copyright (C) 2006 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@1852
|
56 |
void write(std::ostream& os, const std::string& value) const {
|
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@1852
|
139 |
void write(std::ostream& os, const char* value) const {
|
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@1852
|
172 |
IterableWriter(const ItemWriter& _item_writer = ItemWriter())
|
deba@1852
|
173 |
: item_writer(_item_writer) {}
|
deba@1852
|
174 |
|
deba@1409
|
175 |
/// \brief Writes the values of the container to the given stream.
|
deba@1409
|
176 |
///
|
deba@1409
|
177 |
/// Writes the values of the container to the given stream.
|
deba@1409
|
178 |
void write(std::ostream& os, const Value& value) const {
|
deba@1409
|
179 |
typename Value::const_iterator it;
|
deba@1409
|
180 |
os << '(';
|
deba@1409
|
181 |
for (it = value.begin(); it != value.end(); ++it) {
|
deba@1409
|
182 |
item_writer.write(os, *it);
|
deba@1409
|
183 |
os << ' ';
|
deba@1409
|
184 |
}
|
deba@1409
|
185 |
os << ')';
|
deba@1409
|
186 |
}
|
deba@1409
|
187 |
|
deba@1409
|
188 |
};
|
deba@1409
|
189 |
|
deba@1415
|
190 |
/// \ingroup item_io
|
deba@1852
|
191 |
///
|
deba@1852
|
192 |
/// \brief Writer for standard pairs.
|
deba@1852
|
193 |
///
|
deba@1852
|
194 |
/// Writer for standard pairs. The representation of a pair is
|
deba@1852
|
195 |
/// \code ( first_value => second_value ) \endcode.
|
deba@1852
|
196 |
/// \author Balazs Dezso
|
deba@1852
|
197 |
template <typename _Pair,
|
deba@1852
|
198 |
typename _FirstWriter =
|
deba@1852
|
199 |
DefaultWriter<typename _Pair::first_type>,
|
deba@1852
|
200 |
typename _SecondWriter =
|
deba@1852
|
201 |
DefaultWriter<typename _Pair::second_type> >
|
deba@1852
|
202 |
class PairWriter {
|
deba@1852
|
203 |
public:
|
deba@1852
|
204 |
|
deba@1852
|
205 |
typedef _Pair Value;
|
deba@1852
|
206 |
|
deba@1852
|
207 |
typedef _FirstWriter FirstWriter;
|
deba@1852
|
208 |
typedef _SecondWriter SecondWriter;
|
deba@1852
|
209 |
|
deba@1852
|
210 |
private:
|
deba@1852
|
211 |
|
deba@1852
|
212 |
FirstWriter first_writer;
|
deba@1852
|
213 |
SecondWriter second_writer;
|
deba@1852
|
214 |
|
deba@1852
|
215 |
public:
|
deba@1852
|
216 |
|
deba@1852
|
217 |
/// \brief Constructor.
|
deba@1852
|
218 |
///
|
deba@1852
|
219 |
/// Constructor for the PairWriter.
|
deba@1852
|
220 |
PairWriter(const FirstWriter& _first_writer = FirstWriter(),
|
deba@1852
|
221 |
const SecondWriter& _second_writer = SecondWriter())
|
deba@1852
|
222 |
: first_writer(_first_writer), second_writer(_second_writer) {}
|
deba@1852
|
223 |
|
deba@1852
|
224 |
/// \brief Writes the pair from the given stream.
|
deba@1852
|
225 |
///
|
deba@1852
|
226 |
/// Writes the pair from the given stream.
|
deba@1852
|
227 |
void write(std::ostream& os, const Value& value) const {
|
deba@1852
|
228 |
os << "( ";
|
deba@1852
|
229 |
first_writer.write(os, value.first);
|
deba@1852
|
230 |
os << " => ";
|
deba@1852
|
231 |
second_writer.write(os, value.second);
|
deba@1852
|
232 |
os << " )";
|
deba@1852
|
233 |
}
|
deba@1852
|
234 |
|
deba@1852
|
235 |
};
|
deba@1852
|
236 |
|
deba@1852
|
237 |
/// \ingroup item_io
|
deba@1409
|
238 |
///
|
deba@1409
|
239 |
/// \brief The default item writer template class.
|
deba@1409
|
240 |
///
|
deba@1409
|
241 |
/// The default item writer template class. If some section writer
|
deba@1409
|
242 |
/// needs to write a value to the stream it will give the default way for it.
|
deba@1409
|
243 |
///
|
deba@1409
|
244 |
/// \author Balazs Dezso
|
deba@1409
|
245 |
template <typename _Value>
|
deba@1409
|
246 |
class DefaultWriter {
|
deba@1409
|
247 |
public:
|
deba@1409
|
248 |
/// The value type.
|
deba@1409
|
249 |
typedef _Value Value;
|
deba@1409
|
250 |
/// \brief Writes the value to the given stream.
|
deba@1409
|
251 |
///
|
deba@1409
|
252 |
/// Writes the value to the given stream.
|
deba@1409
|
253 |
void write(std::ostream& os, const Value& value) const {
|
deba@1409
|
254 |
os << value;
|
deba@1409
|
255 |
}
|
deba@1409
|
256 |
};
|
deba@1409
|
257 |
|
deba@1429
|
258 |
template <>
|
deba@1429
|
259 |
class DefaultWriter<std::string>
|
ladanyi@1434
|
260 |
: public QuotedStringWriter {};
|
deba@1429
|
261 |
|
deba@1533
|
262 |
template <int length>
|
deba@1533
|
263 |
class DefaultWriter<char[length]>
|
deba@1533
|
264 |
: public QuotedCharArrayWriter {};
|
deba@1533
|
265 |
|
deba@1533
|
266 |
template <int length>
|
deba@1533
|
267 |
class DefaultWriter<const char[length]>
|
deba@1533
|
268 |
: public QuotedCharArrayWriter {};
|
deba@1533
|
269 |
|
deba@1852
|
270 |
template <>
|
deba@1852
|
271 |
class DefaultWriter<char*>
|
deba@1852
|
272 |
: public QuotedCharArrayWriter {};
|
deba@1852
|
273 |
|
deba@1852
|
274 |
template <>
|
deba@1852
|
275 |
class DefaultWriter<const char*>
|
deba@1852
|
276 |
: public QuotedCharArrayWriter {};
|
deba@1852
|
277 |
|
deba@1409
|
278 |
template <typename Item>
|
deba@1409
|
279 |
class DefaultWriter<std::vector<Item> >
|
deba@1409
|
280 |
: public IterableWriter<std::vector<Item> > {};
|
deba@1409
|
281 |
|
deba@1409
|
282 |
template <typename Item>
|
deba@1409
|
283 |
class DefaultWriter<std::deque<Item> >
|
deba@1409
|
284 |
: public IterableWriter<std::deque<Item> > {};
|
deba@1409
|
285 |
|
deba@1409
|
286 |
template <typename Item>
|
deba@1409
|
287 |
class DefaultWriter<std::list<Item> >
|
deba@1409
|
288 |
: public IterableWriter<std::list<Item> > {};
|
deba@1409
|
289 |
|
deba@1409
|
290 |
template <typename Item>
|
deba@1409
|
291 |
class DefaultWriter<std::set<Item> >
|
deba@1409
|
292 |
: public IterableWriter<std::set<Item> > {};
|
deba@1409
|
293 |
|
deba@1852
|
294 |
template <typename Key, typename Value>
|
deba@1852
|
295 |
class DefaultWriter<std::map<Key, Value> >
|
deba@1852
|
296 |
: public IterableWriter<std::map<Key, Value> > {};
|
deba@1852
|
297 |
|
deba@1409
|
298 |
template <typename Item>
|
deba@1409
|
299 |
class DefaultWriter<std::multiset<Item> >
|
deba@1409
|
300 |
: public IterableWriter<std::multiset<Item> > {};
|
deba@1409
|
301 |
|
deba@1852
|
302 |
template <typename Key, typename Value>
|
deba@1852
|
303 |
class DefaultWriter<std::multimap<Key, Value> >
|
deba@1852
|
304 |
: public IterableWriter<std::multimap<Key, Value> > {};
|
deba@1852
|
305 |
|
deba@1852
|
306 |
template <typename First, typename Second>
|
deba@1852
|
307 |
class DefaultWriter<std::pair<First, Second> >
|
deba@1852
|
308 |
: public PairWriter<std::pair<First, Second> > {};
|
deba@1852
|
309 |
|
deba@1415
|
310 |
/// \ingroup item_io
|
deba@1409
|
311 |
/// \brief Standard WriterTraits for the section writers.
|
deba@1409
|
312 |
///
|
deba@1409
|
313 |
/// Standard WriterTraits for the section writers.
|
deba@1409
|
314 |
/// It defines standard writing method for all type of value.
|
deba@1409
|
315 |
/// \author Balazs Dezso
|
deba@1409
|
316 |
struct DefaultWriterTraits {
|
deba@1409
|
317 |
|
deba@1409
|
318 |
template <typename _Value>
|
deba@1533
|
319 |
struct Writer : DefaultWriter<_Value> {
|
deba@1533
|
320 |
typedef DefaultWriter<_Value> Parent;
|
deba@1533
|
321 |
};
|
deba@1409
|
322 |
|
deba@1409
|
323 |
};
|
deba@1409
|
324 |
|
deba@1409
|
325 |
}
|
deba@1409
|
326 |
|
deba@1409
|
327 |
#endif
|