src/lemon/graph_reader.h
changeset 1317 83f80464f111
parent 1287 984723507b86
child 1333 2640cf6547ff
equal deleted inserted replaced
6:5f8231def067 7:358e3ee5c5f1
    80     ///
    80     ///
    81     /// Reads a quoted string from the given stream.
    81     /// Reads a quoted string from the given stream.
    82     void read(std::istream& is, std::string& value) {
    82     void read(std::istream& is, std::string& value) {
    83       char c;
    83       char c;
    84       value.clear();
    84       value.clear();
    85       is >> ws;
    85       is >> std::ws;
    86       if (!is.get(c) || c != '\"') 
    86       if (!is.get(c) || c != '\"') 
    87 	throw DataFormatError("Quoted string format error");
    87 	throw DataFormatError("Quoted string format error");
    88       while (is.get(c) && c != '\"') {
    88       while (is.get(c) && c != '\"') {
    89 	if (escaped && c == '\\') {
    89 	if (escaped && c == '\\') {
    90 	  value += readEscape(is);
    90 	  value += readEscape(is);
   355   private:
   355   private:
   356 
   356 
   357     template <typename Item> class InverterBase;
   357     template <typename Item> class InverterBase;
   358 
   358 
   359     std::string readNodeSet(int& line_num, 
   359     std::string readNodeSet(int& line_num, 
   360 			    auto_ptr<InverterBase<Node> > & nodeInverter) {
   360 			    std::auto_ptr<InverterBase<Node> >& nodeInverter) {
   361       std::vector<ReaderBase<Node>* > index;
   361       std::vector<ReaderBase<Node>* > index;
   362       {
   362       {
   363 	std::string line = readNotEmptyLine(is, line_num);    
   363 	std::string line = readNotEmptyLine(is, line_num);    
   364 	std::string id;
   364 	std::string id;
   365 	std::istringstream ls(line);	
   365 	std::istringstream ls(line);	
   377 
   377 
   378       if (index.size() == 0) {
   378       if (index.size() == 0) {
   379 	throw DataFormatError("Cannot find node id map");
   379 	throw DataFormatError("Cannot find node id map");
   380       }
   380       }
   381 
   381 
   382       nodeInverter = auto_ptr<InverterBase<Node> >(index[0]->getInverter());
   382       nodeInverter = 
       
   383 	std::auto_ptr<InverterBase<Node> >(index[0]->getInverter());
   383       std::string line;
   384       std::string line;
   384       while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
   385       while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
   385 	Node node = graph.addNode();
   386 	Node node = graph.addNode();
   386 	std::istringstream ls(line);
   387 	std::istringstream ls(line);
   387 	nodeInverter->read(ls, node);
   388 	nodeInverter->read(ls, node);
   391       }
   392       }
   392       return line;
   393       return line;
   393     }
   394     }
   394 
   395 
   395     std::string readEdgeSet(int& line_num, 
   396     std::string readEdgeSet(int& line_num, 
   396 			    auto_ptr<InverterBase<Edge> > & edgeInverter, 
   397 			    std::auto_ptr<InverterBase<Edge> >& edgeInverter, 
   397 			    auto_ptr<InverterBase<Node> > & nodeInverter) {
   398 			    std::auto_ptr<InverterBase<Node> >& nodeInverter) {
   398       std::vector<ReaderBase<Edge>*> index;
   399       std::vector<ReaderBase<Edge>*> index;
   399       {
   400       {
   400 	std::string line = readNotEmptyLine(is, line_num);    
   401 	std::string line = readNotEmptyLine(is, line_num);    
   401 	std::string id;
   402 	std::string id;
   402 	std::istringstream ls(line);	
   403 	std::istringstream ls(line);	
   414 
   415 
   415       if (index.size() == 0) {
   416       if (index.size() == 0) {
   416 	throw DataFormatError("Cannot find edge id map");
   417 	throw DataFormatError("Cannot find edge id map");
   417       }
   418       }
   418 
   419 
   419       edgeInverter = auto_ptr<InverterBase<Edge> >(index[0]->getInverter());
   420       edgeInverter = 
       
   421 	std::auto_ptr<InverterBase<Edge> >(index[0]->getInverter());
   420       std::string line;
   422       std::string line;
   421       while (line = readNotEmptyLine(is, line_num), line[0] != '@') {	
   423       while (line = readNotEmptyLine(is, line_num), line[0] != '@') {	
   422 	std::istringstream ls(line);
   424 	std::istringstream ls(line);
   423 	Node source = nodeInverter->read(ls);
   425 	Node source = nodeInverter->read(ls);
   424 	Node target = nodeInverter->read(ls);
   426 	Node target = nodeInverter->read(ls);
   430       }      
   432       }      
   431       return line;
   433       return line;
   432     }
   434     }
   433 
   435 
   434     std::string readNodes(int& line_num, 
   436     std::string readNodes(int& line_num, 
   435 			  auto_ptr<InverterBase<Node> >& nodeInverter) {
   437 			  std::auto_ptr<InverterBase<Node> >& nodeInverter) {
   436       std::string line;
   438       std::string line;
   437       while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
   439       while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
   438 	std::istringstream ls(line);
   440 	std::istringstream ls(line);
   439 	std::string name;
   441 	std::string name;
   440 	ls >> name;
   442 	ls >> name;
   445       }        
   447       }        
   446       return line;
   448       return line;
   447     }
   449     }
   448 
   450 
   449     std::string readEdges(int& line_num, 
   451     std::string readEdges(int& line_num, 
   450 			  auto_ptr<InverterBase<Edge> >& edgeInverter) {
   452 			  std::auto_ptr<InverterBase<Edge> >& edgeInverter) {
   451       std::string line;
   453       std::string line;
   452       while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
   454       while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
   453 	std::istringstream ls(line);
   455 	std::istringstream ls(line);
   454 	std::string name;
   456 	std::string name;
   455 	ls >> name;
   457 	ls >> name;
   463 
   465 
   464     std::string readNotEmptyLine(std::istream& is, int& line_num) {
   466     std::string readNotEmptyLine(std::istream& is, int& line_num) {
   465       std::string line;
   467       std::string line;
   466       while (++line_num, getline(is, line)) {	
   468       while (++line_num, getline(is, line)) {	
   467 	int vi = line.find_first_not_of(" \t");
   469 	int vi = line.find_first_not_of(" \t");
   468 	if (vi != (int)string::npos && line[vi] != '#') {
   470 	if (vi != (int)std::string::npos && line[vi] != '#') {
   469 	  return line.substr(vi);
   471 	  return line.substr(vi);
   470 	}
   472 	}
   471       }
   473       }
   472       throw DataFormatError("End of stream error");
   474       throw DataFormatError("End of stream error");
   473     }
   475     }
   505 	Value value;
   507 	Value value;
   506 	reader.read(is, value);
   508 	reader.read(is, value);
   507 	map.set(item, value);
   509 	map.set(item, value);
   508 	typename Inverse::iterator it = inverse.find(value);
   510 	typename Inverse::iterator it = inverse.find(value);
   509 	if (it == inverse.end()) {
   511 	if (it == inverse.end()) {
   510 	  inverse.insert(make_pair(value, item));
   512 	  inverse.insert(std::make_pair(value, item));
   511 	} else {
   513 	} else {
   512 	  throw DataFormatError("Multiple ID occurence");
   514 	  throw DataFormatError("Multiple ID occurence");
   513 	}
   515 	}
   514       }
   516       }
   515 
   517 
   543       virtual void read(std::istream& is, const Item& item) {
   545       virtual void read(std::istream& is, const Item& item) {
   544 	Value value;
   546 	Value value;
   545 	reader.read(is, value);
   547 	reader.read(is, value);
   546 	typename Inverse::iterator it = inverse.find(value);
   548 	typename Inverse::iterator it = inverse.find(value);
   547 	if (it == inverse.end()) {
   549 	if (it == inverse.end()) {
   548 	  inverse.insert(make_pair(value, item));
   550 	  inverse.insert(std::make_pair(value, item));
   549 	} else {
   551 	} else {
   550 	  throw DataFormatError("Multiple ID occurence error");
   552 	  throw DataFormatError("Multiple ID occurence error");
   551 	}
   553 	}
   552       }
   554       }
   553 
   555