cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 1 | /* |
cristy | 45ef08f | 2012-12-07 13:13:34 +0000 | [diff] [blame] | 2 | Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 3 | dedicated to making software imaging solutions freely available. |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 4 | |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 5 | You may not use this file except in compliance with the License. |
| 6 | obtain a copy of the License at |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 7 | |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 8 | http://www.imagemagick.org/script/license.php |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 9 | |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 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. |
| 15 | |
| 16 | MagickCore private utility methods. |
| 17 | */ |
| 18 | #ifndef _MAGICKCORE_UTILITY_PRIVATE_H |
| 19 | #define _MAGICKCORE_UTILITY_PRIVATE_H |
| 20 | |
| 21 | #if defined(__cplusplus) || defined(c_plusplus) |
| 22 | extern "C" { |
| 23 | #endif |
| 24 | |
cristy | 0740a98 | 2011-10-13 15:01:01 +0000 | [diff] [blame] | 25 | #include "MagickCore/memory_.h" |
cristy | fd84e3c | 2012-02-20 17:50:55 +0000 | [diff] [blame] | 26 | #include "MagickCore/nt-base.h" |
cristy | d2d11ec | 2012-03-28 13:53:49 +0000 | [diff] [blame] | 27 | #include "MagickCore/nt-base-private.h" |
cristy | 0740a98 | 2011-10-13 15:01:01 +0000 | [diff] [blame] | 28 | |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 29 | extern MagickPrivate char |
| 30 | **GetPathComponents(const char *,size_t *), |
| 31 | **ListFiles(const char *,const char *,size_t *); |
| 32 | |
| 33 | extern MagickPrivate MagickBooleanType |
cristy | 5986456 | 2013-04-18 11:47:41 +0000 | [diff] [blame] | 34 | GetExecutionPath(char *,const size_t), |
| 35 | ShredFile(const char *); |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 36 | |
| 37 | extern MagickPrivate ssize_t |
| 38 | GetMagickPageSize(void); |
| 39 | |
| 40 | extern MagickPrivate void |
| 41 | ChopPathComponents(char *,const size_t), |
| 42 | ExpandFilename(char *), |
| 43 | MagickDelay(const MagickSizeType); |
| 44 | |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 45 | /* |
| 46 | Windows UTF8 compatibility methods. |
| 47 | */ |
| 48 | |
cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 49 | static inline int access_utf8(const char *path,int mode) |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 50 | { |
cristy | 07a3cca | 2012-12-10 13:09:10 +0000 | [diff] [blame] | 51 | #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 52 | return(access(path,mode)); |
| 53 | #else |
| 54 | int |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 55 | count, |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 56 | status; |
| 57 | |
cristy | d9b279b | 2012-02-11 01:34:21 +0000 | [diff] [blame] | 58 | WCHAR |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 59 | *path_wide; |
cristy | d9b279b | 2012-02-11 01:34:21 +0000 | [diff] [blame] | 60 | |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 61 | path_wide=(WCHAR *) NULL; |
| 62 | count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0); |
| 63 | path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide)); |
| 64 | if (path_wide == (WCHAR *) NULL) |
| 65 | return(-1); |
| 66 | count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count); |
| 67 | status=_waccess(path_wide,mode); |
| 68 | path_wide=(WCHAR *) RelinquishMagickMemory(path_wide); |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 69 | return(status); |
| 70 | #endif |
| 71 | } |
| 72 | |
| 73 | static inline FILE *fopen_utf8(const char *path,const char *mode) |
| 74 | { |
cristy | 07a3cca | 2012-12-10 13:09:10 +0000 | [diff] [blame] | 75 | #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 76 | return(fopen(path,mode)); |
| 77 | #else |
| 78 | FILE |
| 79 | *file; |
| 80 | |
cristy | f07b085 | 2011-09-23 21:36:56 +0000 | [diff] [blame] | 81 | int |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 82 | count; |
cristy | f07b085 | 2011-09-23 21:36:56 +0000 | [diff] [blame] | 83 | |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 84 | WCHAR |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 85 | *mode_wide, |
| 86 | *path_wide; |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 87 | |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 88 | path_wide=(WCHAR *) NULL; |
| 89 | count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0); |
| 90 | path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide)); |
| 91 | if (path_wide == (WCHAR *) NULL) |
cristy | f07b085 | 2011-09-23 21:36:56 +0000 | [diff] [blame] | 92 | return((FILE *) NULL); |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 93 | count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count); |
| 94 | count=MultiByteToWideChar(CP_UTF8,0,mode,-1,NULL,0); |
| 95 | mode_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*mode_wide)); |
| 96 | if (mode_wide == (WCHAR *) NULL) |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 97 | { |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 98 | path_wide=(WCHAR *) RelinquishMagickMemory(path_wide); |
cristy | f07b085 | 2011-09-23 21:36:56 +0000 | [diff] [blame] | 99 | return((FILE *) NULL); |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 100 | } |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 101 | count=MultiByteToWideChar(CP_UTF8,0,mode,-1,mode_wide,count); |
| 102 | file=_wfopen(path_wide,mode_wide); |
| 103 | mode_wide=(WCHAR *) RelinquishMagickMemory(mode_wide); |
| 104 | path_wide=(WCHAR *) RelinquishMagickMemory(path_wide); |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 105 | return(file); |
| 106 | #endif |
| 107 | } |
| 108 | |
cristy | 277f3a2 | 2012-12-10 13:18:45 +0000 | [diff] [blame] | 109 | #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__) |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 110 | typedef int |
| 111 | mode_t; |
| 112 | #endif |
| 113 | |
cristy | 9e0719b | 2011-12-29 03:45:45 +0000 | [diff] [blame] | 114 | static inline int open_utf8(const char *path,int flags,mode_t mode) |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 115 | { |
cristy | 07a3cca | 2012-12-10 13:09:10 +0000 | [diff] [blame] | 116 | #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 117 | return(open(path,flags,mode)); |
| 118 | #else |
| 119 | int |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 120 | count, |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 121 | status; |
| 122 | |
cristy | d9b279b | 2012-02-11 01:34:21 +0000 | [diff] [blame] | 123 | WCHAR |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 124 | *path_wide; |
cristy | d9b279b | 2012-02-11 01:34:21 +0000 | [diff] [blame] | 125 | |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 126 | path_wide=(WCHAR *) NULL; |
| 127 | count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0); |
| 128 | path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide)); |
| 129 | if (path_wide == (WCHAR *) NULL) |
| 130 | return(-1); |
| 131 | count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count); |
| 132 | status=_wopen(path_wide,flags,mode); |
| 133 | path_wide=(WCHAR *) RelinquishMagickMemory(path_wide); |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 134 | return(status); |
| 135 | #endif |
| 136 | } |
| 137 | |
cristy | 18c6c27 | 2011-09-23 14:40:37 +0000 | [diff] [blame] | 138 | static inline FILE *popen_utf8(const char *command,const char *type) |
| 139 | { |
cristy | 07a3cca | 2012-12-10 13:09:10 +0000 | [diff] [blame] | 140 | #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 141 | return(popen(command,type)); |
cristy | 18c6c27 | 2011-09-23 14:40:37 +0000 | [diff] [blame] | 142 | #else |
| 143 | FILE |
| 144 | *file; |
| 145 | |
cristy | f07b085 | 2011-09-23 21:36:56 +0000 | [diff] [blame] | 146 | int |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 147 | count; |
cristy | f07b085 | 2011-09-23 21:36:56 +0000 | [diff] [blame] | 148 | |
cristy | 18c6c27 | 2011-09-23 14:40:37 +0000 | [diff] [blame] | 149 | WCHAR |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 150 | *type_wide, |
| 151 | *command_wide; |
cristy | 18c6c27 | 2011-09-23 14:40:37 +0000 | [diff] [blame] | 152 | |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 153 | command_wide=(WCHAR *) NULL; |
| 154 | count=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0); |
| 155 | command_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*command_wide)); |
| 156 | if (command_wide == (WCHAR *) NULL) |
cristy | f07b085 | 2011-09-23 21:36:56 +0000 | [diff] [blame] | 157 | return((FILE *) NULL); |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 158 | count=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,count); |
| 159 | count=MultiByteToWideChar(CP_UTF8,0,type,-1,NULL,0); |
| 160 | type_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*type_wide)); |
| 161 | if (type_wide == (WCHAR *) NULL) |
cristy | 18c6c27 | 2011-09-23 14:40:37 +0000 | [diff] [blame] | 162 | { |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 163 | command_wide=(WCHAR *) RelinquishMagickMemory(command_wide); |
cristy | f07b085 | 2011-09-23 21:36:56 +0000 | [diff] [blame] | 164 | return((FILE *) NULL); |
cristy | 18c6c27 | 2011-09-23 14:40:37 +0000 | [diff] [blame] | 165 | } |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 166 | count=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,count); |
| 167 | file=_wpopen(command_wide,type_wide); |
| 168 | type_wide=(WCHAR *) RelinquishMagickMemory(type_wide); |
| 169 | command_wide=(WCHAR *) RelinquishMagickMemory(command_wide); |
cristy | 18c6c27 | 2011-09-23 14:40:37 +0000 | [diff] [blame] | 170 | return(file); |
| 171 | #endif |
| 172 | } |
| 173 | |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 174 | static inline int remove_utf8(const char *path) |
| 175 | { |
cristy | 07a3cca | 2012-12-10 13:09:10 +0000 | [diff] [blame] | 176 | #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 177 | return(unlink(path)); |
| 178 | #else |
| 179 | int |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 180 | count, |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 181 | status; |
| 182 | |
cristy | d9b279b | 2012-02-11 01:34:21 +0000 | [diff] [blame] | 183 | WCHAR |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 184 | *path_wide; |
cristy | d9b279b | 2012-02-11 01:34:21 +0000 | [diff] [blame] | 185 | |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 186 | path_wide=(WCHAR *) NULL; |
| 187 | count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0); |
| 188 | path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide)); |
| 189 | if (path_wide == (WCHAR *) NULL) |
| 190 | return(-1); |
| 191 | count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count); |
| 192 | status=_wremove(path_wide); |
| 193 | path_wide=(WCHAR *) RelinquishMagickMemory(path_wide); |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 194 | return(status); |
| 195 | #endif |
| 196 | } |
| 197 | |
cristy | 320684d | 2011-09-23 14:55:47 +0000 | [diff] [blame] | 198 | static inline int rename_utf8(const char *source,const char *destination) |
| 199 | { |
cristy | 07a3cca | 2012-12-10 13:09:10 +0000 | [diff] [blame] | 200 | #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) |
cristy | 320684d | 2011-09-23 14:55:47 +0000 | [diff] [blame] | 201 | return(rename(source,destination)); |
| 202 | #else |
cristy | 320684d | 2011-09-23 14:55:47 +0000 | [diff] [blame] | 203 | int |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 204 | count, |
cristy | 320684d | 2011-09-23 14:55:47 +0000 | [diff] [blame] | 205 | status; |
| 206 | |
cristy | d9b279b | 2012-02-11 01:34:21 +0000 | [diff] [blame] | 207 | WCHAR |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 208 | *destination_wide, |
| 209 | *source_wide; |
cristy | d9b279b | 2012-02-11 01:34:21 +0000 | [diff] [blame] | 210 | |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 211 | source_wide=(WCHAR *) NULL; |
| 212 | count=MultiByteToWideChar(CP_UTF8,0,source,-1,NULL,0); |
| 213 | source_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*source_wide)); |
| 214 | if (source_wide == (WCHAR *) NULL) |
| 215 | return(-1); |
| 216 | count=MultiByteToWideChar(CP_UTF8,0,source,-1,source_wide,count); |
| 217 | count=MultiByteToWideChar(CP_UTF8,0,destination,-1,NULL,0); |
| 218 | destination_wide=(WCHAR *) AcquireQuantumMemory(count, |
| 219 | sizeof(*destination_wide)); |
| 220 | if (destination_wide == (WCHAR *) NULL) |
cristy | 320684d | 2011-09-23 14:55:47 +0000 | [diff] [blame] | 221 | { |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 222 | source_wide=(WCHAR *) RelinquishMagickMemory(source_wide); |
| 223 | return(-1); |
cristy | 320684d | 2011-09-23 14:55:47 +0000 | [diff] [blame] | 224 | } |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 225 | count=MultiByteToWideChar(CP_UTF8,0,destination,-1,destination_wide,count); |
| 226 | status=_wrename(source_wide,destination_wide); |
| 227 | destination_wide=(WCHAR *) RelinquishMagickMemory(destination_wide); |
| 228 | source_wide=(WCHAR *) RelinquishMagickMemory(source_wide); |
cristy | 320684d | 2011-09-23 14:55:47 +0000 | [diff] [blame] | 229 | return(status); |
| 230 | #endif |
| 231 | } |
| 232 | |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 233 | static inline int stat_utf8(const char *path,struct stat *attributes) |
| 234 | { |
cristy | 07a3cca | 2012-12-10 13:09:10 +0000 | [diff] [blame] | 235 | #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 236 | return(stat(path,attributes)); |
| 237 | #else |
| 238 | int |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 239 | count, |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 240 | status; |
| 241 | |
cristy | d9b279b | 2012-02-11 01:34:21 +0000 | [diff] [blame] | 242 | WCHAR |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 243 | *path_wide; |
cristy | d9b279b | 2012-02-11 01:34:21 +0000 | [diff] [blame] | 244 | |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 245 | path_wide=(WCHAR *) NULL; |
| 246 | count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0); |
| 247 | path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide)); |
| 248 | if (path_wide == (WCHAR *) NULL) |
| 249 | return(-1); |
| 250 | count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count); |
dirk | 94041a9 | 2013-09-06 04:56:30 +0000 | [diff] [blame] | 251 | status=wstat(path_wide,attributes); |
cristy | 1b4c17f | 2012-02-12 15:48:43 +0000 | [diff] [blame] | 252 | path_wide=(WCHAR *) RelinquishMagickMemory(path_wide); |
cristy | 99d6fa0 | 2011-09-23 13:09:30 +0000 | [diff] [blame] | 253 | return(status); |
| 254 | #endif |
| 255 | } |
| 256 | |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 257 | #if defined(__cplusplus) || defined(c_plusplus) |
| 258 | } |
| 259 | #endif |
| 260 | |
| 261 | #endif |