Small whitespace cleanup
[lsnes.git] / src / library / directory.cpp
bloba69f2a69b24530d26e0f3c4c36e4ee457c33fbc6
1 #include "directory.hpp"
2 #include "string.hpp"
3 #include <dirent.h>
4 #include <boost/filesystem.hpp>
5 #if defined(_WIN32) || defined(_WIN64) || defined(TEST_WIN32_CODE)
6 #include <windows.h>
7 #endif
9 #ifdef BOOST_FILESYSTEM3
10 namespace boost_fs = boost::filesystem3;
11 #else
12 namespace boost_fs = boost::filesystem;
13 #endif
15 namespace directory
17 std::set<std::string> enumerate(const std::string& dir, const std::string& match)
19 std::set<std::string> x;
20 DIR* d;
21 dirent* d2;
22 d = opendir(dir.c_str());
23 if(!d) {
24 (stringfmt() << "Can't read directory '" << dir << "'").throwex();
25 return x;
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);
30 closedir(d);
31 return x;
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);
48 if(t == -1)
49 return 0;
50 return t;
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);
64 return e;
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;
83 #else
84 return rename(oldname, newname);
85 #endif