Move directory stuff to dedicated namespace
This commit is contained in:
parent
436b5c4238
commit
927467b6ec
18 changed files with 49 additions and 43 deletions
|
@ -5,13 +5,16 @@
|
|||
#include <string>
|
||||
#include <cstdlib>
|
||||
|
||||
std::set<std::string> enumerate_directory(const std::string& dir, const std::string& match);
|
||||
std::string get_absolute_path(const std::string& relative);
|
||||
uintmax_t file_get_size(const std::string& path);
|
||||
time_t file_get_mtime(const std::string& path);
|
||||
bool file_exists(const std::string& filename);
|
||||
bool file_is_regular(const std::string& filename);
|
||||
bool file_is_directory(const std::string& filename);
|
||||
bool ensure_directory_exists(const std::string& path);
|
||||
namespace directory
|
||||
{
|
||||
std::set<std::string> enumerate(const std::string& dir, const std::string& match);
|
||||
std::string absolute_path(const std::string& relative);
|
||||
uintmax_t size(const std::string& path);
|
||||
time_t mtime(const std::string& path);
|
||||
bool exists(const std::string& filename);
|
||||
bool is_regular(const std::string& filename);
|
||||
bool is_directory(const std::string& filename);
|
||||
bool ensure_exists(const std::string& path);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -254,7 +254,7 @@ void debug_tracelog(uint64_t proc, const std::string& filename)
|
|||
return;
|
||||
}
|
||||
if(trace_outputs.count(proc)) throw std::runtime_error("Already tracelogging");
|
||||
std::string full_filename = get_absolute_path(filename);
|
||||
std::string full_filename = directory::absolute_path(filename);
|
||||
bool found = false;
|
||||
for(auto i : trace_outputs) {
|
||||
if(i.second.first->full_filename == full_filename) {
|
||||
|
|
|
@ -141,13 +141,13 @@ namespace
|
|||
void autoload_libraries(void(*on_error)(const std::string& libname, const std::string& err, bool system))
|
||||
{
|
||||
try {
|
||||
auto libs = enumerate_directory(get_user_library_dir(), ".*");
|
||||
auto libs = directory::enumerate(get_user_library_dir(), ".*");
|
||||
load_libraries(libs, false, on_error);
|
||||
} catch(std::exception& e) {
|
||||
messages << e.what() << std::endl;
|
||||
}
|
||||
try {
|
||||
auto libs = enumerate_directory(get_system_library_dir(), ".*");
|
||||
auto libs = directory::enumerate(get_system_library_dir(), ".*");
|
||||
load_libraries(libs, true, on_error);
|
||||
} catch(std::exception& e) {
|
||||
messages << e.what() << std::endl;
|
||||
|
|
|
@ -41,7 +41,7 @@ namespace
|
|||
//Handle NULL font.
|
||||
if(filename == "")
|
||||
return &get_builtin_font2();
|
||||
std::string abs_filename = get_absolute_path(filename);
|
||||
std::string abs_filename = directory::absolute_path(filename);
|
||||
if(fonts_in_use.count(abs_filename)) {
|
||||
fonts_in_use[abs_filename].second++;
|
||||
return fonts_in_use[abs_filename].first;
|
||||
|
|
|
@ -169,7 +169,7 @@ std::string get_config_path() throw(std::bad_alloc)
|
|||
}
|
||||
//Try to create 'lsnes'. If it exists (or is created) and is directory, great. Otherwise error out.
|
||||
std::string lsnes_path = basedir + "/lsnes";
|
||||
if(!ensure_directory_exists(lsnes_path)) {
|
||||
if(!directory::ensure_exists(lsnes_path)) {
|
||||
messages << "FATAL: Can't create configuration directory '" << lsnes_path << "'" << std::endl;
|
||||
fatal_error();
|
||||
}
|
||||
|
|
|
@ -189,7 +189,7 @@ std::pair<std::string, std::string> split_author(const std::string& author) thro
|
|||
std::string resolve_relative_path(const std::string& path)
|
||||
{
|
||||
try {
|
||||
return get_absolute_path(path);
|
||||
return directory::absolute_path(path);
|
||||
} catch(...) {
|
||||
return path;
|
||||
}
|
||||
|
|
|
@ -410,7 +410,7 @@ std::map<std::string, std::string> project_enumerate()
|
|||
std::set<std::string> projects;
|
||||
std::map<std::string, std::string> projects2;
|
||||
|
||||
projects = enumerate_directory(get_config_path(), ".*\\.prj");
|
||||
projects = directory::enumerate(get_config_path(), ".*\\.prj");
|
||||
for(auto i : projects) {
|
||||
std::string id = i;
|
||||
size_t split;
|
||||
|
|
|
@ -50,7 +50,7 @@ namespace
|
|||
{
|
||||
if(!db_loaded) load_db();
|
||||
//Database write. If there is existing entry for file, it is overwritten.
|
||||
std::string file = get_absolute_path(_file);
|
||||
std::string file = directory::absolute_path(_file);
|
||||
std::pair<std::string, uint64_t> key = std::make_pair(file, prefix);
|
||||
if(hash == "" && !our_db.count(key))
|
||||
return; //Already correct.
|
||||
|
|
|
@ -9,7 +9,9 @@ namespace boost_fs = boost::filesystem3;
|
|||
namespace boost_fs = boost::filesystem;
|
||||
#endif
|
||||
|
||||
std::set<std::string> enumerate_directory(const std::string& dir, const std::string& match)
|
||||
namespace directory
|
||||
{
|
||||
std::set<std::string> enumerate(const std::string& dir, const std::string& match)
|
||||
{
|
||||
std::set<std::string> x;
|
||||
DIR* d;
|
||||
|
@ -26,17 +28,17 @@ std::set<std::string> enumerate_directory(const std::string& dir, const std::str
|
|||
return x;
|
||||
}
|
||||
|
||||
std::string get_absolute_path(const std::string& relative)
|
||||
std::string absolute_path(const std::string& relative)
|
||||
{
|
||||
return boost_fs::absolute(boost_fs::path(relative)).string();
|
||||
}
|
||||
|
||||
uintmax_t file_get_size(const std::string& path)
|
||||
uintmax_t size(const std::string& path)
|
||||
{
|
||||
return boost_fs::file_size(boost_fs::path(path));
|
||||
}
|
||||
|
||||
time_t file_get_mtime(const std::string& path)
|
||||
time_t mtime(const std::string& path)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
time_t t = boost_fs::last_write_time(boost_fs::path(path), ec);
|
||||
|
@ -45,13 +47,13 @@ time_t file_get_mtime(const std::string& path)
|
|||
return t;
|
||||
}
|
||||
|
||||
bool file_exists(const std::string& filename)
|
||||
bool exists(const std::string& filename)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
return boost_fs::exists(boost_fs::path(filename), ec);
|
||||
}
|
||||
|
||||
bool file_is_regular(const std::string& filename)
|
||||
bool is_regular(const std::string& filename)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
boost_fs::file_status stat = status(boost_fs::path(filename), ec);
|
||||
|
@ -59,14 +61,15 @@ bool file_is_regular(const std::string& filename)
|
|||
return e;
|
||||
}
|
||||
|
||||
bool file_is_directory(const std::string& filename)
|
||||
bool is_directory(const std::string& filename)
|
||||
{
|
||||
boost_fs::path p(filename);
|
||||
return boost_fs::is_directory(p);
|
||||
}
|
||||
|
||||
bool ensure_directory_exists(const std::string& path)
|
||||
bool ensure_exists(const std::string& path)
|
||||
{
|
||||
boost_fs::path p(path);
|
||||
return boost_fs::create_directories(p) || boost_fs::is_directory(p);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ namespace
|
|||
{
|
||||
std::string cache = filename + ".sha256";
|
||||
if(prefixlen) cache += (stringfmt() << "-" << prefixlen).str();
|
||||
time_t filetime = file_get_mtime(filename);
|
||||
time_t filetime = directory::mtime(filename);
|
||||
if(cached_entries.count(cache)) {
|
||||
//Found the cache entry...
|
||||
if(cached_entries[cache].first == filetime)
|
||||
|
@ -81,7 +81,7 @@ namespace
|
|||
{
|
||||
std::string cache = filename + ".sha256";
|
||||
if(prefixlen) cache += (stringfmt() << "-" << prefixlen).str();
|
||||
time_t filetime = file_get_mtime(filename);
|
||||
time_t filetime = directory::mtime(filename);
|
||||
std::ofstream out(cache);
|
||||
cached_entries[cache] = std::make_pair(filetime, value);
|
||||
if(!out)
|
||||
|
@ -93,7 +93,7 @@ namespace
|
|||
|
||||
uint64_t get_file_size(const std::string& filename)
|
||||
{
|
||||
uintmax_t size = file_get_size(filename);
|
||||
uintmax_t size = directory::size(filename);
|
||||
if(size == static_cast<uintmax_t>(-1))
|
||||
return 0;
|
||||
return size;
|
||||
|
@ -482,7 +482,7 @@ image::image(hash& h, const std::string& _filename, const std::string& base,
|
|||
|
||||
if(info.type == info::IT_FILE) {
|
||||
filename = zip::resolverel(_filename, base);
|
||||
filename = get_absolute_path(filename);
|
||||
filename = directory::absolute_path(filename);
|
||||
type = info::IT_FILE;
|
||||
data.reset(new std::vector<char>(filename.begin(), filename.end()));
|
||||
stripped = 0;
|
||||
|
|
|
@ -30,7 +30,7 @@ std::set<std::string> filelist::enumerate()
|
|||
void filelist::add(const std::string& filename)
|
||||
{
|
||||
auto contents = readfile();
|
||||
int64_t ts = file_get_mtime(directory + "/" + filename);
|
||||
int64_t ts = directory::mtime(directory + "/" + filename);
|
||||
contents[filename] = ts;
|
||||
writeback(contents);
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ void filelist::remove(const std::string& filename)
|
|||
{
|
||||
auto contents = readfile();
|
||||
//FIXME: Do something with this?
|
||||
//int64_t ts = file_get_mtime(directory + "/" + filename);
|
||||
//int64_t ts = directory::mtime(directory + "/" + filename);
|
||||
contents.erase(filename);
|
||||
writeback(contents);
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ std::map<std::string, int64_t> filelist::readfile()
|
|||
void filelist::check_stale(std::map<std::string, int64_t>& data)
|
||||
{
|
||||
for(auto& i : data) {
|
||||
int64_t ts = file_get_mtime(directory + "/" + i.first);
|
||||
int64_t ts = directory::mtime(directory + "/" + i.first);
|
||||
//If file timestamp does not match, mark the file as stale.
|
||||
if(i.second != ts)
|
||||
i.second = 0;
|
||||
|
|
|
@ -78,5 +78,5 @@ std::string _running_executable()
|
|||
|
||||
std::string running_executable()
|
||||
{
|
||||
return get_absolute_path(_running_executable());
|
||||
return directory::absolute_path(_running_executable());
|
||||
}
|
||||
|
|
|
@ -341,7 +341,7 @@ reader::~reader() throw()
|
|||
|
||||
reader::reader(const std::string& zipfile) throw(std::bad_alloc, std::runtime_error)
|
||||
{
|
||||
if(!file_is_regular(zipfile))
|
||||
if(!directory::is_regular(zipfile))
|
||||
throw std::runtime_error("Zipfile '" + zipfile + "' is not regular file");
|
||||
zipstream = NULL;
|
||||
refcnt = NULL;
|
||||
|
@ -694,7 +694,7 @@ std::istream& openrel(const std::string& name, const std::string& referencing_pa
|
|||
std::string path_to_open = combine_path(name, referencing_path);
|
||||
std::string final_path = path_to_open;
|
||||
//Try to open this from the main OS filesystem.
|
||||
if(file_is_regular(path_to_open)) {
|
||||
if(directory::is_regular(path_to_open)) {
|
||||
std::ifstream* i = new std::ifstream(path_to_open.c_str(), std::ios::binary);
|
||||
if(i->is_open()) {
|
||||
return *i;
|
||||
|
@ -713,7 +713,7 @@ std::istream& openrel(const std::string& name, const std::string& referencing_pa
|
|||
else
|
||||
membername = path_to_open.substr(split + 1);
|
||||
path_to_open = path_to_open.substr(0, split);
|
||||
if(file_is_regular(path_to_open))
|
||||
if(directory::is_regular(path_to_open))
|
||||
try {
|
||||
reader r(path_to_open);
|
||||
return r[membername];
|
||||
|
@ -739,7 +739,7 @@ bool file_exists(const std::string& name) throw(std::bad_alloc)
|
|||
{
|
||||
std::string path_to_open = name;
|
||||
std::string final_path = path_to_open;
|
||||
if(file_is_regular(path_to_open))
|
||||
if(directory::is_regular(path_to_open))
|
||||
return true;
|
||||
//Didn't succeed. Try to open as ZIP archive.
|
||||
std::string membername;
|
||||
|
@ -753,7 +753,7 @@ bool file_exists(const std::string& name) throw(std::bad_alloc)
|
|||
else
|
||||
membername = path_to_open.substr(split + 1);
|
||||
path_to_open = path_to_open.substr(0, split);
|
||||
if(file_is_regular(path_to_open))
|
||||
if(directory::is_regular(path_to_open))
|
||||
try {
|
||||
reader r(path_to_open);
|
||||
return r.has_member(membername);
|
||||
|
|
|
@ -142,7 +142,7 @@ void wxeditor_plugins::reload_plugins()
|
|||
else
|
||||
name = pluginstbl[sel].first;
|
||||
|
||||
auto dir = enumerate_directory(pathpfx, ".*\\." + extension);
|
||||
auto dir = directory::enumerate(pathpfx, ".*\\." + extension);
|
||||
plugins->Clear();
|
||||
pluginstbl.clear();
|
||||
for(auto i : dir) {
|
||||
|
@ -198,7 +198,7 @@ void wxeditor_plugins::on_add(wxCommandEvent& e)
|
|||
bool overwrite_ok = false;
|
||||
bool first = true;
|
||||
int counter = 2;
|
||||
while(!overwrite_ok && file_exists(nname)) {
|
||||
while(!overwrite_ok && directory::exists(nname)) {
|
||||
if(first) {
|
||||
wxMessageDialog* d3 = new wxMessageDialog(this,
|
||||
towxstring("Plugin '" + name + "' already exists.\n\nOverwrite?"),
|
||||
|
|
|
@ -493,7 +493,7 @@ namespace
|
|||
if(!p)
|
||||
return "";
|
||||
else {
|
||||
auto files = enumerate_directory(p->directory, ".*-[0-9]+\\.png");
|
||||
auto files = directory::enumerate(p->directory, ".*-[0-9]+\\.png");
|
||||
std::set<std::string> numbers;
|
||||
for(auto i : files) {
|
||||
size_t split;
|
||||
|
|
|
@ -329,7 +329,7 @@ no_watch:
|
|||
ok = ok && (projname->GetValue().length() > 0);
|
||||
ok = ok && (projdir->GetValue().length() > 0);
|
||||
ok = ok && (projpfx->GetValue().length() > 0);
|
||||
ok = ok && file_is_directory(tostdstring(projdir->GetValue()));
|
||||
ok = ok && directory::is_directory(tostdstring(projdir->GetValue()));
|
||||
okbutton->Enable(ok);
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
uint64_t get_file_size(const std::string& filename)
|
||||
{
|
||||
uintmax_t size = file_get_size(filename);
|
||||
uintmax_t size = directory::size(filename);
|
||||
if(size == static_cast<uintmax_t>(-1))
|
||||
return 0;
|
||||
return size;
|
||||
|
|
|
@ -37,7 +37,7 @@ namespace
|
|||
{
|
||||
if(!regex_match("[A-Za-z][A-Za-z0-9+.-]*:.+", origname))
|
||||
return origname; //File.
|
||||
if(file_is_regular(origname))
|
||||
if(directory::is_regular(origname))
|
||||
return origname; //Even exists.
|
||||
//Okay, we need to download this.
|
||||
auto download_in_progress = new file_download();
|
||||
|
|
Loading…
Add table
Reference in a new issue