• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
9 /*
10    IMPORTANT:
11    ~~~~~~~~~~
12 
13    This example appears to be broken and crashes at runtime, see
14    https://github.com/boostorg/graph/issues/148
15 
16 */
17 
18 #ifdef _MSC_VER
19 #define _CRT_SECURE_NO_WARNINGS
20 #endif
21 
22 #include <boost/config.hpp>
23 #include <iostream>
24 #include <fstream>
25 #include <string>
26 #include <ctime>
27 #ifdef BOOST_HAS_UNISTD_H
28 #include <unistd.h>
29 #endif
30 #include <sys/stat.h>
31 #include <boost/graph/adjacency_list.hpp>
32 
33 using namespace boost;
34 
35 template < typename Graph, typename VertexNamePropertyMap >
read_graph_file(std::istream & graph_in,std::istream & name_in,Graph & g,VertexNamePropertyMap name_map)36 void read_graph_file(std::istream& graph_in, std::istream& name_in, Graph& g,
37     VertexNamePropertyMap name_map)
38 {
39     typedef typename graph_traits< Graph >::vertices_size_type size_type;
40     size_type n_vertices;
41     typename graph_traits< Graph >::vertex_descriptor u;
42     typename property_traits< VertexNamePropertyMap >::value_type name;
43 
44     graph_in >> n_vertices; // read in number of vertices
45     for (size_type i = 0; i < n_vertices; ++i)
46     { // Add n vertices to the graph
47         u = add_vertex(g);
48         name_in >> name;
49         put(name_map, u, name); // ** Attach name property to vertex u **
50     }
51     size_type src, targ;
52     while (graph_in >> src) // Read in edges
53         if (graph_in >> targ)
54             add_edge(src, targ, g); // add an edge to the graph
55         else
56             break;
57 }
58 
main(int argc,const char ** argv)59 int main(int argc, const char** argv)
60 {
61     typedef adjacency_list< listS, // Store out-edges of each vertex in a
62                                    // std::list
63         vecS, // Store vertex set in a std::vector
64         directedS, // The graph is directed
65         property< vertex_name_t, std::string > // Add a vertex property
66         >
67         graph_type;
68 
69     graph_type g; // use default constructor to create empty graph
70     std::ifstream file_in(argc >= 2 ? argv[1] : "makefile-dependencies.dat"),
71         name_in(argc >= 2 ? argv[1] : "makefile-target-names.dat");
72     if (!file_in)
73     {
74         std::cerr << "** Error: could not open file makefile-target-names.dat"
75                   << std::endl;
76         exit(-1);
77     }
78     // Obtain internal property map from the graph
79     property_map< graph_type, vertex_name_t >::type name_map
80         = get(vertex_name, g);
81     read_graph_file(file_in, name_in, g, name_map);
82 
83     // Create storage for last modified times
84     std::vector< time_t > last_mod_vec(num_vertices(g));
85     // Create nickname for the property map type
86     typedef iterator_property_map< std::vector< time_t >::iterator,
87         property_map< graph_type, vertex_index_t >::type, time_t, time_t& >
88         iter_map_t;
89     // Create last modified time property map
90     iter_map_t mod_time_map(last_mod_vec.begin(), get(vertex_index, g));
91 
92     property_map< graph_type, vertex_name_t >::type name = get(vertex_name, g);
93     struct stat stat_buf;
94     graph_traits< graph_type >::vertex_descriptor u;
95     typedef graph_traits< graph_type >::vertex_iterator vertex_iter_t;
96     std::pair< vertex_iter_t, vertex_iter_t > p;
97     for (p = vertices(g); p.first != p.second; ++p.first)
98     {
99         u = *p.first;
100         if (stat(name[u].c_str(), &stat_buf) != 0)
101             std::cerr << "error in stat() for file " << name[u] << std::endl;
102         put(mod_time_map, u, stat_buf.st_mtime);
103     }
104 
105     for (p = vertices(g); p.first != p.second; ++p.first)
106     {
107         std::cout << name[*p.first] << " was last modified at "
108                   << ctime(&mod_time_map[*p.first]);
109     }
110     assert(num_vertices(g) == 15);
111     assert(num_edges(g) == 19);
112     return 0;
113 }
114