diff -r d8475431bbbb -r 8e85e6bbefdf lemon/bits/item_writer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lemon/bits/item_writer.h Mon May 23 04:48:14 2005 +0000 @@ -0,0 +1,219 @@ +/* -*- C++ -*- + * lemon/bits/item_reader.h - Part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport + * (Egervary Research Group on Combinatorial Optimization, EGRES). + * + * Permission to use, modify and distribute this software is granted + * provided that this copyright notice appears in all copies. For + * precise terms see the accompanying LICENSE file. + * + * This software is provided "AS IS" with no warranty of any kind, + * express or implied, and with no claim as to its suitability for any + * purpose. + * + */ + +/// \ingroup item_io +/// \file +/// \brief Item writer bits for lemon output. + +#ifndef LEMON_BITS_ITEM_WRITER_H +#define LEMON_BITS_ITEM_WRITER_H + +#include +#include + +#include +#include +#include +#include + +namespace lemon { + + template + class DefaultWriter; + + /// \ingroup item_io + /// \brief Writer class for quoted strings. + /// + /// Writer class for quoted strings. It can process the escape + /// sequences in the string. + /// \author Balazs Dezso + class QuotedStringWriter { + public: + typedef std::string Value; + + /// \brief Constructor for the writer. + /// + /// Constructor for the writer. If the given parameter is true + /// the writer creates escape sequences from special characters. + QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {} + + /// \brief Writes a quoted string to the given stream. + /// + /// Writes a quoted string to the given stream. + void write(std::ostream& os, const std::string& value) { + os << "\""; + if (escaped) { + std::ostringstream ls; + for (int i = 0; i < (int)value.size(); ++i) { + writeEscape(ls, value[i]); + } + os << ls.str(); + } else { + os << value; + } + os << "\""; + } + + private: + + static void writeEscape(std::ostream& os, char c) { + switch (c) { + case '\\': + os << "\\\\"; + return; + case '\"': + os << "\\\""; + return; + case '\'': + os << "\\\'"; + return; + case '\?': + os << "\\\?"; + return; + case '\a': + os << "\\a"; + return; + case '\b': + os << "\\b"; + return; + case '\f': + os << "\\f"; + return; + case '\r': + os << "\\r"; + return; + case '\n': + os << "\\n"; + return; + case '\t': + os << "\\t"; + return; + case '\v': + os << "\\v"; + return; + default: + if (c < 0x20) { + os << '\\' << std::oct << (int)c; + } else { + os << c; + } + return; + } + } + private: + bool escaped; + }; + + /// \ingroup item_io + /// + /// \brief Writer for standard containers. + /// + /// Writer for each iterable standard containers. The representation + /// of the container is the values enumerated between an open and a + /// close parse. + /// + /// \author Balazs Dezso + template < + typename _Container, + typename _ItemWriter = DefaultWriter + > + class IterableWriter { + public: + typedef _Container Value; + typedef _ItemWriter ItemWriter; + + private: + + ItemWriter item_writer; + + public: + + /// \brief Writes the values of the container to the given stream. + /// + /// Writes the values of the container to the given stream. + void write(std::ostream& os, const Value& value) const { + typename Value::const_iterator it; + os << '('; + for (it = value.begin(); it != value.end(); ++it) { + item_writer.write(os, *it); + os << ' '; + } + os << ')'; + } + + }; + + /// \ingroup item_io + /// + /// \brief The default item writer template class. + /// + /// The default item writer template class. If some section writer + /// needs to write a value to the stream it will give the default way for it. + /// + /// \author Balazs Dezso + template + class DefaultWriter { + public: + /// The value type. + typedef _Value Value; + /// \brief Writes the value to the given stream. + /// + /// Writes the value to the given stream. + void write(std::ostream& os, const Value& value) const { + os << value; + } + }; + + template <> + class DefaultWriter + : public QuotedStringWriter {}; + + template + class DefaultWriter > + : public IterableWriter > {}; + + template + class DefaultWriter > + : public IterableWriter > {}; + + template + class DefaultWriter > + : public IterableWriter > {}; + + template + class DefaultWriter > + : public IterableWriter > {}; + + template + class DefaultWriter > + : public IterableWriter > {}; + + /// \ingroup item_io + /// \brief Standard WriterTraits for the section writers. + /// + /// Standard WriterTraits for the section writers. + /// It defines standard writing method for all type of value. + /// \author Balazs Dezso + struct DefaultWriterTraits { + + template + struct Writer : DefaultWriter<_Value> {}; + + }; + +} + +#endif