lemon/lemon_writer.h
changeset 2099 eb126f29038c
parent 2016 ecb067198349
child 2101 439b7f21ccc4
equal deleted inserted replaced
13:cba0d41a1e54 14:ee9bb6fc00c0
    14  * express or implied, and with no claim as to its suitability for any
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    15  * purpose.
    16  *
    16  *
    17  */
    17  */
    18 
    18 
    19 ///\ingroup io_group
    19 ///\ingroup lemon_io
    20 ///\file
    20 ///\file
    21 ///\brief Lemon Format writer.
    21 ///\brief Lemon Format writer.
    22 
    22 
    23 #ifndef LEMON_LEMON_WRITER_H
    23 #ifndef LEMON_LEMON_WRITER_H
    24 #define LEMON_LEMON_WRITER_H
    24 #define LEMON_LEMON_WRITER_H
   255       }
   255       }
   256     };
   256     };
   257 
   257 
   258   }
   258   }
   259 
   259 
   260   /// \ingroup io_group
   260   /// \ingroup lemon_io
   261   /// \brief Lemon Format writer class.
   261   /// \brief Lemon Format writer class.
   262   /// 
   262   /// 
   263   /// The Lemon Format contains several sections. We do not want to
   263   /// The Lemon Format contains several sections. We do not want to
   264   /// determine what sections are in a lemon file we give only a framework
   264   /// determine what sections are in a lemon file we give only a framework
   265   /// to write a section oriented format.
   265   /// to write a section oriented format.
   308       /// \brief The header of section.
   308       /// \brief The header of section.
   309       ///
   309       ///
   310       /// It gives back the header of the section.
   310       /// It gives back the header of the section.
   311       virtual std::string header() = 0;
   311       virtual std::string header() = 0;
   312 
   312 
   313       /// \brief  Writer function of the section.
   313       /// \brief Writer function of the section.
   314       ///
   314       ///
   315       /// Write the content of the section.
   315       /// Write the content of the section.
   316       virtual void write(std::ostream& os) = 0;
   316       virtual void write(std::ostream& os) = 0;
       
   317       
       
   318       /// \brief Gives back true when the section should be written.
       
   319       ///
       
   320       /// Gives back true when the section should be written.
       
   321       virtual bool valid() { return true; }
   317     };
   322     };
   318 
   323 
   319     /// \brief Constructor for LemonWriter.
   324     /// \brief Constructor for LemonWriter.
   320     ///
   325     ///
   321     /// Constructor for LemonWriter which writes to the given stream.
   326     /// Constructor for LemonWriter which writes to the given stream.
   353     /// 
   358     /// 
   354     /// It executes the LemonWriter.
   359     /// It executes the LemonWriter.
   355     void run() {
   360     void run() {
   356       SectionWriters::iterator it;
   361       SectionWriters::iterator it;
   357       for (it = writers.begin(); it != writers.end(); ++it) {
   362       for (it = writers.begin(); it != writers.end(); ++it) {
   358 	*os << (*it)->header() << std::endl;
   363         if ((*it)->valid()) {
   359 	(*it)->write(*os);
   364           *os << (*it)->header() << std::endl;
       
   365           (*it)->write(*os);
       
   366         }
   360       }
   367       }
   361       *os << "@end" << std::endl;
   368       *os << "@end" << std::endl;
   362     }
   369     }
   363 
   370 
   364 
   371 
   462     /// \brief  Writer function of the section.
   469     /// \brief  Writer function of the section.
   463     ///
   470     ///
   464     /// Write the content of the section.
   471     /// Write the content of the section.
   465     virtual void write(std::ostream& os) {
   472     virtual void write(std::ostream& os) {
   466       for (int i = 0; i < (int)writers.size(); ++i) {
   473       for (int i = 0; i < (int)writers.size(); ++i) {
   467 	if (writers[i].first == "label" || (writers[i].first == "id" && labelMap == 0)) {
   474 	if (writers[i].first == "label") {
   468 	  labelMap = writers[i].second;
   475 	  labelMap = writers[i].second;
   469 	  forceLabelMap = false;
   476 	  forceLabelMap = false;
   470 	  break;
   477 	  break;
   471 	}
   478 	}
   472       }
   479       }
   634     virtual void write(std::ostream& os) {
   641     virtual void write(std::ostream& os) {
   635       if (!nodeLabelWriter->isLabelWriter()) {
   642       if (!nodeLabelWriter->isLabelWriter()) {
   636 	throw DataFormatError("Cannot find nodeset or label map");
   643 	throw DataFormatError("Cannot find nodeset or label map");
   637       }
   644       }
   638       for (int i = 0; i < (int)writers.size(); ++i) {
   645       for (int i = 0; i < (int)writers.size(); ++i) {
   639 	if (writers[i].first == "label" || (writers[i].first == "id" && labelMap == 0)) {
   646 	if (writers[i].first == "label") {
   640 	  labelMap = writers[i].second;
   647 	  labelMap = writers[i].second;
   641 	  forceLabelMap = false;
   648 	  forceLabelMap = false;
   642 	  break;
   649 	  break;
   643 	}
   650 	}
   644       }
   651       }
  1006 	os << writers[i].first << ' ';
  1013 	os << writers[i].first << ' ';
  1007 	labelWriter->write(os, *(writers[i].second));
  1014 	labelWriter->write(os, *(writers[i].second));
  1008 	os << std::endl;
  1015 	os << std::endl;
  1009       }
  1016       }
  1010     }
  1017     }
       
  1018 
       
  1019     /// \brief Gives back true when the section should be written.
       
  1020     ///
       
  1021     /// Gives back true when the section should be written.
       
  1022     virtual bool valid() { return !writers.empty(); }
  1011     
  1023     
  1012   private:
  1024   private:
  1013 
  1025 
  1014     std::string name;
  1026     std::string name;
  1015 
  1027 
  1085 	os << writers[i].first << ' ';
  1097 	os << writers[i].first << ' ';
  1086 	labelWriter->write(os, *(writers[i].second));
  1098 	labelWriter->write(os, *(writers[i].second));
  1087 	os << std::endl;
  1099 	os << std::endl;
  1088       }
  1100       }
  1089     }
  1101     }
       
  1102 
       
  1103     /// \brief Gives back true when the section should be written.
       
  1104     ///
       
  1105     /// Gives back true when the section should be written.
       
  1106     virtual bool valid() { return !writers.empty(); }
  1090     
  1107     
  1091   private:
  1108   private:
  1092 
  1109 
  1093     std::string name;
  1110     std::string name;
  1094 
  1111 
  1187 	os << edgeWriters[i].first << ' ';
  1204 	os << edgeWriters[i].first << ' ';
  1188 	edgeLabelWriter->write(os, *(edgeWriters[i].second));
  1205 	edgeLabelWriter->write(os, *(edgeWriters[i].second));
  1189 	os << std::endl;
  1206 	os << std::endl;
  1190       }
  1207       }
  1191     }
  1208     }
       
  1209 
       
  1210     /// \brief Gives back true when the section should be written.
       
  1211     ///
       
  1212     /// Gives back true when the section should be written.
       
  1213     virtual bool valid() { 
       
  1214       return !uEdgeWriters.empty() || !edgeWriters.empty(); 
       
  1215     }
  1192     
  1216     
  1193   private:
  1217   private:
  1194 
  1218 
  1195     std::string name;
  1219     std::string name;
  1196 
  1220 
  1286 	it->second->write(os);
  1310 	it->second->write(os);
  1287 	os << std::endl;
  1311 	os << std::endl;
  1288       }
  1312       }
  1289     }    
  1313     }    
  1290 
  1314 
       
  1315     /// \brief Gives back true when the section should be written.
       
  1316     ///
       
  1317     /// Gives back true when the section should be written.
       
  1318     virtual bool valid() { return !writers.empty(); }
       
  1319 
  1291   private:
  1320   private:
  1292     std::string name;
  1321     std::string name;
  1293 
  1322 
  1294     typedef std::vector<std::pair<std::string, 
  1323     typedef std::vector<std::pair<std::string, 
  1295 				  _writer_bits::ValueWriterBase*> > Writers;
  1324 				  _writer_bits::ValueWriterBase*> > Writers;