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(count,
sizeof(*longPath));
67 if (longPath == (
wchar_t *) NULL)
68 return((
wchar_t *) NULL);
69 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
71 count=GetShortPathNameW(longPath,shortPath,MAX_PATH);
72 longPath=(
wchar_t *) RelinquishMagickMemory(longPath);
73 if ((count < 5) || (count >= MAX_PATH))
74 return((
wchar_t *) NULL);
75 wideChar=(
wchar_t *) NTAcquireQuantumMemory(count-3,
sizeof(*wideChar));
76 wcscpy(wideChar,shortPath+4);
79 wideChar=(
wchar_t *) NTAcquireQuantumMemory(count,
sizeof(*wideChar));
80 if (wideChar == (
wchar_t *) NULL)
81 return((
wchar_t *) NULL);
82 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
85 wideChar=(
wchar_t *) RelinquishMagickMemory(wideChar);
86 return((
wchar_t *) NULL);
92 static inline int access_utf8(
const char *path,
int mode)
94 if (path == (
const char *) NULL)
96 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
97 return(access(path,mode));
105 path_wide=create_wchar_path(path);
106 if (path_wide == (
wchar_t *) NULL)
108 status=_waccess(path_wide,mode);
109 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
114 static inline FILE *fopen_utf8(
const char *path,
const char *mode)
116 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
117 return(fopen(path,mode));
126 path_wide=create_wchar_path(path);
127 if (path_wide == (
wchar_t *) NULL)
128 return((FILE *) NULL);
129 mode_wide=create_wchar_path(mode);
130 if (mode_wide == (
wchar_t *) NULL)
132 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
133 return((FILE *) NULL);
135 file=_wfopen(path_wide,mode_wide);
136 mode_wide=(
wchar_t *) RelinquishMagickMemory(mode_wide);
137 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
142 static inline void getcwd_utf8(
char *path,
size_t extent)
144 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
148 directory=getcwd(path,extent);
152 wide_path[MaxTextExtent];
154 (void) _wgetcwd(wide_path,MaxTextExtent-1);
155 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
159 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
164 static inline int open_utf8(
const char *path,
int flags,mode_t mode)
166 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
167 return(open(path,flags,mode));
175 path_wide=create_wchar_path(path);
176 if (path_wide == (
wchar_t *) NULL)
178 status=_wopen(path_wide,flags,mode);
179 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
184 static inline FILE *popen_utf8(
const char *command,
const char *type)
186 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
187 return(popen(command,type));
200 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
203 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
206 command_wide=(
wchar_t *) AcquireQuantumMemory(length,
sizeof(*command_wide));
207 if (command_wide == (
wchar_t *) NULL)
209 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
211 file=_wpopen(command_wide,type_wide);
212 command_wide=(
wchar_t *) RelinquishMagickMemory(command_wide);
217 static inline int remove_utf8(
const char *path)
219 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
220 return(unlink(path));
228 path_wide=create_wchar_path(path);
229 if (path_wide == (
wchar_t *) NULL)
231 status=_wremove(path_wide);
232 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
237 static inline int rename_utf8(
const char *source,
const char *destination)
239 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
240 return(rename(source,destination));
249 source_wide=create_wchar_path(source);
250 if (source_wide == (
wchar_t *) NULL)
252 destination_wide=create_wchar_path(destination);
253 if (destination_wide == (
wchar_t *) NULL)
255 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
258 status=_wrename(source_wide,destination_wide);
259 destination_wide=(
wchar_t *) RelinquishMagickMemory(destination_wide);
260 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
265 static inline int stat_utf8(
const char *path,
struct stat *attributes)
267 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
268 return(stat(path,attributes));
276 path_wide=create_wchar_path(path);
277 if (path_wide == (WCHAR *) NULL)
279 status=wstat(path_wide,attributes);
280 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
285 #if defined(__cplusplus) || defined(c_plusplus)