deba@127: /* -*- C++ -*- deba@127: * deba@127: * This file is a part of LEMON, a generic C++ optimization library deba@127: * deba@127: * Copyright (C) 2003-2008 deba@127: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@127: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@127: * deba@127: * Permission to use, modify and distribute this software is granted deba@127: * provided that this copyright notice appears in all copies. For deba@127: * precise terms see the accompanying LICENSE file. deba@127: * deba@127: * This software is provided "AS IS" with no warranty of any kind, deba@127: * express or implied, and with no claim as to its suitability for any deba@127: * purpose. deba@127: * deba@127: */ deba@127: deba@127: ///\ingroup lemon_io deba@127: ///\file kpeter@192: ///\brief \ref lgf-format "Lemon Graph Format" reader. deba@127: deba@127: deba@127: #ifndef LEMON_LGF_READER_H deba@127: #define LEMON_LGF_READER_H deba@127: deba@127: #include deba@127: #include deba@127: #include deba@127: deba@127: #include deba@127: #include deba@127: deba@127: #include deba@127: #include deba@127: deba@127: #include deba@127: deba@127: #include deba@127: #include deba@127: deba@127: namespace lemon { deba@127: deba@127: namespace _reader_bits { deba@127: deba@127: template deba@127: struct DefaultConverter { deba@127: Value operator()(const std::string& str) { deba@127: std::istringstream is(str); deba@127: Value value; deba@127: is >> value; deba@127: deba@127: char c; deba@127: if (is >> std::ws >> c) { deba@127: throw DataFormatError("Remaining characters in token"); deba@127: } deba@127: return value; deba@127: } deba@127: }; deba@127: deba@127: template <> deba@127: struct DefaultConverter { deba@127: std::string operator()(const std::string& str) { deba@127: return str; deba@127: } deba@127: }; deba@127: deba@127: template deba@127: class MapStorageBase { deba@127: public: deba@127: typedef _Item Item; deba@127: deba@127: public: deba@127: MapStorageBase() {} deba@127: virtual ~MapStorageBase() {} deba@127: deba@127: virtual void set(const Item& item, const std::string& value) = 0; deba@127: deba@127: }; deba@127: deba@127: template > deba@127: class MapStorage : public MapStorageBase<_Item> { deba@127: public: deba@127: typedef _Map Map; deba@127: typedef _Converter Converter; deba@127: typedef _Item Item; deba@127: deba@127: private: deba@127: Map& _map; deba@127: Converter _converter; deba@127: deba@127: public: deba@127: MapStorage(Map& map, const Converter& converter = Converter()) deba@127: : _map(map), _converter(converter) {} deba@127: virtual ~MapStorage() {} deba@127: deba@127: virtual void set(const Item& item ,const std::string& value) { deba@127: _map.set(item, _converter(value)); deba@127: } deba@127: }; deba@127: deba@165: template > deba@165: class GraphArcMapStorage : public MapStorageBase { deba@165: public: deba@165: typedef _Map Map; deba@165: typedef _Converter Converter; deba@165: typedef _Graph Graph; deba@165: typedef typename Graph::Edge Item; deba@165: static const bool dir = _dir; deba@165: deba@165: private: deba@165: const Graph& _graph; deba@165: Map& _map; deba@165: Converter _converter; deba@165: deba@165: public: deba@165: GraphArcMapStorage(const Graph& graph, Map& map, deba@165: const Converter& converter = Converter()) deba@165: : _graph(graph), _map(map), _converter(converter) {} deba@165: virtual ~GraphArcMapStorage() {} deba@165: deba@165: virtual void set(const Item& item ,const std::string& value) { deba@165: _map.set(_graph.direct(item, dir), _converter(value)); deba@165: } deba@165: }; deba@165: deba@127: class ValueStorageBase { deba@127: public: deba@127: ValueStorageBase() {} deba@127: virtual ~ValueStorageBase() {} deba@127: deba@127: virtual void set(const std::string&) = 0; deba@127: }; deba@127: deba@127: template > deba@127: class ValueStorage : public ValueStorageBase { deba@127: public: deba@127: typedef _Value Value; deba@127: typedef _Converter Converter; deba@127: deba@127: private: deba@127: Value& _value; deba@127: Converter _converter; deba@127: deba@127: public: deba@127: ValueStorage(Value& value, const Converter& converter = Converter()) deba@127: : _value(value), _converter(converter) {} deba@127: deba@127: virtual void set(const std::string& value) { deba@127: _value = _converter(value); deba@127: } deba@127: }; deba@127: deba@127: template deba@127: struct MapLookUpConverter { deba@127: const std::map& _map; deba@127: deba@127: MapLookUpConverter(const std::map& map) deba@127: : _map(map) {} deba@127: deba@127: Value operator()(const std::string& str) { deba@127: typename std::map::const_iterator it = deba@127: _map.find(str); deba@127: if (it == _map.end()) { deba@127: std::ostringstream msg; deba@127: msg << "Item not found: " << str; deba@127: throw DataFormatError(msg.str().c_str()); deba@127: } deba@127: return it->second; deba@127: } deba@127: }; deba@127: deba@165: template deba@165: struct GraphArcLookUpConverter { deba@165: const Graph& _graph; deba@165: const std::map& _map; deba@165: deba@165: GraphArcLookUpConverter(const Graph& graph, deba@165: const std::map& map) deba@165: : _graph(graph), _map(map) {} deba@165: deba@165: typename Graph::Arc operator()(const std::string& str) { deba@165: if (str.empty() || (str[0] != '+' && str[0] != '-')) { deba@165: throw DataFormatError("Item must start with '+' or '-'"); deba@165: } deba@165: typename std::map deba@165: ::const_iterator it = _map.find(str.substr(1)); deba@165: if (it == _map.end()) { deba@165: throw DataFormatError("Item not found"); deba@165: } deba@165: return _graph.direct(it->second, str[0] == '+'); deba@165: } deba@165: }; deba@165: deba@127: bool isWhiteSpace(char c) { deba@127: return c == ' ' || c == '\t' || c == '\v' || deba@127: c == '\n' || c == '\r' || c == '\f'; deba@127: } deba@127: deba@127: bool isOct(char c) { deba@127: return '0' <= c && c <='7'; deba@127: } deba@127: deba@127: int valueOct(char c) { deba@127: LEMON_ASSERT(isOct(c), "The character is not octal."); deba@127: return c - '0'; deba@127: } deba@127: deba@127: bool isHex(char c) { deba@127: return ('0' <= c && c <= '9') || deba@127: ('a' <= c && c <= 'z') || deba@127: ('A' <= c && c <= 'Z'); deba@127: } deba@127: deba@127: int valueHex(char c) { deba@127: LEMON_ASSERT(isHex(c), "The character is not hexadecimal."); deba@127: if ('0' <= c && c <= '9') return c - '0'; deba@127: if ('a' <= c && c <= 'z') return c - 'a' + 10; deba@127: return c - 'A' + 10; deba@127: } deba@127: deba@127: bool isIdentifierFirstChar(char c) { deba@127: return ('a' <= c && c <= 'z') || deba@127: ('A' <= c && c <= 'Z') || c == '_'; deba@127: } deba@127: deba@127: bool isIdentifierChar(char c) { deba@127: return isIdentifierFirstChar(c) || deba@127: ('0' <= c && c <= '9'); deba@127: } deba@127: deba@127: char readEscape(std::istream& is) { deba@127: char c; deba@127: if (!is.get(c)) deba@127: throw DataFormatError("Escape format error"); deba@127: deba@127: switch (c) { deba@127: case '\\': deba@127: return '\\'; deba@127: case '\"': deba@127: return '\"'; deba@127: case '\'': deba@127: return '\''; deba@127: case '\?': deba@127: return '\?'; deba@127: case 'a': deba@127: return '\a'; deba@127: case 'b': deba@127: return '\b'; deba@127: case 'f': deba@127: return '\f'; deba@127: case 'n': deba@127: return '\n'; deba@127: case 'r': deba@127: return '\r'; deba@127: case 't': deba@127: return '\t'; deba@127: case 'v': deba@127: return '\v'; deba@127: case 'x': deba@127: { deba@127: int code; deba@127: if (!is.get(c) || !isHex(c)) deba@127: throw DataFormatError("Escape format error"); deba@127: else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c); deba@127: else code = code * 16 + valueHex(c); deba@127: return code; deba@127: } deba@127: default: deba@127: { deba@127: int code; deba@127: if (!isOct(c)) deba@127: throw DataFormatError("Escape format error"); deba@127: else if (code = valueOct(c), !is.get(c) || !isOct(c)) deba@127: is.putback(c); deba@127: else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c)) deba@127: is.putback(c); deba@127: else code = code * 8 + valueOct(c); deba@127: return code; deba@127: } deba@127: } deba@127: } deba@127: deba@127: std::istream& readToken(std::istream& is, std::string& str) { deba@127: std::ostringstream os; deba@127: deba@127: char c; deba@127: is >> std::ws; deba@127: deba@127: if (!is.get(c)) deba@127: return is; deba@127: deba@127: if (c == '\"') { deba@127: while (is.get(c) && c != '\"') { deba@127: if (c == '\\') deba@127: c = readEscape(is); deba@127: os << c; deba@127: } deba@127: if (!is) deba@127: throw DataFormatError("Quoted format error"); deba@127: } else { deba@127: is.putback(c); deba@127: while (is.get(c) && !isWhiteSpace(c)) { deba@127: if (c == '\\') deba@127: c = readEscape(is); deba@127: os << c; deba@127: } deba@127: if (!is) { deba@127: is.clear(); deba@127: } else { deba@127: is.putback(c); deba@127: } deba@127: } deba@127: str = os.str(); deba@127: return is; deba@127: } deba@162: deba@162: class Section { deba@162: public: deba@162: virtual ~Section() {} deba@162: virtual void process(std::istream& is, int& line_num) = 0; deba@162: }; deba@162: deba@162: template deba@162: class LineSection : public Section { deba@162: private: deba@162: deba@162: Functor _functor; deba@162: deba@162: public: deba@162: deba@162: LineSection(const Functor& functor) : _functor(functor) {} deba@162: virtual ~LineSection() {} deba@162: deba@162: virtual void process(std::istream& is, int& line_num) { deba@162: char c; deba@162: std::string line; deba@162: while (is.get(c) && c != '@') { deba@162: if (c == '\n') { deba@162: ++line_num; deba@162: } else if (c == '#') { deba@162: getline(is, line); deba@162: ++line_num; deba@162: } else if (!isWhiteSpace(c)) { deba@162: is.putback(c); deba@162: getline(is, line); deba@162: _functor(line); deba@162: ++line_num; deba@162: } deba@162: } deba@162: if (is) is.putback(c); deba@162: else if (is.eof()) is.clear(); deba@162: } deba@162: }; deba@162: deba@162: template deba@162: class StreamSection : public Section { deba@162: private: deba@162: deba@162: Functor _functor; deba@162: deba@162: public: deba@162: deba@162: StreamSection(const Functor& functor) : _functor(functor) {} deba@162: virtual ~StreamSection() {} deba@162: deba@162: virtual void process(std::istream& is, int& line_num) { deba@162: _functor(is, line_num); deba@162: char c; deba@162: std::string line; deba@162: while (is.get(c) && c != '@') { deba@162: if (c == '\n') { deba@162: ++line_num; deba@162: } else if (!isWhiteSpace(c)) { deba@162: getline(is, line); deba@162: ++line_num; deba@162: } deba@162: } deba@162: if (is) is.putback(c); deba@162: else if (is.eof()) is.clear(); deba@162: } deba@162: }; deba@127: deba@127: } alpar@156: deba@190: template deba@190: class DigraphReader; deba@190: deba@190: template deba@190: DigraphReader digraphReader(std::istream& is, Digraph& digraph); deba@190: deba@190: template deba@190: DigraphReader digraphReader(const std::string& fn, Digraph& digraph); deba@190: deba@190: template deba@190: DigraphReader digraphReader(const char *fn, Digraph& digraph); deba@190: alpar@156: /// \ingroup lemon_io alpar@156: /// kpeter@192: /// \brief \ref lgf-format "LGF" reader for directed graphs alpar@156: /// alpar@156: /// This utility reads an \ref lgf-format "LGF" file. alpar@156: /// alpar@156: /// The reading method does a batch processing. The user creates a alpar@156: /// reader object, then various reading rules can be added to the alpar@156: /// reader, and eventually the reading is executed with the \c run() alpar@156: /// member function. A map reading rule can be added to the reader alpar@156: /// with the \c nodeMap() or \c arcMap() members. An optional deba@162: /// converter parameter can also be added as a standard functor kpeter@192: /// converting from \c std::string to the value type of the map. If it deba@162: /// is set, it will determine how the tokens in the file should be kpeter@192: /// converted to the value type of the map. If the functor is not set, deba@162: /// then a default conversion will be used. One map can be read into deba@162: /// multiple map objects at the same time. The \c attribute(), \c deba@162: /// node() and \c arc() functions are used to add attribute reading deba@162: /// rules. alpar@156: /// alpar@156: ///\code kpeter@192: /// DigraphReader(std::cin, digraph). kpeter@192: /// nodeMap("coordinates", coord_map). kpeter@192: /// arcMap("capacity", cap_map). kpeter@192: /// node("source", src). kpeter@192: /// node("target", trg). kpeter@192: /// attribute("caption", caption). kpeter@192: /// run(); alpar@156: ///\endcode alpar@156: /// alpar@156: /// By default the reader uses the first section in the file of the alpar@156: /// proper type. If a section has an optional name, then it can be deba@162: /// selected for reading by giving an optional name parameter to the deba@189: /// \c nodes(), \c arcs() or \c attributes() functions. alpar@156: /// alpar@156: /// The \c useNodes() and \c useArcs() functions are used to tell the reader alpar@156: /// that the nodes or arcs should not be constructed (added to the alpar@156: /// graph) during the reading, but instead the label map of the items alpar@156: /// are given as a parameter of these functions. An kpeter@192: /// application of these functions is multipass reading, which is kpeter@192: /// important if two \c \@arcs sections must be read from the kpeter@192: /// file. In this case the first phase would read the node set and one alpar@156: /// of the arc sets, while the second phase would read the second arc alpar@156: /// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet). alpar@156: /// The previously read label node map should be passed to the \c alpar@156: /// useNodes() functions. Another application of multipass reading when kpeter@192: /// paths are given as a node map or an arc map. It is impossible to read this in alpar@156: /// a single pass, because the arcs are not constructed when the node alpar@156: /// maps are read. deba@127: template deba@127: class DigraphReader { deba@127: public: deba@127: deba@127: typedef _Digraph Digraph; deba@148: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); deba@127: deba@127: private: deba@127: deba@127: deba@127: std::istream* _is; deba@127: bool local_is; deba@127: deba@127: Digraph& _digraph; deba@127: deba@127: std::string _nodes_caption; deba@127: std::string _arcs_caption; deba@127: std::string _attributes_caption; deba@127: deba@127: typedef std::map NodeIndex; deba@127: NodeIndex _node_index; deba@127: typedef std::map ArcIndex; deba@127: ArcIndex _arc_index; deba@127: deba@127: typedef std::vector*> > NodeMaps; deba@127: NodeMaps _node_maps; deba@127: deba@127: typedef std::vector*> >ArcMaps; deba@127: ArcMaps _arc_maps; deba@127: deba@127: typedef std::multimap deba@127: Attributes; deba@127: Attributes _attributes; deba@127: deba@127: bool _use_nodes; deba@127: bool _use_arcs; deba@127: deba@188: bool _skip_nodes; deba@188: bool _skip_arcs; deba@188: deba@127: int line_num; deba@127: std::istringstream line; deba@127: deba@127: public: deba@127: alpar@156: /// \brief Constructor alpar@156: /// alpar@156: /// Construct a directed graph reader, which reads from the given alpar@156: /// input stream. deba@127: DigraphReader(std::istream& is, Digraph& digraph) deba@127: : _is(&is), local_is(false), _digraph(digraph), deba@188: _use_nodes(false), _use_arcs(false), deba@188: _skip_nodes(false), _skip_arcs(false) {} deba@127: alpar@156: /// \brief Constructor alpar@156: /// alpar@156: /// Construct a directed graph reader, which reads from the given alpar@156: /// file. deba@127: DigraphReader(const std::string& fn, Digraph& digraph) deba@127: : _is(new std::ifstream(fn.c_str())), local_is(true), _digraph(digraph), deba@188: _use_nodes(false), _use_arcs(false), deba@188: _skip_nodes(false), _skip_arcs(false) {} alpar@156: alpar@156: /// \brief Constructor alpar@156: /// alpar@156: /// Construct a directed graph reader, which reads from the given alpar@156: /// file. deba@127: DigraphReader(const char* fn, Digraph& digraph) deba@127: : _is(new std::ifstream(fn)), local_is(true), _digraph(digraph), deba@188: _use_nodes(false), _use_arcs(false), deba@188: _skip_nodes(false), _skip_arcs(false) {} deba@127: alpar@156: /// \brief Destructor deba@127: ~DigraphReader() { deba@127: for (typename NodeMaps::iterator it = _node_maps.begin(); deba@127: it != _node_maps.end(); ++it) { deba@127: delete it->second; deba@127: } deba@127: deba@127: for (typename ArcMaps::iterator it = _arc_maps.begin(); deba@127: it != _arc_maps.end(); ++it) { deba@127: delete it->second; deba@127: } deba@127: deba@127: for (typename Attributes::iterator it = _attributes.begin(); deba@127: it != _attributes.end(); ++it) { deba@127: delete it->second; deba@127: } deba@127: deba@127: if (local_is) { deba@127: delete _is; deba@127: } deba@127: deba@127: } deba@127: deba@127: private: deba@190: deba@190: friend DigraphReader digraphReader<>(std::istream& is, deba@190: Digraph& digraph); deba@190: friend DigraphReader digraphReader<>(const std::string& fn, deba@190: Digraph& digraph); deba@190: friend DigraphReader digraphReader<>(const char *fn, deba@190: Digraph& digraph); deba@190: deba@190: DigraphReader(DigraphReader& other) deba@190: : _is(other._is), local_is(other.local_is), _digraph(other._digraph), deba@190: _use_nodes(other._use_nodes), _use_arcs(other._use_arcs), deba@190: _skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) { deba@190: deba@190: other._is = 0; deba@190: other.local_is = false; deba@190: deba@190: _node_index.swap(other._node_index); deba@190: _arc_index.swap(other._arc_index); deba@190: deba@190: _node_maps.swap(other._node_maps); deba@190: _arc_maps.swap(other._arc_maps); deba@190: _attributes.swap(other._attributes); deba@190: deba@190: _nodes_caption = other._nodes_caption; deba@190: _arcs_caption = other._arcs_caption; deba@190: _attributes_caption = other._attributes_caption; deba@190: deba@190: } deba@190: deba@127: DigraphReader& operator=(const DigraphReader&); deba@127: deba@127: public: deba@127: alpar@156: /// \name Reading rules alpar@156: /// @{ alpar@156: alpar@156: /// \brief Node map reading rule alpar@156: /// alpar@156: /// Add a node map reading rule to the reader. deba@127: template deba@127: DigraphReader& nodeMap(const std::string& caption, Map& map) { deba@127: checkConcept, Map>(); deba@127: _reader_bits::MapStorageBase* storage = deba@127: new _reader_bits::MapStorage(map); deba@127: _node_maps.push_back(std::make_pair(caption, storage)); deba@127: return *this; deba@127: } deba@127: alpar@156: /// \brief Node map reading rule alpar@156: /// alpar@156: /// Add a node map reading rule with specialized converter to the alpar@156: /// reader. deba@127: template deba@127: DigraphReader& nodeMap(const std::string& caption, Map& map, deba@127: const Converter& converter = Converter()) { deba@127: checkConcept, Map>(); deba@127: _reader_bits::MapStorageBase* storage = deba@127: new _reader_bits::MapStorage(map, converter); deba@127: _node_maps.push_back(std::make_pair(caption, storage)); deba@127: return *this; deba@127: } deba@127: alpar@156: /// \brief Arc map reading rule alpar@156: /// alpar@156: /// Add an arc map reading rule to the reader. deba@127: template deba@127: DigraphReader& arcMap(const std::string& caption, Map& map) { deba@127: checkConcept, Map>(); deba@127: _reader_bits::MapStorageBase* storage = deba@127: new _reader_bits::MapStorage(map); deba@127: _arc_maps.push_back(std::make_pair(caption, storage)); deba@127: return *this; deba@127: } deba@127: alpar@156: /// \brief Arc map reading rule alpar@156: /// alpar@156: /// Add an arc map reading rule with specialized converter to the alpar@156: /// reader. deba@127: template deba@127: DigraphReader& arcMap(const std::string& caption, Map& map, deba@127: const Converter& converter = Converter()) { deba@127: checkConcept, Map>(); deba@127: _reader_bits::MapStorageBase* storage = deba@127: new _reader_bits::MapStorage(map, converter); deba@127: _arc_maps.push_back(std::make_pair(caption, storage)); deba@127: return *this; deba@127: } deba@127: alpar@156: /// \brief Attribute reading rule alpar@156: /// alpar@156: /// Add an attribute reading rule to the reader. deba@127: template deba@127: DigraphReader& attribute(const std::string& caption, Value& value) { deba@127: _reader_bits::ValueStorageBase* storage = deba@127: new _reader_bits::ValueStorage(value); deba@127: _attributes.insert(std::make_pair(caption, storage)); deba@127: return *this; deba@127: } deba@127: alpar@156: /// \brief Attribute reading rule alpar@156: /// alpar@156: /// Add an attribute reading rule with specialized converter to the alpar@156: /// reader. deba@127: template deba@127: DigraphReader& attribute(const std::string& caption, Value& value, deba@127: const Converter& converter = Converter()) { deba@127: _reader_bits::ValueStorageBase* storage = deba@127: new _reader_bits::ValueStorage(value, converter); deba@127: _attributes.insert(std::make_pair(caption, storage)); deba@127: return *this; deba@127: } deba@127: alpar@156: /// \brief Node reading rule alpar@156: /// alpar@156: /// Add a node reading rule to reader. deba@127: DigraphReader& node(const std::string& caption, Node& node) { deba@127: typedef _reader_bits::MapLookUpConverter Converter; deba@127: Converter converter(_node_index); deba@127: _reader_bits::ValueStorageBase* storage = deba@127: new _reader_bits::ValueStorage(node, converter); deba@127: _attributes.insert(std::make_pair(caption, storage)); deba@127: return *this; deba@127: } deba@127: alpar@156: /// \brief Arc reading rule alpar@156: /// alpar@156: /// Add an arc reading rule to reader. deba@127: DigraphReader& arc(const std::string& caption, Arc& arc) { deba@127: typedef _reader_bits::MapLookUpConverter Converter; deba@127: Converter converter(_arc_index); deba@127: _reader_bits::ValueStorageBase* storage = deba@127: new _reader_bits::ValueStorage(arc, converter); deba@127: _attributes.insert(std::make_pair(caption, storage)); deba@127: return *this; deba@127: } deba@127: alpar@156: /// @} alpar@156: alpar@156: /// \name Select section by name alpar@156: /// @{ alpar@156: alpar@156: /// \brief Set \c \@nodes section to be read alpar@156: /// alpar@156: /// Set \c \@nodes section to be read deba@127: DigraphReader& nodes(const std::string& caption) { deba@127: _nodes_caption = caption; deba@127: return *this; deba@127: } deba@127: alpar@156: /// \brief Set \c \@arcs section to be read alpar@156: /// alpar@156: /// Set \c \@arcs section to be read deba@127: DigraphReader& arcs(const std::string& caption) { deba@127: _arcs_caption = caption; deba@127: return *this; deba@127: } deba@127: alpar@156: /// \brief Set \c \@attributes section to be read alpar@156: /// alpar@156: /// Set \c \@attributes section to be read deba@127: DigraphReader& attributes(const std::string& caption) { deba@127: _attributes_caption = caption; deba@127: return *this; deba@127: } deba@127: alpar@156: /// @} alpar@156: alpar@156: /// \name Using previously constructed node or arc set alpar@156: /// @{ alpar@156: alpar@156: /// \brief Use previously constructed node set alpar@156: /// alpar@156: /// Use previously constructed node set, and specify the node alpar@156: /// label map. deba@127: template deba@127: DigraphReader& useNodes(const Map& map) { deba@127: checkConcept, Map>(); deba@127: LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); deba@127: _use_nodes = true; deba@127: _writer_bits::DefaultConverter converter; deba@127: for (NodeIt n(_digraph); n != INVALID; ++n) { deba@127: _node_index.insert(std::make_pair(converter(map[n]), n)); deba@127: } deba@127: return *this; deba@127: } deba@127: alpar@156: /// \brief Use previously constructed node set alpar@156: /// alpar@156: /// Use previously constructed node set, and specify the node alpar@156: /// label map and a functor which converts the label map values to kpeter@192: /// \c std::string. deba@127: template deba@127: DigraphReader& useNodes(const Map& map, deba@127: const Converter& converter = Converter()) { deba@127: checkConcept, Map>(); deba@127: LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); deba@127: _use_nodes = true; deba@127: for (NodeIt n(_digraph); n != INVALID; ++n) { deba@127: _node_index.insert(std::make_pair(converter(map[n]), n)); deba@127: } deba@127: return *this; deba@127: } deba@127: alpar@156: /// \brief Use previously constructed arc set alpar@156: /// alpar@156: /// Use previously constructed arc set, and specify the arc alpar@156: /// label map. deba@127: template deba@127: DigraphReader& useArcs(const Map& map) { deba@127: checkConcept, Map>(); deba@127: LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); deba@127: _use_arcs = true; deba@127: _writer_bits::DefaultConverter converter; deba@127: for (ArcIt a(_digraph); a != INVALID; ++a) { deba@127: _arc_index.insert(std::make_pair(converter(map[a]), a)); deba@127: } deba@127: return *this; deba@127: } deba@127: alpar@156: /// \brief Use previously constructed arc set alpar@156: /// alpar@156: /// Use previously constructed arc set, and specify the arc alpar@156: /// label map and a functor which converts the label map values to kpeter@192: /// \c std::string. deba@127: template deba@127: DigraphReader& useArcs(const Map& map, deba@188: const Converter& converter = Converter()) { deba@127: checkConcept, Map>(); deba@127: LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); deba@127: _use_arcs = true; deba@127: for (ArcIt a(_digraph); a != INVALID; ++a) { deba@127: _arc_index.insert(std::make_pair(converter(map[a]), a)); deba@127: } deba@127: return *this; deba@127: } deba@127: deba@188: /// \brief Skips the reading of node section deba@188: /// deba@188: /// Omit the reading of the node section. This implies that each node kpeter@192: /// map reading rule will be abandoned, and the nodes of the graph deba@188: /// will not be constructed, which usually cause that the arc set kpeter@192: /// could not be read due to lack of node name resolving. kpeter@192: /// Therefore \c skipArcs() function should also be used, or kpeter@192: /// \c useNodes() should be used to specify the label of the nodes. deba@188: DigraphReader& skipNodes() { deba@188: LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); deba@188: _skip_nodes = true; deba@188: return *this; deba@188: } deba@188: deba@188: /// \brief Skips the reading of arc section deba@188: /// deba@188: /// Omit the reading of the arc section. This implies that each arc kpeter@192: /// map reading rule will be abandoned, and the arcs of the graph deba@188: /// will not be constructed. deba@188: DigraphReader& skipArcs() { deba@188: LEMON_ASSERT(!_skip_arcs, "Skip arcs already set"); deba@188: _skip_arcs = true; deba@188: return *this; deba@188: } deba@188: alpar@156: /// @} alpar@156: deba@127: private: deba@127: deba@127: bool readLine() { deba@127: std::string str; deba@127: while(++line_num, std::getline(*_is, str)) { deba@127: line.clear(); line.str(str); deba@127: char c; deba@127: if (line >> std::ws >> c && c != '#') { deba@127: line.putback(c); deba@127: return true; deba@127: } deba@127: } deba@127: return false; deba@127: } deba@127: deba@127: bool readSuccess() { deba@127: return static_cast(*_is); deba@127: } deba@127: deba@127: void skipSection() { deba@127: char c; deba@127: while (readSuccess() && line >> c && c != '@') { deba@127: readLine(); deba@127: } deba@127: line.putback(c); deba@127: } deba@127: deba@127: void readNodes() { deba@127: deba@127: std::vector map_index(_node_maps.size()); deba@127: int map_num, label_index; deba@127: deba@186: char c; deba@186: if (!readLine() || !(line >> c) || c == '@') { deba@186: if (readSuccess() && line) line.putback(c); deba@186: if (!_node_maps.empty()) deba@186: throw DataFormatError("Cannot find map names"); deba@186: return; deba@186: } deba@186: line.putback(c); deba@186: deba@127: { deba@127: std::map maps; deba@127: deba@127: std::string map; deba@127: int index = 0; alpar@156: while (_reader_bits::readToken(line, map)) { deba@127: if (maps.find(map) != maps.end()) { deba@127: std::ostringstream msg; deba@127: msg << "Multiple occurence of node map: " << map; deba@127: throw DataFormatError(msg.str().c_str()); deba@127: } deba@127: maps.insert(std::make_pair(map, index)); deba@127: ++index; deba@127: } deba@127: deba@127: for (int i = 0; i < static_cast(_node_maps.size()); ++i) { deba@127: std::map::iterator jt = deba@127: maps.find(_node_maps[i].first); deba@127: if (jt == maps.end()) { deba@127: std::ostringstream msg; deba@127: msg << "Map not found in file: " << _node_maps[i].first; deba@127: throw DataFormatError(msg.str().c_str()); deba@127: } deba@127: map_index[i] = jt->second; deba@127: } deba@127: deba@127: { deba@127: std::map::iterator jt = maps.find("label"); deba@186: if (jt != maps.end()) { deba@186: label_index = jt->second; deba@186: } else { deba@186: label_index = -1; deba@186: } deba@127: } deba@127: map_num = maps.size(); deba@127: } deba@127: deba@127: while (readLine() && line >> c && c != '@') { deba@127: line.putback(c); deba@127: deba@127: std::vector tokens(map_num); deba@127: for (int i = 0; i < map_num; ++i) { deba@127: if (!_reader_bits::readToken(line, tokens[i])) { deba@127: std::ostringstream msg; deba@127: msg << "Column not found (" << i + 1 << ")"; deba@127: throw DataFormatError(msg.str().c_str()); deba@127: } deba@127: } deba@127: if (line >> std::ws >> c) deba@127: throw DataFormatError("Extra character on the end of line"); deba@127: deba@127: Node n; deba@127: if (!_use_nodes) { deba@127: n = _digraph.addNode(); deba@186: if (label_index != -1) deba@186: _node_index.insert(std::make_pair(tokens[label_index], n)); deba@127: } else { deba@186: if (label_index == -1) deba@186: throw DataFormatError("Label map not found in file"); deba@127: typename std::map::iterator it = deba@127: _node_index.find(tokens[label_index]); deba@127: if (it == _node_index.end()) { deba@127: std::ostringstream msg; deba@127: msg << "Node with label not found: " << tokens[label_index]; deba@127: throw DataFormatError(msg.str().c_str()); deba@127: } deba@127: n = it->second; deba@127: } deba@127: deba@127: for (int i = 0; i < static_cast(_node_maps.size()); ++i) { deba@127: _node_maps[i].second->set(n, tokens[map_index[i]]); deba@127: } deba@127: deba@127: } deba@127: if (readSuccess()) { deba@127: line.putback(c); deba@127: } deba@127: } deba@127: deba@127: void readArcs() { deba@127: deba@127: std::vector map_index(_arc_maps.size()); deba@127: int map_num, label_index; deba@127: deba@186: char c; deba@186: if (!readLine() || !(line >> c) || c == '@') { deba@186: if (readSuccess() && line) line.putback(c); deba@186: if (!_arc_maps.empty()) deba@186: throw DataFormatError("Cannot find map names"); deba@186: return; deba@186: } deba@186: line.putback(c); deba@127: deba@127: { deba@127: std::map maps; deba@127: deba@127: std::string map; deba@127: int index = 0; alpar@156: while (_reader_bits::readToken(line, map)) { deba@127: if (maps.find(map) != maps.end()) { deba@127: std::ostringstream msg; deba@127: msg << "Multiple occurence of arc map: " << map; deba@127: throw DataFormatError(msg.str().c_str()); deba@127: } deba@127: maps.insert(std::make_pair(map, index)); deba@127: ++index; deba@127: } deba@127: deba@127: for (int i = 0; i < static_cast(_arc_maps.size()); ++i) { deba@127: std::map::iterator jt = deba@127: maps.find(_arc_maps[i].first); deba@127: if (jt == maps.end()) { deba@127: std::ostringstream msg; deba@127: msg << "Map not found in file: " << _arc_maps[i].first; deba@127: throw DataFormatError(msg.str().c_str()); deba@127: } deba@127: map_index[i] = jt->second; deba@127: } deba@127: deba@127: { deba@127: std::map::iterator jt = maps.find("label"); deba@186: if (jt != maps.end()) { deba@186: label_index = jt->second; deba@186: } else { deba@186: label_index = -1; deba@186: } deba@127: } deba@127: map_num = maps.size(); deba@127: } deba@127: deba@127: while (readLine() && line >> c && c != '@') { deba@127: line.putback(c); deba@127: deba@127: std::string source_token; deba@127: std::string target_token; deba@127: deba@127: if (!_reader_bits::readToken(line, source_token)) deba@127: throw DataFormatError("Source not found"); deba@127: deba@127: if (!_reader_bits::readToken(line, target_token)) deba@186: throw DataFormatError("Target not found"); deba@127: deba@127: std::vector tokens(map_num); deba@127: for (int i = 0; i < map_num; ++i) { deba@127: if (!_reader_bits::readToken(line, tokens[i])) { deba@127: std::ostringstream msg; deba@127: msg << "Column not found (" << i + 1 << ")"; deba@127: throw DataFormatError(msg.str().c_str()); deba@127: } deba@127: } deba@127: if (line >> std::ws >> c) deba@127: throw DataFormatError("Extra character on the end of line"); deba@127: deba@127: Arc a; deba@127: if (!_use_arcs) { deba@127: deba@127: typename NodeIndex::iterator it; deba@127: deba@127: it = _node_index.find(source_token); deba@127: if (it == _node_index.end()) { deba@127: std::ostringstream msg; deba@127: msg << "Item not found: " << source_token; deba@127: throw DataFormatError(msg.str().c_str()); deba@127: } deba@127: Node source = it->second; deba@127: deba@127: it = _node_index.find(target_token); deba@127: if (it == _node_index.end()) { deba@127: std::ostringstream msg; deba@127: msg << "Item not found: " << target_token; deba@127: throw DataFormatError(msg.str().c_str()); deba@127: } deba@127: Node target = it->second; deba@127: deba@127: a = _digraph.addArc(source, target); deba@186: if (label_index != -1) deba@186: _arc_index.insert(std::make_pair(tokens[label_index], a)); deba@127: } else { deba@186: if (label_index == -1) deba@186: throw DataFormatError("Label map not found in file"); deba@127: typename std::map::iterator it = deba@127: _arc_index.find(tokens[label_index]); deba@127: if (it == _arc_index.end()) { deba@127: std::ostringstream msg; deba@127: msg << "Arc with label not found: " << tokens[label_index]; deba@127: throw DataFormatError(msg.str().c_str()); deba@127: } deba@127: a = it->second; deba@127: } deba@127: deba@127: for (int i = 0; i < static_cast(_arc_maps.size()); ++i) { deba@127: _arc_maps[i].second->set(a, tokens[map_index[i]]); deba@127: } deba@127: deba@127: } deba@127: if (readSuccess()) { deba@127: line.putback(c); deba@127: } deba@127: } deba@127: deba@127: void readAttributes() { deba@127: deba@127: std::set read_attr; deba@127: deba@127: char c; deba@127: while (readLine() && line >> c && c != '@') { deba@127: line.putback(c); deba@127: deba@127: std::string attr, token; alpar@156: if (!_reader_bits::readToken(line, attr)) deba@127: throw DataFormatError("Attribute name not found"); deba@127: if (!_reader_bits::readToken(line, token)) deba@127: throw DataFormatError("Attribute value not found"); deba@127: if (line >> c) deba@127: throw DataFormatError("Extra character on the end of line"); deba@127: deba@127: { deba@127: std::set::iterator it = read_attr.find(attr); deba@127: if (it != read_attr.end()) { deba@127: std::ostringstream msg; deba@127: msg << "Multiple occurence of attribute " << attr; deba@127: throw DataFormatError(msg.str().c_str()); deba@127: } deba@127: read_attr.insert(attr); deba@127: } deba@127: deba@127: { deba@127: typename Attributes::iterator it = _attributes.lower_bound(attr); deba@127: while (it != _attributes.end() && it->first == attr) { deba@127: it->second->set(token); deba@127: ++it; deba@127: } deba@127: } deba@127: deba@127: } deba@127: if (readSuccess()) { deba@127: line.putback(c); deba@127: } deba@127: for (typename Attributes::iterator it = _attributes.begin(); deba@127: it != _attributes.end(); ++it) { deba@127: if (read_attr.find(it->first) == read_attr.end()) { deba@127: std::ostringstream msg; deba@127: msg << "Attribute not found in file: " << it->first; deba@127: throw DataFormatError(msg.str().c_str()); deba@127: } deba@127: } deba@127: } deba@127: deba@127: public: alpar@156: alpar@156: /// \name Execution of the reader alpar@156: /// @{ alpar@156: alpar@156: /// \brief Start the batch processing alpar@156: /// alpar@156: /// This function starts the batch processing deba@127: void run() { deba@127: LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); deba@163: if (!*_is) { deba@163: throw DataFormatError("Cannot find file"); deba@163: } deba@127: deba@188: bool nodes_done = _skip_nodes; deba@188: bool arcs_done = _skip_arcs; deba@127: bool attributes_done = false; deba@127: deba@127: line_num = 0; deba@127: readLine(); deba@172: skipSection(); deba@127: deba@127: while (readSuccess()) { deba@127: try { deba@127: char c; deba@127: std::string section, caption; deba@127: line >> c; alpar@156: _reader_bits::readToken(line, section); alpar@156: _reader_bits::readToken(line, caption); deba@127: deba@127: if (line >> c) deba@127: throw DataFormatError("Extra character on the end of line"); deba@127: deba@127: if (section == "nodes" && !nodes_done) { deba@127: if (_nodes_caption.empty() || _nodes_caption == caption) { deba@127: readNodes(); deba@127: nodes_done = true; deba@127: } deba@127: } else if ((section == "arcs" || section == "edges") && deba@127: !arcs_done) { deba@127: if (_arcs_caption.empty() || _arcs_caption == caption) { deba@127: readArcs(); deba@127: arcs_done = true; deba@127: } deba@127: } else if (section == "attributes" && !attributes_done) { deba@127: if (_attributes_caption.empty() || _attributes_caption == caption) { deba@127: readAttributes(); deba@127: attributes_done = true; deba@127: } deba@127: } else { deba@172: readLine(); deba@172: skipSection(); deba@127: } deba@127: } catch (DataFormatError& error) { deba@127: error.line(line_num); deba@127: throw; deba@127: } deba@127: } deba@127: deba@127: if (!nodes_done) { deba@127: throw DataFormatError("Section @nodes not found"); deba@127: } deba@127: deba@127: if (!arcs_done) { deba@127: throw DataFormatError("Section @arcs not found"); deba@127: } deba@127: deba@127: if (!attributes_done && !_attributes.empty()) { deba@127: throw DataFormatError("Section @attributes not found"); deba@127: } deba@127: deba@127: } alpar@156: alpar@156: /// @} deba@127: deba@127: }; deba@127: kpeter@192: /// \brief Return a \ref DigraphReader class kpeter@192: /// kpeter@192: /// This function just returns a \ref DigraphReader class. alpar@156: /// \relates DigraphReader deba@127: template deba@127: DigraphReader digraphReader(std::istream& is, Digraph& digraph) { deba@163: DigraphReader tmp(is, digraph); deba@163: return tmp; deba@127: } deba@127: kpeter@192: /// \brief Return a \ref DigraphReader class kpeter@192: /// kpeter@192: /// This function just returns a \ref DigraphReader class. alpar@156: /// \relates DigraphReader deba@127: template deba@127: DigraphReader digraphReader(const std::string& fn, deba@127: Digraph& digraph) { deba@163: DigraphReader tmp(fn, digraph); deba@163: return tmp; deba@127: } deba@127: kpeter@192: /// \brief Return a \ref DigraphReader class kpeter@192: /// kpeter@192: /// This function just returns a \ref DigraphReader class. alpar@156: /// \relates DigraphReader deba@127: template deba@127: DigraphReader digraphReader(const char* fn, Digraph& digraph) { deba@163: DigraphReader tmp(fn, digraph); deba@163: return tmp; deba@127: } deba@165: deba@190: template deba@190: class GraphReader; deba@190: deba@190: template deba@190: GraphReader graphReader(std::istream& is, Graph& graph); deba@190: deba@190: template deba@190: GraphReader graphReader(const std::string& fn, Graph& graph); deba@190: deba@190: template deba@190: GraphReader graphReader(const char *fn, Graph& graph); deba@190: deba@165: /// \ingroup lemon_io deba@165: /// kpeter@192: /// \brief \ref lgf-format "LGF" reader for undirected graphs deba@165: /// deba@165: /// This utility reads an \ref lgf-format "LGF" file. kpeter@192: /// kpeter@192: /// It can be used almost the same way as \c DigraphReader. kpeter@192: /// The only difference is that this class can handle edges and kpeter@192: /// edge maps as well as arcs and arc maps. deba@165: template deba@165: class GraphReader { deba@165: public: deba@165: deba@165: typedef _Graph Graph; deba@165: TEMPLATE_GRAPH_TYPEDEFS(Graph); deba@165: deba@165: private: deba@165: deba@165: std::istream* _is; deba@165: bool local_is; deba@165: deba@165: Graph& _graph; deba@165: deba@165: std::string _nodes_caption; deba@165: std::string _edges_caption; deba@165: std::string _attributes_caption; deba@165: deba@165: typedef std::map NodeIndex; deba@165: NodeIndex _node_index; deba@165: typedef std::map EdgeIndex; deba@165: EdgeIndex _edge_index; deba@165: deba@165: typedef std::vector*> > NodeMaps; deba@165: NodeMaps _node_maps; deba@165: deba@165: typedef std::vector*> > EdgeMaps; deba@165: EdgeMaps _edge_maps; deba@165: deba@165: typedef std::multimap deba@165: Attributes; deba@165: Attributes _attributes; deba@165: deba@165: bool _use_nodes; deba@165: bool _use_edges; deba@165: deba@188: bool _skip_nodes; deba@188: bool _skip_edges; deba@188: deba@165: int line_num; deba@165: std::istringstream line; deba@165: deba@165: public: deba@165: deba@165: /// \brief Constructor deba@165: /// kpeter@192: /// Construct an undirected graph reader, which reads from the given deba@165: /// input stream. deba@165: GraphReader(std::istream& is, Graph& graph) deba@165: : _is(&is), local_is(false), _graph(graph), deba@188: _use_nodes(false), _use_edges(false), deba@188: _skip_nodes(false), _skip_edges(false) {} deba@165: deba@165: /// \brief Constructor deba@165: /// kpeter@192: /// Construct an undirected graph reader, which reads from the given deba@165: /// file. deba@165: GraphReader(const std::string& fn, Graph& graph) deba@165: : _is(new std::ifstream(fn.c_str())), local_is(true), _graph(graph), deba@188: _use_nodes(false), _use_edges(false), deba@188: _skip_nodes(false), _skip_edges(false) {} deba@165: deba@165: /// \brief Constructor deba@165: /// kpeter@192: /// Construct an undirected graph reader, which reads from the given deba@165: /// file. deba@165: GraphReader(const char* fn, Graph& graph) deba@165: : _is(new std::ifstream(fn)), local_is(true), _graph(graph), deba@188: _use_nodes(false), _use_edges(false), deba@188: _skip_nodes(false), _skip_edges(false) {} deba@165: deba@165: /// \brief Destructor deba@165: ~GraphReader() { deba@165: for (typename NodeMaps::iterator it = _node_maps.begin(); deba@165: it != _node_maps.end(); ++it) { deba@165: delete it->second; deba@165: } deba@165: deba@165: for (typename EdgeMaps::iterator it = _edge_maps.begin(); deba@165: it != _edge_maps.end(); ++it) { deba@165: delete it->second; deba@165: } deba@165: deba@165: for (typename Attributes::iterator it = _attributes.begin(); deba@165: it != _attributes.end(); ++it) { deba@165: delete it->second; deba@165: } deba@165: deba@165: if (local_is) { deba@165: delete _is; deba@165: } deba@165: deba@165: } deba@165: deba@165: private: deba@190: friend GraphReader graphReader<>(std::istream& is, Graph& graph); deba@190: friend GraphReader graphReader<>(const std::string& fn, deba@190: Graph& graph); deba@190: friend GraphReader graphReader<>(const char *fn, Graph& graph); deba@190: deba@190: GraphReader(GraphReader& other) deba@190: : _is(other._is), local_is(other.local_is), _graph(other._graph), deba@190: _use_nodes(other._use_nodes), _use_edges(other._use_edges), deba@190: _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) { deba@190: deba@190: other._is = 0; deba@190: other.local_is = false; deba@190: deba@190: _node_index.swap(other._node_index); deba@190: _edge_index.swap(other._edge_index); deba@190: deba@190: _node_maps.swap(other._node_maps); deba@190: _edge_maps.swap(other._edge_maps); deba@190: _attributes.swap(other._attributes); deba@190: deba@190: _nodes_caption = other._nodes_caption; deba@190: _edges_caption = other._edges_caption; deba@190: _attributes_caption = other._attributes_caption; deba@190: deba@190: } deba@190: deba@165: GraphReader& operator=(const GraphReader&); deba@165: deba@165: public: deba@165: deba@165: /// \name Reading rules deba@165: /// @{ deba@165: deba@165: /// \brief Node map reading rule deba@165: /// deba@165: /// Add a node map reading rule to the reader. deba@165: template deba@165: GraphReader& nodeMap(const std::string& caption, Map& map) { deba@165: checkConcept, Map>(); deba@165: _reader_bits::MapStorageBase* storage = deba@165: new _reader_bits::MapStorage(map); deba@165: _node_maps.push_back(std::make_pair(caption, storage)); deba@165: return *this; deba@165: } deba@165: deba@165: /// \brief Node map reading rule deba@165: /// deba@165: /// Add a node map reading rule with specialized converter to the deba@165: /// reader. deba@165: template deba@165: GraphReader& nodeMap(const std::string& caption, Map& map, deba@165: const Converter& converter = Converter()) { deba@165: checkConcept, Map>(); deba@165: _reader_bits::MapStorageBase* storage = deba@165: new _reader_bits::MapStorage(map, converter); deba@165: _node_maps.push_back(std::make_pair(caption, storage)); deba@165: return *this; deba@165: } deba@165: deba@165: /// \brief Edge map reading rule deba@165: /// deba@165: /// Add an edge map reading rule to the reader. deba@165: template deba@165: GraphReader& edgeMap(const std::string& caption, Map& map) { deba@165: checkConcept, Map>(); deba@165: _reader_bits::MapStorageBase* storage = deba@165: new _reader_bits::MapStorage(map); deba@165: _edge_maps.push_back(std::make_pair(caption, storage)); deba@165: return *this; deba@165: } deba@165: deba@165: /// \brief Edge map reading rule deba@165: /// deba@165: /// Add an edge map reading rule with specialized converter to the deba@165: /// reader. deba@165: template deba@165: GraphReader& edgeMap(const std::string& caption, Map& map, deba@165: const Converter& converter = Converter()) { deba@165: checkConcept, Map>(); deba@165: _reader_bits::MapStorageBase* storage = deba@165: new _reader_bits::MapStorage(map, converter); deba@165: _edge_maps.push_back(std::make_pair(caption, storage)); deba@165: return *this; deba@165: } deba@165: deba@165: /// \brief Arc map reading rule deba@165: /// deba@165: /// Add an arc map reading rule to the reader. deba@165: template deba@165: GraphReader& arcMap(const std::string& caption, Map& map) { deba@165: checkConcept, Map>(); deba@165: _reader_bits::MapStorageBase* forward_storage = deba@165: new _reader_bits::GraphArcMapStorage(_graph, map); deba@165: _edge_maps.push_back(std::make_pair('+' + caption, forward_storage)); deba@165: _reader_bits::MapStorageBase* backward_storage = deba@165: new _reader_bits::GraphArcMapStorage(_graph, map); deba@165: _edge_maps.push_back(std::make_pair('-' + caption, backward_storage)); deba@165: return *this; deba@165: } deba@165: deba@165: /// \brief Arc map reading rule deba@165: /// deba@165: /// Add an arc map reading rule with specialized converter to the deba@165: /// reader. deba@165: template deba@165: GraphReader& arcMap(const std::string& caption, Map& map, deba@165: const Converter& converter = Converter()) { deba@165: checkConcept, Map>(); deba@165: _reader_bits::MapStorageBase* forward_storage = deba@165: new _reader_bits::GraphArcMapStorage deba@165: (_graph, map, converter); deba@165: _edge_maps.push_back(std::make_pair('+' + caption, forward_storage)); deba@165: _reader_bits::MapStorageBase* backward_storage = deba@165: new _reader_bits::GraphArcMapStorage deba@165: (_graph, map, converter); deba@165: _edge_maps.push_back(std::make_pair('-' + caption, backward_storage)); deba@165: return *this; deba@165: } deba@165: deba@165: /// \brief Attribute reading rule deba@165: /// deba@165: /// Add an attribute reading rule to the reader. deba@165: template deba@165: GraphReader& attribute(const std::string& caption, Value& value) { deba@165: _reader_bits::ValueStorageBase* storage = deba@165: new _reader_bits::ValueStorage(value); deba@165: _attributes.insert(std::make_pair(caption, storage)); deba@165: return *this; deba@165: } deba@165: deba@165: /// \brief Attribute reading rule deba@165: /// deba@165: /// Add an attribute reading rule with specialized converter to the deba@165: /// reader. deba@165: template deba@165: GraphReader& attribute(const std::string& caption, Value& value, deba@165: const Converter& converter = Converter()) { deba@165: _reader_bits::ValueStorageBase* storage = deba@165: new _reader_bits::ValueStorage(value, converter); deba@165: _attributes.insert(std::make_pair(caption, storage)); deba@165: return *this; deba@165: } deba@165: deba@165: /// \brief Node reading rule deba@165: /// deba@165: /// Add a node reading rule to reader. deba@165: GraphReader& node(const std::string& caption, Node& node) { deba@165: typedef _reader_bits::MapLookUpConverter Converter; deba@165: Converter converter(_node_index); deba@165: _reader_bits::ValueStorageBase* storage = deba@165: new _reader_bits::ValueStorage(node, converter); deba@165: _attributes.insert(std::make_pair(caption, storage)); deba@165: return *this; deba@165: } deba@165: deba@165: /// \brief Edge reading rule deba@165: /// deba@165: /// Add an edge reading rule to reader. deba@165: GraphReader& edge(const std::string& caption, Edge& edge) { deba@165: typedef _reader_bits::MapLookUpConverter Converter; deba@165: Converter converter(_edge_index); deba@165: _reader_bits::ValueStorageBase* storage = deba@165: new _reader_bits::ValueStorage(edge, converter); deba@165: _attributes.insert(std::make_pair(caption, storage)); deba@165: return *this; deba@165: } deba@165: deba@165: /// \brief Arc reading rule deba@165: /// deba@165: /// Add an arc reading rule to reader. deba@165: GraphReader& arc(const std::string& caption, Arc& arc) { deba@165: typedef _reader_bits::GraphArcLookUpConverter Converter; deba@165: Converter converter(_graph, _edge_index); deba@165: _reader_bits::ValueStorageBase* storage = deba@165: new _reader_bits::ValueStorage(arc, converter); deba@165: _attributes.insert(std::make_pair(caption, storage)); deba@165: return *this; deba@165: } deba@165: deba@165: /// @} deba@165: deba@165: /// \name Select section by name deba@165: /// @{ deba@165: deba@165: /// \brief Set \c \@nodes section to be read deba@165: /// kpeter@192: /// Set \c \@nodes section to be read. deba@165: GraphReader& nodes(const std::string& caption) { deba@165: _nodes_caption = caption; deba@165: return *this; deba@165: } deba@165: deba@165: /// \brief Set \c \@edges section to be read deba@165: /// kpeter@192: /// Set \c \@edges section to be read. deba@165: GraphReader& edges(const std::string& caption) { deba@165: _edges_caption = caption; deba@165: return *this; deba@165: } deba@165: deba@165: /// \brief Set \c \@attributes section to be read deba@165: /// kpeter@192: /// Set \c \@attributes section to be read. deba@165: GraphReader& attributes(const std::string& caption) { deba@165: _attributes_caption = caption; deba@165: return *this; deba@165: } deba@165: deba@165: /// @} deba@165: deba@165: /// \name Using previously constructed node or edge set deba@165: /// @{ deba@165: deba@165: /// \brief Use previously constructed node set deba@165: /// deba@165: /// Use previously constructed node set, and specify the node deba@165: /// label map. deba@165: template deba@165: GraphReader& useNodes(const Map& map) { deba@165: checkConcept, Map>(); deba@165: LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); deba@165: _use_nodes = true; deba@165: _writer_bits::DefaultConverter converter; deba@165: for (NodeIt n(_graph); n != INVALID; ++n) { deba@165: _node_index.insert(std::make_pair(converter(map[n]), n)); deba@165: } deba@165: return *this; deba@165: } deba@165: deba@165: /// \brief Use previously constructed node set deba@165: /// deba@165: /// Use previously constructed node set, and specify the node deba@165: /// label map and a functor which converts the label map values to kpeter@192: /// \c std::string. deba@165: template deba@165: GraphReader& useNodes(const Map& map, deba@165: const Converter& converter = Converter()) { deba@165: checkConcept, Map>(); deba@165: LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); deba@165: _use_nodes = true; deba@165: for (NodeIt n(_graph); n != INVALID; ++n) { deba@165: _node_index.insert(std::make_pair(converter(map[n]), n)); deba@165: } deba@165: return *this; deba@165: } deba@165: deba@165: /// \brief Use previously constructed edge set deba@165: /// deba@165: /// Use previously constructed edge set, and specify the edge deba@165: /// label map. deba@165: template deba@165: GraphReader& useEdges(const Map& map) { deba@165: checkConcept, Map>(); deba@165: LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); deba@165: _use_edges = true; deba@165: _writer_bits::DefaultConverter converter; deba@165: for (EdgeIt a(_graph); a != INVALID; ++a) { deba@165: _edge_index.insert(std::make_pair(converter(map[a]), a)); deba@165: } deba@165: return *this; deba@165: } deba@165: deba@165: /// \brief Use previously constructed edge set deba@165: /// deba@165: /// Use previously constructed edge set, and specify the edge deba@165: /// label map and a functor which converts the label map values to kpeter@192: /// \c std::string. deba@165: template deba@165: GraphReader& useEdges(const Map& map, deba@165: const Converter& converter = Converter()) { deba@165: checkConcept, Map>(); deba@165: LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); deba@165: _use_edges = true; deba@165: for (EdgeIt a(_graph); a != INVALID; ++a) { deba@165: _edge_index.insert(std::make_pair(converter(map[a]), a)); deba@165: } deba@165: return *this; deba@165: } deba@165: kpeter@192: /// \brief Skip the reading of node section deba@188: /// deba@188: /// Omit the reading of the node section. This implies that each node kpeter@192: /// map reading rule will be abandoned, and the nodes of the graph deba@188: /// will not be constructed, which usually cause that the edge set deba@188: /// could not be read due to lack of node name kpeter@192: /// could not be read due to lack of node name resolving. kpeter@192: /// Therefore \c skipEdges() function should also be used, or kpeter@192: /// \c useNodes() should be used to specify the label of the nodes. deba@188: GraphReader& skipNodes() { deba@188: LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); deba@188: _skip_nodes = true; deba@188: return *this; deba@188: } deba@188: kpeter@192: /// \brief Skip the reading of edge section deba@188: /// deba@188: /// Omit the reading of the edge section. This implies that each edge kpeter@192: /// map reading rule will be abandoned, and the edges of the graph deba@188: /// will not be constructed. deba@188: GraphReader& skipEdges() { deba@188: LEMON_ASSERT(!_skip_edges, "Skip edges already set"); deba@188: _skip_edges = true; deba@188: return *this; deba@188: } deba@188: deba@165: /// @} deba@165: deba@165: private: deba@165: deba@165: bool readLine() { deba@165: std::string str; deba@165: while(++line_num, std::getline(*_is, str)) { deba@165: line.clear(); line.str(str); deba@165: char c; deba@165: if (line >> std::ws >> c && c != '#') { deba@165: line.putback(c); deba@165: return true; deba@165: } deba@165: } deba@165: return false; deba@165: } deba@165: deba@165: bool readSuccess() { deba@165: return static_cast(*_is); deba@165: } deba@165: deba@165: void skipSection() { deba@165: char c; deba@165: while (readSuccess() && line >> c && c != '@') { deba@165: readLine(); deba@165: } deba@165: line.putback(c); deba@165: } deba@165: deba@165: void readNodes() { deba@165: deba@165: std::vector map_index(_node_maps.size()); deba@165: int map_num, label_index; deba@165: deba@186: char c; deba@186: if (!readLine() || !(line >> c) || c == '@') { deba@186: if (readSuccess() && line) line.putback(c); deba@186: if (!_node_maps.empty()) deba@186: throw DataFormatError("Cannot find map names"); deba@186: return; deba@186: } deba@186: line.putback(c); deba@165: deba@165: { deba@165: std::map maps; deba@165: deba@165: std::string map; deba@165: int index = 0; deba@165: while (_reader_bits::readToken(line, map)) { deba@165: if (maps.find(map) != maps.end()) { deba@165: std::ostringstream msg; deba@165: msg << "Multiple occurence of node map: " << map; deba@165: throw DataFormatError(msg.str().c_str()); deba@165: } deba@165: maps.insert(std::make_pair(map, index)); deba@165: ++index; deba@165: } deba@165: deba@165: for (int i = 0; i < static_cast(_node_maps.size()); ++i) { deba@165: std::map::iterator jt = deba@165: maps.find(_node_maps[i].first); deba@165: if (jt == maps.end()) { deba@165: std::ostringstream msg; deba@165: msg << "Map not found in file: " << _node_maps[i].first; deba@165: throw DataFormatError(msg.str().c_str()); deba@165: } deba@165: map_index[i] = jt->second; deba@165: } deba@165: deba@165: { deba@165: std::map::iterator jt = maps.find("label"); deba@186: if (jt != maps.end()) { deba@186: label_index = jt->second; deba@186: } else { deba@186: label_index = -1; deba@186: } deba@165: } deba@165: map_num = maps.size(); deba@165: } deba@165: deba@165: while (readLine() && line >> c && c != '@') { deba@165: line.putback(c); deba@165: deba@165: std::vector tokens(map_num); deba@165: for (int i = 0; i < map_num; ++i) { deba@165: if (!_reader_bits::readToken(line, tokens[i])) { deba@165: std::ostringstream msg; deba@165: msg << "Column not found (" << i + 1 << ")"; deba@165: throw DataFormatError(msg.str().c_str()); deba@165: } deba@165: } deba@165: if (line >> std::ws >> c) deba@165: throw DataFormatError("Extra character on the end of line"); deba@165: deba@165: Node n; deba@165: if (!_use_nodes) { deba@165: n = _graph.addNode(); deba@186: if (label_index != -1) deba@186: _node_index.insert(std::make_pair(tokens[label_index], n)); deba@165: } else { deba@186: if (label_index == -1) deba@186: throw DataFormatError("Label map not found in file"); deba@165: typename std::map::iterator it = deba@165: _node_index.find(tokens[label_index]); deba@165: if (it == _node_index.end()) { deba@165: std::ostringstream msg; deba@165: msg << "Node with label not found: " << tokens[label_index]; deba@165: throw DataFormatError(msg.str().c_str()); deba@165: } deba@165: n = it->second; deba@165: } deba@165: deba@165: for (int i = 0; i < static_cast(_node_maps.size()); ++i) { deba@165: _node_maps[i].second->set(n, tokens[map_index[i]]); deba@165: } deba@165: deba@165: } deba@165: if (readSuccess()) { deba@165: line.putback(c); deba@165: } deba@165: } deba@165: deba@165: void readEdges() { deba@165: deba@165: std::vector map_index(_edge_maps.size()); deba@165: int map_num, label_index; deba@165: deba@186: char c; deba@186: if (!readLine() || !(line >> c) || c == '@') { deba@186: if (readSuccess() && line) line.putback(c); deba@186: if (!_edge_maps.empty()) deba@186: throw DataFormatError("Cannot find map names"); deba@186: return; deba@186: } deba@186: line.putback(c); deba@165: deba@165: { deba@165: std::map maps; deba@165: deba@165: std::string map; deba@165: int index = 0; deba@165: while (_reader_bits::readToken(line, map)) { deba@165: if (maps.find(map) != maps.end()) { deba@165: std::ostringstream msg; deba@165: msg << "Multiple occurence of edge map: " << map; deba@165: throw DataFormatError(msg.str().c_str()); deba@165: } deba@165: maps.insert(std::make_pair(map, index)); deba@165: ++index; deba@165: } deba@165: deba@165: for (int i = 0; i < static_cast(_edge_maps.size()); ++i) { deba@165: std::map::iterator jt = deba@165: maps.find(_edge_maps[i].first); deba@165: if (jt == maps.end()) { deba@165: std::ostringstream msg; deba@165: msg << "Map not found in file: " << _edge_maps[i].first; deba@165: throw DataFormatError(msg.str().c_str()); deba@165: } deba@165: map_index[i] = jt->second; deba@165: } deba@165: deba@165: { deba@165: std::map::iterator jt = maps.find("label"); deba@186: if (jt != maps.end()) { deba@186: label_index = jt->second; deba@186: } else { deba@186: label_index = -1; deba@186: } deba@165: } deba@165: map_num = maps.size(); deba@165: } deba@165: deba@165: while (readLine() && line >> c && c != '@') { deba@165: line.putback(c); deba@165: deba@165: std::string source_token; deba@165: std::string target_token; deba@165: deba@165: if (!_reader_bits::readToken(line, source_token)) deba@186: throw DataFormatError("Node u not found"); deba@165: deba@165: if (!_reader_bits::readToken(line, target_token)) deba@186: throw DataFormatError("Node v not found"); deba@165: deba@165: std::vector tokens(map_num); deba@165: for (int i = 0; i < map_num; ++i) { deba@165: if (!_reader_bits::readToken(line, tokens[i])) { deba@165: std::ostringstream msg; deba@165: msg << "Column not found (" << i + 1 << ")"; deba@165: throw DataFormatError(msg.str().c_str()); deba@165: } deba@165: } deba@165: if (line >> std::ws >> c) deba@165: throw DataFormatError("Extra character on the end of line"); deba@165: deba@165: Edge e; deba@165: if (!_use_edges) { deba@165: deba@165: typename NodeIndex::iterator it; deba@165: deba@165: it = _node_index.find(source_token); deba@165: if (it == _node_index.end()) { deba@165: std::ostringstream msg; deba@165: msg << "Item not found: " << source_token; deba@165: throw DataFormatError(msg.str().c_str()); deba@165: } deba@165: Node source = it->second; deba@165: deba@165: it = _node_index.find(target_token); deba@165: if (it == _node_index.end()) { deba@165: std::ostringstream msg; deba@165: msg << "Item not found: " << target_token; deba@165: throw DataFormatError(msg.str().c_str()); deba@165: } deba@165: Node target = it->second; deba@165: deba@165: e = _graph.addEdge(source, target); deba@186: if (label_index != -1) deba@186: _edge_index.insert(std::make_pair(tokens[label_index], e)); deba@165: } else { deba@186: if (label_index == -1) deba@186: throw DataFormatError("Label map not found in file"); deba@165: typename std::map::iterator it = deba@165: _edge_index.find(tokens[label_index]); deba@165: if (it == _edge_index.end()) { deba@165: std::ostringstream msg; deba@165: msg << "Edge with label not found: " << tokens[label_index]; deba@165: throw DataFormatError(msg.str().c_str()); deba@165: } deba@165: e = it->second; deba@165: } deba@165: deba@165: for (int i = 0; i < static_cast(_edge_maps.size()); ++i) { deba@165: _edge_maps[i].second->set(e, tokens[map_index[i]]); deba@165: } deba@165: deba@165: } deba@165: if (readSuccess()) { deba@165: line.putback(c); deba@165: } deba@165: } deba@165: deba@165: void readAttributes() { deba@165: deba@165: std::set read_attr; deba@165: deba@165: char c; deba@165: while (readLine() && line >> c && c != '@') { deba@165: line.putback(c); deba@165: deba@165: std::string attr, token; deba@165: if (!_reader_bits::readToken(line, attr)) deba@165: throw DataFormatError("Attribute name not found"); deba@165: if (!_reader_bits::readToken(line, token)) deba@165: throw DataFormatError("Attribute value not found"); deba@165: if (line >> c) deba@165: throw DataFormatError("Extra character on the end of line"); deba@165: deba@165: { deba@165: std::set::iterator it = read_attr.find(attr); deba@165: if (it != read_attr.end()) { deba@165: std::ostringstream msg; deba@165: msg << "Multiple occurence of attribute " << attr; deba@165: throw DataFormatError(msg.str().c_str()); deba@165: } deba@165: read_attr.insert(attr); deba@165: } deba@165: deba@165: { deba@165: typename Attributes::iterator it = _attributes.lower_bound(attr); deba@165: while (it != _attributes.end() && it->first == attr) { deba@165: it->second->set(token); deba@165: ++it; deba@165: } deba@165: } deba@165: deba@165: } deba@165: if (readSuccess()) { deba@165: line.putback(c); deba@165: } deba@165: for (typename Attributes::iterator it = _attributes.begin(); deba@165: it != _attributes.end(); ++it) { deba@165: if (read_attr.find(it->first) == read_attr.end()) { deba@165: std::ostringstream msg; deba@165: msg << "Attribute not found in file: " << it->first; deba@165: throw DataFormatError(msg.str().c_str()); deba@165: } deba@165: } deba@165: } deba@165: deba@165: public: deba@165: deba@165: /// \name Execution of the reader deba@165: /// @{ deba@165: deba@165: /// \brief Start the batch processing deba@165: /// deba@165: /// This function starts the batch processing deba@165: void run() { deba@165: deba@165: LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); deba@165: deba@188: bool nodes_done = _skip_nodes; deba@188: bool edges_done = _skip_edges; deba@165: bool attributes_done = false; deba@165: deba@165: line_num = 0; deba@165: readLine(); deba@172: skipSection(); deba@165: deba@165: while (readSuccess()) { deba@165: try { deba@165: char c; deba@165: std::string section, caption; deba@165: line >> c; deba@165: _reader_bits::readToken(line, section); deba@165: _reader_bits::readToken(line, caption); deba@165: deba@165: if (line >> c) deba@165: throw DataFormatError("Extra character on the end of line"); deba@165: deba@165: if (section == "nodes" && !nodes_done) { deba@165: if (_nodes_caption.empty() || _nodes_caption == caption) { deba@165: readNodes(); deba@165: nodes_done = true; deba@165: } deba@165: } else if ((section == "edges" || section == "arcs") && deba@165: !edges_done) { deba@165: if (_edges_caption.empty() || _edges_caption == caption) { deba@165: readEdges(); deba@165: edges_done = true; deba@165: } deba@165: } else if (section == "attributes" && !attributes_done) { deba@165: if (_attributes_caption.empty() || _attributes_caption == caption) { deba@165: readAttributes(); deba@165: attributes_done = true; deba@165: } deba@165: } else { deba@172: readLine(); deba@172: skipSection(); deba@165: } deba@165: } catch (DataFormatError& error) { deba@165: error.line(line_num); deba@165: throw; deba@165: } deba@165: } deba@165: deba@165: if (!nodes_done) { deba@165: throw DataFormatError("Section @nodes not found"); deba@165: } deba@165: deba@165: if (!edges_done) { deba@165: throw DataFormatError("Section @edges not found"); deba@165: } deba@165: deba@165: if (!attributes_done && !_attributes.empty()) { deba@165: throw DataFormatError("Section @attributes not found"); deba@165: } deba@165: deba@165: } deba@165: deba@165: /// @} deba@165: deba@165: }; deba@165: kpeter@192: /// \brief Return a \ref GraphReader class kpeter@192: /// kpeter@192: /// This function just returns a \ref GraphReader class. deba@165: /// \relates GraphReader deba@165: template deba@165: GraphReader graphReader(std::istream& is, Graph& graph) { deba@165: GraphReader tmp(is, graph); deba@165: return tmp; deba@165: } deba@165: kpeter@192: /// \brief Return a \ref GraphReader class kpeter@192: /// kpeter@192: /// This function just returns a \ref GraphReader class. deba@165: /// \relates GraphReader deba@165: template deba@165: GraphReader graphReader(const std::string& fn, deba@165: Graph& graph) { deba@165: GraphReader tmp(fn, graph); deba@165: return tmp; deba@165: } deba@165: kpeter@192: /// \brief Return a \ref GraphReader class kpeter@192: /// kpeter@192: /// This function just returns a \ref GraphReader class. deba@165: /// \relates GraphReader deba@165: template deba@165: GraphReader graphReader(const char* fn, Graph& graph) { deba@165: GraphReader tmp(fn, graph); deba@165: return tmp; deba@165: } deba@173: deba@190: class SectionReader; deba@190: deba@190: SectionReader sectionReader(std::istream& is); deba@190: SectionReader sectionReader(const std::string& fn); deba@190: SectionReader sectionReader(const char* fn); deba@190: kpeter@192: /// \ingroup lemon_io kpeter@192: /// deba@189: /// \brief Section reader class deba@189: /// kpeter@192: /// In the \ref lgf-format "LGF" file extra sections can be placed, kpeter@192: /// which contain any data in arbitrary format. Such sections can be kpeter@192: /// read with this class. A reading rule can be added to the class kpeter@192: /// with two different functions. With the \c sectionLines() function a kpeter@192: /// functor can process the section line-by-line, while with the \c deba@189: /// sectionStream() member the section can be read from an input deba@189: /// stream. deba@189: class SectionReader { deba@189: private: deba@189: deba@189: std::istream* _is; deba@189: bool local_is; deba@189: deba@189: typedef std::map Sections; deba@189: Sections _sections; deba@189: deba@189: int line_num; deba@189: std::istringstream line; deba@189: deba@189: public: deba@189: deba@189: /// \brief Constructor deba@189: /// deba@189: /// Construct a section reader, which reads from the given input deba@189: /// stream. deba@189: SectionReader(std::istream& is) deba@189: : _is(&is), local_is(false) {} deba@189: deba@189: /// \brief Constructor deba@189: /// deba@189: /// Construct a section reader, which reads from the given file. deba@189: SectionReader(const std::string& fn) deba@189: : _is(new std::ifstream(fn.c_str())), local_is(true) {} deba@189: deba@189: /// \brief Constructor deba@189: /// deba@189: /// Construct a section reader, which reads from the given file. deba@189: SectionReader(const char* fn) deba@189: : _is(new std::ifstream(fn)), local_is(true) {} deba@189: deba@189: /// \brief Destructor deba@189: ~SectionReader() { deba@189: for (Sections::iterator it = _sections.begin(); deba@189: it != _sections.end(); ++it) { deba@189: delete it->second; deba@189: } deba@189: deba@189: if (local_is) { deba@189: delete _is; deba@189: } deba@189: deba@189: } deba@189: deba@189: private: deba@190: deba@190: friend SectionReader sectionReader(std::istream& is); deba@190: friend SectionReader sectionReader(const std::string& fn); deba@190: friend SectionReader sectionReader(const char* fn); deba@190: deba@190: SectionReader(SectionReader& other) deba@190: : _is(other._is), local_is(other.local_is) { deba@190: deba@190: other._is = 0; deba@190: other.local_is = false; deba@190: deba@190: _sections.swap(other._sections); deba@190: } deba@189: deba@189: SectionReader& operator=(const SectionReader&); deba@189: deba@189: public: deba@189: deba@189: /// \name Section readers deba@189: /// @{ deba@189: deba@189: /// \brief Add a section processor with line oriented reading deba@189: /// deba@189: /// The first parameter is the type descriptor of the section, the deba@189: /// second is a functor, which takes just one \c std::string deba@189: /// parameter. At the reading process, each line of the section deba@189: /// will be given to the functor object. However, the empty lines deba@189: /// and the comment lines are filtered out, and the leading deba@189: /// whitespaces are trimmed from each processed string. deba@189: /// deba@189: /// For example let's see a section, which contain several deba@189: /// integers, which should be inserted into a vector. deba@189: ///\code deba@189: /// @numbers deba@189: /// 12 45 23 deba@189: /// 4 deba@189: /// 23 6 deba@189: ///\endcode deba@189: /// kpeter@192: /// The functor is implemented as a struct: deba@189: ///\code deba@189: /// struct NumberSection { deba@189: /// std::vector& _data; deba@189: /// NumberSection(std::vector& data) : _data(data) {} deba@189: /// void operator()(const std::string& line) { deba@189: /// std::istringstream ls(line); deba@189: /// int value; deba@189: /// while (ls >> value) _data.push_back(value); deba@189: /// } deba@189: /// }; deba@189: /// deba@189: /// // ... deba@189: /// deba@189: /// reader.sectionLines("numbers", NumberSection(vec)); deba@189: ///\endcode deba@189: template deba@189: SectionReader& sectionLines(const std::string& type, Functor functor) { kpeter@192: LEMON_ASSERT(!type.empty(), "Type is empty."); deba@189: LEMON_ASSERT(_sections.find(type) == _sections.end(), deba@189: "Multiple reading of section."); deba@189: _sections.insert(std::make_pair(type, deba@189: new _reader_bits::LineSection(functor))); deba@189: return *this; deba@189: } deba@189: deba@189: deba@189: /// \brief Add a section processor with stream oriented reading deba@189: /// deba@189: /// The first parameter is the type of the section, the second is kpeter@192: /// a functor, which takes an \c std::istream& and an \c int& deba@189: /// parameter, the latter regard to the line number of stream. The deba@189: /// functor can read the input while the section go on, and the deba@189: /// line number should be modified accordingly. deba@189: template deba@189: SectionReader& sectionStream(const std::string& type, Functor functor) { kpeter@192: LEMON_ASSERT(!type.empty(), "Type is empty."); deba@189: LEMON_ASSERT(_sections.find(type) == _sections.end(), deba@189: "Multiple reading of section."); deba@189: _sections.insert(std::make_pair(type, deba@189: new _reader_bits::StreamSection(functor))); deba@189: return *this; deba@189: } deba@189: deba@189: /// @} deba@189: deba@189: private: deba@189: deba@189: bool readLine() { deba@189: std::string str; deba@189: while(++line_num, std::getline(*_is, str)) { deba@189: line.clear(); line.str(str); deba@189: char c; deba@189: if (line >> std::ws >> c && c != '#') { deba@189: line.putback(c); deba@189: return true; deba@189: } deba@189: } deba@189: return false; deba@189: } deba@189: deba@189: bool readSuccess() { deba@189: return static_cast(*_is); deba@189: } deba@189: deba@189: void skipSection() { deba@189: char c; deba@189: while (readSuccess() && line >> c && c != '@') { deba@189: readLine(); deba@189: } deba@189: line.putback(c); deba@189: } deba@189: deba@189: public: deba@189: deba@189: deba@189: /// \name Execution of the reader deba@189: /// @{ deba@189: deba@189: /// \brief Start the batch processing deba@189: /// kpeter@192: /// This function starts the batch processing. deba@189: void run() { deba@189: deba@189: LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); deba@189: deba@189: std::set extra_sections; deba@189: deba@189: line_num = 0; deba@189: readLine(); deba@189: skipSection(); deba@189: deba@189: while (readSuccess()) { deba@189: try { deba@189: char c; deba@189: std::string section, caption; deba@189: line >> c; deba@189: _reader_bits::readToken(line, section); deba@189: _reader_bits::readToken(line, caption); deba@189: deba@189: if (line >> c) deba@189: throw DataFormatError("Extra character on the end of line"); deba@189: deba@189: if (extra_sections.find(section) != extra_sections.end()) { deba@189: std::ostringstream msg; deba@189: msg << "Multiple occurence of section " << section; deba@189: throw DataFormatError(msg.str().c_str()); deba@189: } deba@189: Sections::iterator it = _sections.find(section); deba@189: if (it != _sections.end()) { deba@189: extra_sections.insert(section); deba@189: it->second->process(*_is, line_num); deba@189: } deba@189: readLine(); deba@189: skipSection(); deba@189: } catch (DataFormatError& error) { deba@189: error.line(line_num); deba@189: throw; deba@189: } deba@189: } deba@189: for (Sections::iterator it = _sections.begin(); deba@189: it != _sections.end(); ++it) { deba@189: if (extra_sections.find(it->first) == extra_sections.end()) { deba@189: std::ostringstream os; deba@189: os << "Cannot find section: " << it->first; deba@189: throw DataFormatError(os.str().c_str()); deba@189: } deba@189: } deba@189: } deba@189: deba@189: /// @} deba@189: deba@189: }; deba@189: kpeter@192: /// \brief Return a \ref SectionReader class kpeter@192: /// kpeter@192: /// This function just returns a \ref SectionReader class. deba@189: /// \relates SectionReader deba@189: inline SectionReader sectionReader(std::istream& is) { deba@189: SectionReader tmp(is); deba@189: return tmp; deba@189: } deba@189: kpeter@192: /// \brief Return a \ref SectionReader class kpeter@192: /// kpeter@192: /// This function just returns a \ref SectionReader class. deba@189: /// \relates SectionReader deba@189: inline SectionReader sectionReader(const std::string& fn) { deba@189: SectionReader tmp(fn); deba@189: return tmp; deba@189: } deba@189: kpeter@192: /// \brief Return a \ref SectionReader class kpeter@192: /// kpeter@192: /// This function just returns a \ref SectionReader class. deba@189: /// \relates SectionReader deba@189: inline SectionReader sectionReader(const char* fn) { deba@189: SectionReader tmp(fn); deba@189: return tmp; deba@189: } deba@189: deba@173: /// \ingroup lemon_io deba@173: /// alpar@179: /// \brief Reader for the contents of the \ref lgf-format "LGF" file deba@173: /// deba@173: /// This class can be used to read the sections, the map names and deba@173: /// the attributes from a file. Usually, the Lemon programs know deba@173: /// that, which type of graph, which maps and which attributes deba@173: /// should be read from a file, but in general tools (like glemon) alpar@179: /// the contents of an LGF file should be guessed somehow. This class deba@173: /// reads the graph and stores the appropriate information for deba@173: /// reading the graph. deba@173: /// kpeter@192: ///\code kpeter@192: /// LgfContents contents("graph.lgf"); alpar@179: /// contents.run(); deba@173: /// kpeter@192: /// // Does it contain any node section and arc section? alpar@179: /// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) { kpeter@192: /// std::cerr << "Failure, cannot find graph." << std::endl; deba@173: /// return -1; deba@173: /// } kpeter@192: /// std::cout << "The name of the default node section: " alpar@179: /// << contents.nodeSection(0) << std::endl; kpeter@192: /// std::cout << "The number of the arc maps: " alpar@179: /// << contents.arcMaps(0).size() << std::endl; kpeter@192: /// std::cout << "The name of second arc map: " alpar@179: /// << contents.arcMaps(0)[1] << std::endl; deba@173: ///\endcode alpar@179: class LgfContents { deba@173: private: deba@173: deba@173: std::istream* _is; deba@173: bool local_is; deba@173: deba@173: std::vector _node_sections; deba@173: std::vector _edge_sections; deba@173: std::vector _attribute_sections; deba@173: std::vector _extra_sections; deba@173: deba@173: std::vector _arc_sections; deba@173: deba@173: std::vector > _node_maps; deba@173: std::vector > _edge_maps; deba@173: deba@173: std::vector > _attributes; deba@173: deba@173: deba@173: int line_num; deba@173: std::istringstream line; deba@173: deba@173: public: deba@173: deba@173: /// \brief Constructor deba@173: /// alpar@179: /// Construct an \e LGF contents reader, which reads from the given deba@173: /// input stream. alpar@179: LgfContents(std::istream& is) deba@173: : _is(&is), local_is(false) {} deba@173: deba@173: /// \brief Constructor deba@173: /// alpar@179: /// Construct an \e LGF contents reader, which reads from the given deba@173: /// file. alpar@179: LgfContents(const std::string& fn) deba@173: : _is(new std::ifstream(fn.c_str())), local_is(true) {} deba@173: deba@173: /// \brief Constructor deba@173: /// alpar@179: /// Construct an \e LGF contents reader, which reads from the given deba@173: /// file. alpar@179: LgfContents(const char* fn) deba@173: : _is(new std::ifstream(fn)), local_is(true) {} deba@173: deba@173: /// \brief Destructor alpar@179: ~LgfContents() { deba@173: if (local_is) delete _is; deba@173: } deba@173: deba@190: private: deba@190: deba@190: LgfContents(const LgfContents&); deba@190: LgfContents& operator=(const LgfContents&); deba@190: deba@190: public: deba@190: deba@173: deba@173: /// \name Node sections deba@173: /// @{ deba@173: deba@173: /// \brief Gives back the number of node sections in the file. deba@173: /// deba@173: /// Gives back the number of node sections in the file. deba@173: int nodeSectionNum() const { deba@173: return _node_sections.size(); deba@173: } deba@173: kpeter@192: /// \brief Returns the node section name at the given position. deba@173: /// kpeter@192: /// Returns the node section name at the given position. deba@173: const std::string& nodeSection(int i) const { deba@173: return _node_sections[i]; deba@173: } deba@173: deba@173: /// \brief Gives back the node maps for the given section. deba@173: /// deba@173: /// Gives back the node maps for the given section. alpar@182: const std::vector& nodeMapNames(int i) const { deba@173: return _node_maps[i]; deba@173: } deba@173: deba@173: /// @} deba@173: alpar@181: /// \name Arc/Edge sections deba@173: /// @{ deba@173: alpar@181: /// \brief Gives back the number of arc/edge sections in the file. deba@173: /// alpar@181: /// Gives back the number of arc/edge sections in the file. alpar@181: /// \note It is synonym of \c edgeSectionNum(). deba@173: int arcSectionNum() const { deba@173: return _edge_sections.size(); deba@173: } deba@173: kpeter@192: /// \brief Returns the arc/edge section name at the given position. deba@173: /// kpeter@192: /// Returns the arc/edge section name at the given position. alpar@181: /// \note It is synonym of \c edgeSection(). deba@173: const std::string& arcSection(int i) const { deba@173: return _edge_sections[i]; deba@173: } deba@173: alpar@181: /// \brief Gives back the arc/edge maps for the given section. deba@173: /// alpar@181: /// Gives back the arc/edge maps for the given section. alpar@182: /// \note It is synonym of \c edgeMapNames(). alpar@182: const std::vector& arcMapNames(int i) const { deba@173: return _edge_maps[i]; deba@173: } deba@173: deba@173: /// @} deba@173: alpar@181: /// \name Synonyms deba@173: /// @{ deba@173: alpar@181: /// \brief Gives back the number of arc/edge sections in the file. deba@173: /// alpar@181: /// Gives back the number of arc/edge sections in the file. alpar@181: /// \note It is synonym of \c arcSectionNum(). deba@173: int edgeSectionNum() const { deba@173: return _edge_sections.size(); deba@173: } deba@173: deba@173: /// \brief Returns the section name at the given position. deba@173: /// deba@173: /// Returns the section name at the given position. alpar@181: /// \note It is synonym of \c arcSection(). deba@173: const std::string& edgeSection(int i) const { deba@173: return _edge_sections[i]; deba@173: } deba@173: deba@173: /// \brief Gives back the edge maps for the given section. deba@173: /// deba@173: /// Gives back the edge maps for the given section. alpar@182: /// \note It is synonym of \c arcMapNames(). alpar@182: const std::vector& edgeMapNames(int i) const { deba@173: return _edge_maps[i]; deba@173: } deba@173: deba@173: /// @} deba@173: deba@173: /// \name Attribute sections deba@173: /// @{ deba@173: deba@173: /// \brief Gives back the number of attribute sections in the file. deba@173: /// deba@173: /// Gives back the number of attribute sections in the file. deba@173: int attributeSectionNum() const { deba@173: return _attribute_sections.size(); deba@173: } deba@173: kpeter@192: /// \brief Returns the attribute section name at the given position. deba@173: /// kpeter@192: /// Returns the attribute section name at the given position. alpar@182: const std::string& attributeSectionNames(int i) const { deba@173: return _attribute_sections[i]; deba@173: } deba@173: deba@173: /// \brief Gives back the attributes for the given section. deba@173: /// deba@173: /// Gives back the attributes for the given section. deba@173: const std::vector& attributes(int i) const { deba@173: return _attributes[i]; deba@173: } deba@173: deba@173: /// @} deba@173: deba@173: /// \name Extra sections deba@173: /// @{ deba@173: deba@173: /// \brief Gives back the number of extra sections in the file. deba@173: /// deba@173: /// Gives back the number of extra sections in the file. deba@173: int extraSectionNum() const { deba@173: return _extra_sections.size(); deba@173: } deba@173: deba@173: /// \brief Returns the extra section type at the given position. deba@173: /// deba@173: /// Returns the section type at the given position. deba@173: const std::string& extraSection(int i) const { deba@173: return _extra_sections[i]; deba@173: } deba@173: deba@173: /// @} deba@173: deba@173: private: deba@173: deba@173: bool readLine() { deba@173: std::string str; deba@173: while(++line_num, std::getline(*_is, str)) { deba@173: line.clear(); line.str(str); deba@173: char c; deba@173: if (line >> std::ws >> c && c != '#') { deba@173: line.putback(c); deba@173: return true; deba@173: } deba@173: } deba@173: return false; deba@173: } deba@173: deba@173: bool readSuccess() { deba@173: return static_cast(*_is); deba@173: } deba@173: deba@173: void skipSection() { deba@173: char c; deba@173: while (readSuccess() && line >> c && c != '@') { deba@173: readLine(); deba@173: } deba@173: line.putback(c); deba@173: } deba@173: deba@173: void readMaps(std::vector& maps) { deba@186: char c; deba@186: if (!readLine() || !(line >> c) || c == '@') { deba@186: if (readSuccess() && line) line.putback(c); deba@186: return; deba@186: } deba@186: line.putback(c); deba@173: std::string map; deba@173: while (_reader_bits::readToken(line, map)) { deba@173: maps.push_back(map); deba@173: } deba@173: } deba@173: deba@173: void readAttributes(std::vector& attrs) { deba@173: readLine(); deba@173: char c; deba@173: while (readSuccess() && line >> c && c != '@') { deba@173: line.putback(c); deba@173: std::string attr; deba@173: _reader_bits::readToken(line, attr); deba@173: attrs.push_back(attr); deba@173: readLine(); deba@173: } deba@173: line.putback(c); deba@173: } deba@173: deba@173: public: deba@173: alpar@179: /// \name Execution of the contents reader deba@173: /// @{ deba@173: kpeter@192: /// \brief Starts the reading deba@173: /// kpeter@192: /// This function starts the reading. deba@173: void run() { deba@173: deba@173: readLine(); deba@173: skipSection(); deba@173: deba@173: while (readSuccess()) { deba@173: deba@173: char c; deba@173: line >> c; deba@173: deba@173: std::string section, caption; deba@173: _reader_bits::readToken(line, section); deba@173: _reader_bits::readToken(line, caption); deba@173: deba@173: if (section == "nodes") { deba@173: _node_sections.push_back(caption); deba@173: _node_maps.push_back(std::vector()); deba@173: readMaps(_node_maps.back()); deba@173: readLine(); skipSection(); deba@173: } else if (section == "arcs" || section == "edges") { deba@173: _edge_sections.push_back(caption); deba@173: _arc_sections.push_back(section == "arcs"); deba@173: _edge_maps.push_back(std::vector()); deba@173: readMaps(_edge_maps.back()); deba@173: readLine(); skipSection(); deba@173: } else if (section == "attributes") { deba@173: _attribute_sections.push_back(caption); deba@173: _attributes.push_back(std::vector()); deba@173: readAttributes(_attributes.back()); deba@173: } else { deba@173: _extra_sections.push_back(section); deba@173: readLine(); skipSection(); deba@173: } deba@173: } deba@173: } deba@173: deba@173: /// @} deba@173: deba@173: }; deba@127: } deba@127: deba@127: #endif