bsnes/nall/directory.hpp
Tim Allen a512d14628 Update to v094r09 release.
byuu says:

This will easily be the biggest diff in the history of higan. And not in
a good way.

* target-higan and target-loki have been blown away completely
* nall and ruby massively updated
* phoenix replaced with hiro (pretty near a total rewrite)
* target-higan restarted using hiro (just a window for now)
* all emulation cores updated to compile again
* installation changed to not require root privileges (installs locally)

For the foreseeable future (maybe even permanently?), the new higan UI
will only build under Linux/BSD with GTK+ 2.20+. Probably the most
likely route for Windows/OS X will be to try and figure out how to build
hiro/GTK on those platforms, as awful as that would be. The other
alternative would be to produce new UIs for those platforms ... which
would actually be a good opportunity to make something much more user
friendly.

Being that I just started on this a few hours ago, that means that for
at least a few weeks, don't expect to be able to actually play any
games. Right now, you can pretty much just compile the binary and that's
it. It's quite possible that some nall changes didn't produce
compilation errors, but will produce runtime errors. So until the UI can
actually load games, we won't know if anything is broken. But we should
mostly be okay. It was mostly just trim<1> -> trim changes, moving to
Hash::SHA256 (much cleaner), and patching some reckless memory copy
functions enough to compile.

Progress isn't going to be like it was before: I'm now dividing my time
much thinner between studying and other hobbies.

My aim this time is not to produce a binary for everyone to play games
on. Rather, it's to keep the emulator alive. I want to be able to apply
critical patches again. And I would also like the base of the emulator
to live on, for use in other emulator frontends that utilize higan.
2015-02-28 12:52:53 +11:00

238 lines
7.3 KiB
C++

