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 deba@127: ///\brief 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: alpar@156: /// \ingroup lemon_io alpar@156: /// alpar@156: /// \brief 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 deba@162: /// converting from 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 deba@162: /// is converted to the map's value type. 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 alpar@156: /// DigraphReader(std::cin, digraph). alpar@156: /// nodeMap("coordinates", coord_map). alpar@156: /// arcMap("capacity", cap_map). alpar@156: /// node("source", src). alpar@156: /// node("target", trg). alpar@156: /// attribute("caption", caption). alpar@156: /// 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@162: /// \c nodes(), \c arcs() or \c attributes() functions. The readers deba@162: /// also can load extra sections with the \c sectionLines() and deba@162: /// sectionStream() 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 alpar@156: /// application of these function is multipass reading, which is alpar@156: /// important if two \e \@arcs sections must be read from the alpar@156: /// file. In this example 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 alpar@156: /// paths are given as a node map or an arc map. It is impossible 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@162: typedef std::map Sections; deba@162: Sections _sections; deba@162: deba@127: bool _use_nodes; deba@127: bool _use_arcs; deba@127: 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@127: _use_nodes(false), _use_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@127: _use_nodes(false), _use_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@127: _use_nodes(false), _use_arcs(false) {} deba@127: alpar@156: /// \brief Copy constructor alpar@156: /// alpar@156: /// The copy constructor transfers all data from the other reader, alpar@156: /// therefore the copied reader will not be usable more. deba@127: DigraphReader(DigraphReader& other) deba@127: : _is(other._is), local_is(other.local_is), _digraph(other._digraph), deba@127: _use_nodes(other._use_nodes), _use_arcs(other._use_arcs) { deba@127: deba@163: other._is = 0; deba@127: other.local_is = false; deba@127: deba@127: _node_index.swap(other._node_index); deba@127: _arc_index.swap(other._arc_index); deba@127: deba@127: _node_maps.swap(other._node_maps); deba@127: _arc_maps.swap(other._arc_maps); deba@127: _attributes.swap(other._attributes); deba@127: deba@127: _nodes_caption = other._nodes_caption; deba@127: _arcs_caption = other._arcs_caption; deba@127: _attributes_caption = other._attributes_caption; deba@162: deba@162: _sections.swap(other._sections); deba@127: } 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@162: for (typename Sections::iterator it = _sections.begin(); deba@162: it != _sections.end(); ++it) { deba@162: delete it->second; deba@162: } deba@162: deba@127: if (local_is) { deba@127: delete _is; deba@127: } deba@127: deba@127: } deba@127: deba@127: private: deba@127: 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: deba@162: /// \name Section readers deba@162: /// @{ deba@162: deba@162: /// \brief Add a section processor with line oriented reading deba@162: /// deba@162: /// In the \e LGF file extra sections can be placed, which contain deba@162: /// any data in arbitrary format. These sections can be read with deba@162: /// this function line by line. The first parameter is the type deba@162: /// descriptor of the section, the second is a functor, which deba@162: /// takes just one \c std::string parameter. At the reading deba@162: /// process, each line of the section will be given to the functor deba@162: /// object. However, the empty lines and the comment lines are deba@162: /// filtered out, and the leading whitespaces are stipped from deba@162: /// each processed string. deba@162: /// deba@162: /// For example let's see a section, which contain several deba@162: /// integers, which should be inserted into a vector. deba@162: ///\code deba@162: /// @numbers deba@162: /// 12 45 23 deba@162: /// 4 deba@162: /// 23 6 deba@162: ///\endcode deba@162: /// deba@162: /// The functor is implemented as an struct: deba@162: ///\code deba@162: /// struct NumberSection { deba@162: /// std::vector& _data; deba@162: /// NumberSection(std::vector& data) : _data(data) {} deba@162: /// void operator()(const std::string& line) { deba@162: /// std::istringstream ls(line); deba@162: /// int value; deba@162: /// while (ls >> value) _data.push_back(value); deba@162: /// } deba@162: /// }; deba@162: /// deba@162: /// // ... deba@162: /// deba@162: /// reader.sectionLines("numbers", NumberSection(vec)); deba@162: ///\endcode deba@162: template deba@162: DigraphReader& sectionLines(const std::string& type, Functor functor) { deba@162: LEMON_ASSERT(!type.empty(), "Type is not empty."); deba@162: LEMON_ASSERT(_sections.find(type) == _sections.end(), deba@162: "Multiple reading of section."); deba@162: LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" && deba@162: type != "attributes", "Multiple reading of section."); deba@162: _sections.insert(std::make_pair(type, deba@162: new _reader_bits::LineSection(functor))); deba@162: return *this; deba@162: } deba@162: deba@162: deba@162: /// \brief Add a section processor with stream oriented reading deba@162: /// deba@162: /// In the \e LGF file extra sections can be placed, which contain deba@162: /// any data in arbitrary format. These sections can be read deba@162: /// directly with this function. The first parameter is the type deba@162: /// of the section, the second is a functor, which takes an \c deba@162: /// std::istream& and an int& parameter, the latter regard to the deba@162: /// line number of stream. The functor can read the input while deba@162: /// the section go on, and the line number should be modified deba@162: /// accordingly. deba@162: template deba@162: DigraphReader& sectionStream(const std::string& type, Functor functor) { deba@162: LEMON_ASSERT(!type.empty(), "Type is not empty."); deba@162: LEMON_ASSERT(_sections.find(type) == _sections.end(), deba@162: "Multiple reading of section."); deba@162: LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" && deba@162: type != "attributes", "Multiple reading of section."); deba@162: _sections.insert(std::make_pair(type, deba@162: new _reader_bits::StreamSection(functor))); deba@162: return *this; deba@162: } deba@162: deba@162: /// @} deba@162: 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 alpar@156: /// 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 alpar@156: /// std::string. deba@127: template deba@127: DigraphReader& useArcs(const Map& map, deba@127: 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: 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@127: if (!readLine()) deba@127: throw DataFormatError("Cannot find map captions"); 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 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@127: if (jt == maps.end()) deba@127: throw DataFormatError("Label map not found in file"); deba@127: label_index = jt->second; deba@127: } deba@127: map_num = maps.size(); deba@127: } deba@127: deba@127: char c; 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@127: _node_index.insert(std::make_pair(tokens[label_index], n)); deba@127: } else { 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@127: if (!readLine()) deba@127: throw DataFormatError("Cannot find map captions"); 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@127: if (jt == maps.end()) deba@127: throw DataFormatError("Label map not found in file"); deba@127: label_index = jt->second; deba@127: } deba@127: map_num = maps.size(); deba@127: } deba@127: deba@127: char c; 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@127: throw DataFormatError("Source 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@127: _arc_index.insert(std::make_pair(tokens[label_index], a)); deba@127: } else { 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@127: bool nodes_done = false; deba@127: bool arcs_done = false; deba@127: bool attributes_done = false; deba@162: std::set extra_sections; 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@162: if (extra_sections.find(section) != extra_sections.end()) { deba@162: std::ostringstream msg; deba@162: msg << "Multiple occurence of section " << section; deba@162: throw DataFormatError(msg.str().c_str()); deba@162: } deba@162: Sections::iterator it = _sections.find(section); deba@162: if (it != _sections.end()) { deba@162: extra_sections.insert(section); deba@162: it->second->process(*_is, line_num); deba@162: } 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: 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: 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: 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@165: /// \ingroup lemon_io deba@165: /// deba@165: /// \brief LGF reader for undirected graphs deba@165: /// deba@165: /// This utility reads an \ref lgf-format "LGF" file. 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: 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: typedef std::map Sections; deba@165: Sections _sections; deba@165: deba@165: bool _use_nodes; deba@165: bool _use_edges; deba@165: deba@165: int line_num; deba@165: std::istringstream line; deba@165: deba@165: public: deba@165: deba@165: /// \brief Constructor deba@165: /// deba@165: /// Construct a 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@165: _use_nodes(false), _use_edges(false) {} deba@165: deba@165: /// \brief Constructor deba@165: /// deba@165: /// Construct a 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@165: _use_nodes(false), _use_edges(false) {} deba@165: deba@165: /// \brief Constructor deba@165: /// deba@165: /// Construct a 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@165: _use_nodes(false), _use_edges(false) {} deba@165: deba@165: /// \brief Copy constructor deba@165: /// deba@165: /// The copy constructor transfers all data from the other reader, deba@165: /// therefore the copied reader will not be usable more. deba@165: GraphReader(GraphReader& other) deba@165: : _is(other._is), local_is(other.local_is), _graph(other._graph), deba@165: _use_nodes(other._use_nodes), _use_edges(other._use_edges) { deba@165: deba@165: other._is = 0; deba@165: other.local_is = false; deba@165: deba@165: _node_index.swap(other._node_index); deba@165: _edge_index.swap(other._edge_index); deba@165: deba@165: _node_maps.swap(other._node_maps); deba@165: _edge_maps.swap(other._edge_maps); deba@165: _attributes.swap(other._attributes); deba@165: deba@165: _nodes_caption = other._nodes_caption; deba@165: _edges_caption = other._edges_caption; deba@165: _attributes_caption = other._attributes_caption; deba@165: deba@165: _sections.swap(other._sections); deba@165: } 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: for (typename Sections::iterator it = _sections.begin(); deba@165: it != _sections.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@165: 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: /// deba@165: /// 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: /// deba@165: /// 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: /// deba@165: /// 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 Section readers deba@165: /// @{ deba@165: deba@165: /// \brief Add a section processor with line oriented reading deba@165: /// deba@165: /// In the \e LGF file extra sections can be placed, which contain deba@165: /// any data in arbitrary format. These sections can be read with deba@165: /// this function line by line. The first parameter is the type deba@165: /// descriptor of the section, the second is a functor, which deba@165: /// takes just one \c std::string parameter. At the reading deba@165: /// process, each line of the section will be given to the functor deba@165: /// object. However, the empty lines and the comment lines are deba@165: /// filtered out, and the leading whitespaces are stipped from deba@165: /// each processed string. deba@165: /// deba@165: /// For example let's see a section, which contain several deba@165: /// integers, which should be inserted into a vector. deba@165: ///\code deba@165: /// @numbers deba@165: /// 12 45 23 deba@165: /// 4 deba@165: /// 23 6 deba@165: ///\endcode deba@165: /// deba@165: /// The functor is implemented as an struct: deba@165: ///\code deba@165: /// struct NumberSection { deba@165: /// std::vector& _data; deba@165: /// NumberSection(std::vector& data) : _data(data) {} deba@165: /// void operator()(const std::string& line) { deba@165: /// std::istringstream ls(line); deba@165: /// int value; deba@165: /// while (ls >> value) _data.push_back(value); deba@165: /// } deba@165: /// }; deba@165: /// deba@165: /// // ... deba@165: /// deba@165: /// reader.sectionLines("numbers", NumberSection(vec)); deba@165: ///\endcode deba@165: template deba@165: GraphReader& sectionLines(const std::string& type, Functor functor) { deba@165: LEMON_ASSERT(!type.empty(), "Type is not empty."); deba@165: LEMON_ASSERT(_sections.find(type) == _sections.end(), deba@165: "Multiple reading of section."); deba@165: LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" && deba@165: type != "attributes", "Multiple reading of section."); deba@165: _sections.insert(std::make_pair(type, deba@165: new _reader_bits::LineSection(functor))); deba@165: return *this; deba@165: } deba@165: deba@165: deba@165: /// \brief Add a section processor with stream oriented reading deba@165: /// deba@165: /// In the \e LGF file extra sections can be placed, which contain deba@165: /// any data in arbitrary format. These sections can be read deba@165: /// directly with this function. The first parameter is the type deba@165: /// of the section, the second is a functor, which takes an \c deba@165: /// std::istream& and an int& parameter, the latter regard to the deba@165: /// line number of stream. The functor can read the input while deba@165: /// the section go on, and the line number should be modified deba@165: /// accordingly. deba@165: template deba@165: GraphReader& sectionStream(const std::string& type, Functor functor) { deba@165: LEMON_ASSERT(!type.empty(), "Type is not empty."); deba@165: LEMON_ASSERT(_sections.find(type) == _sections.end(), deba@165: "Multiple reading of section."); deba@165: LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" && deba@165: type != "attributes", "Multiple reading of section."); deba@165: _sections.insert(std::make_pair(type, deba@165: new _reader_bits::StreamSection(functor))); 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 deba@165: /// 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 deba@165: /// 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: 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@165: if (!readLine()) deba@165: throw DataFormatError("Cannot find map captions"); 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@165: if (jt == maps.end()) deba@165: throw DataFormatError("Label map not found in file"); deba@165: label_index = jt->second; deba@165: } deba@165: map_num = maps.size(); deba@165: } deba@165: deba@165: char c; 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@165: _node_index.insert(std::make_pair(tokens[label_index], n)); deba@165: } else { 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@165: if (!readLine()) deba@165: throw DataFormatError("Cannot find map captions"); 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@165: if (jt == maps.end()) deba@165: throw DataFormatError("Label map not found in file"); deba@165: label_index = jt->second; deba@165: } deba@165: map_num = maps.size(); deba@165: } deba@165: deba@165: char c; 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@165: throw DataFormatError("Source not found"); deba@165: deba@165: if (!_reader_bits::readToken(line, target_token)) deba@165: throw DataFormatError("Source 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@165: _edge_index.insert(std::make_pair(tokens[label_index], e)); deba@165: } else { 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@165: bool nodes_done = false; deba@165: bool edges_done = false; deba@165: bool attributes_done = false; deba@165: std::set extra_sections; 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@165: if (extra_sections.find(section) != extra_sections.end()) { deba@165: std::ostringstream msg; deba@165: msg << "Multiple occurence of section " << section; deba@165: throw DataFormatError(msg.str().c_str()); deba@165: } deba@165: Sections::iterator it = _sections.find(section); deba@165: if (it != _sections.end()) { deba@165: extra_sections.insert(section); deba@165: it->second->process(*_is, line_num); deba@165: } 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: 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: 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: 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@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: /// alpar@179: ///\code LgfContents contents("graph.lgf"); alpar@179: /// contents.run(); deba@173: /// deba@173: /// // does it contain any node section and arc section alpar@179: /// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) { deba@173: /// std::cerr << "Failure, cannot find graph" << std::endl; deba@173: /// return -1; deba@173: /// } deba@173: /// std::cout << "The name of the default node section : " alpar@179: /// << contents.nodeSection(0) << std::endl; deba@173: /// std::cout << "The number of the arc maps : " alpar@179: /// << contents.arcMaps(0).size() << std::endl; deba@173: /// 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 Copy constructor deba@173: /// deba@173: /// The copy constructor transfers all data from the other reader, deba@173: /// therefore the copied reader will not be usable more. alpar@179: LgfContents(LgfContents& other) deba@173: : _is(other._is), local_is(other.local_is) { deba@173: deba@173: other._is = 0; deba@173: other.local_is = false; deba@173: deba@173: _node_sections.swap(other._node_sections); deba@173: _edge_sections.swap(other._edge_sections); deba@173: _attribute_sections.swap(other._attribute_sections); deba@173: _extra_sections.swap(other._extra_sections); deba@173: deba@173: _arc_sections.swap(other._arc_sections); deba@173: deba@173: _node_maps.swap(other._node_maps); deba@173: _edge_maps.swap(other._edge_maps); deba@173: _attributes.swap(other._attributes); deba@173: } deba@173: deba@173: /// \brief Destructor alpar@179: ~LgfContents() { deba@173: if (local_is) delete _is; deba@173: } deba@173: 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: deba@173: /// \brief Returns the section name at the given position. deba@173: /// deba@173: /// Returns the 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. deba@173: const std::vector& nodeMaps(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: 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 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@181: /// \note It is synonym of \c edgeMaps(). deba@173: const std::vector& arcMaps(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@181: /// \note It is synonym of \c arcMaps(). deba@173: const std::vector& edgeMaps(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: deba@173: /// \brief Returns the section name at the given position. deba@173: /// deba@173: /// Returns the section name at the given position. deba@173: const std::string& attributeSection(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@173: if (!readLine()) deba@173: throw DataFormatError("Cannot find map captions"); 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: deba@173: /// \brief Start the reading deba@173: /// deba@173: /// 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