src/lemon/graph_reader.h
changeset 1398 2f21cc34a245
parent 1394 f0c48d7fa73d
child 1408 892c29484414
equal deleted inserted replaced
12:98b3e0f48126 13:b292b0051a99
   179     }
   179     }
   180 
   180 
   181     bool escaped;
   181     bool escaped;
   182   };
   182   };
   183 
   183 
       
   184   class GUIReader {
       
   185   public:
       
   186     virtual void read(std::istream& is) = 0;
       
   187   };
       
   188 
   184   /// \brief The graph reader class.
   189   /// \brief The graph reader class.
   185   ///
   190   ///
   186   ///
   191   ///
   187   /// The given file format may contain several maps and labeled nodes or 
   192   /// The given file format may contain several maps and labeled nodes or 
   188   /// edges.
   193   /// edges.
   261     ///
   266     ///
   262     /// Construct a new GraphReader. It reads into the given graph
   267     /// Construct a new GraphReader. It reads into the given graph
   263     /// and it use the given reader as the default skipper.
   268     /// and it use the given reader as the default skipper.
   264     GraphReader(std::istream& _is, Graph& _graph, 
   269     GraphReader(std::istream& _is, Graph& _graph, 
   265 		const DefaultReader& _reader = DefaultReader()) 
   270 		const DefaultReader& _reader = DefaultReader()) 
   266       : is(_is), graph(_graph), nodeSkipper(_reader), edgeSkipper(_reader) {}
   271       : gui_reader(0), is(_is), graph(_graph), 
       
   272 	nodeSkipper(_reader), edgeSkipper(_reader) {}
   267 
   273 
   268     /// \brief Destruct the graph reader.
   274     /// \brief Destruct the graph reader.
   269     ///
   275     ///
   270     /// Destruct the graph reader.
   276     /// Destruct the graph reader.
   271     ~GraphReader() {
   277     ~GraphReader() {
   409 	  line = readNodes(line_num, nodeInverter);
   415 	  line = readNodes(line_num, nodeInverter);
   410 	}
   416 	}
   411 	if (line.find("@edges") == 0) {
   417 	if (line.find("@edges") == 0) {
   412 	  line = readEdges(line_num, edgeInverter);
   418 	  line = readEdges(line_num, edgeInverter);
   413 	}
   419 	}
       
   420 	if (line.find("@gui") == 0) {
       
   421 	  line = readGUI(line_num);
       
   422 	}
   414 	if (line.find("@end") != 0) {
   423 	if (line.find("@end") != 0) {
   415 	  throw DataFormatError("Invalid control sequence error");
   424 	  throw DataFormatError("Invalid control sequence error");
   416 	}
   425 	}
   417       } catch (DataFormatError e) {
   426       } catch (DataFormatError e) {
   418 	e.line(line_num);
   427 	e.line(line_num);
   419 	throw e;
   428 	throw e;
   420       }
   429       }
       
   430     }
       
   431 
       
   432     GraphReader& readGUI(GUIReader& reader) {
       
   433       gui_reader = &reader;
       
   434       return *this;
   421     }
   435     }
   422 
   436 
   423   private:
   437   private:
   424 
   438 
   425     template <typename Item> class InverterBase;
   439     template <typename Item> class InverterBase;
   542 	} 
   556 	} 
   543       }        
   557       }        
   544       return line;    
   558       return line;    
   545     }
   559     }
   546 
   560 
       
   561     std::string readGUI(int& line_num) {
       
   562       std::stringstream section;
       
   563       std::string line;
       
   564       while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
       
   565 	section << line << std::endl;
       
   566       }
       
   567       if (gui_reader != 0) {
       
   568 	gui_reader->read(section);
       
   569       }
       
   570       return line;
       
   571     }
       
   572 
   547     std::string readNotEmptyLine(std::istream& is, int& line_num) {
   573     std::string readNotEmptyLine(std::istream& is, int& line_num) {
   548       std::string line;
   574       std::string line;
   549       while (++line_num, getline(is, line)) {	
   575       while (++line_num, getline(is, line)) {	
   550 	int vi = line.find('#');
   576 	int vi = line.find('#');
   551 	if (vi != (int)::std::string::npos) {
   577 	if (vi != (int)::std::string::npos) {
   729     typedef std::map<std::string, Node*> NodeReaders;
   755     typedef std::map<std::string, Node*> NodeReaders;
   730     NodeReaders node_readers;
   756     NodeReaders node_readers;
   731 
   757 
   732     typedef std::map<std::string, Edge*> EdgeReaders;
   758     typedef std::map<std::string, Edge*> EdgeReaders;
   733     EdgeReaders edge_readers;
   759     EdgeReaders edge_readers;
       
   760 
       
   761     GUIReader* gui_reader;
   734 
   762 
   735     std::istream& is;
   763     std::istream& is;
   736     Graph& graph;
   764     Graph& graph;
   737 
   765 
   738     SkipReader<Node, DefaultReader> nodeSkipper;
   766     SkipReader<Node, DefaultReader> nodeSkipper;