read_graphviz_new.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. namespace read_graphviz_detail {
  40. typedef std::string node_name;
  41. typedef std::string subgraph_name;
  42. typedef std::map<std::string, std::string> properties;
  43. struct node_and_port {
  44. node_name name;
  45. std::string angle; // Or empty if no angle
  46. std::vector<std::string> location; // Up to two identifiers
  47. friend inline bool operator==(const node_and_port& a, const node_and_port& b) {
  48. return a.name == b.name &&
  49. a.angle == b.angle &&
  50. a.location == b.location;
  51. }
  52. friend inline bool operator<(const node_and_port& a, const node_and_port& b) {
  53. if (a.name != b.name) return a.name < b.name;
  54. if (a.angle != b.angle) return a.angle < b.angle;
  55. return a.location < b.location;
  56. }
  57. };
  58. struct edge_info {
  59. node_and_port source;
  60. node_and_port target;
  61. properties props;
  62. };
  63. struct parser_result {
  64. bool graph_is_directed;
  65. bool graph_is_strict;
  66. std::map<node_name, properties> nodes; // Global set
  67. std::vector<edge_info> edges;
  68. std::map<subgraph_name, properties> graph_props; // Root and subgraphs
  69. };
  70. // The actual parser, from libs/graph/src/read_graphviz_new.cpp
  71. void parse_graphviz_from_string(const std::string& str, parser_result& result, bool want_directed);
  72. // Translate from those results to a graph
  73. void translate_results_to_graph(const parser_result& r, ::boost::detail::graph::mutate_graph* mg);
  74. } // namespace read_graphviz_detail
  75. namespace detail {
  76. namespace graph {
  77. BOOST_GRAPH_DECL bool read_graphviz_new(const std::string& str, boost::detail::graph::mutate_graph* mg);
  78. } // end namespace graph
  79. } // end namespace detail
  80. template <typename MutableGraph>
  81. bool read_graphviz_new(const std::string& str,
  82. MutableGraph& graph, boost::dynamic_properties& dp,
  83. std::string const& node_id = "node_id") {
  84. boost::detail::graph::mutate_graph_impl<MutableGraph> mg(graph, dp, node_id);
  85. return detail::graph::read_graphviz_new(str, &mg);
  86. }
  87. } // namespace boost
  88. #endif // BOOST_READ_GRAPHVIZ_NEW_HPP