cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 1 | /* |
cristy | fe676ee | 2013-11-18 13:03:38 +0000 | [diff] [blame] | 2 | Copyright 1999-2014 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 | |
cristy | 0740a98 | 2011-10-13 15:01:01 +0000 | [diff] [blame] | 21 | #include "MagickCore/memory_.h" |
cristy | fd84e3c | 2012-02-20 17:50:55 +0000 | [diff] [blame] | 22 | #include "MagickCore/nt-base.h" |
cristy | d2d11ec | 2012-03-28 13:53:49 +0000 | [diff] [blame] | 23 | #include "MagickCore/nt-base-private.h" |
cristy | 0740a98 | 2011-10-13 15:01:01 +0000 | [diff] [blame] | 24 | |
cristy | 6398ec7 | 2013-11-28 02:00:27 +0000 | [diff] [blame] | 25 | #if defined(__cplusplus) || defined(c_plusplus) |
| 26 | extern "C" { |
| 27 | #endif |
| 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 | |
dirk | b74b286 | 2014-03-08 11:36:42 +0000 | [diff] [blame] | 49 | #if defined(MAGICKCORE_WINDOWS_SUPPORT) |
| 50 | static inline wchar_t *create_wchar_path(const char *utf8) |
| 51 | { |
| 52 | int |
| 53 | count; |
| 54 | |
| 55 | wchar_t |
| 56 | *wideChar; |
| 57 | |
| 58 | count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0); |
| 59 | if (count > MAX_PATH) |
| 60 | { |
| 61 | char |
| 62 | buffer[MaxTextExtent]; |
| 63 | |
| 64 | wchar_t |
| 65 | shortPath[MAX_PATH], |
| 66 | *longPath; |
| 67 | |
| 68 | (void) FormatLocaleString(buffer,MaxTextExtent,"\\\\?\\%s",utf8); |
| 69 | count+=4; |
| 70 | longPath=(wchar_t *) AcquireQuantumMemory(count,sizeof(*longPath)); |
| 71 | if (longPath == (wchar_t *) NULL) |
| 72 | return((wchar_t *) NULL); |
| 73 | count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count); |
| 74 | if (count != 0) |
| 75 | count=GetShortPathNameW(longPath,shortPath,MAX_PATH); |
| 76 | longPath=(wchar_t *) RelinquishMagickMemory(longPath); |
| 77 | if (count < 5) |
| 78 | return((wchar_t *) NULL); |
| 79 | wideChar=(wchar_t *) AcquireQuantumMemory(count-3,sizeof(*wideChar)); |
| 80 | wcscpy(wideChar,shortPath+4); |
| 81 | return(wideChar); |
| 82 | } |
| 83 | wideChar=(wchar_t *) AcquireQuantumMemory(count,sizeof(*wideChar)); |
| 84 | if (wideChar == (wchar_t *) NULL) |
| 85 | return((wchar_t *) NULL); |
| 86 | count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count); |
| 87 | if (count == 0) |
| 88 | { |
| 89 | wideChar=(wchar_t *) RelinquishMagickMemory(wideChar); |
| 90 | return((wchar_t *) NULL); |
| 91 | } |
| 92 | return(wideChar); |
| 93 | } |
| 94 | #endif |
| 95 | |
| 96 | static inline int access_utf8(const char *path,int mode) |
| 97 | { |
cristy | b3a6791 | 2014-06-23 11:12:24 +0000 | [diff] [blame] | 98 | #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) |
dirk | b74b286 | 2014-03-08 11:36:42 +0000 | [diff] [blame] | 99 | return(access(path,mode)); |
| 100 | #else |
| 101 | int |
| 102 | status; |
| 103 | |
| 104 | wchar_t |
| 105 | *path_wide; |
| 106 | |
| 107 | path_wide=create_wchar_path(path); |
| 108 | if (path_wide == (wchar_t *) NULL) |
| 109 | return(-1); |
| 110 | status=_waccess(path_wide,mode); |
| 111 | path_wide=(wchar_t *) RelinquishMagickMemory(path_wide); |
| 112 | return(status); |
| 113 | #endif |
| 114 | } |
| 115 | |
| 116 | static inline FILE *fopen_utf8(const char *path,const char *mode) |
| 117 | { |
| 118 | #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) |
| 119 | return(fopen(path,mode)); |
| 120 | #else |
| 121 | FILE |
| 122 | *file; |
| 123 | |
| 124 | wchar_t |
| 125 | *mode_wide, |
| 126 | *path_wide; |
| 127 | |
| 128 | path_wide=create_wchar_path(path); |
| 129 | if (path_wide == (wchar_t *) NULL) |
| 130 | return((FILE *) NULL); |
| 131 | mode_wide=create_wchar_path(mode); |
| 132 | if (mode_wide == (wchar_t *) NULL) |
| 133 | { |
| 134 | path_wide=(wchar_t *) RelinquishMagickMemory(path_wide); |
| 135 | return((FILE *) NULL); |
| 136 | } |
| 137 | file=_wfopen(path_wide,mode_wide); |
| 138 | mode_wide=(wchar_t *) RelinquishMagickMemory(mode_wide); |
| 139 | path_wide=(wchar_t *) RelinquishMagickMemory(path_wide); |
| 140 | return(file); |
| 141 | #endif |
| 142 | } |
| 143 | |
| 144 | #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__) |
| 145 | typedef int |
| 146 | mode_t; |
| 147 | #endif |
| 148 | |
| 149 | static inline int open_utf8(const char *path,int flags,mode_t mode) |
| 150 | { |
| 151 | #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) |
| 152 | return(open(path,flags,mode)); |
| 153 | #else |
| 154 | int |
| 155 | status; |
| 156 | |
| 157 | wchar_t |
| 158 | *path_wide; |
| 159 | |
| 160 | path_wide=create_wchar_path(path); |
| 161 | if (path_wide == (wchar_t *) NULL) |
| 162 | return(-1); |
| 163 | status=_wopen(path_wide,flags,mode); |
| 164 | path_wide=(wchar_t *) RelinquishMagickMemory(path_wide); |
| 165 | return(status); |
| 166 | #endif |
| 167 | } |
| 168 | |
| 169 | static inline FILE *popen_utf8(const char *command,const char *type) |
| 170 | { |
| 171 | #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) |
| 172 | return(popen(command,type)); |
| 173 | #else |
| 174 | FILE |
| 175 | *file; |
| 176 | |
| 177 | wchar_t |
| 178 | *type_wide, |
| 179 | *command_wide; |
| 180 | |
| 181 | command_wide=create_wchar_path(command); |
| 182 | if (command_wide == (wchar_t *) NULL) |
| 183 | return((FILE *) NULL); |
| 184 | type_wide=create_wchar_path(type); |
| 185 | if (type_wide == (wchar_t *) NULL) |
| 186 | { |
| 187 | command_wide=(wchar_t *) RelinquishMagickMemory(command_wide); |
| 188 | return((FILE *) NULL); |
| 189 | } |
| 190 | file=_wpopen(command_wide,type_wide); |
| 191 | type_wide=(wchar_t *) RelinquishMagickMemory(type_wide); |
| 192 | command_wide=(wchar_t *) RelinquishMagickMemory(command_wide); |
| 193 | return(file); |
| 194 | #endif |
| 195 | } |
| 196 | |
| 197 | static inline int remove_utf8(const char *path) |
| 198 | { |
| 199 | #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) |
| 200 | return(unlink(path)); |
| 201 | #else |
| 202 | int |
| 203 | status; |
| 204 | |
| 205 | wchar_t |
| 206 | *path_wide; |
| 207 | |
| 208 | path_wide=create_wchar_path(path); |
| 209 | if (path_wide == (wchar_t *) NULL) |
| 210 | return(-1); |
| 211 | status=_wremove(path_wide); |
| 212 | path_wide=(wchar_t *) RelinquishMagickMemory(path_wide); |
| 213 | return(status); |
| 214 | #endif |
| 215 | } |
| 216 | |
| 217 | static inline int rename_utf8(const char *source,const char *destination) |
| 218 | { |
| 219 | #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) |
| 220 | return(rename(source,destination)); |
| 221 | #else |
| 222 | int |
| 223 | status; |
| 224 | |
| 225 | wchar_t |
| 226 | *destination_wide, |
| 227 | *source_wide; |
| 228 | |
| 229 | source_wide=create_wchar_path(source); |
| 230 | if (source_wide == (wchar_t *) NULL) |
| 231 | return(-1); |
| 232 | destination_wide=create_wchar_path(destination); |
| 233 | if (destination_wide == (wchar_t *) NULL) |
| 234 | { |
| 235 | source_wide=(wchar_t *) RelinquishMagickMemory(source_wide); |
| 236 | return(-1); |
| 237 | } |
| 238 | status=_wrename(source_wide,destination_wide); |
| 239 | destination_wide=(wchar_t *) RelinquishMagickMemory(destination_wide); |
| 240 | source_wide=(wchar_t *) RelinquishMagickMemory(source_wide); |
| 241 | return(status); |
| 242 | #endif |
| 243 | } |
| 244 | |
| 245 | static inline int stat_utf8(const char *path,struct stat *attributes) |
| 246 | { |
| 247 | #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) |
| 248 | return(stat(path,attributes)); |
| 249 | #else |
| 250 | int |
| 251 | status; |
| 252 | |
| 253 | wchar_t |
| 254 | *path_wide; |
| 255 | |
| 256 | path_wide=create_wchar_path(path); |
| 257 | if (path_wide == (WCHAR *) NULL) |
| 258 | return(-1); |
| 259 | status=wstat(path_wide,attributes); |
| 260 | path_wide=(WCHAR *) RelinquishMagickMemory(path_wide); |
| 261 | return(status); |
| 262 | #endif |
| 263 | } |
| 264 | |
| 265 | #if defined(__cplusplus) || defined(c_plusplus) |
| 266 | } |
| 267 | #endif |
| 268 | |
| 269 | #endif |