C++-ified osd_directory (now osd::directory), and added last_modified to osd::directory::entry

This commit is contained in:
Nathan Woods 2016-06-24 06:54:56 -04:00
parent c962154875
commit cd8b414e6b
25 changed files with 232 additions and 217 deletions

View File

@ -295,24 +295,23 @@ void device_image_interface::message(const char *format, ...)
-------------------------------------------------*/ -------------------------------------------------*/
bool device_image_interface::try_change_working_directory(const char *subdir) bool device_image_interface::try_change_working_directory(const char *subdir)
{ {
osd_directory *directory; const osd::directory::entry *entry;
const osd_directory_entry *entry; bool success = false;
bool success = FALSE; bool done = false;
bool done = FALSE;
directory = osd_opendir(m_working_directory.c_str()); auto directory = osd::directory::open(m_working_directory.c_str());
if (directory != nullptr) if (directory != nullptr)
{ {
while(!done && (entry = osd_readdir(directory)) != nullptr) while(!done && (entry = directory->read()) != nullptr)
{ {
if (!core_stricmp(subdir, entry->name)) if (!core_stricmp(subdir, entry->name))
{ {
done = TRUE; done = true;
success = entry->type == ENTTYPE_DIR; success = entry->type == osd::directory::entry::entry_type::DIR;
} }
} }
osd_closedir(directory); delete directory;
} }
/* did we successfully identify the directory? */ /* did we successfully identify the directory? */

View File

@ -91,7 +91,7 @@ file_enumerator::~file_enumerator()
{ {
// close anything open // close anything open
if (m_curdir != nullptr) if (m_curdir != nullptr)
osd_closedir(m_curdir); delete m_curdir;
} }
@ -100,7 +100,7 @@ file_enumerator::~file_enumerator()
// in the search path // in the search path
//------------------------------------------------- //-------------------------------------------------
const osd_directory_entry *file_enumerator::next() const osd::directory::entry *file_enumerator::next()
{ {
// loop over potentially empty directories // loop over potentially empty directories
while (1) while (1)
@ -113,16 +113,16 @@ const osd_directory_entry *file_enumerator::next()
return nullptr; return nullptr;
// open the path // open the path
m_curdir = osd_opendir(m_pathbuffer.c_str()); m_curdir = osd::directory::open(m_pathbuffer.c_str());
} }
// get the next entry from the current directory // get the next entry from the current directory
const osd_directory_entry *result = osd_readdir(m_curdir); const osd::directory::entry *result = m_curdir->read();
if (result != nullptr) if (result != nullptr)
return result; return result;
// we're done; close this directory // we're done; close this directory
osd_closedir(m_curdir); delete m_curdir;
m_curdir = nullptr; m_curdir = nullptr;
} }
} }

View File

@ -63,12 +63,12 @@ public:
~file_enumerator(); ~file_enumerator();
// iterator // iterator
const osd_directory_entry *next(); const osd::directory::entry *next();
private: private:
// internal state // internal state
path_iterator m_iterator; path_iterator m_iterator;
osd_directory * m_curdir; osd::directory *m_curdir;
std::string m_pathbuffer; std::string m_pathbuffer;
//int m_buflen; //int m_buflen;
}; };

View File

@ -568,7 +568,7 @@ void running_machine::set_saveload_filename(const char *filename)
// take into account the statename option // take into account the statename option
const char *stateopt = options().state_name(); const char *stateopt = options().state_name();
std::string statename = get_statename(stateopt); std::string statename = get_statename(stateopt);
m_saveload_pending_file.assign(statename.c_str()).append(PATH_SEPARATOR).append(filename).append(".sta"); m_saveload_pending_file = string_format("%s%s%s.sta", statename.c_str(), PATH_SEPARATOR, filename);
} }
} }

View File

