2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2014-01-15 08:38:00 +11:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup bli
|
2014-01-16 19:15:53 +11:00
|
|
|
*
|
|
|
|
* Posix compatibility functions for windows dealing with DIR
|
|
|
|
* (opendir, readdir, closedir)
|
2014-01-15 08:38:00 +11:00
|
|
|
*/
|
|
|
|
|
2014-06-23 10:07:06 +09:00
|
|
|
#ifdef WIN32
|
2014-01-15 08:38:00 +11:00
|
|
|
|
2021-08-31 13:36:28 +10:00
|
|
|
/* Standalone for inclusion in binaries other than Blender. */
|
2014-01-15 08:38:00 +11:00
|
|
|
# ifdef USE_STANDALONE
|
|
|
|
# define MEM_mallocN(size, str) ((void)str, malloc(size))
|
|
|
|
# define MEM_callocN(size, str) ((void)str, calloc(size, 1))
|
|
|
|
# define MEM_freeN(ptr) free(ptr)
|
|
|
|
# else
|
|
|
|
# include "MEM_guardedalloc.h"
|
|
|
|
# endif
|
|
|
|
|
2021-08-31 13:36:28 +10:00
|
|
|
# define WIN32_SKIP_HKEY_PROTECTION /* Need to use `HKEY`. */
|
2014-01-14 22:53:52 +01:00
|
|
|
# include "BLI_utildefines.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
# include "BLI_winstuff.h"
|
2014-01-15 08:38:00 +11:00
|
|
|
# include "utfconv.h"
|
|
|
|
|
2015-06-16 10:01:07 +10:00
|
|
|
# define PATH_SUFFIX "\\*"
|
|
|
|
# define PATH_SUFFIX_LEN 2
|
|
|
|
|
2015-06-16 09:40:17 +10:00
|
|
|
/* keep local to this file */
|
|
|
|
struct __dirstream {
|
|
|
|
HANDLE handle;
|
|
|
|
WIN32_FIND_DATAW data;
|
2015-06-16 10:01:07 +10:00
|
|
|
char path[MAX_PATH + PATH_SUFFIX_LEN];
|
2015-06-16 09:40:17 +10:00
|
|
|
long dd_loc;
|
|
|
|
long dd_size;
|
|
|
|
char dd_buf[4096];
|
|
|
|
void *dd_direct;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-06-16 09:40:17 +10:00
|
|
|
struct dirent direntry;
|
|
|
|
};
|
|
|
|
|
2019-04-14 10:48:42 +02:00
|
|
|
/**
|
|
|
|
* \note MinGW (FREE_WINDOWS) has #opendir() and #_wopendir(), and only the
|
|
|
|
* latter accepts a path name of #wchar_t type. Rather than messing up with
|
|
|
|
* extra #ifdef's here and there, Blender's own implementations of #opendir()
|
2015-01-02 19:18:57 +11:00
|
|
|
* and related functions are used to properly support paths with non-ASCII
|
|
|
|
* characters. (kjym3)
|
|
|
|
*/
|
2014-06-23 10:07:06 +09:00
|
|
|
|
2014-01-15 08:38:00 +11:00
|
|
|
DIR *opendir(const char *path)
|
|
|
|
{
|
|
|
|
wchar_t *path_16 = alloc_utf16_from_8(path, 0);
|
2015-06-16 10:01:07 +10:00
|
|
|
int path_len;
|
|
|
|
DIR *newd = NULL;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-06-16 10:01:07 +10:00
|
|
|
if ((GetFileAttributesW(path_16) & FILE_ATTRIBUTE_DIRECTORY) &&
|
|
|
|
((path_len = strlen(path)) < (sizeof(newd->path) - PATH_SUFFIX_LEN))) {
|
|
|
|
newd = MEM_mallocN(sizeof(DIR), "opendir");
|
2014-01-15 08:38:00 +11:00
|
|
|
newd->handle = INVALID_HANDLE_VALUE;
|
2015-06-16 10:01:07 +10:00
|
|
|
memcpy(newd->path, path, path_len);
|
|
|
|
memcpy(newd->path + path_len, PATH_SUFFIX, PATH_SUFFIX_LEN + 1);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-15 08:38:00 +11:00
|
|
|
newd->direntry.d_ino = 0;
|
|
|
|
newd->direntry.d_off = 0;
|
|
|
|
newd->direntry.d_reclen = 0;
|
|
|
|
newd->direntry.d_name = NULL;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-06-16 10:01:07 +10:00
|
|
|
free(path_16);
|
|
|
|
return newd;
|
2014-01-15 08:38:00 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *BLI_alloc_utf_8_from_16(wchar_t *in16, size_t add)
|
|
|
|
{
|
|
|
|
size_t bsize = count_utf_8_from_16(in16);
|
|
|
|
char *out8 = NULL;
|
2019-03-27 13:16:10 +11:00
|
|
|
if (!bsize) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-01-15 08:38:00 +11:00
|
|
|
out8 = (char *)MEM_mallocN(sizeof(char) * (bsize + add), "UTF-8 String");
|
|
|
|
conv_utf_16_to_8(in16, out8, bsize);
|
|
|
|
return out8;
|
|
|
|
}
|
|
|
|
|
|
|
|
static wchar_t *UNUSED_FUNCTION(BLI_alloc_utf16_from_8)(char *in8, size_t add)
|
|
|
|
{
|
|
|
|
size_t bsize = count_utf_16_from_8(in8);
|
|
|
|
wchar_t *out16 = NULL;
|
2019-03-27 13:16:10 +11:00
|
|
|
if (!bsize) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-01-15 08:38:00 +11:00
|
|
|
out16 = (wchar_t *)MEM_mallocN(sizeof(wchar_t) * (bsize + add), "UTF-16 String");
|
|
|
|
conv_utf_8_to_16(in8, out16, bsize);
|
|
|
|
return out16;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent *readdir(DIR *dp)
|
|
|
|
{
|
|
|
|
if (dp->direntry.d_name) {
|
|
|
|
MEM_freeN(dp->direntry.d_name);
|
|
|
|
dp->direntry.d_name = NULL;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-15 08:38:00 +11:00
|
|
|
if (dp->handle == INVALID_HANDLE_VALUE) {
|
|
|
|
wchar_t *path_16 = alloc_utf16_from_8(dp->path, 0);
|
|
|
|
dp->handle = FindFirstFileW(path_16, &(dp->data));
|
|
|
|
free(path_16);
|
2019-03-27 13:16:10 +11:00
|
|
|
if (dp->handle == INVALID_HANDLE_VALUE) {
|
2014-01-15 08:38:00 +11:00
|
|
|
return NULL;
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-15 08:38:00 +11:00
|
|
|
dp->direntry.d_name = BLI_alloc_utf_8_from_16(dp->data.cFileName, 0);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-15 08:38:00 +11:00
|
|
|
return &dp->direntry;
|
|
|
|
}
|
|
|
|
else if (FindNextFileW(dp->handle, &(dp->data))) {
|
|
|
|
dp->direntry.d_name = BLI_alloc_utf_8_from_16(dp->data.cFileName, 0);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-15 08:38:00 +11:00
|
|
|
return &dp->direntry;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int closedir(DIR *dp)
|
|
|
|
{
|
2019-03-27 13:16:10 +11:00
|
|
|
if (dp->direntry.d_name) {
|
|
|
|
MEM_freeN(dp->direntry.d_name);
|
|
|
|
}
|
|
|
|
if (dp->handle != INVALID_HANDLE_VALUE) {
|
|
|
|
FindClose(dp->handle);
|
|
|
|
}
|
2014-01-15 08:38:00 +11:00
|
|
|
|
|
|
|
MEM_freeN(dp);
|
2018-06-17 16:32:54 +02:00
|
|
|
|
2014-01-15 08:38:00 +11:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End of copied part */
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/* intentionally empty for UNIX */
|
|
|
|
|
|
|
|
#endif
|