3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
21 ///\brief Lemon Graph Format reader.
24 #ifndef LEMON_LGF_READER_H
25 #define LEMON_LGF_READER_H
34 #include <lemon/assert.h>
35 #include <lemon/graph_utils.h>
37 #include <lemon/lgf_writer.h>
39 #include <lemon/concept_check.h>
40 #include <lemon/concepts/maps.h>
44 namespace _reader_bits {
46 template <typename Value>
47 struct DefaultConverter {
48 Value operator()(const std::string& str) {
49 std::istringstream is(str);
54 if (is >> std::ws >> c) {
55 throw DataFormatError("Remaining characters in token");
62 struct DefaultConverter<std::string> {
63 std::string operator()(const std::string& str) {
68 template <typename _Item>
69 class MapStorageBase {
75 virtual ~MapStorageBase() {}
77 virtual void set(const Item& item, const std::string& value) = 0;
81 template <typename _Item, typename _Map,
82 typename _Converter = DefaultConverter<typename _Map::Value> >
83 class MapStorage : public MapStorageBase<_Item> {
86 typedef _Converter Converter;
94 MapStorage(Map& map, const Converter& converter = Converter())
95 : _map(map), _converter(converter) {}
96 virtual ~MapStorage() {}
98 virtual void set(const Item& item ,const std::string& value) {
99 _map.set(item, _converter(value));
103 class ValueStorageBase {
105 ValueStorageBase() {}
106 virtual ~ValueStorageBase() {}
108 virtual void set(const std::string&) = 0;
111 template <typename _Value, typename _Converter = DefaultConverter<_Value> >
112 class ValueStorage : public ValueStorageBase {
114 typedef _Value Value;
115 typedef _Converter Converter;
119 Converter _converter;
122 ValueStorage(Value& value, const Converter& converter = Converter())
123 : _value(value), _converter(converter) {}
125 virtual void set(const std::string& value) {
126 _value = _converter(value);
130 template <typename Value>
131 struct MapLookUpConverter {
132 const std::map<std::string, Value>& _map;
134 MapLookUpConverter(const std::map<std::string, Value>& map)
137 Value operator()(const std::string& str) {
138 typename std::map<std::string, Value>::const_iterator it =
140 if (it == _map.end()) {
141 std::ostringstream msg;
142 msg << "Item not found: " << str;
143 throw DataFormatError(msg.str().c_str());
149 bool isWhiteSpace(char c) {
150 return c == ' ' || c == '\t' || c == '\v' ||
151 c == '\n' || c == '\r' || c == '\f';
155 return '0' <= c && c <='7';
158 int valueOct(char c) {
159 LEMON_ASSERT(isOct(c), "The character is not octal.");
164 return ('0' <= c && c <= '9') ||
165 ('a' <= c && c <= 'z') ||
166 ('A' <= c && c <= 'Z');
169 int valueHex(char c) {
170 LEMON_ASSERT(isHex(c), "The character is not hexadecimal.");
171 if ('0' <= c && c <= '9') return c - '0';
172 if ('a' <= c && c <= 'z') return c - 'a' + 10;
176 bool isIdentifierFirstChar(char c) {
177 return ('a' <= c && c <= 'z') ||
178 ('A' <= c && c <= 'Z') || c == '_';
181 bool isIdentifierChar(char c) {
182 return isIdentifierFirstChar(c) ||
183 ('0' <= c && c <= '9');
186 char readEscape(std::istream& is) {
189 throw DataFormatError("Escape format error");
217 if (!is.get(c) || !isHex(c))
218 throw DataFormatError("Escape format error");
219 else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
220 else code = code * 16 + valueHex(c);
227 throw DataFormatError("Escape format error");
228 else if (code = valueOct(c), !is.get(c) || !isOct(c))
230 else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c))
232 else code = code * 8 + valueOct(c);
238 std::istream& readToken(std::istream& is, std::string& str) {
239 std::ostringstream os;
248 while (is.get(c) && c != '\"') {
254 throw DataFormatError("Quoted format error");
257 while (is.get(c) && !isWhiteSpace(c)) {
274 virtual ~Section() {}
275 virtual void process(std::istream& is, int& line_num) = 0;
278 template <typename Functor>
279 class LineSection : public Section {
286 LineSection(const Functor& functor) : _functor(functor) {}
287 virtual ~LineSection() {}
289 virtual void process(std::istream& is, int& line_num) {
292 while (is.get(c) && c != '@') {
295 } else if (c == '#') {
298 } else if (!isWhiteSpace(c)) {
305 if (is) is.putback(c);
306 else if (is.eof()) is.clear();
310 template <typename Functor>
311 class StreamSection : public Section {
318 StreamSection(const Functor& functor) : _functor(functor) {}
319 virtual ~StreamSection() {}
321 virtual void process(std::istream& is, int& line_num) {
322 _functor(is, line_num);
325 while (is.get(c) && c != '@') {
328 } else if (!isWhiteSpace(c)) {
333 if (is) is.putback(c);
334 else if (is.eof()) is.clear();
340 /// \ingroup lemon_io
342 /// \brief LGF reader for directed graphs
344 /// This utility reads an \ref lgf-format "LGF" file.
346 /// The reading method does a batch processing. The user creates a
347 /// reader object, then various reading rules can be added to the
348 /// reader, and eventually the reading is executed with the \c run()
349 /// member function. A map reading rule can be added to the reader
350 /// with the \c nodeMap() or \c arcMap() members. An optional
351 /// converter parameter can also be added as a standard functor
352 /// converting from std::string to the value type of the map. If it
353 /// is set, it will determine how the tokens in the file should be
354 /// is converted to the map's value type. If the functor is not set,
355 /// then a default conversion will be used. One map can be read into
356 /// multiple map objects at the same time. The \c attribute(), \c
357 /// node() and \c arc() functions are used to add attribute reading
361 /// DigraphReader<Digraph>(std::cin, digraph).
362 /// nodeMap("coordinates", coord_map).
363 /// arcMap("capacity", cap_map).
364 /// node("source", src).
365 /// node("target", trg).
366 /// attribute("caption", caption).
370 /// By default the reader uses the first section in the file of the
371 /// proper type. If a section has an optional name, then it can be
372 /// selected for reading by giving an optional name parameter to the
373 /// \c nodes(), \c arcs() or \c attributes() functions. The readers
374 /// also can load extra sections with the \c sectionLines() and
375 /// sectionStream() functions.
377 /// The \c useNodes() and \c useArcs() functions are used to tell the reader
378 /// that the nodes or arcs should not be constructed (added to the
379 /// graph) during the reading, but instead the label map of the items
380 /// are given as a parameter of these functions. An
381 /// application of these function is multipass reading, which is
382 /// important if two \e \@arcs sections must be read from the
383 /// file. In this example the first phase would read the node set and one
384 /// of the arc sets, while the second phase would read the second arc
385 /// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet).
386 /// The previously read label node map should be passed to the \c
387 /// useNodes() functions. Another application of multipass reading when
388 /// paths are given as a node map or an arc map. It is impossible read this in
389 /// a single pass, because the arcs are not constructed when the node
391 template <typename _Digraph>
392 class DigraphReader {
395 typedef _Digraph Digraph;
396 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
406 std::string _nodes_caption;
407 std::string _arcs_caption;
408 std::string _attributes_caption;
410 typedef std::map<std::string, Node> NodeIndex;
411 NodeIndex _node_index;
412 typedef std::map<std::string, Arc> ArcIndex;
415 typedef std::vector<std::pair<std::string,
416 _reader_bits::MapStorageBase<Node>*> > NodeMaps;
419 typedef std::vector<std::pair<std::string,
420 _reader_bits::MapStorageBase<Arc>*> >ArcMaps;
423 typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
425 Attributes _attributes;
427 typedef std::map<std::string, _reader_bits::Section*> Sections;
434 std::istringstream line;
438 /// \brief Constructor
440 /// Construct a directed graph reader, which reads from the given
442 DigraphReader(std::istream& is, Digraph& digraph)
443 : _is(&is), local_is(false), _digraph(digraph),
444 _use_nodes(false), _use_arcs(false) {}
446 /// \brief Constructor
448 /// Construct a directed graph reader, which reads from the given
450 DigraphReader(const std::string& fn, Digraph& digraph)
451 : _is(new std::ifstream(fn.c_str())), local_is(true), _digraph(digraph),
452 _use_nodes(false), _use_arcs(false) {}
454 /// \brief Constructor
456 /// Construct a directed graph reader, which reads from the given
458 DigraphReader(const char* fn, Digraph& digraph)
459 : _is(new std::ifstream(fn)), local_is(true), _digraph(digraph),
460 _use_nodes(false), _use_arcs(false) {}
462 /// \brief Copy constructor
464 /// The copy constructor transfers all data from the other reader,
465 /// therefore the copied reader will not be usable more.
466 DigraphReader(DigraphReader& other)
467 : _is(other._is), local_is(other.local_is), _digraph(other._digraph),
468 _use_nodes(other._use_nodes), _use_arcs(other._use_arcs) {
471 other.local_is = false;
473 _node_index.swap(other._node_index);
474 _arc_index.swap(other._arc_index);
476 _node_maps.swap(other._node_maps);
477 _arc_maps.swap(other._arc_maps);
478 _attributes.swap(other._attributes);
480 _nodes_caption = other._nodes_caption;
481 _arcs_caption = other._arcs_caption;
482 _attributes_caption = other._attributes_caption;
484 _sections.swap(other._sections);
487 /// \brief Destructor
489 for (typename NodeMaps::iterator it = _node_maps.begin();
490 it != _node_maps.end(); ++it) {
494 for (typename ArcMaps::iterator it = _arc_maps.begin();
495 it != _arc_maps.end(); ++it) {
499 for (typename Attributes::iterator it = _attributes.begin();
500 it != _attributes.end(); ++it) {
504 for (typename Sections::iterator it = _sections.begin();
505 it != _sections.end(); ++it) {
517 DigraphReader& operator=(const DigraphReader&);
521 /// \name Reading rules
524 /// \brief Node map reading rule
526 /// Add a node map reading rule to the reader.
527 template <typename Map>
528 DigraphReader& nodeMap(const std::string& caption, Map& map) {
529 checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
530 _reader_bits::MapStorageBase<Node>* storage =
531 new _reader_bits::MapStorage<Node, Map>(map);
532 _node_maps.push_back(std::make_pair(caption, storage));
536 /// \brief Node map reading rule
538 /// Add a node map reading rule with specialized converter to the
540 template <typename Map, typename Converter>
541 DigraphReader& nodeMap(const std::string& caption, Map& map,
542 const Converter& converter = Converter()) {
543 checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
544 _reader_bits::MapStorageBase<Node>* storage =
545 new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
546 _node_maps.push_back(std::make_pair(caption, storage));
550 /// \brief Arc map reading rule
552 /// Add an arc map reading rule to the reader.
553 template <typename Map>
554 DigraphReader& arcMap(const std::string& caption, Map& map) {
555 checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
556 _reader_bits::MapStorageBase<Arc>* storage =
557 new _reader_bits::MapStorage<Arc, Map>(map);
558 _arc_maps.push_back(std::make_pair(caption, storage));
562 /// \brief Arc map reading rule
564 /// Add an arc map reading rule with specialized converter to the
566 template <typename Map, typename Converter>
567 DigraphReader& arcMap(const std::string& caption, Map& map,
568 const Converter& converter = Converter()) {
569 checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
570 _reader_bits::MapStorageBase<Arc>* storage =
571 new _reader_bits::MapStorage<Arc, Map, Converter>(map, converter);
572 _arc_maps.push_back(std::make_pair(caption, storage));
576 /// \brief Attribute reading rule
578 /// Add an attribute reading rule to the reader.
579 template <typename Value>
580 DigraphReader& attribute(const std::string& caption, Value& value) {
581 _reader_bits::ValueStorageBase* storage =
582 new _reader_bits::ValueStorage<Value>(value);
583 _attributes.insert(std::make_pair(caption, storage));
587 /// \brief Attribute reading rule
589 /// Add an attribute reading rule with specialized converter to the
591 template <typename Value, typename Converter>
592 DigraphReader& attribute(const std::string& caption, Value& value,
593 const Converter& converter = Converter()) {
594 _reader_bits::ValueStorageBase* storage =
595 new _reader_bits::ValueStorage<Value, Converter>(value, converter);
596 _attributes.insert(std::make_pair(caption, storage));
600 /// \brief Node reading rule
602 /// Add a node reading rule to reader.
603 DigraphReader& node(const std::string& caption, Node& node) {
604 typedef _reader_bits::MapLookUpConverter<Node> Converter;
605 Converter converter(_node_index);
606 _reader_bits::ValueStorageBase* storage =
607 new _reader_bits::ValueStorage<Node, Converter>(node, converter);
608 _attributes.insert(std::make_pair(caption, storage));
612 /// \brief Arc reading rule
614 /// Add an arc reading rule to reader.
615 DigraphReader& arc(const std::string& caption, Arc& arc) {
616 typedef _reader_bits::MapLookUpConverter<Arc> Converter;
617 Converter converter(_arc_index);
618 _reader_bits::ValueStorageBase* storage =
619 new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
620 _attributes.insert(std::make_pair(caption, storage));
626 /// \name Select section by name
629 /// \brief Set \c \@nodes section to be read
631 /// Set \c \@nodes section to be read
632 DigraphReader& nodes(const std::string& caption) {
633 _nodes_caption = caption;
637 /// \brief Set \c \@arcs section to be read
639 /// Set \c \@arcs section to be read
640 DigraphReader& arcs(const std::string& caption) {
641 _arcs_caption = caption;
645 /// \brief Set \c \@attributes section to be read
647 /// Set \c \@attributes section to be read
648 DigraphReader& attributes(const std::string& caption) {
649 _attributes_caption = caption;
655 /// \name Section readers
658 /// \brief Add a section processor with line oriented reading
660 /// In the \e LGF file extra sections can be placed, which contain
661 /// any data in arbitrary format. These sections can be read with
662 /// this function line by line. The first parameter is the type
663 /// descriptor of the section, the second is a functor, which
664 /// takes just one \c std::string parameter. At the reading
665 /// process, each line of the section will be given to the functor
666 /// object. However, the empty lines and the comment lines are
667 /// filtered out, and the leading whitespaces are stipped from
668 /// each processed string.
670 /// For example let's see a section, which contain several
671 /// integers, which should be inserted into a vector.
679 /// The functor is implemented as an struct:
681 /// struct NumberSection {
682 /// std::vector<int>& _data;
683 /// NumberSection(std::vector<int>& data) : _data(data) {}
684 /// void operator()(const std::string& line) {
685 /// std::istringstream ls(line);
687 /// while (ls >> value) _data.push_back(value);
693 /// reader.sectionLines("numbers", NumberSection(vec));
695 template <typename Functor>
696 DigraphReader& sectionLines(const std::string& type, Functor functor) {
697 LEMON_ASSERT(!type.empty(), "Type is not empty.");
698 LEMON_ASSERT(_sections.find(type) == _sections.end(),
699 "Multiple reading of section.");
700 LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" &&
701 type != "attributes", "Multiple reading of section.");
702 _sections.insert(std::make_pair(type,
703 new _reader_bits::LineSection<Functor>(functor)));
708 /// \brief Add a section processor with stream oriented reading
710 /// In the \e LGF file extra sections can be placed, which contain
711 /// any data in arbitrary format. These sections can be read
712 /// directly with this function. The first parameter is the type
713 /// of the section, the second is a functor, which takes an \c
714 /// std::istream& and an int& parameter, the latter regard to the
715 /// line number of stream. The functor can read the input while
716 /// the section go on, and the line number should be modified
718 template <typename Functor>
719 DigraphReader& sectionStream(const std::string& type, Functor functor) {
720 LEMON_ASSERT(!type.empty(), "Type is not empty.");
721 LEMON_ASSERT(_sections.find(type) == _sections.end(),
722 "Multiple reading of section.");
723 LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" &&
724 type != "attributes", "Multiple reading of section.");
725 _sections.insert(std::make_pair(type,
726 new _reader_bits::StreamSection<Functor>(functor)));
732 /// \name Using previously constructed node or arc set
735 /// \brief Use previously constructed node set
737 /// Use previously constructed node set, and specify the node
739 template <typename Map>
740 DigraphReader& useNodes(const Map& map) {
741 checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
742 LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
744 _writer_bits::DefaultConverter<typename Map::Value> converter;
745 for (NodeIt n(_digraph); n != INVALID; ++n) {
746 _node_index.insert(std::make_pair(converter(map[n]), n));
751 /// \brief Use previously constructed node set
753 /// Use previously constructed node set, and specify the node
754 /// label map and a functor which converts the label map values to
756 template <typename Map, typename Converter>
757 DigraphReader& useNodes(const Map& map,
758 const Converter& converter = Converter()) {
759 checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
760 LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
762 for (NodeIt n(_digraph); n != INVALID; ++n) {
763 _node_index.insert(std::make_pair(converter(map[n]), n));
768 /// \brief Use previously constructed arc set
770 /// Use previously constructed arc set, and specify the arc
772 template <typename Map>
773 DigraphReader& useArcs(const Map& map) {
774 checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
775 LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
777 _writer_bits::DefaultConverter<typename Map::Value> converter;
778 for (ArcIt a(_digraph); a != INVALID; ++a) {
779 _arc_index.insert(std::make_pair(converter(map[a]), a));
784 /// \brief Use previously constructed arc set
786 /// Use previously constructed arc set, and specify the arc
787 /// label map and a functor which converts the label map values to
789 template <typename Map, typename Converter>
790 DigraphReader& useArcs(const Map& map,
791 const Converter& converter = Converter()) {
792 checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
793 LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
795 for (ArcIt a(_digraph); a != INVALID; ++a) {
796 _arc_index.insert(std::make_pair(converter(map[a]), a));
807 while(++line_num, std::getline(*_is, str)) {
808 line.clear(); line.str(str);
810 if (line >> std::ws >> c && c != '#') {
819 return static_cast<bool>(*_is);
824 while (readSuccess() && line >> c && c != '@') {
832 std::vector<int> map_index(_node_maps.size());
833 int map_num, label_index;
836 throw DataFormatError("Cannot find map captions");
839 std::map<std::string, int> maps;
843 while (_reader_bits::readToken(line, map)) {
844 if (maps.find(map) != maps.end()) {
845 std::ostringstream msg;
846 msg << "Multiple occurence of node map: " << map;
847 throw DataFormatError(msg.str().c_str());
849 maps.insert(std::make_pair(map, index));
853 for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
854 std::map<std::string, int>::iterator jt =
855 maps.find(_node_maps[i].first);
856 if (jt == maps.end()) {
857 std::ostringstream msg;
858 msg << "Map not found in file: " << _node_maps[i].first;
859 throw DataFormatError(msg.str().c_str());
861 map_index[i] = jt->second;
865 std::map<std::string, int>::iterator jt = maps.find("label");
866 if (jt == maps.end())
867 throw DataFormatError("Label map not found in file");
868 label_index = jt->second;
870 map_num = maps.size();
874 while (readLine() && line >> c && c != '@') {
877 std::vector<std::string> tokens(map_num);
878 for (int i = 0; i < map_num; ++i) {
879 if (!_reader_bits::readToken(line, tokens[i])) {
880 std::ostringstream msg;
881 msg << "Column not found (" << i + 1 << ")";
882 throw DataFormatError(msg.str().c_str());
885 if (line >> std::ws >> c)
886 throw DataFormatError("Extra character on the end of line");
890 n = _digraph.addNode();
891 _node_index.insert(std::make_pair(tokens[label_index], n));
893 typename std::map<std::string, Node>::iterator it =
894 _node_index.find(tokens[label_index]);
895 if (it == _node_index.end()) {
896 std::ostringstream msg;
897 msg << "Node with label not found: " << tokens[label_index];
898 throw DataFormatError(msg.str().c_str());
903 for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
904 _node_maps[i].second->set(n, tokens[map_index[i]]);
915 std::vector<int> map_index(_arc_maps.size());
916 int map_num, label_index;
919 throw DataFormatError("Cannot find map captions");
922 std::map<std::string, int> maps;
926 while (_reader_bits::readToken(line, map)) {
927 if (maps.find(map) != maps.end()) {
928 std::ostringstream msg;
929 msg << "Multiple occurence of arc map: " << map;
930 throw DataFormatError(msg.str().c_str());
932 maps.insert(std::make_pair(map, index));
936 for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
937 std::map<std::string, int>::iterator jt =
938 maps.find(_arc_maps[i].first);
939 if (jt == maps.end()) {
940 std::ostringstream msg;
941 msg << "Map not found in file: " << _arc_maps[i].first;
942 throw DataFormatError(msg.str().c_str());
944 map_index[i] = jt->second;
948 std::map<std::string, int>::iterator jt = maps.find("label");
949 if (jt == maps.end())
950 throw DataFormatError("Label map not found in file");
951 label_index = jt->second;
953 map_num = maps.size();
957 while (readLine() && line >> c && c != '@') {
960 std::string source_token;
961 std::string target_token;
963 if (!_reader_bits::readToken(line, source_token))
964 throw DataFormatError("Source not found");
966 if (!_reader_bits::readToken(line, target_token))
967 throw DataFormatError("Source not found");
969 std::vector<std::string> tokens(map_num);
970 for (int i = 0; i < map_num; ++i) {
971 if (!_reader_bits::readToken(line, tokens[i])) {
972 std::ostringstream msg;
973 msg << "Column not found (" << i + 1 << ")";
974 throw DataFormatError(msg.str().c_str());
977 if (line >> std::ws >> c)
978 throw DataFormatError("Extra character on the end of line");
983 typename NodeIndex::iterator it;
985 it = _node_index.find(source_token);
986 if (it == _node_index.end()) {
987 std::ostringstream msg;
988 msg << "Item not found: " << source_token;
989 throw DataFormatError(msg.str().c_str());
991 Node source = it->second;
993 it = _node_index.find(target_token);
994 if (it == _node_index.end()) {
995 std::ostringstream msg;
996 msg << "Item not found: " << target_token;
997 throw DataFormatError(msg.str().c_str());
999 Node target = it->second;
1001 a = _digraph.addArc(source, target);
1002 _arc_index.insert(std::make_pair(tokens[label_index], a));
1004 typename std::map<std::string, Arc>::iterator it =
1005 _arc_index.find(tokens[label_index]);
1006 if (it == _arc_index.end()) {
1007 std::ostringstream msg;
1008 msg << "Arc with label not found: " << tokens[label_index];
1009 throw DataFormatError(msg.str().c_str());
1014 for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
1015 _arc_maps[i].second->set(a, tokens[map_index[i]]);
1019 if (readSuccess()) {
1024 void readAttributes() {
1026 std::set<std::string> read_attr;
1029 while (readLine() && line >> c && c != '@') {
1032 std::string attr, token;
1033 if (!_reader_bits::readToken(line, attr))
1034 throw DataFormatError("Attribute name not found");
1035 if (!_reader_bits::readToken(line, token))
1036 throw DataFormatError("Attribute value not found");
1038 throw DataFormatError("Extra character on the end of line");
1041 std::set<std::string>::iterator it = read_attr.find(attr);
1042 if (it != read_attr.end()) {
1043 std::ostringstream msg;
1044 msg << "Multiple occurence of attribute " << attr;
1045 throw DataFormatError(msg.str().c_str());
1047 read_attr.insert(attr);
1051 typename Attributes::iterator it = _attributes.lower_bound(attr);
1052 while (it != _attributes.end() && it->first == attr) {
1053 it->second->set(token);
1059 if (readSuccess()) {
1062 for (typename Attributes::iterator it = _attributes.begin();
1063 it != _attributes.end(); ++it) {
1064 if (read_attr.find(it->first) == read_attr.end()) {
1065 std::ostringstream msg;
1066 msg << "Attribute not found in file: " << it->first;
1067 throw DataFormatError(msg.str().c_str());
1074 /// \name Execution of the reader
1077 /// \brief Start the batch processing
1079 /// This function starts the batch processing
1081 LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
1083 throw DataFormatError("Cannot find file");
1086 bool nodes_done = false;
1087 bool arcs_done = false;
1088 bool attributes_done = false;
1089 std::set<std::string> extra_sections;
1094 while (readSuccess()) {
1098 std::string section, caption;
1100 _reader_bits::readToken(line, section);
1101 _reader_bits::readToken(line, caption);
1104 throw DataFormatError("Extra character on the end of line");
1106 if (section == "nodes" && !nodes_done) {
1107 if (_nodes_caption.empty() || _nodes_caption == caption) {
1111 } else if ((section == "arcs" || section == "edges") &&
1113 if (_arcs_caption.empty() || _arcs_caption == caption) {
1117 } else if (section == "attributes" && !attributes_done) {
1118 if (_attributes_caption.empty() || _attributes_caption == caption) {
1120 attributes_done = true;
1123 if (extra_sections.find(section) != extra_sections.end()) {
1124 std::ostringstream msg;
1125 msg << "Multiple occurence of section " << section;
1126 throw DataFormatError(msg.str().c_str());
1128 Sections::iterator it = _sections.find(section);
1129 if (it != _sections.end()) {
1130 extra_sections.insert(section);
1131 it->second->process(*_is, line_num);
1138 } catch (DataFormatError& error) {
1139 error.line(line_num);
1145 throw DataFormatError("Section @nodes not found");
1149 throw DataFormatError("Section @arcs not found");
1152 if (!attributes_done && !_attributes.empty()) {
1153 throw DataFormatError("Section @attributes not found");
1162 /// \relates DigraphReader
1163 template <typename Digraph>
1164 DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph) {
1165 DigraphReader<Digraph> tmp(is, digraph);
1169 /// \relates DigraphReader
1170 template <typename Digraph>
1171 DigraphReader<Digraph> digraphReader(const std::string& fn,
1173 DigraphReader<Digraph> tmp(fn, digraph);
1177 /// \relates DigraphReader
1178 template <typename Digraph>
1179 DigraphReader<Digraph> digraphReader(const char* fn, Digraph& digraph) {
1180 DigraphReader<Digraph> tmp(fn, digraph);