1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/lemon/bits/item_writer.h Wed May 11 11:50:13 2005 +0000
1.3 @@ -0,0 +1,214 @@
1.4 +/* -*- C++ -*-
1.5 + * src/lemon/bits/item_reader.h - Part of LEMON, a generic C++ optimization library
1.6 + *
1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.9 + *
1.10 + * Permission to use, modify and distribute this software is granted
1.11 + * provided that this copyright notice appears in all copies. For
1.12 + * precise terms see the accompanying LICENSE file.
1.13 + *
1.14 + * This software is provided "AS IS" with no warranty of any kind,
1.15 + * express or implied, and with no claim as to its suitability for any
1.16 + * purpose.
1.17 + *
1.18 + */
1.19 +
1.20 +/// \ingroup io_group
1.21 +/// \file
1.22 +/// \brief Item writer bits for lemon output.
1.23 +
1.24 +#ifndef LEMON_BITS_ITEM_WRITER_H
1.25 +#define LEMON_BITS_ITEM_WRITER_H
1.26 +
1.27 +#include <iostream>
1.28 +#include <string>
1.29 +
1.30 +#include <vector>
1.31 +#include <deque>
1.32 +#include <list>
1.33 +#include <set>
1.34 +
1.35 +namespace lemon {
1.36 +
1.37 + template <typename Value>
1.38 + class DefaultWriter;
1.39 +
1.40 + /// \ingroup io_group
1.41 + /// \brief Writer class for quoted strings.
1.42 + ///
1.43 + /// Writer class for quoted strings. It can process the escape
1.44 + /// sequences in the string.
1.45 + /// \author Balazs Dezso
1.46 + class QuotedStringWriter {
1.47 + public:
1.48 + typedef std::string Value;
1.49 +
1.50 + /// \brief Constructor for the writer.
1.51 + ///
1.52 + /// Constructor for the writer. If the given parameter is true
1.53 + /// the writer creates escape sequences from special characters.
1.54 + QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {}
1.55 +
1.56 + /// \brief Writes a quoted string to the given stream.
1.57 + ///
1.58 + /// Writes a quoted string to the given stream.
1.59 + void write(std::ostream& os, const std::string& value) {
1.60 + os << "\"";
1.61 + if (escaped) {
1.62 + std::ostringstream ls;
1.63 + for (int i = 0; i < (int)value.size(); ++i) {
1.64 + writeEscape(ls, value[i]);
1.65 + }
1.66 + os << ls.str();
1.67 + } else {
1.68 + os << value;
1.69 + }
1.70 + os << "\"";
1.71 + }
1.72 +
1.73 + private:
1.74 +
1.75 + static void writeEscape(std::ostream& os, char c) {
1.76 + switch (c) {
1.77 + case '\\':
1.78 + os << "\\\\";
1.79 + return;
1.80 + case '\"':
1.81 + os << "\\\"";
1.82 + return;
1.83 + case '\'':
1.84 + os << "\\\'";
1.85 + return;
1.86 + case '\?':
1.87 + os << "\\\?";
1.88 + return;
1.89 + case '\a':
1.90 + os << "\\a";
1.91 + return;
1.92 + case '\b':
1.93 + os << "\\b";
1.94 + return;
1.95 + case '\f':
1.96 + os << "\\f";
1.97 + return;
1.98 + case '\r':
1.99 + os << "\\r";
1.100 + return;
1.101 + case '\n':
1.102 + os << "\\n";
1.103 + return;
1.104 + case '\t':
1.105 + os << "\\t";
1.106 + return;
1.107 + case '\v':
1.108 + os << "\\v";
1.109 + return;
1.110 + default:
1.111 + if (c < 0x20) {
1.112 + os << '\\' << std::oct << (int)c;
1.113 + } else {
1.114 + os << c;
1.115 + }
1.116 + return;
1.117 + }
1.118 + }
1.119 + private:
1.120 + bool escaped;
1.121 + };
1.122 +
1.123 + /// \ingroup io_group
1.124 + ///
1.125 + /// \brief Writer for standard containers.
1.126 + ///
1.127 + /// Writer for each iterable standard containers. The representation
1.128 + /// of the container is the values enumerated between an open and a
1.129 + /// close parse.
1.130 + ///
1.131 + /// \author Balazs Dezso
1.132 + template <
1.133 + typename _Container,
1.134 + typename _ItemWriter = DefaultWriter<typename _Container::value_type>
1.135 + >
1.136 + class IterableWriter {
1.137 + public:
1.138 + typedef _Container Value;
1.139 + typedef _ItemWriter ItemWriter;
1.140 +
1.141 + private:
1.142 +
1.143 + ItemWriter item_writer;
1.144 +
1.145 + public:
1.146 +
1.147 + /// \brief Writes the values of the container to the given stream.
1.148 + ///
1.149 + /// Writes the values of the container to the given stream.
1.150 + void write(std::ostream& os, const Value& value) const {
1.151 + typename Value::const_iterator it;
1.152 + os << '(';
1.153 + for (it = value.begin(); it != value.end(); ++it) {
1.154 + item_writer.write(os, *it);
1.155 + os << ' ';
1.156 + }
1.157 + os << ')';
1.158 + }
1.159 +
1.160 + };
1.161 +
1.162 + /// \ingroup io_group
1.163 + ///
1.164 + /// \brief The default item writer template class.
1.165 + ///
1.166 + /// The default item writer template class. If some section writer
1.167 + /// needs to write a value to the stream it will give the default way for it.
1.168 + ///
1.169 + /// \author Balazs Dezso
1.170 + template <typename _Value>
1.171 + class DefaultWriter {
1.172 + public:
1.173 + /// The value type.
1.174 + typedef _Value Value;
1.175 + /// \brief Writes the value to the given stream.
1.176 + ///
1.177 + /// Writes the value to the given stream.
1.178 + void write(std::ostream& os, const Value& value) const {
1.179 + os << value;
1.180 + }
1.181 + };
1.182 +
1.183 + template <typename Item>
1.184 + class DefaultWriter<std::vector<Item> >
1.185 + : public IterableWriter<std::vector<Item> > {};
1.186 +
1.187 + template <typename Item>
1.188 + class DefaultWriter<std::deque<Item> >
1.189 + : public IterableWriter<std::deque<Item> > {};
1.190 +
1.191 + template <typename Item>
1.192 + class DefaultWriter<std::list<Item> >
1.193 + : public IterableWriter<std::list<Item> > {};
1.194 +
1.195 + template <typename Item>
1.196 + class DefaultWriter<std::set<Item> >
1.197 + : public IterableWriter<std::set<Item> > {};
1.198 +
1.199 + template <typename Item>
1.200 + class DefaultWriter<std::multiset<Item> >
1.201 + : public IterableWriter<std::multiset<Item> > {};
1.202 +
1.203 + /// \brief Standard WriterTraits for the section writers.
1.204 + ///
1.205 + /// Standard WriterTraits for the section writers.
1.206 + /// It defines standard writing method for all type of value.
1.207 + /// \author Balazs Dezso
1.208 + struct DefaultWriterTraits {
1.209 +
1.210 + template <typename _Value>
1.211 + struct Writer : DefaultWriter<_Value> {};
1.212 +
1.213 + };
1.214 +
1.215 +}
1.216 +
1.217 +#endif