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