#ifndef NALL_DIRECTORY_HPP
#define NALL_DIRECTORY_HPP
#include <nall/file.hpp>
#include <nall/intrinsics.hpp>
#include <nall/sort.hpp>
#include <nall/storage.hpp>
#include <nall/string.hpp>
#include <nall/vector.hpp>
#if defined(PLATFORM_WINDOWS)
#include <nall/windows/utf8.hpp>
#else
#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>
#endif
namespace nall {
struct directory : storage {
static bool create(const string& pathname, unsigned permissions = 0755); //recursive
static bool remove(const string& pathname); //recursive
static bool exists(const string& pathname);
static lstring folders(const string& pathname, const string& pattern = "*") {
lstring folders = directory::ufolders(pathname, pattern);
folders.sort();
return folders;
}
static lstring files(const string& pathname, const string& pattern = "*") {
lstring files = directory::ufiles(pathname, pattern);
files.sort();
return files;
}
static lstring contents(const string& pathname, const string& pattern = "*") {
lstring folders = directory::ufolders(pathname); //pattern search of contents should only filter files
lstring files = directory::ufiles(pathname, pattern);
folders.sort();
files.sort();
for(auto& file : files) folders.append(file);
return folders;
}
static lstring ifolders(const string& pathname, const string& pattern = "*") {
lstring folders = ufolders(pathname, pattern);
folders.isort();
return folders;
}
static lstring ifiles(const string& pathname, const string& pattern = "*") {
lstring files = ufiles(pathname, pattern);
files.isort();
return files;
}
static lstring icontents(const string& pathname, const string& pattern = "*") {
lstring folders = directory::ufolders(pathname); //pattern search of contents should only filter files
lstring files = directory::ufiles(pathname, pattern);
folders.isort();
files.isort();
for(auto& file : files) folders.append(file);
return folders;
}
private:
//internal functions; these return unsorted lists
static lstring ufolders(const string& pathname, const string& pattern = "*");
static lstring ufiles(const string& pathname, const string& pattern = "*");
};
#if defined(PLATFORM_WINDOWS)
inline bool directory::create(const string& pathname, unsigned permissions) {
string path;
lstring list = string{pathname}.transform("\\", "/").rtrim("/").split("/");
bool result = true;
for(auto& part : list) {
path.append(part, "/");
result &= (_wmkdir(utf16_t(path)) == 0);
}
return result;
}
inline bool directory::remove(const string& pathname) {
lstring list = directory::contents(pathname);
for(auto& name : list) {
if(name.endsWith("/")) directory::remove({pathname, name});
else file::remove({pathname, name});
}
return _wrmdir(utf16_t(pathname)) == 0;
}
inline bool directory::exists(const string& pathname) {
string name = pathname;
name.trim("\"", "\"");
DWORD result = GetFileAttributes(utf16_t(name));
if(result == INVALID_FILE_ATTRIBUTES) return false;
return (result & FILE_ATTRIBUTE_DIRECTORY);
}
inline lstring directory::ufolders(const string& pathname, const string& pattern) {
lstring list;
string path = pathname;
path.transform("/", "\\");
if(!path.endsWith("\\")) path.append("\\");
path.append("*");
HANDLE handle;
WIN32_FIND_DATA data;
handle = FindFirstFile(utf16_t(path), &data);
if(handle != INVALID_HANDLE_VALUE) {
if(wcscmp(data.cFileName, L".") && wcscmp(data.cFileName, L"..")) {
if(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
string name = (const char*)utf8_t(data.cFileName);
if(name.match(pattern)) list.append(name);
}
}
while(FindNextFile(handle, &data) != false) {
if(wcscmp(data.cFileName, L".") && wcscmp(data.cFileName, L"..")) {
if(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
string name = (const char*)utf8_t(data.cFileName);
if(name.match(pattern)) list.append(name);
}
}
}
FindClose(handle);
}
for(auto& name : list) name.append("/"); //must append after sorting
return list;
}
inline lstring directory::ufiles(const string& pathname, const string& pattern) {
lstring list;
string path = pathname;
path.transform("/", "\\");
if(!path.endsWith("\\")) path.append("\\");
path.append("*");
HANDLE handle;
WIN32_FIND_DATA data;
handle = FindFirstFile(utf16_t(path), &data);
if(handle != INVALID_HANDLE_VALUE) {
if((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
string name = (const char*)utf8_t(data.cFileName);
if(name.match(pattern)) list.append(name);
}
while(FindNextFile(handle, &data) != false) {
if((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
string name = (const char*)utf8_t(data.cFileName);
if(name.match(pattern)) list.append(name);
}
}
FindClose(handle);
}
return list;
}
#else
inline bool directory_is_folder(DIR* dp, struct dirent* ep) {
if(ep->d_type == DT_DIR) return true;
if(ep->d_type == DT_LNK || ep->d_type == DT_UNKNOWN) {
//symbolic links must be resolved to determine type
struct stat sp = {0};
fstatat(dirfd(dp), ep->d_name, &sp, 0);
return S_ISDIR(sp.st_mode);
}
return false;
}
inline bool directory::create(const string& pathname, unsigned permissions) {
string path;
lstring list = string{pathname}.rtrim("/").split("/");
bool result = true;
for(auto& part : list) {
path.append(part, "/");
if(directory::exists(path)) continue;
result &= (mkdir(path, permissions) == 0);
}
return result;
}
inline bool directory::remove(const string& pathname) {
lstring list = directory::contents(pathname);
for(auto& name : list) {
if(name.endsWith("/")) directory::remove({pathname, name});
else file::remove({pathname, name});
}
return rmdir(pathname) == 0;
}
inline bool directory::exists(const string& pathname) {
DIR* dp = opendir(pathname);
if(!dp) return false;
closedir(dp);
return true;
}
inline lstring directory::ufolders(const string& pathname, const string& pattern) {
lstring list;
DIR* dp;
struct dirent* ep;
dp = opendir(pathname);
if(dp) {
while(ep = readdir(dp)) {
if(!strcmp(ep->d_name, ".")) continue;
if(!strcmp(ep->d_name, "..")) continue;
if(!directory_is_folder(dp, ep)) continue;
string name{ep->d_name};
if(name.match(pattern)) list.append(std::move(name));
}
closedir(dp);
}
for(auto& name : list) name.append("/"); //must append after sorting
return list;
}
inline lstring directory::ufiles(const string& pathname, const string& pattern) {
lstring list;
DIR* dp;
struct dirent* ep;
dp = opendir(pathname);
if(dp) {
while(ep = readdir(dp)) {
if(!strcmp(ep->d_name, ".")) continue;
if(!strcmp(ep->d_name, "..")) continue;
if(directory_is_folder(dp, ep)) continue;
string name{ep->d_name};
if(name.match(pattern)) list.append(std::move(name));
}
closedir(dp);
}
return list;
}
#endif
}
#endif