| 1 | //======================================================================= |
|---|
| 2 | // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee, |
|---|
| 3 | // |
|---|
| 4 | // Distributed under the Boost Software License, Version 1.0. (See |
|---|
| 5 | // accompanying file LICENSE_1_0.txt or copy at |
|---|
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 7 | //======================================================================= |
|---|
| 8 | #include <boost/config.hpp> |
|---|
| 9 | #include <iostream> |
|---|
| 10 | #include <fstream> |
|---|
| 11 | #include <boost/graph/adjacency_list.hpp> |
|---|
| 12 | #include <boost/graph/depth_first_search.hpp> |
|---|
| 13 | #include <boost/graph/graphviz.hpp> |
|---|
| 14 | #include <boost/graph/copy.hpp> |
|---|
| 15 | |
|---|
| 16 | int |
|---|
| 17 | main(int argc, char *argv[]) |
|---|
| 18 | { |
|---|
| 19 | if (argc < 3) { |
|---|
| 20 | std::cerr << "usage: reachable-loop-head.exe <in-file> <out-file>" |
|---|
| 21 | << std::endl; |
|---|
| 22 | return -1; |
|---|
| 23 | } |
|---|
| 24 | using namespace boost; |
|---|
| 25 | GraphvizDigraph g; |
|---|
| 26 | read_graphviz(argv[1], g); |
|---|
| 27 | graph_traits < GraphvizDigraph >::vertex_descriptor loop_head = 1; |
|---|
| 28 | typedef color_traits < default_color_type > Color; |
|---|
| 29 | |
|---|
| 30 | std::vector < default_color_type > |
|---|
| 31 | reachable_from_head(num_vertices(g), Color::white()); |
|---|
| 32 | default_color_type c; |
|---|
| 33 | depth_first_visit(g, loop_head, default_dfs_visitor(), |
|---|
| 34 | make_iterator_property_map(reachable_from_head.begin(), |
|---|
| 35 | get(vertex_index, g), c)); |
|---|
| 36 | |
|---|
| 37 | property_map<GraphvizDigraph, vertex_attribute_t>::type |
|---|
| 38 | vattr_map = get(vertex_attribute, g); |
|---|
| 39 | |
|---|
| 40 | graph_traits < GraphvizDigraph >::vertex_iterator i, i_end; |
|---|
| 41 | for (tie(i, i_end) = vertices(g); i != i_end; ++i) |
|---|
| 42 | if (reachable_from_head[*i] != Color::white()) { |
|---|
| 43 | vattr_map[*i]["color"] = "gray"; |
|---|
| 44 | vattr_map[*i]["style"] = "filled"; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | std::ofstream loops_out(argv[2]); |
|---|
| 48 | #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 |
|---|
| 49 | // VC++ has trouble with the get_property() functions |
|---|
| 50 | loops_out << "digraph G {\n" |
|---|
| 51 | << "size=\"3,3\"\n" |
|---|
| 52 | << "ratio=\"fill\"\n" |
|---|
| 53 | << "shape=\"box\"\n"; |
|---|
| 54 | graph_traits<GraphvizDigraph>::vertex_iterator vi, vi_end; |
|---|
| 55 | for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) { |
|---|
| 56 | loops_out << *vi << "["; |
|---|
| 57 | for (std::map<std::string,std::string>::iterator ai = vattr_map[*vi].begin(); |
|---|
| 58 | ai != vattr_map[*vi].end(); ++ai) { |
|---|
| 59 | loops_out << ai->first << "=" << ai->second; |
|---|
| 60 | if (next(ai) != vattr_map[*vi].end()) |
|---|
| 61 | loops_out << ", "; |
|---|
| 62 | } |
|---|
| 63 | loops_out<< "]"; |
|---|
| 64 | } |
|---|
| 65 | property_map<GraphvizDigraph, edge_attribute_t>::type |
|---|
| 66 | eattr_map = get(edge_attribute, g); |
|---|
| 67 | graph_traits<GraphvizDigraph>::edge_iterator ei, ei_end; |
|---|
| 68 | for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) { |
|---|
| 69 | loops_out << source(*ei, g) << " -> " << target(*ei, g) << "["; |
|---|
| 70 | std::map<std::string,std::string>& attr_map = eattr_map[*ei]; |
|---|
| 71 | for (std::map<std::string,std::string>::iterator eai = attr_map.begin(); |
|---|
| 72 | eai != attr_map.end(); ++eai) { |
|---|
| 73 | loops_out << eai->first << "=" << eai->second; |
|---|
| 74 | if (next(eai) != attr_map.end()) |
|---|
| 75 | loops_out << ", "; |
|---|
| 76 | } |
|---|
| 77 | loops_out<< "]"; |
|---|
| 78 | } |
|---|
| 79 | loops_out << "}\n"; |
|---|
| 80 | #else |
|---|
| 81 | get_property(g, graph_graph_attribute)["size"] = "3,3"; |
|---|
| 82 | get_property(g, graph_graph_attribute)["ratio"] = "fill"; |
|---|
| 83 | get_property(g, graph_vertex_attribute)["shape"] = "box"; |
|---|
| 84 | |
|---|
| 85 | write_graphviz(loops_out, g, |
|---|
| 86 | make_vertex_attributes_writer(g), |
|---|
| 87 | make_edge_attributes_writer(g), |
|---|
| 88 | make_graph_attributes_writer(g)); |
|---|
| 89 | #endif |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | return EXIT_SUCCESS; |
|---|
| 93 | } |
|---|