1 #include <boost/filesystem.hpp>
10 namespace boost_fs
= boost::filesystem
;
12 bool is_cmdhelp_file(const std::string
& filename
)
14 std::string _filename
= filename
;
15 return (_filename
.length() > 8 && _filename
.substr(0, 8) == "cmdhelp/");
18 std::string
search_include(const std::list
<std::string
>& searchpath
, const std::string
& _filename
,
19 const std::string
& ref_by
)
21 std::string filename
= _filename
;
22 //Hack: process cmdhelp includes internally as the date were for the JSON include.
23 if(is_cmdhelp_file(filename
)) {
24 if(filename
!= "cmdhelp/inverselist.hpp") {
25 filename
= "../src/" + filename
;
26 //Replace the extension with .json.
27 size_t split
= filename
.find_last_of("./\\");
28 if(split
< filename
.length() && filename
[split
] == '.') {
29 filename
= filename
.substr(0, split
) + ".json";
33 size_t p
= ref_by
.find_last_of("/");
34 if(p
< ref_by
.length()) {
35 std::string i
= ref_by
;
37 std::string real_fn
= i
+ "/" + filename
;
38 boost_fs::path
p(real_fn
);
39 if(boost_fs::exists(p
) && boost_fs::is_regular_file(p
))
42 for(auto& i
: searchpath
) {
43 std::string real_fn
= i
+ "/" + filename
;
44 boost_fs::path
p(real_fn
);
45 if(boost_fs::exists(p
) && boost_fs::is_regular_file(p
))
48 std::cerr
<< "WARNING: Include file '" << filename
<< "' not found." << std::endl
;
52 time_t get_timestamp(const std::string
& path
)
54 boost_fs::path
p(path
);
55 if(!boost_fs::exists(p
)) return 0;
56 return boost_fs::last_write_time(p
);
59 time_t recursive_scan(const std::list
<std::string
>& searchpath
, const std::string
& filename
,
60 std::map
<std::string
, time_t>& scanned
)
64 if(scanned
.count(filename
))
66 std::ifstream
fp(filename
);
68 std::cerr
<< "WARNING: File '" << filename
<< "' can't be opened." << std::endl
;
71 time_t newest
= get_timestamp(filename
);
72 scanned
[filename
] = newest
;
74 while(std::getline(fp
, tmp
)) {
75 if(tmp
.length() > 0 && tmp
[0] == '#') {
78 if(strncmp(tmp
.c_str(), "#include", 8))
81 while(ptr
< tmp
.length() && isspace((unsigned char)tmp
[ptr
]))
83 if(ptr
== tmp
.length())
88 while(ptr
< tmp
.length() && tmp
[ptr
] != '\"')
90 if(ptr
== tmp
.length())
92 included
= tmp
.substr(iptr
, ptr
- iptr
);
93 newest
= std::max(newest
, recursive_scan(searchpath
, search_include(searchpath
, included
,
100 int main(int argc
, char** argv
)
102 std::list
<std::string
> searchpath
;
103 std::list
<std::string
> files
;
105 for(int i
= 1; i
< argc
; i
++) {
106 if(!step
&& !strcmp(argv
[i
], "--"))
109 searchpath
.push_back(argv
[i
]);
111 files
.push_back(argv
[i
]);
113 searchpath
.push_back(".");
114 for(auto& i
: files
) {
115 std::map
<std::string
, time_t> x
;
116 time_t t
= recursive_scan(searchpath
, i
, x
);
117 if(get_timestamp(i
+ ".dep") < t
) {
118 std::ofstream
y(i
+ ".dep");
120 y
<< j
.second
<< " " << j
.first
<< std::endl
;