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