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