2 Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization
3 dedicated to making software imaging solutions freely available.
5 You may not use this file except in compliance with the License.
6 obtain a copy of the License at
8 http://www.imagemagick.org/script/license.php
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
16 MagickCore private utility methods.
18 #ifndef _MAGICKCORE_UTILITY_PRIVATE_H
19 #define _MAGICKCORE_UTILITY_PRIVATE_H
21 #include "MagickCore/memory_.h"
22 #include "MagickCore/nt-base.h"
23 #include "MagickCore/nt-base-private.h"
25 #if defined(__cplusplus) || defined(c_plusplus)
29 extern MagickPrivate char
30 **GetPathComponents(const char *,size_t *),
31 **ListFiles(const char *,const char *,size_t *);
33 extern MagickPrivate MagickBooleanType
34 GetExecutionPath(char *,const size_t),
35 ShredFile(const char *);
37 extern MagickPrivate ssize_t
38 GetMagickPageSize(void);
40 extern MagickPrivate void
41 ChopPathComponents(char *,const size_t),
42 ExpandFilename(char *),
43 MagickDelay(const MagickSizeType);
46 Windows UTF8 compatibility methods.
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);
62 buffer[MaxTextExtent];
68 (void) FormatLocaleString(buffer,MaxTextExtent,"\\\\?\\%s",utf8);
70 longPath=(wchar_t *) AcquireQuantumMemory(count,sizeof(*longPath));
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);
76 longPath=(wchar_t *) RelinquishMagickMemory(longPath);
78 return((wchar_t *) NULL);
79 wideChar=(wchar_t *) AcquireQuantumMemory(count-3,sizeof(*wideChar));
80 wcscpy(wideChar,shortPath+4);
83 wideChar=(wchar_t *) AcquireQuantumMemory(count,sizeof(*wideChar));
84 if (wideChar == (wchar_t *) NULL)
85 return((wchar_t *) NULL);
86 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
89 wideChar=(wchar_t *) RelinquishMagickMemory(wideChar);
90 return((wchar_t *) NULL);
96 static inline int access_utf8(const char *path,int mode)
98 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
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__) || defined(__MINGW32__) || defined(__MINGW64__)
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 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__)
149 static inline int open_utf8(const char *path,int flags,mode_t mode)
151 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
152 return(open(path,flags,mode));
160 path_wide=create_wchar_path(path);
161 if (path_wide == (wchar_t *) NULL)
163 status=_wopen(path_wide,flags,mode);
164 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
169 static inline FILE *popen_utf8(const char *command,const char *type)
171 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
172 return(popen(command,type));
181 command_wide=create_wchar_path(command);
182 if (command_wide == (wchar_t *) NULL)
183 return((FILE *) NULL);
184 type_wide=create_wchar_path(type);
185 if (type_wide == (wchar_t *) NULL)
187 command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
188 return((FILE *) NULL);
190 file=_wpopen(command_wide,type_wide);
191 type_wide=(wchar_t *) RelinquishMagickMemory(type_wide);
192 command_wide=(wchar_t *) RelinquishMagickMemory(command_wide);
197 static inline int remove_utf8(const char *path)
199 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
200 return(unlink(path));
208 path_wide=create_wchar_path(path);
209 if (path_wide == (wchar_t *) NULL)
211 status=_wremove(path_wide);
212 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
217 static inline int rename_utf8(const char *source,const char *destination)
219 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
220 return(rename(source,destination));
229 source_wide=create_wchar_path(source);
230 if (source_wide == (wchar_t *) NULL)
232 destination_wide=create_wchar_path(destination);
233 if (destination_wide == (wchar_t *) NULL)
235 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
238 status=_wrename(source_wide,destination_wide);
239 destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide);
240 source_wide=(wchar_t *) RelinquishMagickMemory(source_wide);
245 static inline int stat_utf8(const char *path,struct stat *attributes)
247 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
248 return(stat(path,attributes));
256 path_wide=create_wchar_path(path);
257 if (path_wide == (WCHAR *) NULL)
259 status=wstat(path_wide,attributes);
260 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
265 #if defined(__cplusplus) || defined(c_plusplus)