1 #include "directory.hpp"
4 #include <boost/filesystem.hpp>
5 #if defined(_WIN32) || defined(_WIN64) || defined(TEST_WIN32_CODE)
9 #ifdef BOOST_FILESYSTEM3
10 namespace boost_fs
= boost::filesystem3
;
12 namespace boost_fs
= boost::filesystem
;
17 std::set
<std::string
> enumerate(const std::string
& dir
, const std::string
& match
)
19 std::set
<std::string
> x
;
22 d
= opendir(dir
.c_str());
24 (stringfmt() << "Can't read directory '" << dir
<< "'").throwex();
27 while((d2
= readdir(d
)))
28 if(regex_match(match
, d2
->d_name
) && strcmp(d2
->d_name
, ".") && strcmp(d2
->d_name
, ".."))
29 x
.insert(dir
+ "/" + d2
->d_name
);
34 std::string
absolute_path(const std::string
& relative
)
36 return boost_fs::absolute(boost_fs::path(relative
)).string();
39 uintmax_t size(const std::string
& path
)
41 return boost_fs::file_size(boost_fs::path(path
));
44 time_t mtime(const std::string
& path
)
46 boost::system::error_code ec
;
47 time_t t
= boost_fs::last_write_time(boost_fs::path(path
), ec
);
53 bool exists(const std::string
& filename
)
55 boost::system::error_code ec
;
56 return boost_fs::exists(boost_fs::path(filename
), ec
);
59 bool is_regular(const std::string
& filename
)
61 boost::system::error_code ec
;
62 boost_fs::file_status stat
= status(boost_fs::path(filename
), ec
);
63 bool e
= is_regular_file(stat
);
67 bool is_directory(const std::string
& filename
)
69 boost_fs::path
p(filename
);
70 return boost_fs::is_directory(p
);
73 bool ensure_exists(const std::string
& path
)
75 boost_fs::path
p(path
);
76 return boost_fs::create_directories(p
) || boost_fs::is_directory(p
);
79 int rename_overwrite(const char* oldname
, const char* newname
)
81 #if defined(_WIN32) || defined(_WIN64) || defined(TEST_WIN32_CODE)
82 return MoveFileEx(oldname
, newname
, MOVEFILE_REPLACE_EXISTING
) ? 0 : -1;
84 return rename(oldname
, newname
);