18 #ifndef MAGICKCORE_UTILITY_PRIVATE_H
19 #define MAGICKCORE_UTILITY_PRIVATE_H
21 #include "magick/memory_.h"
22 #include "magick/nt-base.h"
23 #include "magick/nt-base-private.h"
25 #if defined(__cplusplus) || defined(c_plusplus)
29 extern MagickPrivate MagickBooleanType
30 ShredFile(
const char *);
32 static inline int MagickReadDirectory(
DIR *directory,
struct dirent *entry,
37 *result=readdir(directory);
45 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
46 static inline wchar_t *create_wchar_path(
const char *utf8)
54 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
55 if ((count > MAX_PATH) && (NTLongPathsEnabled() == MagickFalse))
58 buffer[MaxTextExtent];
64 (void) FormatLocaleString(buffer,MaxTextExtent,
"\\\\?\\%s",utf8);
66 longPath=(
wchar_t *) NTAcquireQuantumMemory((
size_t) count,
68 if (longPath == (
wchar_t *) NULL)
69 return((
wchar_t *) NULL);
70 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
72 count=(int) GetShortPathNameW(longPath,shortPath,MAX_PATH);
73 longPath=(
wchar_t *) RelinquishMagickMemory(longPath);
74 if ((count < 5) || (count >= MAX_PATH))
75 return((
wchar_t *) NULL);
76 wideChar=(
wchar_t *) NTAcquireQuantumMemory((
size_t) count-3,
78 wcscpy(wideChar,shortPath+4);
81 wideChar=(
wchar_t *) NTAcquireQuantumMemory(count,
sizeof(*wideChar));
82 if (wideChar == (
wchar_t *) NULL)
83 return((
wchar_t *) NULL);
84 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
87 wideChar=(
wchar_t *) RelinquishMagickMemory(wideChar);
88 return((
wchar_t *) NULL);
94 static inline int access_utf8(
const char *path,
int mode)
96 if (path == (
const char *) NULL)
98 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
99 return(access(path,mode));
107 path_wide=create_wchar_path(path);
108 if (path_wide == (
wchar_t *) NULL)
110 status=_waccess(path_wide,mode);
111 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
116 static inline FILE *fopen_utf8(
const char *path,
const char *mode)
118 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
119 return(fopen(path,mode));
128 path_wide=create_wchar_path(path);
129 if (path_wide == (
wchar_t *) NULL)
130 return((FILE *) NULL);
131 mode_wide=create_wchar_path(mode);
132 if (mode_wide == (
wchar_t *) NULL)
134 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
135 return((FILE *) NULL);
137 file=_wfopen(path_wide,mode_wide);
138 mode_wide=(
wchar_t *) RelinquishMagickMemory(mode_wide);
139 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
144 static inline void getcwd_utf8(
char *path,
size_t extent)
146 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
150 directory=getcwd(path,extent);
154 wide_path[MaxTextExtent];
156 (void) _wgetcwd(wide_path,MaxTextExtent-1);
157 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
161 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
166 static inline int open_utf8(
const char *path,
int flags,mode_t mode)
168 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
169 return(open(path,flags,mode));
177 path_wide=create_wchar_path(path);
178 if (path_wide == (
wchar_t *) NULL)
180 status=_wopen(path_wide,flags,mode);
181 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
186 static inline FILE *popen_utf8(
const char *command,
const char *type)
188 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
189 return(popen(command,type));
202 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
205 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
208 command_wide=(
wchar_t *) AcquireQuantumMemory((
size_t) length,
209 sizeof(*command_wide));
210 if (command_wide == (
wchar_t *) NULL)
212 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
214 file=_wpopen(command_wide,type_wide);
215 command_wide=(
wchar_t *) RelinquishMagickMemory(command_wide);
220 static inline int remove_utf8(
const char *path)
222 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
223 return(unlink(path));
231 path_wide=create_wchar_path(path);
232 if (path_wide == (
wchar_t *) NULL)
234 status=_wremove(path_wide);
235 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
240 static inline int rename_utf8(
const char *source,
const char *destination)
242 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
243 return(rename(source,destination));
252 source_wide=create_wchar_path(source);
253 if (source_wide == (
wchar_t *) NULL)
255 destination_wide=create_wchar_path(destination);
256 if (destination_wide == (
wchar_t *) NULL)
258 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
261 status=_wrename(source_wide,destination_wide);
262 destination_wide=(
wchar_t *) RelinquishMagickMemory(destination_wide);
263 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
268 static inline int stat_utf8(
const char *path,
struct stat *attributes)
270 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
271 return(stat(path,attributes));
279 path_wide=create_wchar_path(path);
280 if (path_wide == (WCHAR *) NULL)
282 status=wstat(path_wide,attributes);
283 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
288 #if defined(__cplusplus) || defined(c_plusplus)