2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * Contributor(s): none yet.
20 * ***** END GPL LICENSE BLOCK *****
21 * Windows-posix compatibility layer for opendir/readdir/closedir
24 /** \file blender/blenlib/intern/winstuff_dir.c
27 * Posix compatibility functions for windows dealing with DIR
28 * (opendir, readdir, closedir)
33 /* standalone for inclusion in binaries other then blender */
34 # ifdef USE_STANDALONE
35 # define MEM_mallocN(size, str) ((void)str, malloc(size))
36 # define MEM_callocN(size, str) ((void)str, calloc(size, 1))
37 # define MEM_freeN(ptr) free(ptr)
39 # include "MEM_guardedalloc.h"
42 #define WIN32_SKIP_HKEY_PROTECTION // need to use HKEY
43 #include "BLI_winstuff.h"
44 #include "BLI_utildefines.h"
47 /* Note: MinGW (FREE_WINDOWS) has opendir() and _wopendir(), and only the
48 * latter accepts a path name of wchar_t type. Rather than messing up with
49 * extra #ifdef's here and there, Blender's own implementations of opendir()
50 * and related functions are used to properly support paths with non-ASCII
54 DIR *opendir(const char *path)
56 wchar_t *path_16 = alloc_utf16_from_8(path, 0);
58 if (GetFileAttributesW(path_16) & FILE_ATTRIBUTE_DIRECTORY) {
59 DIR *newd = MEM_mallocN(sizeof(DIR), "opendir");
61 newd->handle = INVALID_HANDLE_VALUE;
62 sprintf(newd->path, "%s\\*", path);
64 newd->direntry.d_ino = 0;
65 newd->direntry.d_off = 0;
66 newd->direntry.d_reclen = 0;
67 newd->direntry.d_name = NULL;
78 static char *BLI_alloc_utf_8_from_16(wchar_t *in16, size_t add)
80 size_t bsize = count_utf_8_from_16(in16);
82 if (!bsize) return NULL;
83 out8 = (char *)MEM_mallocN(sizeof(char) * (bsize + add), "UTF-8 String");
84 conv_utf_16_to_8(in16, out8, bsize);
88 static wchar_t *UNUSED_FUNCTION(BLI_alloc_utf16_from_8) (char *in8, size_t add)
90 size_t bsize = count_utf_16_from_8(in8);
91 wchar_t *out16 = NULL;
92 if (!bsize) return NULL;
93 out16 = (wchar_t *) MEM_mallocN(sizeof(wchar_t) * (bsize + add), "UTF-16 String");
94 conv_utf_8_to_16(in8, out16, bsize);
100 struct dirent *readdir(DIR *dp)
102 if (dp->direntry.d_name) {
103 MEM_freeN(dp->direntry.d_name);
104 dp->direntry.d_name = NULL;
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));
111 if (dp->handle == INVALID_HANDLE_VALUE)
114 dp->direntry.d_name = BLI_alloc_utf_8_from_16(dp->data.cFileName, 0);
116 return &dp->direntry;
118 else if (FindNextFileW(dp->handle, &(dp->data))) {
119 dp->direntry.d_name = BLI_alloc_utf_8_from_16(dp->data.cFileName, 0);
121 return &dp->direntry;
128 int closedir(DIR *dp)
130 if (dp->direntry.d_name) MEM_freeN(dp->direntry.d_name);
131 if (dp->handle != INVALID_HANDLE_VALUE) FindClose(dp->handle);
138 /* End of copied part */
142 /* intentionally empty for UNIX */