Some tweaks to Lua docs
[lsnes.git] / src / library / directory.cpp
bloba8c7c6f87e5519bb5f1f56ff327a94e649f7c279
1 #include "directory.hpp"
2 #include "string.hpp"
3 #include <dirent.h>
4 #include <boost/filesystem.hpp>
6 #ifdef BOOST_FILESYSTEM3
7 namespace boost_fs = boost::filesystem3;
8 #else
9 namespace boost_fs = boost::filesystem;
10 #endif
12 std::set<std::string> enumerate_directory(const std::string& dir, const std::string& match)
14 std::set<std::string> x;
15 DIR* d;
16 dirent* d2;
17 d = opendir(dir.c_str());
18 if(!d) {
19 (stringfmt() << "Can't read directory '" << dir << "'").throwex();
20 return x;
22 while((d2 = readdir(d)))
23 if(regex_match(match, d2->d_name) && strcmp(d2->d_name, ".") && strcmp(d2->d_name, ".."))
24 x.insert(dir + "/" + d2->d_name);
25 closedir(d);
26 return x;
29 std::string get_absolute_path(const std::string& relative)
31 return boost_fs::absolute(boost_fs::path(relative)).string();
34 uintmax_t file_get_size(const std::string& path)
36 return boost_fs::file_size(boost_fs::path(path));
39 time_t file_get_mtime(const std::string& path)
41 boost::system::error_code ec;
42 time_t t = boost_fs::last_write_time(boost_fs::path(path), ec);
43 if(t == -1)
44 return 0;
45 return t;
48 bool file_exists(const std::string& filename)
50 boost::system::error_code ec;
51 return boost_fs::exists(boost_fs::path(filename), ec);
54 bool file_is_regular(const std::string& filename)
56 boost::system::error_code ec;
57 boost_fs::file_status stat = status(boost_fs::path(filename), ec);
58 bool e = is_regular_file(stat);
59 return e;
62 bool file_is_directory(const std::string& filename)
64 boost_fs::path p(filename);
65 return boost_fs::is_directory(p);
68 bool ensure_directory_exists(const std::string& path)
70 boost_fs::path p(path);
71 return boost_fs::create_directories(p) || boost_fs::is_directory(p);