@ -1810,19 +1810,19 @@ media_identifier::media_identifier(emu_options &options)
void media_identifier::identify(const char *filename) void media_identifier::identify(const char *filename)
{ {
// first try to open as a directory // first try to open as a directory
osd_directory *directory = osd_opendir(filename); osd::directory *directory = osd::directory::open(filename);
if (directory != nullptr) if (directory != nullptr)
{ {
// iterate over all files in the directory // iterate over all files in the directory
for (const osd_directory_entry *entry = osd_readdir(directory); entry != nullptr; entry = osd_readdir(directory)) for (const osd::directory::entry *entry = directory->read(); entry != nullptr; entry = directory->read())
if (entry->type == ENTTYPE_FILE) if (entry->type == osd::directory::entry::entry_type::FILE)
{ {
std::string curfile = std::string(filename).append(PATH_SEPARATOR).append(entry->name); std::string curfile = std::string(filename).append(PATH_SEPARATOR).append(entry->name);
identify(curfile.c_str()); identify(curfile.c_str());
} }
// close the directory and be done // close the directory and be done
osd_closedir(directory); delete directory;
} }
// if that failed, and the filename ends with .zip, identify as a ZIP file // if that failed, and the filename ends with .zip, identify as a ZIP file

View File

@ -39,13 +39,13 @@ plugin_options::plugin_options()
void plugin_options::parse_json(std::string path) void plugin_options::parse_json(std::string path)
{ {
// first try to open as a directory // first try to open as a directory
osd_directory *directory = osd_opendir(path.c_str()); osd::directory *directory = osd::directory::open(path.c_str());
if (directory != nullptr) if (directory != nullptr)
{ {
// iterate over all files in the directory // iterate over all files in the directory
for (const osd_directory_entry *entry = osd_readdir(directory); entry != nullptr; entry = osd_readdir(directory)) for (const osd::directory::entry *entry = directory->read(); entry != nullptr; entry = directory->read())
{ {
if (entry->type == ENTTYPE_FILE) if (entry->type == osd::directory::entry::entry_type::FILE)
{ {
std::string name = entry->name; std::string name = entry->name;
if (name == "plugin.json") if (name == "plugin.json")
@ -80,7 +80,7 @@ void plugin_options::parse_json(std::string path)
} }
} }
else if (entry->type == ENTTYPE_DIR) else if (entry->type == osd::directory::entry::entry_type::DIR)
{ {
std::string name = entry->name; std::string name = entry->name;
if (!(name == "." || name == "..")) if (!(name == "." || name == ".."))
@ -91,6 +91,6 @@ void plugin_options::parse_json(std::string path)
} }
// close the directory and be done // close the directory and be done
osd_closedir(directory); delete directory;
} }
} }

View File

@ -36,10 +36,10 @@ menu_custom_ui::menu_custom_ui(mame_ui_manager &mui, render_container *container
// load languages // load languages
file_enumerator path(mui.machine().options().language_path()); file_enumerator path(mui.machine().options().language_path());
auto lang = mui.machine().options().language(); auto lang = mui.machine().options().language();
const osd_directory_entry *dirent; const osd::directory::entry *dirent;
int cnt = 0; int cnt = 0;
while ((dirent = path.next()) != nullptr) while ((dirent = path.next()) != nullptr)
if (dirent->type == ENTTYPE_DIR && strcmp(dirent->name, ".") != 0 && strcmp(dirent->name, "..") != 0) if (dirent->type == osd::directory::entry::entry_type::DIR && strcmp(dirent->name, ".") != 0 && strcmp(dirent->name, "..") != 0)
{ {
auto name = std::string(dirent->name); auto name = std::string(dirent->name);
auto i = strreplace(name, "_", " ("); auto i = strreplace(name, "_", " (");

View File

@ -466,7 +466,7 @@ void menu_add_change_folder::populate()
// open a path // open a path
const char *volume_name = nullptr; const char *volume_name = nullptr;
file_enumerator path(m_current_path.c_str()); file_enumerator path(m_current_path.c_str());
const osd_directory_entry *dirent; const osd::directory::entry *dirent;
int folders_count = 0; int folders_count = 0;
// add the drives // add the drives
@ -476,7 +476,7 @@ void menu_add_change_folder::populate()
// add the directories // add the directories
while ((dirent = path.next()) != nullptr) while ((dirent = path.next()) != nullptr)
{ {
if (dirent->type == ENTTYPE_DIR && strcmp(dirent->name, ".") != 0) if (dirent->type == osd::directory::entry::entry_type::DIR && strcmp(dirent->name, ".") != 0)
item_append(dirent->name, "[DIR]", 0, (void *)(FPTR)++folders_count); item_append(dirent->name, "[DIR]", 0, (void *)(FPTR)++folders_count);
} }

View File

@ -451,7 +451,7 @@ menu_file_selector::file_selector_entry *menu_file_selector::append_entry(
// a menu item for a file selector entry // a menu item for a file selector entry
//------------------------------------------------- //-------------------------------------------------
menu_file_selector::file_selector_entry *menu_file_selector::append_dirent_entry(const osd_directory_entry *dirent) menu_file_selector::file_selector_entry *menu_file_selector::append_dirent_entry(const osd::directory::entry *dirent)
{ {
std::string buffer; std::string buffer;
file_selector_entry_type entry_type; file_selector_entry_type entry_type;
@ -459,11 +459,11 @@ menu_file_selector::file_selector_entry *menu_file_selector::append_dirent_entry
switch(dirent->type) switch(dirent->type)
{ {
case ENTTYPE_FILE: case osd::directory::entry::entry_type::FILE:
entry_type = SELECTOR_ENTRY_TYPE_FILE; entry_type = SELECTOR_ENTRY_TYPE_FILE;
break; break;
case ENTTYPE_DIR: case osd::directory::entry::entry_type::DIR:
entry_type = SELECTOR_ENTRY_TYPE_DIRECTORY; entry_type = SELECTOR_ENTRY_TYPE_DIRECTORY;
break; break;
@ -536,7 +536,7 @@ void menu_file_selector::populate()
{ {
util::zippath_directory *directory = nullptr; util::zippath_directory *directory = nullptr;
osd_file::error err; osd_file::error err;
const osd_directory_entry *dirent; const osd::directory::entry *dirent;
const file_selector_entry *entry; const file_selector_entry *entry;
const file_selector_entry *selected_entry = nullptr; const file_selector_entry *selected_entry = nullptr;
int i; int i;

View File

@ -107,7 +107,7 @@ private:
// methods // methods
int compare_entries(const file_selector_entry *e1, const file_selector_entry *e2); int compare_entries(const file_selector_entry *e1, const file_selector_entry *e2);
file_selector_entry *append_entry(file_selector_entry_type entry_type, const char *entry_basename, const char *entry_fullpath); file_selector_entry *append_entry(file_selector_entry_type entry_type, const char *entry_basename, const char *entry_fullpath);
file_selector_entry *append_dirent_entry(const osd_directory_entry *dirent); file_selector_entry *append_dirent_entry(const osd::directory::entry *dirent);
void append_entry_menu_item(const file_selector_entry *entry); void append_entry_menu_item(const file_selector_entry *entry);
}; };

View File

@ -88,31 +88,31 @@ menu_control_device_image::~menu_control_device_image()
void menu_control_device_image::test_create(bool &can_create, bool &need_confirm) void menu_control_device_image::test_create(bool &can_create, bool &need_confirm)
{ {
std::string path; std::string path;
osd_directory_entry *entry; osd::directory::entry *entry;
osd_dir_entry_type file_type; osd::directory::entry::entry_type file_type;
/* assemble the full path */ /* assemble the full path */
util::zippath_combine(path, current_directory.c_str(), current_file.c_str()); util::zippath_combine(path, current_directory.c_str(), current_file.c_str());
/* does a file or a directory exist at the path */ /* does a file or a directory exist at the path */
entry = osd_stat(path.c_str()); entry = osd_stat(path.c_str());
file_type = (entry != nullptr) ? entry->type : ENTTYPE_NONE; file_type = (entry != nullptr) ? entry->type : osd::directory::entry::entry_type::NONE;
switch(file_type) switch(file_type)
{ {
case ENTTYPE_NONE: case osd::directory::entry::entry_type::NONE:
/* no file/dir here - always create */ /* no file/dir here - always create */
can_create = true; can_create = true;
need_confirm = false; need_confirm = false;
break; break;
case ENTTYPE_FILE: case osd::directory::entry::entry_type::FILE:
/* a file exists here - ask for permission from the user */ /* a file exists here - ask for permission from the user */
can_create = true; can_create = true;
need_confirm = true; need_confirm = true;
break; break;
case ENTTYPE_DIR: case osd::directory::entry::entry_type::DIR:
/* a directory exists here - we can't save over it */ /* a directory exists here - we can't save over it */
ui().popup_time(5, "%s", _("Cannot save over directory")); ui().popup_time(5, "%s", _("Cannot save over directory"));
can_create = false; can_create = false;

View File

@ -39,7 +39,7 @@ inifile_manager::inifile_manager(running_machine &machine, ui_options &moptions)
void inifile_manager::directory_scan() void inifile_manager::directory_scan()
{ {
file_enumerator path(m_options.extraini_path()); file_enumerator path(m_options.extraini_path());
const osd_directory_entry *dir; const osd::directory::entry *dir;
while ((dir = path.next()) != nullptr) while ((dir = path.next()) != nullptr)
if (core_filename_ends_with(dir->name, ".ini") && parseopen(dir->name)) if (core_filename_ends_with(dir->name, ".ini") && parseopen(dir->name))

View File

@ -433,7 +433,7 @@ void menu_crosshair::populate()
/* open a path to the crosshairs */ /* open a path to the crosshairs */
file_enumerator path(machine().options().crosshair_path()); file_enumerator path(machine().options().crosshair_path());
const osd_directory_entry *dir; const osd::directory::entry *dir;
/* reset search flags */ /* reset search flags */
bool using_default = false; bool using_default = false;
bool finished = false; bool finished = false;

View File

@ -69,7 +69,7 @@ menu_select_game::menu_select_game(mame_ui_manager &mui, render_container *conta
// check if there are available icons // check if there are available icons
ui_globals::has_icons = false; ui_globals::has_icons = false;
file_enumerator path(moptions.icons_directory()); file_enumerator path(moptions.icons_directory());
const osd_directory_entry *dir; const osd::directory::entry *dir;
while ((dir = path.next()) != nullptr) while ((dir = path.next()) != nullptr)
{ {
std::string src(dir->name); std::string src(dir->name);
@ -644,7 +644,7 @@ void menu_select_game::build_available_list()
// open a path to the ROMs and find them in the array // open a path to the ROMs and find them in the array
file_enumerator path(machine().options().media_path()); file_enumerator path(machine().options().media_path());
const osd_directory_entry *dir; const osd::directory::entry *dir;
// iterate while we get new objects // iterate while we get new objects
while ((dir = path.next()) != nullptr) while ((dir = path.next()) != nullptr)

View File

@ -608,7 +608,7 @@ void menu_select_software::build_software_list()
} }
std::string searchstr, curpath; std::string searchstr, curpath;
const osd_directory_entry *dir; const osd::directory::entry *dir;
for (auto & elem : m_filter.swlist.name) for (auto & elem : m_filter.swlist.name)
{ {
path_iterator path(machine().options().media_path()); path_iterator path(machine().options().media_path());
@ -621,9 +621,9 @@ void menu_select_software::build_software_list()
while ((dir = fpath.next()) != nullptr) while ((dir = fpath.next()) != nullptr)
{ {
std::string name; std::string name;
if (dir->type == ENTTYPE_FILE) if (dir->type == osd::directory::entry::entry_type::FILE)
name = core_filename_extract_base(dir->name, true); name = core_filename_extract_base(dir->name, true);
else if (dir->type == ENTTYPE_DIR && strcmp(dir->name, ".") != 0) else if (dir->type == osd::directory::entry::entry_type::DIR && strcmp(dir->name, ".") != 0)
name = dir->name; name = dir->name;
else else
continue; continue;

View File

@ -63,7 +63,7 @@ void simple_menu_select_game::build_driver_list()
// open a path to the ROMs and find them in the array // open a path to the ROMs and find them in the array
file_enumerator path(machine().options().media_path()); file_enumerator path(machine().options().media_path());
const osd_directory_entry *dir; const osd::directory::entry *dir;
// iterate while we get new objects // iterate while we get new objects
while ((dir = path.next()) != nullptr) while ((dir = path.next()) != nullptr)

View File

@ -48,11 +48,11 @@ public:
/** @brief true to returned parent. */ /** @brief true to returned parent. */
bool returned_parent; bool returned_parent;
/** @brief The returned entry. */ /** @brief The returned entry. */
osd_directory_entry returned_entry; osd::directory::entry returned_entry;
/* specific to normal directories */ /* specific to normal directories */
/** @brief Pathname of the directory. */ /** @brief Pathname of the directory. */
osd_directory *directory; osd::directory *directory;
/* specific to ZIP directories */ /* specific to ZIP directories */
/** @brief true to called zip first. */ /** @brief true to called zip first. */
@ -70,7 +70,7 @@ public:
FUNCTION PROTOTYPES FUNCTION PROTOTYPES
***************************************************************************/ ***************************************************************************/
static int zippath_find_sub_path(archive_file &zipfile, std::string const &subpath, osd_dir_entry_type &type); static int zippath_find_sub_path(archive_file &zipfile, std::string const &subpath, osd::directory::entry::entry_type &type);
static bool is_zip_file(std::string const &path); static bool is_zip_file(std::string const &path);
static bool is_zip_file_separator(char c); static bool is_zip_file_separator(char c);
static bool is_7z_file(std::string const &path); static bool is_7z_file(std::string const &path);
@ -341,7 +341,7 @@ osd_file::error zippath_fopen(const char *filename, UINT32 openflags, util::core
archive_file::error ziperr; archive_file::error ziperr;
archive_file::ptr zip; archive_file::ptr zip;
int header; int header;
osd_dir_entry_type entry_type; osd::directory::entry::entry_type entry_type;
int len; int len;
/* first, set up the two types of paths */ /* first, set up the two types of paths */
@ -616,7 +616,7 @@ static char next_path_char(std::string const &s, std::string::size_type &pos)
-------------------------------------------------*/ -------------------------------------------------*/
/** /**
* @fn static const zip_file_header *zippath_find_sub_path(archive_file *zipfile, const char *subpath, osd_dir_entry_type *type) * @fn static const zip_file_header *zippath_find_sub_path(archive_file *zipfile, const char *subpath, osd::directory::entry::entry_type *type)
* *
* @brief Zippath find sub path. * @brief Zippath find sub path.
* *
@ -627,7 +627,7 @@ static char next_path_char(std::string const &s, std::string::size_type &pos)
* @return null if it fails, else a zip_file_header*. * @return null if it fails, else a zip_file_header*.
*/ */
static int zippath_find_sub_path(archive_file &zipfile, std::string const &subpath, osd_dir_entry_type &type) static int zippath_find_sub_path(archive_file &zipfile, std::string const &subpath, osd::directory::entry::entry_type &type)
{ {
for (int header = zipfile.first_file(); header >= 0; header = zipfile.next_file()) for (int header = zipfile.first_file(); header >= 0; header = zipfile.next_file())
{ {
@ -644,18 +644,18 @@ static int zippath_find_sub_path(archive_file &zipfile, std::string const &subpa
{ {
if (!c1) if (!c1)
{ {
type = zipfile.current_is_directory() ? ENTTYPE_DIR : ENTTYPE_FILE; type = zipfile.current_is_directory() ? osd::directory::entry::entry_type::DIR : osd::directory::entry::entry_type::FILE;
return header; return header;
} }
else if ((c1 == '/') || (i <= 1U)) else if ((c1 == '/') || (i <= 1U))
{ {
type = ENTTYPE_DIR; type = osd::directory::entry::entry_type::DIR;
return header; return header;
} }
} }
} }
type = ENTTYPE_NONE; type = osd::directory::entry::entry_type::NONE;
return -1; return -1;
} }
@ -667,7 +667,7 @@ static int zippath_find_sub_path(archive_file &zipfile, std::string const &subpa
-------------------------------------------------*/ -------------------------------------------------*/
/** /**
* @fn static osd_file::error zippath_resolve(const char *path, osd_dir_entry_type &entry_type, archive_file *&zipfile, std::string &newpath) * @fn static osd_file::error zippath_resolve(const char *path, osd::directory::entry::entry_type &entry_type, archive_file *&zipfile, std::string &newpath)
* *
* @brief Zippath resolve. * @brief Zippath resolve.
* *
@ -679,17 +679,17 @@ static int zippath_find_sub_path(archive_file &zipfile, std::string const &subpa
* @return A osd_file::error. * @return A osd_file::error.
*/ */
static osd_file::error zippath_resolve(const char *path, osd_dir_entry_type &entry_type, archive_file::ptr &zipfile, std::string &newpath) static osd_file::error zippath_resolve(const char *path, osd::directory::entry::entry_type &entry_type, archive_file::ptr &zipfile, std::string &newpath)
{ {
newpath.clear(); newpath.clear();
// be conservative // be conservative
entry_type = ENTTYPE_NONE; entry_type = osd::directory::entry::entry_type::NONE;
zipfile.reset(); zipfile.reset();
std::string apath(path); std::string apath(path);
std::string apath_trimmed; std::string apath_trimmed;
osd_dir_entry_type current_entry_type; osd::directory::entry::entry_type current_entry_type;
bool went_up = false; bool went_up = false;
do do
{ {
@ -701,7 +701,7 @@ static osd_file::error zippath_resolve(const char *path, osd_dir_entry_type &ent
apath_trimmed = apath; apath_trimmed = apath;
// stat the path // stat the path
std::unique_ptr<osd_directory_entry, void (*)(void *)> current_entry(osd_stat(apath_trimmed), &osd_free); std::unique_ptr<osd::directory::entry, void (*)(void *)> current_entry(osd_stat(apath_trimmed), &osd_free);
// did we find anything? // did we find anything?
if (current_entry) if (current_entry)
@ -712,20 +712,20 @@ static osd_file::error zippath_resolve(const char *path, osd_dir_entry_type &ent
else else
{ {
// if we have not found the file or directory, go up // if we have not found the file or directory, go up
current_entry_type = ENTTYPE_NONE; current_entry_type = osd::directory::entry::entry_type::NONE;
went_up = true; went_up = true;
std::string parent; std::string parent;
apath = zippath_parent(parent, apath.c_str()); apath = zippath_parent(parent, apath.c_str());
} }
} }
while ((current_entry_type == ENTTYPE_NONE) && !is_root(apath.c_str())); while ((current_entry_type == osd::directory::entry::entry_type::NONE) && !is_root(apath.c_str()));
// if we did not find anything, then error out // if we did not find anything, then error out
if (current_entry_type == ENTTYPE_NONE) if (current_entry_type == osd::directory::entry::entry_type::NONE)
return osd_file::error::NOT_FOUND; return osd_file::error::NOT_FOUND;
// is this file a ZIP file? // is this file a ZIP file?
if ((current_entry_type == ENTTYPE_FILE) && if ((current_entry_type == osd::directory::entry::entry_type::FILE) &&
((is_zip_file(apath_trimmed) && (archive_file::open_zip(apath_trimmed, zipfile) == archive_file::error::NONE)) || ((is_zip_file(apath_trimmed) && (archive_file::open_zip(apath_trimmed, zipfile) == archive_file::error::NONE)) ||
(is_7z_file(apath_trimmed) && (archive_file::open_7z(apath_trimmed, zipfile) == archive_file::error::NONE)))) (is_7z_file(apath_trimmed) && (archive_file::open_7z(apath_trimmed, zipfile) == archive_file::error::NONE))))
{ {
@ -736,7 +736,7 @@ static osd_file::error zippath_resolve(const char *path, osd_dir_entry_type &ent
// this was a true ZIP path - attempt to identify the type of path // this was a true ZIP path - attempt to identify the type of path
zippath_find_sub_path(*zipfile, newpath, current_entry_type); zippath_find_sub_path(*zipfile, newpath, current_entry_type);
if (current_entry_type == ENTTYPE_NONE) if (current_entry_type == osd::directory::entry::entry_type::NONE)
return osd_file::error::NOT_FOUND; return osd_file::error::NOT_FOUND;
} }
else else
@ -785,13 +785,13 @@ osd_file::error zippath_opendir(const char *path, zippath_directory **directory)
goto done; goto done;
} }
/* resolve the path */ /* resolve the path */
osd_dir_entry_type entry_type; osd::directory::entry::entry_type entry_type;
err = zippath_resolve(path, entry_type, result->zipfile, result->zipprefix); err = zippath_resolve(path, entry_type, result->zipfile, result->zipprefix);
if (err != osd_file::error::NONE) if (err != osd_file::error::NONE)
goto done; goto done;
/* we have to be a directory */ /* we have to be a directory */
if (entry_type != ENTTYPE_DIR) if (entry_type != osd::directory::entry::entry_type::DIR)
{ {
err = osd_file::error::NOT_FOUND; err = osd_file::error::NOT_FOUND;
goto done; goto done;
@ -801,7 +801,7 @@ osd_file::error zippath_opendir(const char *path, zippath_directory **directory)
if (!result->zipfile) if (!result->zipfile)
{ {
/* a conventional directory */ /* a conventional directory */
result->directory = osd_opendir(path); result->directory = osd::directory::open(path);
if (!result->directory) if (!result->directory)
{ {
err = osd_file::error::FAILURE; err = osd_file::error::FAILURE;
@ -840,7 +840,7 @@ done:
void zippath_closedir(zippath_directory *directory) void zippath_closedir(zippath_directory *directory)
{ {
if (directory->directory != nullptr) if (directory->directory != nullptr)
osd_closedir(directory->directory); delete directory->directory;
if (directory->zipfile != nullptr) if (directory->zipfile != nullptr)
directory->zipfile.reset(); directory->zipfile.reset();
@ -901,18 +901,18 @@ static const char *get_relative_path(zippath_directory const &directory)
-------------------------------------------------*/ -------------------------------------------------*/
/** /**
* @fn const osd_directory_entry *zippath_readdir(zippath_directory *directory) * @fn const osd::directory::entry *zippath_readdir(zippath_directory *directory)
* *
* @brief Zippath readdir. * @brief Zippath readdir.
* *
* @param [in,out] directory If non-null, pathname of the directory. * @param [in,out] directory If non-null, pathname of the directory.
* *
* @return null if it fails, else an osd_directory_entry*. * @return null if it fails, else an osd::directory::entry*.
*/ */
const osd_directory_entry *zippath_readdir(zippath_directory *directory) const osd::directory::entry *zippath_readdir(zippath_directory *directory)
{ {
const osd_directory_entry *result = nullptr; const osd::directory::entry *result = nullptr;
if (!directory->returned_parent) if (!directory->returned_parent)
{ {
@ -920,7 +920,7 @@ const osd_directory_entry *zippath_readdir(zippath_directory *directory)
directory->returned_parent = true; directory->returned_parent = true;
memset(&directory->returned_entry, 0, sizeof(directory->returned_entry)); memset(&directory->returned_entry, 0, sizeof(directory->returned_entry));
directory->returned_entry.name = ".."; directory->returned_entry.name = "..";
directory->returned_entry.type = ENTTYPE_DIR; directory->returned_entry.type = osd::directory::entry::entry_type::DIR;
result = &directory->returned_entry; result = &directory->returned_entry;
} }
else if (directory->directory) else if (directory->directory)
@ -928,7 +928,7 @@ const osd_directory_entry *zippath_readdir(zippath_directory *directory)
/* a normal directory read */ /* a normal directory read */
do do
{ {
result = osd_readdir(directory->directory); result = directory->directory->read();
} }
while (result && (!strcmp(result->name, ".") || !strcmp(result->name, ".."))); while (result && (!strcmp(result->name, ".") || !strcmp(result->name, "..")));
@ -937,7 +937,7 @@ const osd_directory_entry *zippath_readdir(zippath_directory *directory)
{ {
/* copy; but change the entry type */ /* copy; but change the entry type */
directory->returned_entry = *result; directory->returned_entry = *result;
directory->returned_entry.type = ENTTYPE_DIR; directory->returned_entry.type = osd::directory::entry::entry_type::DIR;
result = &directory->returned_entry; result = &directory->returned_entry;
} }
} }
@ -983,7 +983,7 @@ const osd_directory_entry *zippath_readdir(zippath_directory *directory)
/* ...and return it */ /* ...and return it */
memset(&directory->returned_entry, 0, sizeof(directory->returned_entry)); memset(&directory->returned_entry, 0, sizeof(directory->returned_entry));
directory->returned_entry.name = directory->returned_dirlist.front().c_str(); directory->returned_entry.name = directory->returned_dirlist.front().c_str();
directory->returned_entry.type = ENTTYPE_DIR; directory->returned_entry.type = osd::directory::entry::entry_type::DIR;
result = &directory->returned_entry; result = &directory->returned_entry;
} }
} }
@ -992,7 +992,7 @@ const osd_directory_entry *zippath_readdir(zippath_directory *directory)
/* a real file */ /* a real file */
memset(&directory->returned_entry, 0, sizeof(directory->returned_entry)); memset(&directory->returned_entry, 0, sizeof(directory->returned_entry));
directory->returned_entry.name = relpath; directory->returned_entry.name = relpath;
directory->returned_entry.type = ENTTYPE_FILE; directory->returned_entry.type = osd::directory::entry::entry_type::FILE;
directory->returned_entry.size = directory->zipfile->current_uncompressed_length(); directory->returned_entry.size = directory->zipfile->current_uncompressed_length();
result = &directory->returned_entry; result = &directory->returned_entry;
} }

View File

@ -58,7 +58,7 @@ osd_file::error zippath_opendir(const char *path, zippath_directory **directory)
void zippath_closedir(zippath_directory *directory); void zippath_closedir(zippath_directory *directory);
/* reads a directory entry */ /* reads a directory entry */
const osd_directory_entry *zippath_readdir(zippath_directory *directory); const osd::directory::entry *zippath_readdir(zippath_directory *directory);
/* returns TRUE if this path is a ZIP path or FALSE if not */ /* returns TRUE if this path is a ZIP path or FALSE if not */
int zippath_is_zip(zippath_directory *directory); int zippath_is_zip(zippath_directory *directory);

View File

@ -354,7 +354,7 @@ int osd_get_physical_drive_geometry(const char *filename, UINT32 *cylinders, UIN
// osd_stat // osd_stat
//============================================================ //============================================================
osd_directory_entry *osd_stat(const std::string &path) osd::directory::entry *osd_stat(const std::string &path)
{ {
// convert the path to TCHARs // convert the path to TCHARs
std::unique_ptr<TCHAR, void (*)(void *)> const t_path(tstring_from_utf8(path.c_str()), &osd_free); std::unique_ptr<TCHAR, void (*)(void *)> const t_path(tstring_from_utf8(path.c_str()), &osd_free);
@ -379,15 +379,16 @@ osd_directory_entry *osd_stat(const std::string &path)
FindClose(find); FindClose(find);
} }
// create an osd_directory_entry; be sure to make sure that the caller can // create an osd::directory::entry; be sure to make sure that the caller can
// free all resources by just freeing the resulting osd_directory_entry // free all resources by just freeing the resulting osd::directory::entry
osd_directory_entry *const result = (osd_directory_entry *)osd_malloc_array(sizeof(*result) + path.length() + 1); osd::directory::entry *const result = (osd::directory::entry *)osd_malloc_array(sizeof(*result) + path.length() + 1);
if (!result) if (!result)
return nullptr; return nullptr;
strcpy(((char *) result) + sizeof(*result), path.c_str()); strcpy(((char *) result) + sizeof(*result), path.c_str());
result->name = ((char *) result) + sizeof(*result); result->name = ((char *) result) + sizeof(*result);
result->type = win_attributes_to_entry_type(find_data.dwFileAttributes); result->type = win_attributes_to_entry_type(find_data.dwFileAttributes);
result->size = find_data.nFileSizeLow | ((UINT64) find_data.nFileSizeHigh << 32); result->size = find_data.nFileSizeLow | ((UINT64) find_data.nFileSizeHigh << 32);
result->last_modified = win_time_point_from_filetime(&find_data.ftLastWriteTime);
return result; return result;
} }

View File

@ -115,12 +115,12 @@ bgfx_chain_entry* chain_entry_reader::read_from_value(const Value& value, std::s
directory_path += "/" + file_directory; directory_path += "/" + file_directory;
} }
osd_directory *directory = osd_opendir(directory_path.c_str()); osd::directory *directory = osd::directory::open(directory_path.c_str());
if (directory != nullptr) if (directory != nullptr)
{ {
for (const osd_directory_entry *entry = osd_readdir(directory); entry != nullptr; entry = osd_readdir(directory)) for (const osd::directory::entry *entry = directory->read(); entry != nullptr; entry = directory->read())
{ {
if (entry->type == ENTTYPE_FILE) if (entry->type == osd::directory::entry::entry_type::FILE)
{ {
std::string file(entry->name); std::string file(entry->name);
std::string extension(".png"); std::string extension(".png");
@ -160,7 +160,7 @@ bgfx_chain_entry* chain_entry_reader::read_from_value(const Value& value, std::s
} }
} }
osd_closedir(directory); delete directory;
} }
} }
} }

View File

@ -91,12 +91,12 @@ void chain_manager::destroy_unloaded_chains()
void chain_manager::find_available_chains(std::string root, std::string path) void chain_manager::find_available_chains(std::string root, std::string path)
{ {
osd_directory *directory = osd_opendir((root + path).c_str()); osd::directory *directory = osd::directory::open((root + path).c_str());
if (directory != nullptr) if (directory != nullptr)
{ {
for (const osd_directory_entry *entry = osd_readdir(directory); entry != nullptr; entry = osd_readdir(directory)) for (const osd::directory::entry *entry = directory->read(); entry != nullptr; entry = directory->read())
{ {
if (entry->type == ENTTYPE_FILE) if (entry->type == osd::directory::entry::entry_type::FILE)
{ {
std::string name(entry->name); std::string name(entry->name);
std::string extension(".json"); std::string extension(".json");
@ -114,7 +114,7 @@ void chain_manager::find_available_chains(std::string root, std::string path)
} }
} }
} }
else if (entry->type == ENTTYPE_DIR) else if (entry->type == osd::directory::entry::entry_type::DIR)
{ {
std::string name = entry->name; std::string name = entry->name;
if (!(name == "." || name == "..")) if (!(name == "." || name == ".."))
@ -129,7 +129,7 @@ void chain_manager::find_available_chains(std::string root, std::string path)
} }
} }
osd_closedir(directory); delete directory;
} }
} }

View File

@ -25,6 +25,7 @@
#include <cstdint> #include <cstdint>
#include <memory> #include <memory>
#include <string> #include <string>
#include <chrono>
/*************************************************************************** /***************************************************************************
@ -297,76 +298,64 @@ int osd_uchar_from_osdchar(UINT32 /* unicode_char */ *uchar, const char *osdchar
DIRECTORY INTERFACES DIRECTORY INTERFACES
***************************************************************************/ ***************************************************************************/
/* types of directory entries that can be returned */ namespace osd
enum osd_dir_entry_type
{ {
ENTTYPE_NONE, // directory is an opaque type which represents an open directory
ENTTYPE_FILE, class directory
ENTTYPE_DIR, {
ENTTYPE_OTHER public:
// osd::directory::entry contains basic information about a file when iterating through
// a directory
class entry
{
public:
enum class entry_type
{
NONE,
FILE,
DIR,
OTHER
};
const char * name; // name of the entry
entry_type type; // type of the entry
UINT64 size; // size of the entry
std::chrono::system_clock::time_point last_modified; // last modified time
};
// -----------------------------------------------------------------------------
// osd::directory::open: open a directory for iteration
//
// Parameters:
//
// dirname - path to the directory in question
//
// Return value:
//
// upon success, this function should return an directory pointer
// which contains opaque data necessary to traverse the directory; on
// failure, this function should return nullptr
// -----------------------------------------------------------------------------
static directory *open(const char *dirname);
// -----------------------------------------------------------------------------
// osd::directory::~directory: close an open directory
// -----------------------------------------------------------------------------
virtual ~directory() { }
// -----------------------------------------------------------------------------
// osd::directory::read: return information about the next entry in the directory
//
// Return value:
//
// a constant pointer to an entry representing the current item
// in the directory, or nullptr, indicating that no more entries are
// present
// -----------------------------------------------------------------------------
virtual const entry *read() = 0;
};
}; };
/* osd_directory is an opaque type which represents an open directory */
struct osd_directory;
/* osd_directory_entry contains basic information about a file when iterating through */
/* a directory */
struct osd_directory_entry
{
const char * name; /* name of the entry */
osd_dir_entry_type type; /* type of the entry */
UINT64 size; /* size of the entry */
};
/*-----------------------------------------------------------------------------
osd_opendir: open a directory for iteration
Parameters:
dirname - path to the directory in question
Return value:
upon success, this function should return an osd_directory pointer
which contains opaque data necessary to traverse the directory; on
failure, this function should return nullptr
-----------------------------------------------------------------------------*/
osd_directory *osd_opendir(const char *dirname);
/*-----------------------------------------------------------------------------
osd_readdir: return information about the next entry in the directory
Parameters:
dir - pointer to an osd_directory that was returned from a prior
call to osd_opendir
Return value:
a constant pointer to an osd_directory_entry representing the current item
in the directory, or nullptr, indicating that no more entries are
present
-----------------------------------------------------------------------------*/
const osd_directory_entry *osd_readdir(osd_directory *dir);
/*-----------------------------------------------------------------------------
osd_closedir: close an open directory for iteration
Parameters:
dir - pointer to an osd_directory that was returned from a prior
call to osd_opendir
Return value:
frees any allocated memory and resources associated with the open
directory
-----------------------------------------------------------------------------*/
void osd_closedir(osd_directory *dir);
/*----------------------------------------------------------------------------- /*-----------------------------------------------------------------------------
osd_is_absolute_path: returns whether the specified path is absolute osd_is_absolute_path: returns whether the specified path is absolute
@ -798,12 +787,12 @@ char *osd_get_clipboard_text(void);
Return value: Return value:
an allocated pointer to an osd_directory_entry representing an allocated pointer to an osd::directory::entry representing
info on the path; even if the file does not exist. info on the path; even if the file does not exist.
free with osd_free() free with osd_free()
-----------------------------------------------------------------------------*/ -----------------------------------------------------------------------------*/
osd_directory_entry *osd_stat(std::string const &path); osd::directory::entry *osd_stat(std::string const &path);
/*************************************************************************** /***************************************************************************
PATH INTERFACES PATH INTERFACES

View File

@ -28,36 +28,39 @@
// TYPE DEFINITIONS // TYPE DEFINITIONS
//============================================================ //============================================================
struct osd_directory namespace
{ {
HANDLE find; // handle to the finder class win_directory : public osd::directory
int is_first; // TRUE if this is the first entry {
osd_directory_entry entry; // current entry's data public:
WIN32_FIND_DATA data; // current raw data win_directory();
~win_directory();
virtual const entry *read() override;
HANDLE m_find; // handle to the finder
bool m_is_first; // true if this is the first entry
entry m_entry; // current entry's data
WIN32_FIND_DATA m_data; // current raw data
};
}; };
//============================================================ //============================================================
// osd_opendir // osd::directory::open
//============================================================ //============================================================
osd_directory *osd_opendir(const char *dirname) osd::directory *osd::directory::open(const char *dirname)
{ {
osd_directory *dir = nullptr; win_directory *dir = nullptr;
TCHAR *t_dirname = nullptr; TCHAR *t_dirname = nullptr;
TCHAR *dirfilter = nullptr; TCHAR *dirfilter = nullptr;
size_t dirfilter_size; size_t dirfilter_size;
// allocate memory to hold the osd_tool_directory structure // allocate memory to hold the osd_tool_directory structure
dir = (osd_directory *)malloc(sizeof(*dir)); dir = new win_directory();
if (dir == nullptr) if (dir == nullptr)
goto error; goto error;
memset(dir, 0, sizeof(*dir));
// initialize the structure
dir->find = INVALID_HANDLE_VALUE;
dir->is_first = TRUE;
// convert the path to TCHARs // convert the path to TCHARs
t_dirname = tstring_from_utf8(dirname); t_dirname = tstring_from_utf8(dirname);
@ -72,7 +75,7 @@ osd_directory *osd_opendir(const char *dirname)
_sntprintf(dirfilter, dirfilter_size, TEXT("%s\\*.*"), t_dirname); _sntprintf(dirfilter, dirfilter_size, TEXT("%s\\*.*"), t_dirname);
// attempt to find the first file // attempt to find the first file
dir->find = FindFirstFileEx(dirfilter, FindExInfoStandard, &dir->data, FindExSearchNameMatch, nullptr, 0); dir->m_find = FindFirstFileEx(dirfilter, FindExInfoStandard, &dir->m_data, FindExSearchNameMatch, nullptr, 0);
error: error:
// cleanup // cleanup
@ -80,9 +83,9 @@ error:
osd_free(t_dirname); osd_free(t_dirname);
if (dirfilter != nullptr) if (dirfilter != nullptr)
free(dirfilter); free(dirfilter);
if (dir != nullptr && dir->find == INVALID_HANDLE_VALUE) if (dir != nullptr && dir->m_find == INVALID_HANDLE_VALUE)
{ {
free(dir); delete dir;
dir = nullptr; dir = nullptr;
} }
return dir; return dir;
@ -90,47 +93,56 @@ error:
//============================================================ //============================================================
// osd_readdir // win_directory::win_directory
//============================================================ //============================================================
const osd_directory_entry *osd_readdir(osd_directory *dir) win_directory::win_directory()
: m_find(INVALID_HANDLE_VALUE), m_is_first(true)
{
memset(&m_entry, 0, sizeof(m_entry));
memset(&m_data, 0, sizeof(m_data));
}
//============================================================
// win_directory::~win_directory
//============================================================
win_directory::~win_directory()
{
// free any data associated
if (m_entry.name != nullptr)
osd_free((void *)m_entry.name);
if (m_find != INVALID_HANDLE_VALUE)
FindClose(m_find);
}
//============================================================
// win_directory::read
//============================================================
const osd::directory::entry *win_directory::read()
{ {
// if we've previously allocated a name, free it now // if we've previously allocated a name, free it now
if (dir->entry.name != nullptr) if (m_entry.name != nullptr)
{ {
osd_free((void *)dir->entry.name); osd_free((void *)m_entry.name);
dir->entry.name = nullptr; m_entry.name = nullptr;
} }
// if this isn't the first file, do a find next // if this isn't the first file, do a find next
if (!dir->is_first) if (!m_is_first)
{ {
if (!FindNextFile(dir->find, &dir->data)) if (!FindNextFile(m_find, &m_data))
return nullptr; return nullptr;
} }
m_is_first = false;
// otherwise, just use the data we already had
else
dir->is_first = FALSE;
// extract the data // extract the data
dir->entry.name = utf8_from_tstring(dir->data.cFileName); m_entry.name = utf8_from_tstring(m_data.cFileName);
dir->entry.type = win_attributes_to_entry_type(dir->data.dwFileAttributes); m_entry.type = win_attributes_to_entry_type(m_data.dwFileAttributes);
dir->entry.size = dir->data.nFileSizeLow | ((UINT64) dir->data.nFileSizeHigh << 32); m_entry.size = m_data.nFileSizeLow | ((UINT64) m_data.nFileSizeHigh << 32);
return (dir->entry.name != nullptr) ? &dir->entry : nullptr; m_entry.last_modified = win_time_point_from_filetime(&m_data.ftLastWriteTime);
} return (m_entry.name != nullptr) ? &m_entry : nullptr;
//============================================================
// osd_closedir
//============================================================
void osd_closedir(osd_directory *dir)
{
// free any data associated
if (dir->entry.name != nullptr)
osd_free((void *)dir->entry.name);
if (dir->find != INVALID_HANDLE_VALUE)
FindClose(dir->find);
free(dir);
} }

View File

@ -23,14 +23,26 @@
// win_attributes_to_entry_type // win_attributes_to_entry_type
//============================================================ //============================================================
osd_dir_entry_type win_attributes_to_entry_type(DWORD attributes) osd::directory::entry::entry_type win_attributes_to_entry_type(DWORD attributes)
{ {
if (attributes == 0xFFFFFFFF) if (attributes == 0xFFFFFFFF)
return ENTTYPE_NONE; return osd::directory::entry::entry_type::NONE;
else if (attributes & FILE_ATTRIBUTE_DIRECTORY) else if (attributes & FILE_ATTRIBUTE_DIRECTORY)
return ENTTYPE_DIR; return osd::directory::entry::entry_type::DIR;
else else
return ENTTYPE_FILE; return osd::directory::entry::entry_type::FILE;
}
//============================================================
// win_time_point_from_filetime
//============================================================
std::chrono::system_clock::time_point win_time_point_from_filetime(LPFILETIME file_time)
{
return std::chrono::system_clock::time_point(std::chrono::system_clock::duration(
(static_cast<__int64>(file_time->dwHighDateTime) << 32) | file_time->dwLowDateTime));
} }

View File

@ -12,9 +12,11 @@
#include "osdcore.h" #include "osdcore.h"
#include <string> #include <string>
#include <vector> #include <vector>
#include <chrono>
// Shared code // Shared code
osd_dir_entry_type win_attributes_to_entry_type(DWORD attributes); osd::directory::entry::entry_type win_attributes_to_entry_type(DWORD attributes);
std::chrono::system_clock::time_point win_time_point_from_filetime(LPFILETIME file_time);
BOOL win_is_gui_application(void); BOOL win_is_gui_application(void);
HMODULE WINAPI GetModuleHandleUni(); HMODULE WINAPI GetModuleHandleUni();