last-mod-time.cpp 3.7 KB

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