Blender V4.3
winstuff_dir.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12#ifdef WIN32
13
14/* Standalone for inclusion in binaries other than Blender. */
15# ifdef USE_STANDALONE
16# define MEM_mallocN(size, str) ((void)str, malloc(size))
17# define MEM_callocN(size, str) ((void)str, calloc(size, 1))
18# define MEM_freeN(ptr) free(ptr)
19# else
20# include "MEM_guardedalloc.h"
21# endif
22
23# define WIN32_SKIP_HKEY_PROTECTION /* Need to use `HKEY`. */
24# include "BLI_utildefines.h"
25# include "BLI_winstuff.h"
26# include "utfconv.hh"
27
28# define PATH_SUFFIX "\\*"
29# define PATH_SUFFIX_LEN 2
30
31/* keep local to this file */
32struct __dirstream {
33 HANDLE handle;
34 WIN32_FIND_DATAW data;
35 char path[MAX_PATH + PATH_SUFFIX_LEN];
36 long dd_loc;
37 long dd_size;
38 char dd_buf[4096];
39 void *dd_direct;
40
41 struct dirent direntry;
42};
43
52DIR *opendir(const char *path)
53{
54 wchar_t *path_16 = alloc_utf16_from_8(path, 0);
55 int path_len;
56 DIR *newd = NULL;
57
58 if ((GetFileAttributesW(path_16) & FILE_ATTRIBUTE_DIRECTORY) &&
59 ((path_len = strlen(path)) < (sizeof(newd->path) - PATH_SUFFIX_LEN)))
60 {
61 newd = static_cast<DIR *>(MEM_mallocN(sizeof(DIR), "opendir"));
62 newd->handle = INVALID_HANDLE_VALUE;
63 memcpy(newd->path, path, path_len);
64 memcpy(newd->path + path_len, PATH_SUFFIX, PATH_SUFFIX_LEN + 1);
65
66 newd->direntry.d_ino = 0;
67 newd->direntry.d_off = 0;
68 newd->direntry.d_reclen = 0;
69 newd->direntry.d_name = NULL;
70 }
71
72 free(path_16);
73 return newd;
74}
75
76static char *BLI_alloc_utf_8_from_16(wchar_t *in16, size_t add)
77{
78 size_t bsize = count_utf_8_from_16(in16);
79 char *out8 = NULL;
80 if (!bsize) {
81 return NULL;
82 }
83 out8 = (char *)MEM_mallocN(sizeof(char) * (bsize + add), "UTF-8 String");
84 conv_utf_16_to_8(in16, out8, bsize);
85 return out8;
86}
87
88static wchar_t *UNUSED_FUNCTION(BLI_alloc_utf16_from_8)(char *in8, size_t add)
89{
90 size_t bsize = count_utf_16_from_8(in8);
91 wchar_t *out16 = NULL;
92 if (!bsize) {
93 return NULL;
94 }
95 out16 = (wchar_t *)MEM_mallocN(sizeof(wchar_t) * (bsize + add), "UTF-16 String");
96 conv_utf_8_to_16(in8, out16, bsize);
97 return out16;
98}
99
100struct dirent *readdir(DIR *dp)
101{
102 if (dp->direntry.d_name) {
103 MEM_freeN(dp->direntry.d_name);
104 dp->direntry.d_name = NULL;
105 }
106
107 if (dp->handle == INVALID_HANDLE_VALUE) {
108 wchar_t *path_16 = alloc_utf16_from_8(dp->path, 0);
109 dp->handle = FindFirstFileW(path_16, &(dp->data));
110 free(path_16);
111 if (dp->handle == INVALID_HANDLE_VALUE) {
112 return NULL;
113 }
114
115 dp->direntry.d_name = BLI_alloc_utf_8_from_16(dp->data.cFileName, 0);
116
117 return &dp->direntry;
118 }
119 else if (FindNextFileW(dp->handle, &(dp->data))) {
120 dp->direntry.d_name = BLI_alloc_utf_8_from_16(dp->data.cFileName, 0);
121
122 return &dp->direntry;
123 }
124 else {
125 return NULL;
126 }
127}
128
129int closedir(DIR *dp)
130{
131 if (dp->direntry.d_name) {
132 MEM_freeN(dp->direntry.d_name);
133 }
134 if (dp->handle != INVALID_HANDLE_VALUE) {
135 FindClose(dp->handle);
136 }
137
138 MEM_freeN(dp);
139
140 return 0;
141}
142
143/* End of copied part */
144
145#else
146
147/* intentionally empty for UNIX */
148
149#endif
void BLI_kdtree_nd_ free(KDTree *tree)
#define PATH_SUFFIX(path_literal, path_literal_max, sep, suffix, expect_result, expect_path)
#define UNUSED_FUNCTION(x)
Compatibility-like things for windows.
struct __dirstream DIR
struct dirent * readdir(DIR *dp)
int closedir(DIR *dp)
DIR * opendir(const char *path)
Read Guarded memory(de)allocation.
#define NULL
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
static void add(blender::Map< std::string, std::string > &messages, Message &msg)
Definition msgfmt.cc:227
wchar_t * alloc_utf16_from_8(const char *in8, size_t add)
Definition utfconv.cc:292
size_t count_utf_8_from_16(const wchar_t *string16)
Definition utfconv.cc:11
int conv_utf_8_to_16(const char *in8, wchar_t *out16, size_t size16)
Definition utfconv.cc:182
int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8)
Definition utfconv.cc:116
size_t count_utf_16_from_8(const char *string8)
Definition utfconv.cc:58