|
|
|
@ -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:\...
|
|
|
|
|