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