mirror of https://github.com/OpenTTD/OpenTTD.git
Remove: Custom opendir implementation for Windows no longer needed.
std::filesystem::directory_iterator is now used instead.
This commit is contained in:
parent
42523379d9
commit
63ce81570c
|
@ -81,40 +81,6 @@ public:
|
|||
|
||||
DECLARE_ENUM_AS_BIT_SET(TarScanner::Mode)
|
||||
|
||||
/* Implementation of opendir/readdir/closedir for Windows */
|
||||
#if defined(_WIN32)
|
||||
struct DIR;
|
||||
|
||||
struct dirent { // XXX - only d_name implemented
|
||||
wchar_t *d_name; // name of found file
|
||||
/* little hack which will point to parent DIR struct which will
|
||||
* save us a call to GetFileAttributes if we want information
|
||||
* about the file (for example in function fio_bla) */
|
||||
DIR *dir;
|
||||
};
|
||||
|
||||
DIR *opendir(const wchar_t *path);
|
||||
struct dirent *readdir(DIR *d);
|
||||
int closedir(DIR *d);
|
||||
#else
|
||||
/* Use system-supplied opendir/readdir/closedir functions */
|
||||
# include <sys/types.h>
|
||||
# include <dirent.h>
|
||||
#endif /* defined(_WIN32) */
|
||||
|
||||
/**
|
||||
* A wrapper around opendir() which will convert the string from
|
||||
* OPENTTD encoding to that of the filesystem. For all purposes this
|
||||
* function behaves the same as the original opendir function
|
||||
* @param path string to open directory of
|
||||
* @return DIR pointer
|
||||
*/
|
||||
inline DIR *ttd_opendir(const char *path)
|
||||
{
|
||||
return opendir(OTTD2FS(path).c_str());
|
||||
}
|
||||
|
||||
|
||||
/** Auto-close a file upon scope exit. */
|
||||
class FileCloser {
|
||||
FILE *f;
|
||||
|
|
|
@ -58,118 +58,6 @@ void OSOpenBrowser(const std::string &url)
|
|||
ShellExecute(GetActiveWindow(), L"open", OTTD2FS(url).c_str(), nullptr, nullptr, SW_SHOWNORMAL);
|
||||
}
|
||||
|
||||
/* Code below for windows version of opendir/readdir/closedir copied and
|
||||
* modified from Jan Wassenberg's GPL implementation posted over at
|
||||
* http://www.gamedev.net/community/forums/topic.asp?topic_id=364584&whichpage=1� */
|
||||
|
||||
struct DIR {
|
||||
HANDLE hFind;
|
||||
/* the dirent returned by readdir.
|
||||
* note: having only one global instance is not possible because
|
||||
* multiple independent opendir/readdir sequences must be supported. */
|
||||
dirent ent;
|
||||
WIN32_FIND_DATA fd;
|
||||
/* since opendir calls FindFirstFile, we need a means of telling the
|
||||
* first call to readdir that we already have a file.
|
||||
* that's the case iff this is true */
|
||||
bool at_first_entry;
|
||||
};
|
||||
|
||||
/* suballocator - satisfies most requests with a reusable static instance.
|
||||
* this avoids hundreds of alloc/free which would fragment the heap.
|
||||
* To guarantee concurrency, we fall back to malloc if the instance is
|
||||
* already in use (it's important to avoid surprises since this is such a
|
||||
* low-level routine). */
|
||||
static DIR _global_dir;
|
||||
static LONG _global_dir_is_in_use = false;
|
||||
|
||||
static inline DIR *dir_calloc()
|
||||
{
|
||||
DIR *d;
|
||||
|
||||
if (InterlockedExchange(&_global_dir_is_in_use, true) == (LONG)true) {
|
||||
d = CallocT<DIR>(1);
|
||||
} else {
|
||||
d = &_global_dir;
|
||||
memset(d, 0, sizeof(*d));
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
static inline void dir_free(DIR *d)
|
||||
{
|
||||
if (d == &_global_dir) {
|
||||
_global_dir_is_in_use = (LONG)false;
|
||||
} else {
|
||||
free(d);
|
||||
}
|
||||
}
|
||||
|
||||
DIR *opendir(const wchar_t *path)
|
||||
{
|
||||
DIR *d;
|
||||
UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS); // disable 'no-disk' message box
|
||||
DWORD fa = GetFileAttributes(path);
|
||||
|
||||
if ((fa != INVALID_FILE_ATTRIBUTES) && (fa & FILE_ATTRIBUTE_DIRECTORY)) {
|
||||
d = dir_calloc();
|
||||
if (d != nullptr) {
|
||||
std::wstring search_path = path;
|
||||
bool slash = path[wcslen(path) - 1] == '\\';
|
||||
|
||||
/* build search path for FindFirstFile, try not to append additional slashes
|
||||
* as it throws Win9x off its groove for root directories */
|
||||
if (!slash) search_path += L"\\";
|
||||
search_path += L"*";
|
||||
d->hFind = FindFirstFile(search_path.c_str(), &d->fd);
|
||||
|
||||
if (d->hFind != INVALID_HANDLE_VALUE ||
|
||||
GetLastError() == ERROR_NO_MORE_FILES) { // the directory is empty
|
||||
d->ent.dir = d;
|
||||
d->at_first_entry = true;
|
||||
} else {
|
||||
dir_free(d);
|
||||
d = nullptr;
|
||||
}
|
||||
} else {
|
||||
errno = ENOMEM;
|
||||
}
|
||||
} else {
|
||||
/* path not found or not a directory */
|
||||
d = nullptr;
|
||||
errno = ENOENT;
|
||||
}
|
||||
|
||||
SetErrorMode(sem); // restore previous setting
|
||||
return d;
|
||||
}
|
||||
|
||||
struct dirent *readdir(DIR *d)
|
||||
{
|
||||
DWORD prev_err = GetLastError(); // avoid polluting last error
|
||||
|
||||
if (d->at_first_entry) {
|
||||
/* the directory was empty when opened */
|
||||
if (d->hFind == INVALID_HANDLE_VALUE) return nullptr;
|
||||
d->at_first_entry = false;
|
||||
} else if (!FindNextFile(d->hFind, &d->fd)) { // determine cause and bail
|
||||
if (GetLastError() == ERROR_NO_MORE_FILES) SetLastError(prev_err);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/* This entry has passed all checks; return information about it.
|
||||
* (note: d_name is a pointer; see struct dirent definition) */
|
||||
d->ent.d_name = d->fd.cFileName;
|
||||
return &d->ent;
|
||||
}
|
||||
|
||||
int closedir(DIR *d)
|
||||
{
|
||||
FindClose(d->hFind);
|
||||
dir_free(d);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool FiosIsRoot(const std::string &file)
|
||||
{
|
||||
return file.size() == 3; // C:\...
|
||||
|
|
Loading…
Reference in New Issue