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);
118 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
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)
135 return((FILE *) NULL);
137 file=_wfopen(path_wide,mode_wide);
146 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
150 directory=getcwd(
path,extent);
157 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,
path,(
int) extent,NULL,NULL);
161 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
168 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
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);
186 static inline FILE *
popen_utf8(
const char *command,
const char *type)
188 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
189 return(popen(command,type));
198 command_wide=create_wchar_path(command);
199 if (command_wide == (
wchar_t *) NULL)
200 return((FILE *) NULL);
201 type_wide=create_wchar_path(type);
202 if (type_wide == (
wchar_t *) NULL)
205 return((FILE *) NULL);
207 file=_wpopen(command_wide,type_wide);
216 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
217 #if defined(MAGICKCORE_HAVE_REALPATH)
218 if (
path == (
const char *) NULL)
219 return((
char *) NULL);
220 return(realpath(
path,(
char *) NULL));
222 if (
path == (
const char *) NULL)
223 return((
char *) NULL);
246 if (
path == (
const char *) NULL)
247 return((
char *) NULL);
248 length=MultiByteToWideChar(CP_UTF8,0,
path,-1,NULL,0);
250 return((
char *) NULL);
252 if (wide_path == (
wchar_t *) NULL)
253 return((
char *) NULL);
254 MultiByteToWideChar(CP_UTF8,0,
path,-1,wide_path,length);
255 full_path_length=GetFullPathNameW(wide_path,0,NULL,NULL);
256 if (full_path_length == 0)
259 return((
char *) NULL);
262 if (full_path == (
wchar_t *) NULL)
265 return((
char *) NULL);
267 GetFullPathNameW(wide_path,full_path_length,full_path,NULL);
269 file_handle=CreateFileW(full_path,GENERIC_READ,FILE_SHARE_READ |
270 FILE_SHARE_WRITE | FILE_SHARE_DELETE,NULL,OPEN_EXISTING,
271 FILE_FLAG_BACKUP_SEMANTICS,NULL);
272 if (file_handle != INVALID_HANDLE_VALUE)
274 final_path_length=GetFinalPathNameByHandleW(file_handle,NULL,0,
275 FILE_NAME_NORMALIZED);
276 if (final_path_length == 0)
278 CloseHandle(file_handle);
280 return((
char *) NULL);
285 if (full_path == (
wchar_t *) NULL)
287 CloseHandle(file_handle);
288 return((
char *) NULL);
290 GetFinalPathNameByHandleW(file_handle,full_path,final_path_length,
291 FILE_NAME_NORMALIZED);
292 CloseHandle(file_handle);
294 clean_path=full_path;
295 if (wcsncmp(full_path,L
"\\\\?\\",4) == 0)
296 clean_path=full_path+4;
297 utf8_length=WideCharToMultiByte(CP_UTF8,0,clean_path,-1,NULL,0,NULL,NULL);
298 if (utf8_length <= 0)
301 return((
char *) NULL);
304 if (real_path == (
char *) NULL)
307 return((
char *) NULL);
309 WideCharToMultiByte(CP_UTF8,0,clean_path,-1,real_path,utf8_length,NULL,NULL);
317 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
318 return(unlink(
path));
326 path_wide=create_wchar_path(
path);
327 if (path_wide == (
wchar_t *) NULL)
329 status=_wremove(path_wide);
335 static inline int rename_utf8(
const char *source,
const char *destination)
337 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
338 return(rename(source,destination));
347 source_wide=create_wchar_path(source);
348 if (source_wide == (
wchar_t *) NULL)
350 destination_wide=create_wchar_path(destination);
351 if (destination_wide == (
wchar_t *) NULL)
356 status=_wrename(source_wide,destination_wide);
365 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
366 return(stat(
path,attributes));
374 path_wide=create_wchar_path(
path);
375 if (path_wide == (WCHAR *) NULL)
377 status=wstat(path_wide,attributes);
383 #if defined(__cplusplus) || defined(c_plusplus)