Changeset 156:e561aa7675de in lemon-main for lemon/lgf_writer.h
- Timestamp:
- 05/17/08 07:30:02 (17 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/lgf_writer.h
r148 r156 205 205 206 206 bool requireEscape(const std::string& str) { 207 if (str.empty() || str[0] == '@') return true; 207 208 std::istringstream is(str); 208 209 char c; … … 232 233 } 233 234 234 /// \e 235 /// \ingroup lemon_io 236 /// 237 /// \brief LGF writer for directed graphs 238 /// 239 /// This utility writes an \ref lgf-format "LGF" file. 240 /// 241 /// The writing method does a batch processing. The user creates a 242 /// writer object, then various writing rules can be added to the 243 /// writer, and eventually the writing is executed with the \c run() 244 /// member function. A map writing rule can be added to the writer 245 /// with the \c nodeMap() or \c arcMap() members. An optional 246 /// converter parameter can also be added as a standard functor converting from 247 /// the value type of the map to std::string. If it is set, it will 248 /// determine how the map's value type is written to the output 249 /// stream. If the functor is not set, then a default conversion 250 /// will be used. The \c attribute(), \c node() and \c arc() functions 251 /// are used to add attribute writing rules. 252 /// 253 ///\code 254 /// DigraphWriter<Digraph>(std::cout, digraph). 255 /// nodeMap("coordinates", coord_map). 256 /// nodeMap("size", size). 257 /// nodeMap("title", title). 258 /// arcMap("capacity", cap_map). 259 /// node("source", src). 260 /// node("target", trg). 261 /// attribute("caption", caption). 262 /// run(); 263 ///\endcode 264 /// 265 /// 266 /// By default, the writer does not write additional captions to the 267 /// sections, but they can be give as an optional parameter of 268 /// the \c nodes(), \c arcs() or \c 269 /// attributes() functions. 270 /// 271 /// The \c skipNodes() and \c skipArcs() functions forbid the 272 /// writing of the sections. If two arc sections should be written to the 273 /// output, it can be done in two passes, the first pass writes the 274 /// node section and the first arc section, then the second pass 275 /// skips the node section and writes just the arc section to the 276 /// stream. The output stream can be retrieved with the \c ostream() 277 /// function, hence the second pass can append its output to the output of the 278 /// first pass. 235 279 template <typename _Digraph> 236 280 class DigraphWriter { … … 274 318 public: 275 319 276 /// \e 320 /// \brief Constructor 321 /// 322 /// Construct a directed graph writer, which writes to the given 323 /// output stream. 277 324 DigraphWriter(std::ostream& is, Digraph& digraph) 278 325 : _os(&is), local_os(false), _digraph(digraph), 279 326 _skip_nodes(false), _skip_arcs(false) {} 280 327 281 /// \e 328 /// \brief Constructor 329 /// 330 /// Construct a directed graph writer, which writes to the given 331 /// output file. 282 332 DigraphWriter(const std::string& fn, Digraph& digraph) 283 333 : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph), 284 334 _skip_nodes(false), _skip_arcs(false) {} 285 335 286 /// \e 336 /// \brief Constructor 337 /// 338 /// Construct a directed graph writer, which writes to the given 339 /// output file. 287 340 DigraphWriter(const char* fn, Digraph& digraph) 288 341 : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph), 289 342 _skip_nodes(false), _skip_arcs(false) {} 290 343 344 /// \brief Copy constructor 345 /// 346 /// The copy constructor transfers all data from the other writer, 347 /// therefore the copied writer will not be usable more. 291 348 DigraphWriter(DigraphWriter& other) 292 349 : _os(other._os), local_os(other.local_os), _digraph(other._digraph), … … 308 365 } 309 366 310 /// \ e367 /// \brief Destructor 311 368 ~DigraphWriter() { 312 369 for (typename NodeMaps::iterator it = _node_maps.begin(); … … 336 393 public: 337 394 338 /// \e 395 /// \name Writing rules 396 /// @{ 397 398 /// \brief Node map reading rule 399 /// 400 /// Add a node map reading rule to the writer. 339 401 template <typename Map> 340 402 DigraphWriter& nodeMap(const std::string& caption, const Map& map) { … … 346 408 } 347 409 348 /// \e 410 /// \brief Node map writing rule 411 /// 412 /// Add a node map writing rule with specialized converter to the 413 /// writer. 349 414 template <typename Map, typename Converter> 350 415 DigraphWriter& nodeMap(const std::string& caption, const Map& map, … … 357 422 } 358 423 359 /// \e 424 /// \brief Arc map writing rule 425 /// 426 /// Add an arc map writing rule to the writer. 360 427 template <typename Map> 361 428 DigraphWriter& arcMap(const std::string& caption, const Map& map) { … … 367 434 } 368 435 369 /// \e 436 /// \brief Arc map writing rule 437 /// 438 /// Add an arc map writing rule with specialized converter to the 439 /// writer. 370 440 template <typename Map, typename Converter> 371 441 DigraphWriter& arcMap(const std::string& caption, const Map& map, … … 378 448 } 379 449 380 /// \e 450 /// \brief Attribute writing rule 451 /// 452 /// Add an attribute writing rule to the writer. 381 453 template <typename Value> 382 454 DigraphWriter& attribute(const std::string& caption, const Value& value) { … … 387 459 } 388 460 389 /// \e 461 /// \brief Attribute writing rule 462 /// 463 /// Add an attribute writing rule with specialized converter to the 464 /// writer. 390 465 template <typename Value, typename Converter> 391 466 DigraphWriter& attribute(const std::string& caption, const Value& value, … … 397 472 } 398 473 399 /// \e 474 /// \brief Node writing rule 475 /// 476 /// Add a node writing rule to the writer. 400 477 DigraphWriter& node(const std::string& caption, const Node& node) { 401 478 typedef _writer_bits::MapLookUpConverter<Node> Converter; … … 407 484 } 408 485 409 /// \e 486 /// \brief Arc writing rule 487 /// 488 /// Add an arc writing rule to writer. 410 489 DigraphWriter& arc(const std::string& caption, const Arc& arc) { 411 490 typedef _writer_bits::MapLookUpConverter<Arc> Converter; … … 417 496 } 418 497 419 /// \e 498 /// \name Select section by name 499 /// @{ 500 501 /// \brief Set \c \@nodes section to be read 502 /// 503 /// Set \c \@nodes section to be read 420 504 DigraphWriter& nodes(const std::string& caption) { 421 505 _nodes_caption = caption; … … 423 507 } 424 508 425 /// \e 509 /// \brief Set \c \@arcs section to be read 510 /// 511 /// Set \c \@arcs section to be read 426 512 DigraphWriter& arcs(const std::string& caption) { 427 513 _arcs_caption = caption; … … 429 515 } 430 516 431 /// \e 517 /// \brief Set \c \@attributes section to be read 518 /// 519 /// Set \c \@attributes section to be read 432 520 DigraphWriter& attributes(const std::string& caption) { 433 521 _attributes_caption = caption; … … 435 523 } 436 524 525 /// \name Skipping section 526 /// @{ 527 528 /// \brief Skip writing the node set 529 /// 530 /// The \c \@nodes section will be not written to the stream. 437 531 DigraphWriter& skipNodes() { 438 532 LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member"); … … 440 534 } 441 535 536 /// \brief Skip writing arc set 537 /// 538 /// The \c \@arcs section will be not written to the stream. 442 539 DigraphWriter& skipArcs() { 443 540 LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member"); 444 541 return *this; 445 542 } 543 544 /// @} 446 545 447 546 private: … … 459 558 *_os << "@nodes"; 460 559 if (!_nodes_caption.empty()) { 461 *_os << ' ' << _nodes_caption;560 _writer_bits::writeToken(*_os << ' ', _nodes_caption); 462 561 } 463 562 *_os << std::endl; … … 468 567 for (typename NodeMaps::iterator it = _node_maps.begin(); 469 568 it != _node_maps.end(); ++it) { 470 *_os << it->first<< '\t';569 _writer_bits::writeToken(*_os, it->first) << '\t'; 471 570 } 472 571 *_os << std::endl; … … 519 618 *_os << "@arcs"; 520 619 if (!_arcs_caption.empty()) { 521 *_os << ' ' << _arcs_caption;620 _writer_bits::writeToken(*_os << ' ', _arcs_caption); 522 621 } 523 622 *_os << std::endl; … … 529 628 for (typename ArcMaps::iterator it = _arc_maps.begin(); 530 629 it != _arc_maps.end(); ++it) { 531 *_os << it->first<< '\t';630 _writer_bits::writeToken(*_os, it->first) << '\t'; 532 631 } 533 632 *_os << std::endl; … … 578 677 *_os << "@attributes"; 579 678 if (!_attributes_caption.empty()) { 580 *_os << ' ' << _attributes_caption;679 _writer_bits::writeToken(*_os << ' ', _attributes_caption); 581 680 } 582 681 *_os << std::endl; 583 682 for (typename Attributes::iterator it = _attributes.begin(); 584 683 it != _attributes.end(); ++it) { 585 *_os << it->first<< ' ';684 _writer_bits::writeToken(*_os, it->first) << ' '; 586 685 _writer_bits::writeToken(*_os, it->second->get()); 587 686 *_os << std::endl; … … 591 690 public: 592 691 593 /// \e 692 /// \name Execution of the writer 693 /// @{ 694 695 /// \brief Start the batch processing 696 /// 697 /// This function starts the batch processing 594 698 void run() { 595 699 if (!_skip_nodes) { … … 602 706 } 603 707 604 /// \e 605 std::ostream& stream() { 708 /// \brief Gives back the stream of the writer 709 /// 710 /// Gives back the stream of the writer 711 std::ostream& ostream() { 606 712 return *_os; 607 713 } 714 715 /// @} 608 716 }; 609 717 718 /// \relates DigraphWriter 610 719 template <typename Digraph> 611 720 DigraphWriter<Digraph> digraphWriter(std::istream& is, Digraph& digraph) { … … 613 722 } 614 723 724 /// \relates DigraphWriter 615 725 template <typename Digraph> 616 726 DigraphWriter<Digraph> digraphWriter(const std::string& fn, … … 619 729 } 620 730 731 /// \relates DigraphWriter 621 732 template <typename Digraph> 622 733 DigraphWriter<Digraph> digraphWriter(const char* fn, Digraph& digraph) {
Note: See TracChangeset
for help on using the changeset viewer.