18 #ifndef MAGICKCORE_UTILITY_PRIVATE_H
19 #define MAGICKCORE_UTILITY_PRIVATE_H
25 #if defined(__cplusplus) || defined(c_plusplus)
35 #if defined(MAGICKCORE_HAVE_READDIR_R)
36 return(readdir_r(directory,entry,result));
49 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
50 static inline wchar_t *create_wchar_path(
const char *utf8)
58 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
71 if (longPath == (
wchar_t *) NULL)
72 return((
wchar_t *) NULL);
73 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
75 count=GetShortPathNameW(longPath,shortPath,MAX_PATH);
78 return((
wchar_t *) NULL);
80 wcscpy(wideChar,shortPath+4);
84 if (wideChar == (
wchar_t *) NULL)
85 return((
wchar_t *) NULL);
86 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
90 return((
wchar_t *) 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);
116 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__)
117 #define close_utf8 _close
119 #define close_utf8 close
124 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
125 return(fopen(
path,mode));
134 path_wide=create_wchar_path(
path);
135 if (path_wide == (
wchar_t *) NULL)
136 return((FILE *) NULL);
137 mode_wide=create_wchar_path(mode);
138 if (mode_wide == (
wchar_t *) NULL)
141 return((FILE *) NULL);
143 file=_wfopen(path_wide,mode_wide);
152 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
156 directory=getcwd(
path,extent);
163 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,
path,(
int) extent,NULL,NULL);
167 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
174 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
175 return(open(
path,flags,mode));
183 path_wide=create_wchar_path(
path);
184 if (path_wide == (
wchar_t *) NULL)
186 status=_wopen(path_wide,flags,mode);
192 static inline FILE *
popen_utf8(
const char *command,
const char *type)
194 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
195 return(popen(command,type));
204 command_wide=create_wchar_path(command);
205 if (command_wide == (
wchar_t *) NULL)
206 return((FILE *) NULL);
207 type_wide=create_wchar_path(type);
208 if (type_wide == (
wchar_t *) NULL)
211 return((FILE *) NULL);
213 file=_wpopen(command_wide,type_wide);
222 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
223 #if defined(MAGICKCORE_HAVE_REALPATH)
224 if (
path == (
const char *) NULL)
225 return((
char *) NULL);
226 return(realpath(
path,(
char *) NULL));
228 if (
path == (
const char *) NULL)
229 return((
char *) NULL);
252 if (
path == (
const char *) NULL)
253 return((
char *) NULL);
254 length=MultiByteToWideChar(CP_UTF8,0,
path,-1,NULL,0);
256 return((
char *) NULL);
258 if (wide_path == (
wchar_t *) NULL)
259 return((
char *) NULL);
260 MultiByteToWideChar(CP_UTF8,0,
path,-1,wide_path,length);
261 full_path_length=GetFullPathNameW(wide_path,0,NULL,NULL);
262 if (full_path_length == 0)
265 return((
char *) NULL);
268 if (full_path == (
wchar_t *) NULL)
271 return((
char *) NULL);
273 GetFullPathNameW(wide_path,full_path_length,full_path,NULL);
275 file_handle=CreateFileW(full_path,GENERIC_READ,FILE_SHARE_READ |
276 FILE_SHARE_WRITE | FILE_SHARE_DELETE,NULL,OPEN_EXISTING,
277 FILE_FLAG_BACKUP_SEMANTICS,NULL);
278 if (file_handle != INVALID_HANDLE_VALUE)
280 final_path_length=GetFinalPathNameByHandleW(file_handle,NULL,0,
281 FILE_NAME_NORMALIZED);
282 if (final_path_length == 0)
284 CloseHandle(file_handle);
286 return((
char *) NULL);
291 if (full_path == (
wchar_t *) NULL)
293 CloseHandle(file_handle);
294 return((
char *) NULL);
296 GetFinalPathNameByHandleW(file_handle,full_path,final_path_length,
297 FILE_NAME_NORMALIZED);
298 CloseHandle(file_handle);
300 clean_path=full_path;
301 if (wcsncmp(full_path,L
"\\\\?\\",4) == 0)
302 clean_path=full_path+4;
303 utf8_length=WideCharToMultiByte(CP_UTF8,0,clean_path,-1,NULL,0,NULL,NULL);
304 if (utf8_length <= 0)
307 return((
char *) NULL);
310 if (real_path == (
char *) NULL)
313 return((
char *) NULL);
315 WideCharToMultiByte(CP_UTF8,0,clean_path,-1,real_path,utf8_length,NULL,NULL);
323 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
324 return(unlink(
path));
332 path_wide=create_wchar_path(
path);
333 if (path_wide == (
wchar_t *) NULL)
335 status=_wremove(path_wide);
341 static inline int rename_utf8(
const char *source,
const char *destination)
343 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
344 return(rename(source,destination));
353 source_wide=create_wchar_path(source);
354 if (source_wide == (
wchar_t *) NULL)
356 destination_wide=create_wchar_path(destination);
357 if (destination_wide == (
wchar_t *) NULL)
362 status=_wrename(source_wide,destination_wide);
371 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
372 return(stat(
path,attributes));
380 path_wide=create_wchar_path(
path);
381 if (path_wide == (WCHAR *) NULL)
383 status=wstat(path_wide,attributes);
389 #if defined(__cplusplus) || defined(c_plusplus)