lemon/lgf_reader.h
changeset 191 abc5b9d0c67e
parent 189 a63ed81c57ba
child 192 7bf5f97d574f
equal deleted inserted replaced
17:c60f07796eb9 18:61133d65d026
   383 	else if (is.eof()) is.clear();	
   383 	else if (is.eof()) is.clear();	
   384       }
   384       }
   385     };
   385     };
   386     
   386     
   387   }
   387   }
       
   388 
       
   389   template <typename Digraph>
       
   390   class DigraphReader;
       
   391 
       
   392   template <typename Digraph>
       
   393   DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph);
       
   394 
       
   395   template <typename Digraph>
       
   396   DigraphReader<Digraph> digraphReader(const std::string& fn, Digraph& digraph);
       
   397 
       
   398   template <typename Digraph>
       
   399   DigraphReader<Digraph> digraphReader(const char *fn, Digraph& digraph);
   388 
   400 
   389   /// \ingroup lemon_io
   401   /// \ingroup lemon_io
   390   ///  
   402   ///  
   391   /// \brief LGF reader for directed graphs
   403   /// \brief LGF reader for directed graphs
   392   ///
   404   ///
   507     DigraphReader(const char* fn, Digraph& digraph) 
   519     DigraphReader(const char* fn, Digraph& digraph) 
   508       : _is(new std::ifstream(fn)), local_is(true), _digraph(digraph),
   520       : _is(new std::ifstream(fn)), local_is(true), _digraph(digraph),
   509     	_use_nodes(false), _use_arcs(false),
   521     	_use_nodes(false), _use_arcs(false),
   510 	_skip_nodes(false), _skip_arcs(false) {}
   522 	_skip_nodes(false), _skip_arcs(false) {}
   511 
   523 
   512     /// \brief Copy constructor
   524     /// \brief Destructor
   513     ///
   525     ~DigraphReader() {
   514     /// The copy constructor transfers all data from the other reader,
   526       for (typename NodeMaps::iterator it = _node_maps.begin(); 
   515     /// therefore the copied reader will not be usable more. 
   527 	   it != _node_maps.end(); ++it) {
       
   528 	delete it->second;
       
   529       }
       
   530 
       
   531       for (typename ArcMaps::iterator it = _arc_maps.begin(); 
       
   532 	   it != _arc_maps.end(); ++it) {
       
   533 	delete it->second;
       
   534       }
       
   535 
       
   536       for (typename Attributes::iterator it = _attributes.begin(); 
       
   537 	   it != _attributes.end(); ++it) {
       
   538 	delete it->second;
       
   539       }
       
   540 
       
   541       if (local_is) {
       
   542 	delete _is;
       
   543       }
       
   544 
       
   545     }
       
   546 
       
   547   private:
       
   548 
       
   549     friend DigraphReader<Digraph> digraphReader<>(std::istream& is, 
       
   550 						  Digraph& digraph);    
       
   551     friend DigraphReader<Digraph> digraphReader<>(const std::string& fn, 
       
   552 						  Digraph& digraph);   
       
   553     friend DigraphReader<Digraph> digraphReader<>(const char *fn, 
       
   554 						  Digraph& digraph);    
       
   555 
   516     DigraphReader(DigraphReader& other) 
   556     DigraphReader(DigraphReader& other) 
   517       : _is(other._is), local_is(other.local_is), _digraph(other._digraph),
   557       : _is(other._is), local_is(other.local_is), _digraph(other._digraph),
   518 	_use_nodes(other._use_nodes), _use_arcs(other._use_arcs),
   558 	_use_nodes(other._use_nodes), _use_arcs(other._use_arcs),
   519 	_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
   559 	_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
   520 
   560 
   532       _arcs_caption = other._arcs_caption;
   572       _arcs_caption = other._arcs_caption;
   533       _attributes_caption = other._attributes_caption;
   573       _attributes_caption = other._attributes_caption;
   534 
   574 
   535     }
   575     }
   536 
   576 
   537     /// \brief Destructor
       
   538     ~DigraphReader() {
       
   539       for (typename NodeMaps::iterator it = _node_maps.begin(); 
       
   540 	   it != _node_maps.end(); ++it) {
       
   541 	delete it->second;
       
   542       }
       
   543 
       
   544       for (typename ArcMaps::iterator it = _arc_maps.begin(); 
       
   545 	   it != _arc_maps.end(); ++it) {
       
   546 	delete it->second;
       
   547       }
       
   548 
       
   549       for (typename Attributes::iterator it = _attributes.begin(); 
       
   550 	   it != _attributes.end(); ++it) {
       
   551 	delete it->second;
       
   552       }
       
   553 
       
   554       if (local_is) {
       
   555 	delete _is;
       
   556       }
       
   557 
       
   558     }
       
   559 
       
   560   private:
       
   561     
       
   562     DigraphReader& operator=(const DigraphReader&);
   577     DigraphReader& operator=(const DigraphReader&);
   563 
   578 
   564   public:
   579   public:
   565 
   580 
   566     /// \name Reading rules
   581     /// \name Reading rules
  1180   DigraphReader<Digraph> digraphReader(const char* fn, Digraph& digraph) {
  1195   DigraphReader<Digraph> digraphReader(const char* fn, Digraph& digraph) {
  1181     DigraphReader<Digraph> tmp(fn, digraph);
  1196     DigraphReader<Digraph> tmp(fn, digraph);
  1182     return tmp;
  1197     return tmp;
  1183   }
  1198   }
  1184 
  1199 
       
  1200   template <typename Graph>
       
  1201   class GraphReader;
       
  1202 
       
  1203   template <typename Graph>
       
  1204   GraphReader<Graph> graphReader(std::istream& is, Graph& graph);    
       
  1205 
       
  1206   template <typename Graph>
       
  1207   GraphReader<Graph> graphReader(const std::string& fn, Graph& graph);   
       
  1208 
       
  1209   template <typename Graph>
       
  1210   GraphReader<Graph> graphReader(const char *fn, Graph& graph);    
       
  1211 
  1185   /// \ingroup lemon_io
  1212   /// \ingroup lemon_io
  1186   ///  
  1213   ///  
  1187   /// \brief LGF reader for undirected graphs
  1214   /// \brief LGF reader for undirected graphs
  1188   ///
  1215   ///
  1189   /// This utility reads an \ref lgf-format "LGF" file.
  1216   /// This utility reads an \ref lgf-format "LGF" file.
  1258     GraphReader(const char* fn, Graph& graph) 
  1285     GraphReader(const char* fn, Graph& graph) 
  1259       : _is(new std::ifstream(fn)), local_is(true), _graph(graph),
  1286       : _is(new std::ifstream(fn)), local_is(true), _graph(graph),
  1260     	_use_nodes(false), _use_edges(false),
  1287     	_use_nodes(false), _use_edges(false),
  1261 	_skip_nodes(false), _skip_edges(false) {}
  1288 	_skip_nodes(false), _skip_edges(false) {}
  1262 
  1289 
  1263     /// \brief Copy constructor
  1290     /// \brief Destructor
  1264     ///
  1291     ~GraphReader() {
  1265     /// The copy constructor transfers all data from the other reader,
  1292       for (typename NodeMaps::iterator it = _node_maps.begin(); 
  1266     /// therefore the copied reader will not be usable more. 
  1293 	   it != _node_maps.end(); ++it) {
       
  1294 	delete it->second;
       
  1295       }
       
  1296 
       
  1297       for (typename EdgeMaps::iterator it = _edge_maps.begin(); 
       
  1298 	   it != _edge_maps.end(); ++it) {
       
  1299 	delete it->second;
       
  1300       }
       
  1301 
       
  1302       for (typename Attributes::iterator it = _attributes.begin(); 
       
  1303 	   it != _attributes.end(); ++it) {
       
  1304 	delete it->second;
       
  1305       }
       
  1306 
       
  1307       if (local_is) {
       
  1308 	delete _is;
       
  1309       }
       
  1310 
       
  1311     }
       
  1312 
       
  1313   private:
       
  1314     friend GraphReader<Graph> graphReader<>(std::istream& is, Graph& graph);    
       
  1315     friend GraphReader<Graph> graphReader<>(const std::string& fn, 
       
  1316 					    Graph& graph);   
       
  1317     friend GraphReader<Graph> graphReader<>(const char *fn, Graph& graph);    
       
  1318 
  1267     GraphReader(GraphReader& other) 
  1319     GraphReader(GraphReader& other) 
  1268       : _is(other._is), local_is(other.local_is), _graph(other._graph),
  1320       : _is(other._is), local_is(other.local_is), _graph(other._graph),
  1269 	_use_nodes(other._use_nodes), _use_edges(other._use_edges),
  1321 	_use_nodes(other._use_nodes), _use_edges(other._use_edges),
  1270 	_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
  1322 	_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
  1271 
  1323 
  1283       _edges_caption = other._edges_caption;
  1335       _edges_caption = other._edges_caption;
  1284       _attributes_caption = other._attributes_caption;
  1336       _attributes_caption = other._attributes_caption;
  1285 
  1337 
  1286     }
  1338     }
  1287 
  1339 
  1288     /// \brief Destructor
       
  1289     ~GraphReader() {
       
  1290       for (typename NodeMaps::iterator it = _node_maps.begin(); 
       
  1291 	   it != _node_maps.end(); ++it) {
       
  1292 	delete it->second;
       
  1293       }
       
  1294 
       
  1295       for (typename EdgeMaps::iterator it = _edge_maps.begin(); 
       
  1296 	   it != _edge_maps.end(); ++it) {
       
  1297 	delete it->second;
       
  1298       }
       
  1299 
       
  1300       for (typename Attributes::iterator it = _attributes.begin(); 
       
  1301 	   it != _attributes.end(); ++it) {
       
  1302 	delete it->second;
       
  1303       }
       
  1304 
       
  1305       if (local_is) {
       
  1306 	delete _is;
       
  1307       }
       
  1308 
       
  1309     }
       
  1310 
       
  1311   private:
       
  1312     
       
  1313     GraphReader& operator=(const GraphReader&);
  1340     GraphReader& operator=(const GraphReader&);
  1314 
  1341 
  1315   public:
  1342   public:
  1316 
  1343 
  1317     /// \name Reading rules
  1344     /// \name Reading rules
  1975   GraphReader<Graph> graphReader(const char* fn, Graph& graph) {
  2002   GraphReader<Graph> graphReader(const char* fn, Graph& graph) {
  1976     GraphReader<Graph> tmp(fn, graph);
  2003     GraphReader<Graph> tmp(fn, graph);
  1977     return tmp;
  2004     return tmp;
  1978   }
  2005   }
  1979 
  2006 
       
  2007   class SectionReader;
       
  2008 
       
  2009   SectionReader sectionReader(std::istream& is);
       
  2010   SectionReader sectionReader(const std::string& fn);
       
  2011   SectionReader sectionReader(const char* fn);
       
  2012   
  1980   /// \brief Section reader class
  2013   /// \brief Section reader class
  1981   ///
  2014   ///
  1982   /// In the \e LGF file extra sections can be placed, which contain
  2015   /// In the \e LGF file extra sections can be placed, which contain
  1983   /// any data in arbitrary format. Such sections can be read with
  2016   /// any data in arbitrary format. Such sections can be read with
  1984   /// this class. A reading rule can be added with two different
  2017   /// this class. A reading rule can be added with two different
  2017     ///
  2050     ///
  2018     /// Construct a section reader, which reads from the given file.
  2051     /// Construct a section reader, which reads from the given file.
  2019     SectionReader(const char* fn) 
  2052     SectionReader(const char* fn) 
  2020       : _is(new std::ifstream(fn)), local_is(true) {}
  2053       : _is(new std::ifstream(fn)), local_is(true) {}
  2021 
  2054 
  2022     /// \brief Copy constructor
       
  2023     ///
       
  2024     /// The copy constructor transfers all data from the other reader,
       
  2025     /// therefore the copied reader will not be usable more. 
       
  2026     SectionReader(SectionReader& other) 
       
  2027       : _is(other._is), local_is(other.local_is) {
       
  2028 
       
  2029       other._is = 0;
       
  2030       other.local_is = false;
       
  2031       
       
  2032       _sections.swap(other._sections);
       
  2033     }
       
  2034 
       
  2035     /// \brief Destructor
  2055     /// \brief Destructor
  2036     ~SectionReader() {
  2056     ~SectionReader() {
  2037       for (Sections::iterator it = _sections.begin(); 
  2057       for (Sections::iterator it = _sections.begin(); 
  2038 	   it != _sections.end(); ++it) {
  2058 	   it != _sections.end(); ++it) {
  2039 	delete it->second;
  2059 	delete it->second;
  2044       }
  2064       }
  2045 
  2065 
  2046     }
  2066     }
  2047 
  2067 
  2048   private:
  2068   private:
       
  2069 
       
  2070     friend SectionReader sectionReader(std::istream& is);
       
  2071     friend SectionReader sectionReader(const std::string& fn);
       
  2072     friend SectionReader sectionReader(const char* fn);
       
  2073 
       
  2074     SectionReader(SectionReader& other) 
       
  2075       : _is(other._is), local_is(other.local_is) {
       
  2076 
       
  2077       other._is = 0;
       
  2078       other.local_is = false;
       
  2079       
       
  2080       _sections.swap(other._sections);
       
  2081     }
  2049     
  2082     
  2050     SectionReader& operator=(const SectionReader&);
  2083     SectionReader& operator=(const SectionReader&);
  2051 
  2084 
  2052   public:
  2085   public:
  2053 
  2086 
  2293     ///
  2326     ///
  2294     /// Construct an \e LGF contents reader, which reads from the given
  2327     /// Construct an \e LGF contents reader, which reads from the given
  2295     /// file.
  2328     /// file.
  2296     LgfContents(const char* fn)
  2329     LgfContents(const char* fn)
  2297       : _is(new std::ifstream(fn)), local_is(true) {}
  2330       : _is(new std::ifstream(fn)), local_is(true) {}
  2298 
       
  2299     /// \brief Copy constructor
       
  2300     ///
       
  2301     /// The copy constructor transfers all data from the other reader,
       
  2302     /// therefore the copied reader will not be usable more. 
       
  2303     LgfContents(LgfContents& other)
       
  2304       : _is(other._is), local_is(other.local_is) {
       
  2305       
       
  2306       other._is = 0;
       
  2307       other.local_is = false;
       
  2308       
       
  2309       _node_sections.swap(other._node_sections);
       
  2310       _edge_sections.swap(other._edge_sections);
       
  2311       _attribute_sections.swap(other._attribute_sections);
       
  2312       _extra_sections.swap(other._extra_sections);
       
  2313 
       
  2314       _arc_sections.swap(other._arc_sections);
       
  2315 
       
  2316       _node_maps.swap(other._node_maps);
       
  2317       _edge_maps.swap(other._edge_maps);
       
  2318       _attributes.swap(other._attributes);
       
  2319     }
       
  2320     
  2331     
  2321     /// \brief Destructor
  2332     /// \brief Destructor
  2322     ~LgfContents() {
  2333     ~LgfContents() {
  2323       if (local_is) delete _is;
  2334       if (local_is) delete _is;
  2324     }
  2335     }
       
  2336 
       
  2337   private:
       
  2338     
       
  2339     LgfContents(const LgfContents&);
       
  2340     LgfContents& operator=(const LgfContents&);
       
  2341 
       
  2342   public:
  2325 
  2343 
  2326 
  2344 
  2327     /// \name Node sections
  2345     /// \name Node sections
  2328     /// @{
  2346     /// @{
  2329 
  2347