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)
{
osd_directory *directory;
const osd_directory_entry *entry;
bool success = FALSE;
bool done = FALSE;
const osd::directory::entry *entry;
bool success = 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)
{
while(!done && (entry = osd_readdir(directory)) != nullptr)
while(!done && (entry = directory->read()) != nullptr)
{
if (!core_stricmp(subdir, entry->name))
{
done = TRUE;
success = entry->type == ENTTYPE_DIR;
done = true;
success = entry->type == osd::directory::entry::entry_type::DIR;
}
}
osd_closedir(directory);
delete directory;
}
/* did we successfully identify the directory? */

View File

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

View File

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

View File

@ -568,7 +568,7 @@ void running_machine::set_saveload_filename(const char *filename)
// take into account the statename option
const char *stateopt = options().state_name();
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)
{
// first try to open as a directory
osd_directory *directory = osd_opendir(filename);
osd::directory *directory = osd::directory::open(filename);
if (directory != nullptr)
{
// iterate over all files in the directory
for (const osd_directory_entry *entry = osd_readdir(directory); entry != nullptr; entry = osd_readdir(directory))
if (entry->type == ENTTYPE_FILE)
for (const osd::directory::entry *entry = directory->read(); entry != nullptr; entry = directory->read())
if (entry->type == osd::directory::entry::entry_type::FILE)
{
std::string curfile = std::string(filename).append(PATH_SEPARATOR).append(entry->name);
identify(curfile.c_str());
}
// 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

View File

@ -39,13 +39,13 @@ plugin_options::plugin_options()
void plugin_options::parse_json(std::string path)
{
// 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)
{
// 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;
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;
if (!(name == "." || name == ".."))
@ -91,6 +91,6 @@ void plugin_options::parse_json(std::string path)
}
// 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
file_enumerator path(mui.machine().options().language_path());
auto lang = mui.machine().options().language();
const osd_directory_entry *dirent;
const osd::directory::entry *dirent;
int cnt = 0;
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 i = strreplace(name, "_", " (");

View File

@ -466,7 +466,7 @@ void menu_add_change_folder::populate()
// open a path
const char *volume_name = nullptr;
file_enumerator path(m_current_path.c_str());
const osd_directory_entry *dirent;
const osd::directory::entry *dirent;
int folders_count = 0;
// add the drives
@ -476,7 +476,7 @@ void menu_add_change_folder::populate()
// add the directories
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);
}

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
//-------------------------------------------------
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;
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)
{
case ENTTYPE_FILE:
case osd::directory::entry::entry_type::FILE:
entry_type = SELECTOR_ENTRY_TYPE_FILE;
break;
case ENTTYPE_DIR:
case osd::directory::entry::entry_type::DIR:
entry_type = SELECTOR_ENTRY_TYPE_DIRECTORY;
break;
@ -536,7 +536,7 @@ void menu_file_selector::populate()
{
util::zippath_directory *directory = nullptr;
osd_file::error err;
const osd_directory_entry *dirent;
const osd::directory::entry *dirent;
const file_selector_entry *entry;
const file_selector_entry *selected_entry = nullptr;
int i;

View File

@ -107,7 +107,7 @@ private:
// methods
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_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);
};

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)
{
std::string path;
osd_directory_entry *entry;
osd_dir_entry_type file_type;
osd::directory::entry *entry;
osd::directory::entry::entry_type file_type;
/* assemble the full path */
util::zippath_combine(path, current_directory.c_str(), current_file.c_str());
/* does a file or a directory exist at the path */
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)
{
case ENTTYPE_NONE:
case osd::directory::entry::entry_type::NONE:
/* no file/dir here - always create */
can_create = true;
need_confirm = false;
break;
case ENTTYPE_FILE:
case osd::directory::entry::entry_type::FILE:
/* a file exists here - ask for permission from the user */
can_create = true;
need_confirm = true;
break;
case ENTTYPE_DIR:
case osd::directory::entry::entry_type::DIR:
/* a directory exists here - we can't save over it */
ui().popup_time(5, "%s", _("Cannot save over directory"));
can_create = false;

View File

@ -39,7 +39,7 @@ inifile_manager::inifile_manager(running_machine &machine, ui_options &moptions)
void inifile_manager::directory_scan()
{
file_enumerator path(m_options.extraini_path());
const osd_directory_entry *dir;
const osd::directory::entry *dir;
while ((dir = path.next()) != nullptr)
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 */
file_enumerator path(machine().options().crosshair_path());
const osd_directory_entry *dir;
const osd::directory::entry *dir;
/* reset search flags */
bool using_default = 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
ui_globals::has_icons = false;
file_enumerator path(moptions.icons_directory());
const osd_directory_entry *dir;
const osd::directory::entry *dir;
while ((dir = path.next()) != nullptr)
{
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
file_enumerator path(machine().options().media_path());
const osd_directory_entry *dir;
const osd::directory::entry *dir;
// iterate while we get new objects
while ((dir = path.next()) != nullptr)

View File

@ -608,7 +608,7 @@ void menu_select_software::build_software_list()
}
std::string searchstr, curpath;
const osd_directory_entry *dir;
const osd::directory::entry *dir;
for (auto & elem : m_filter.swlist.name)
{
path_iterator path(machine().options().media_path());
@ -621,9 +621,9 @@ void menu_select_software::build_software_list()
while ((dir = fpath.next()) != nullptr)
{
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);
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;
else
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
file_enumerator path(machine().options().media_path());
const osd_directory_entry *dir;
const osd::directory::entry *dir;
// iterate while we get new objects
while ((dir = path.next()) != nullptr)

View File

@ -48,11 +48,11 @@ public:
/** @brief true to returned parent. */
bool returned_parent;
/** @brief The returned entry. */
osd_directory_entry returned_entry;
osd::directory::entry returned_entry;
/* specific to normal directories */
/** @brief Pathname of the directory. */
osd_directory *directory;
osd::directory *directory;
/* specific to ZIP directories */
/** @brief true to called zip first. */
@ -70,7 +70,7 @@ public:
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_separator(char c);
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::ptr zip;
int header;
osd_dir_entry_type entry_type;
osd::directory::entry::entry_type entry_type;
int len;
/* 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.
*
@ -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*.
*/
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())
{
@ -644,18 +644,18 @@ static int zippath_find_sub_path(archive_file &zipfile, std::string const &subpa
{
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;
}
else if ((c1 == '/') || (i <= 1U))
{
type = ENTTYPE_DIR;
type = osd::directory::entry::entry_type::DIR;
return header;
}
}
}
type = ENTTYPE_NONE;
type = osd::directory::entry::entry_type::NONE;
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.
*
@ -679,17 +679,17 @@ static int zippath_find_sub_path(archive_file &zipfile, std::string const &subpa
* @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();
// be conservative
entry_type = ENTTYPE_NONE;
entry_type = osd::directory::entry::entry_type::NONE;
zipfile.reset();
std::string apath(path);
std::string apath_trimmed;
osd_dir_entry_type current_entry_type;
osd::directory::entry::entry_type current_entry_type;
bool went_up = false;
do
{
@ -701,7 +701,7 @@ static osd_file::error zippath_resolve(const char *path, osd_dir_entry_type &ent
apath_trimmed = apath;
// 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?
if (current_entry)
@ -712,20 +712,20 @@ static osd_file::error zippath_resolve(const char *path, osd_dir_entry_type &ent
else
{
// 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;
std::string parent;
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 (current_entry_type == ENTTYPE_NONE)
if (current_entry_type == osd::directory::entry::entry_type::NONE)
return osd_file::error::NOT_FOUND;
// 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_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
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;
}
else
@ -785,13 +785,13 @@ osd_file::error zippath_opendir(const char *path, zippath_directory **directory)
goto done;
}
/* 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);
if (err != osd_file::error::NONE)
goto done;
/* 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;
goto done;
@ -801,7 +801,7 @@ osd_file::error zippath_opendir(const char *path, zippath_directory **directory)
if (!result->zipfile)
{
/* a conventional directory */
result->directory = osd_opendir(path);
result->directory = osd::directory::open(path);
if (!result->directory)
{
err = osd_file::error::FAILURE;
@ -840,7 +840,7 @@ done:
void zippath_closedir(zippath_directory *directory)
{
if (directory->directory != nullptr)
osd_closedir(directory->directory);
delete directory->directory;
if (directory->zipfile != nullptr)
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.
*
* @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)
{
@ -920,7 +920,7 @@ const osd_directory_entry *zippath_readdir(zippath_directory *directory)
directory->returned_parent = true;
memset(&directory->returned_entry, 0, sizeof(directory->returned_entry));
directory->returned_entry.name = "..";
directory->returned_entry.type = ENTTYPE_DIR;
directory->returned_entry.type = osd::directory::entry::entry_type::DIR;
result = &directory->returned_entry;
}
else if (directory->directory)
@ -928,7 +928,7 @@ const osd_directory_entry *zippath_readdir(zippath_directory *directory)
/* a normal directory read */
do
{
result = osd_readdir(directory->directory);
result = directory->directory->read();
}
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 */
directory->returned_entry = *result;
directory->returned_entry.type = ENTTYPE_DIR;
directory->returned_entry.type = osd::directory::entry::entry_type::DIR;
result = &directory->returned_entry;
}
}
@ -983,7 +983,7 @@ const osd_directory_entry *zippath_readdir(zippath_directory *directory)
/* ...and return it */
memset(&directory->returned_entry, 0, sizeof(directory->returned_entry));
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;
}
}
@ -992,7 +992,7 @@ const osd_directory_entry *zippath_readdir(zippath_directory *directory)
/* a real file */
memset(&directory->returned_entry, 0, sizeof(directory->returned_entry));
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();
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);
/* 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 */
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_directory_entry *osd_stat(const std::string &path)
osd::directory::entry *osd_stat(const std::string &path)
{
// convert the path to TCHARs
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);
}
// 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
osd_directory_entry *const result = (osd_directory_entry *)osd_malloc_array(sizeof(*result) + path.length() + 1);
// 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
osd::directory::entry *const result = (osd::directory::entry *)osd_malloc_array(sizeof(*result) + path.length() + 1);
if (!result)
return nullptr;
strcpy(((char *) result) + sizeof(*result), path.c_str());
result->name = ((char *) result) + sizeof(*result);
result->type = win_attributes_to_entry_type(find_data.dwFileAttributes);
result->size = find_data.nFileSizeLow | ((UINT64) find_data.nFileSizeHigh << 32);
result->last_modified = win_time_point_from_filetime(&find_data.ftLastWriteTime);
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;
}
osd_directory *directory = osd_opendir(directory_path.c_str());
osd::directory *directory = osd::directory::open(directory_path.c_str());
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 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)
{
osd_directory *directory = osd_opendir((root + path).c_str());
osd::directory *directory = osd::directory::open((root + path).c_str());
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 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;
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 <memory>
#include <string>
#include <chrono>
/***************************************************************************
@ -297,76 +298,64 @@ int osd_uchar_from_osdchar(UINT32 /* unicode_char */ *uchar, const char *osdchar
DIRECTORY INTERFACES
***************************************************************************/
/* types of directory entries that can be returned */
enum osd_dir_entry_type
namespace osd
{
ENTTYPE_NONE,
ENTTYPE_FILE,
ENTTYPE_DIR,
ENTTYPE_OTHER
// directory is an opaque type which represents an open directory
class directory
{
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
@ -798,12 +787,12 @@ char *osd_get_clipboard_text(void);
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.
free with osd_free()
-----------------------------------------------------------------------------*/
osd_directory_entry *osd_stat(std::string const &path);
osd::directory::entry *osd_stat(std::string const &path);
/***************************************************************************
PATH INTERFACES

View File

@ -28,36 +28,39 @@
// TYPE DEFINITIONS
//============================================================
struct osd_directory
namespace
{
HANDLE find; // handle to the finder
int is_first; // TRUE if this is the first entry
osd_directory_entry entry; // current entry's data
WIN32_FIND_DATA data; // current raw data
class win_directory : public osd::directory
{
public:
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 *dirfilter = nullptr;
size_t dirfilter_size;
// allocate memory to hold the osd_tool_directory structure
dir = (osd_directory *)malloc(sizeof(*dir));
dir = new win_directory();
if (dir == nullptr)
goto error;
memset(dir, 0, sizeof(*dir));
// initialize the structure
dir->find = INVALID_HANDLE_VALUE;
dir->is_first = TRUE;
// convert the path to TCHARs
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);
// 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:
// cleanup
@ -80,9 +83,9 @@ error:
osd_free(t_dirname);
if (dirfilter != nullptr)
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;
}
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 (dir->entry.name != nullptr)
if (m_entry.name != nullptr)
{
osd_free((void *)dir->entry.name);
dir->entry.name = nullptr;
osd_free((void *)m_entry.name);
m_entry.name = nullptr;
}
// 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;
}
// otherwise, just use the data we already had
else
dir->is_first = FALSE;
m_is_first = false;
// extract the data
dir->entry.name = utf8_from_tstring(dir->data.cFileName);
dir->entry.type = win_attributes_to_entry_type(dir->data.dwFileAttributes);
dir->entry.size = dir->data.nFileSizeLow | ((UINT64) dir->data.nFileSizeHigh << 32);
return (dir->entry.name != nullptr) ? &dir->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);
m_entry.name = utf8_from_tstring(m_data.cFileName);
m_entry.type = win_attributes_to_entry_type(m_data.dwFileAttributes);
m_entry.size = m_data.nFileSizeLow | ((UINT64) m_data.nFileSizeHigh << 32);
m_entry.last_modified = win_time_point_from_filetime(&m_data.ftLastWriteTime);
return (m_entry.name != nullptr) ? &m_entry : nullptr;
}

View File

@ -23,14 +23,26 @@
// 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)
return ENTTYPE_NONE;
return osd::directory::entry::entry_type::NONE;
else if (attributes & FILE_ATTRIBUTE_DIRECTORY)
return ENTTYPE_DIR;
return osd::directory::entry::entry_type::DIR;
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 <string>
#include <vector>
#include <chrono>
// 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);
HMODULE WINAPI GetModuleHandleUni();