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