1 #include <boost/filesystem.hpp>
10 namespace boost_fs
= boost::filesystem
;
12 std::string
search_include(const std::list
<std::string
>& searchpath
, const std::string
& filename
,
13 const std::string
& ref_by
)
15 size_t p
= ref_by
.find_last_of("/");
16 if(p
< ref_by
.length()) {
17 std::string i
= ref_by
;
19 std::string real_fn
= i
+ "/" + filename
;
20 boost_fs::path
p(real_fn
);
21 if(boost_fs::exists(p
) && boost_fs::is_regular_file(p
))
24 for(auto& i
: searchpath
) {
25 std::string real_fn
= i
+ "/" + filename
;
26 boost_fs::path
p(real_fn
);
27 if(boost_fs::exists(p
) && boost_fs::is_regular_file(p
))
30 std::cerr
<< "WARNING: Include file '" << filename
<< "' not found." << std::endl
;
34 time_t get_timestamp(const std::string
& path
)
36 boost_fs::path
p(path
);
37 if(!boost_fs::exists(p
)) return 0;
38 return boost_fs::last_write_time(p
);
41 time_t recursive_scan(const std::list
<std::string
>& searchpath
, const std::string
& filename
,
42 std::map
<std::string
, time_t>& scanned
)
46 if(scanned
.count(filename
))
48 std::ifstream
fp(filename
);
50 std::cerr
<< "WARNING: File '" << filename
<< "' can't be opened." << std::endl
;
53 time_t newest
= get_timestamp(filename
);
54 scanned
[filename
] = newest
;
56 while(std::getline(fp
, tmp
)) {
57 if(tmp
.length() > 0 && tmp
[0] == '#') {
60 if(strncmp(tmp
.c_str(), "#include", 8))
63 while(ptr
< tmp
.length() && isspace((unsigned char)tmp
[ptr
]))
65 if(ptr
== tmp
.length())
70 while(ptr
< tmp
.length() && tmp
[ptr
] != '\"')
72 if(ptr
== tmp
.length())
74 included
= tmp
.substr(iptr
, ptr
- iptr
);
75 newest
= std::max(newest
, recursive_scan(searchpath
, search_include(searchpath
, included
,
82 int main(int argc
, char** argv
)
84 std::list
<std::string
> searchpath
;
85 std::list
<std::string
> files
;
87 for(int i
= 1; i
< argc
; i
++) {
88 if(!step
&& !strcmp(argv
[i
], "--"))
91 searchpath
.push_back(argv
[i
]);
93 files
.push_back(argv
[i
]);
95 searchpath
.push_back(".");
96 for(auto& i
: files
) {
97 std::map
<std::string
, time_t> x
;
98 time_t t
= recursive_scan(searchpath
, i
, x
);
99 if(get_timestamp(i
+ ".dep") < t
) {
100 std::ofstream
y(i
+ ".dep");
102 y
<< j
.second
<< " " << j
.first
<< std::endl
;