Blender  V2.93
winstuff_dir.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  * Windows-posix compatibility layer for opendir/readdir/closedir
16  */
17 
25 #ifdef WIN32
26 
27 /* standalone for inclusion in binaries other than blender */
28 # ifdef USE_STANDALONE
29 # define MEM_mallocN(size, str) ((void)str, malloc(size))
30 # define MEM_callocN(size, str) ((void)str, calloc(size, 1))
31 # define MEM_freeN(ptr) free(ptr)
32 # else
33 # include "MEM_guardedalloc.h"
34 # endif
35 
36 # define WIN32_SKIP_HKEY_PROTECTION // need to use HKEY
37 # include "BLI_utildefines.h"
38 # include "BLI_winstuff.h"
39 # include "utfconv.h"
40 
41 # define PATH_SUFFIX "\\*"
42 # define PATH_SUFFIX_LEN 2
43 
44 /* keep local to this file */
45 struct __dirstream {
46  HANDLE handle;
47  WIN32_FIND_DATAW data;
48  char path[MAX_PATH + PATH_SUFFIX_LEN];
49  long dd_loc;
50  long dd_size;
51  char dd_buf[4096];
52  void *dd_direct;
53 
54  struct dirent direntry;
55 };
56 
65 DIR *opendir(const char *path)
66 {
67  wchar_t *path_16 = alloc_utf16_from_8(path, 0);
68  int path_len;
69  DIR *newd = NULL;
70 
71  if ((GetFileAttributesW(path_16) & FILE_ATTRIBUTE_DIRECTORY) &&
72  ((path_len = strlen(path)) < (sizeof(newd->path) - PATH_SUFFIX_LEN))) {
73  newd = MEM_mallocN(sizeof(DIR), "opendir");
74  newd->handle = INVALID_HANDLE_VALUE;
75  memcpy(newd->path, path, path_len);
76  memcpy(newd->path + path_len, PATH_SUFFIX, PATH_SUFFIX_LEN + 1);
77 
78  newd->direntry.d_ino = 0;
79  newd->direntry.d_off = 0;
80  newd->direntry.d_reclen = 0;
81  newd->direntry.d_name = NULL;
82  }
83 
84  free(path_16);
85  return newd;
86 }
87 
88 static char *BLI_alloc_utf_8_from_16(wchar_t *in16, size_t add)
89 {
90  size_t bsize = count_utf_8_from_16(in16);
91  char *out8 = NULL;
92  if (!bsize) {
93  return NULL;
94  }
95  out8 = (char *)MEM_mallocN(sizeof(char) * (bsize + add), "UTF-8 String");
96  conv_utf_16_to_8(in16, out8, bsize);
97  return out8;
98 }
99 
100 static wchar_t *UNUSED_FUNCTION(BLI_alloc_utf16_from_8)(char *in8, size_t add)
101 {
102  size_t bsize = count_utf_16_from_8(in8);
103  wchar_t *out16 = NULL;
104  if (!bsize) {
105  return NULL;
106  }
107  out16 = (wchar_t *)MEM_mallocN(sizeof(wchar_t) * (bsize + add), "UTF-16 String");
108  conv_utf_8_to_16(in8, out16, bsize);
109  return out16;
110 }
111 
112 struct dirent *readdir(DIR *dp)
113 {
114  if (dp->direntry.d_name) {
115  MEM_freeN(dp->direntry.d_name);
116  dp->direntry.d_name = NULL;
117  }
118 
119  if (dp->handle == INVALID_HANDLE_VALUE) {
120  wchar_t *path_16 = alloc_utf16_from_8(dp->path, 0);
121  dp->handle = FindFirstFileW(path_16, &(dp->data));
122  free(path_16);
123  if (dp->handle == INVALID_HANDLE_VALUE) {
124  return NULL;
125  }
126 
127  dp->direntry.d_name = BLI_alloc_utf_8_from_16(dp->data.cFileName, 0);
128 
129  return &dp->direntry;
130  }
131  else if (FindNextFileW(dp->handle, &(dp->data))) {
132  dp->direntry.d_name = BLI_alloc_utf_8_from_16(dp->data.cFileName, 0);
133 
134  return &dp->direntry;
135  }
136  else {
137  return NULL;
138  }
139 }
140 
141 int closedir(DIR *dp)
142 {
143  if (dp->direntry.d_name) {
144  MEM_freeN(dp->direntry.d_name);
145  }
146  if (dp->handle != INVALID_HANDLE_VALUE) {
147  FindClose(dp->handle);
148  }
149 
150  MEM_freeN(dp);
151 
152  return 0;
153 }
154 
155 /* End of copied part */
156 
157 #else
158 
159 /* intentionally empty for UNIX */
160 
161 #endif
void BLI_kdtree_nd_() free(KDTree *tree)
Definition: kdtree_impl.h:116
#define UNUSED_FUNCTION(x)
Compatibility-like things for windows.
struct __dirstream DIR
Definition: BLI_winstuff.h:100
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:41
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
static void add(GHash *messages, MemArena *memarena, const Message *msg)
Definition: msgfmt.c:268
size_t count_utf_8_from_16(const wchar_t *string16)
Definition: utfconv.c:23
wchar_t * alloc_utf16_from_8(const char *in8, size_t add)
Definition: utfconv.c:296
int conv_utf_8_to_16(const char *in8, wchar_t *out16, size_t size16)
Definition: utfconv.c:189
int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8)
Definition: utfconv.c:127
size_t count_utf_16_from_8(const char *string8)
Definition: utfconv.c:70