read_graphviz_new.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2004-9 Trustees of Indiana University
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. // read_graphviz_new.hpp -
  7. // Initialize a model of the BGL's MutableGraph concept and an associated
  8. // collection of property maps using a graph expressed in the GraphViz
  9. // DOT Language.
  10. //
  11. // Based on the grammar found at:
  12. // https://web.archive.org/web/20041213234742/http://www.graphviz.org/cvs/doc/info/lang.html
  13. //
  14. // Jeremiah rewrite used grammar found at:
  15. // http://www.graphviz.org/doc/info/lang.html
  16. // and page 34 or http://www.graphviz.org/pdf/dotguide.pdf
  17. //
  18. // See documentation for this code at:
  19. // http://www.boost.org/libs/graph/doc/read_graphviz.html
  20. //
  21. // Author: Jeremiah Willcock
  22. // Ronald Garcia
  23. //
  24. #ifndef BOOST_READ_GRAPHVIZ_NEW_HPP
  25. #define BOOST_READ_GRAPHVIZ_NEW_HPP
  26. #include <boost/ref.hpp>
  27. #include <boost/property_map/dynamic_property_map.hpp>
  28. #include <boost/graph/graph_traits.hpp>
  29. #include <boost/detail/workaround.hpp>
  30. #include <algorithm>
  31. #include <string>
  32. #include <vector>
  33. #include <set>
  34. #include <utility>
  35. #include <map>
  36. #include <iostream>
  37. #include <cstdlib>
  38. namespace boost
  39. {
  40. namespace read_graphviz_detail
  41. {
  42. typedef std::string node_name;
  43. typedef std::string subgraph_name;
  44. typedef std::map< std::string, std::string > properties;
  45. struct node_and_port
  46. {
  47. node_name name;
  48. std::string angle; // Or empty if no angle
  49. std::vector< std::string > location; // Up to two identifiers
  50. friend inline bool operator==(
  51. const node_and_port& a, const node_and_port& b)
  52. {
  53. return a.name == b.name && a.angle == b.angle
  54. && a.location == b.location;
  55. }
  56. friend inline bool operator<(
  57. const node_and_port& a, const node_and_port& b)
  58. {
  59. if (a.name != b.name)
  60. return a.name < b.name;
  61. if (a.angle != b.angle)
  62. return a.angle < b.angle;
  63. return a.location < b.location;
  64. }
  65. };
  66. struct edge_info
  67. {
  68. node_and_port source;
  69. node_and_port target;
  70. properties props;
  71. };
  72. struct parser_result
  73. {
  74. bool graph_is_directed;
  75. bool graph_is_strict;
  76. std::map< node_name, properties > nodes; // Global set
  77. std::vector< edge_info > edges;
  78. std::map< subgraph_name, properties > graph_props; // Root and subgraphs
  79. };
  80. // The actual parser, from libs/graph/src/read_graphviz_new.cpp
  81. void parse_graphviz_from_string(
  82. const std::string& str, parser_result& result, bool want_directed);
  83. // Translate from those results to a graph
  84. void translate_results_to_graph(
  85. const parser_result& r, ::boost::detail::graph::mutate_graph* mg);
  86. } // namespace read_graphviz_detail
  87. namespace detail
  88. {
  89. namespace graph
  90. {
  91. BOOST_GRAPH_DECL bool read_graphviz_new(
  92. const std::string& str, boost::detail::graph::mutate_graph* mg);
  93. } // end namespace graph
  94. } // end namespace detail
  95. template < typename MutableGraph >
  96. bool read_graphviz_new(const std::string& str, MutableGraph& graph,
  97. boost::dynamic_properties& dp, std::string const& node_id = "node_id")
  98. {
  99. boost::detail::graph::mutate_graph_impl< MutableGraph > mg(
  100. graph, dp, node_id);
  101. return detail::graph::read_graphviz_new(str, &mg);
  102. }
  103. } // namespace boost
  104. #endif // BOOST_READ_GRAPHVIZ_NEW_HPP