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 | 7c63cd3 | 2014-03-08 08:40:20 +0000 | [diff] [blame^] | 49 | #if defined(MAGICKCORE_WINDOWS_SUPPORT)
|
| 50 | static inline wchar_t *create_wchar_string(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 | wideChar=(WCHAR *) AcquireQuantumMemory(count,sizeof(*wideChar));
|
| 60 | if (wideChar == (WCHAR *) NULL)
|
| 61 | return((WCHAR *) NULL);
|
| 62 | count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
|
| 63 | if (count == 0)
|
| 64 | {
|
| 65 | wideChar=(WCHAR *) RelinquishMagickMemory(wideChar);
|
| 66 | return((WCHAR *) NULL);
|
| 67 | }
|
| 68 | return(wideChar);
|
| 69 | }
|
| 70 |
|
| 71 | static inline char *create_utf8_string(const wchar_t *wideChar)
|
| 72 | {
|
| 73 | char
|
| 74 | *utf8;
|
| 75 |
|
| 76 | int
|
| 77 | count;
|
| 78 |
|
| 79 | count=WideCharToMultiByte(CP_UTF8,0,wideChar,-1,NULL,0,NULL,NULL);
|
| 80 | if (count < 0)
|
| 81 | return((char *) NULL);
|
| 82 | utf8=(char *) AcquireQuantumMemory(count+1,sizeof(*utf8));
|
| 83 | if (utf8 == (char *) NULL)
|
| 84 | return((char *) NULL);
|
| 85 | count=WideCharToMultiByte(CP_UTF8,0,wideChar,-1,utf8,count,NULL,NULL);
|
| 86 | if (count == 0)
|
| 87 | {
|
| 88 | utf8=DestroyString(utf8);
|
| 89 | return((char *) NULL);
|
| 90 | }
|
| 91 | utf8[count]=0;
|
| 92 | return(utf8);
|
| 93 | }
|
| 94 | #endif
|
| 95 |
|
| 96 | static inline int access_utf8(const char *path,int mode)
|
| 97 | {
|
| 98 | #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
|
| 99 | return(access(path,mode));
|
| 100 | #else
|
| 101 | int
|
| 102 | status;
|
| 103 |
|
| 104 | wchar_t
|
| 105 | *path_wide;
|
| 106 |
|
| 107 | path_wide=create_wchar_string(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_string(path);
|
| 129 | if (path_wide == (wchar_t *) NULL)
|
| 130 | return((FILE *) NULL);
|
| 131 | mode_wide=create_wchar_string(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_string(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_string(command);
|
| 182 | if (command_wide == (wchar_t *) NULL)
|
| 183 | return((FILE *) NULL);
|
| 184 | type_wide=create_wchar_string(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_string(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_string(source);
|
| 230 | if (source_wide == (wchar_t *) NULL)
|
| 231 | return(-1);
|
| 232 | destination_wide=create_wchar_string(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_string(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
|