blob: 5afc1cd58f12b7c69bfea74b9bd5adb5c2b7dba0 [file] [log] [blame]
cristyd1dd6e42011-09-04 01:46:08 +00001/*
cristy1454be72011-12-19 01:52:48 +00002 Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization
cristyd1dd6e42011-09-04 01:46:08 +00003 dedicated to making software imaging solutions freely available.
cristy99d6fa02011-09-23 13:09:30 +00004
cristyd1dd6e42011-09-04 01:46:08 +00005 You may not use this file except in compliance with the License.
6 obtain a copy of the License at
cristy99d6fa02011-09-23 13:09:30 +00007
cristyd1dd6e42011-09-04 01:46:08 +00008 http://www.imagemagick.org/script/license.php
cristy99d6fa02011-09-23 13:09:30 +00009
cristyd1dd6e42011-09-04 01:46:08 +000010 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)
22extern "C" {
23#endif
24
cristy0740a982011-10-13 15:01:01 +000025#include "MagickCore/memory_.h"
cristyfd84e3c2012-02-20 17:50:55 +000026#include "MagickCore/nt-base.h"
cristy0740a982011-10-13 15:01:01 +000027
cristyd1dd6e42011-09-04 01:46:08 +000028extern MagickPrivate char
29 **GetPathComponents(const char *,size_t *),
30 **ListFiles(const char *,const char *,size_t *);
31
32extern MagickPrivate MagickBooleanType
33 GetExecutionPath(char *,const size_t);
34
35extern MagickPrivate ssize_t
36 GetMagickPageSize(void);
37
38extern MagickPrivate void
39 ChopPathComponents(char *,const size_t),
40 ExpandFilename(char *),
41 MagickDelay(const MagickSizeType);
42
cristy99d6fa02011-09-23 13:09:30 +000043/*
44 Windows UTF8 compatibility methods.
45*/
46
cristye42f6582012-02-11 17:59:50 +000047static inline int access_utf8(const char *path,int mode)
cristy99d6fa02011-09-23 13:09:30 +000048{
49#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
50 return(access(path,mode));
51#else
52 int
cristy1b4c17f2012-02-12 15:48:43 +000053 count,
cristy99d6fa02011-09-23 13:09:30 +000054 status;
55
cristyd9b279b2012-02-11 01:34:21 +000056 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +000057 *path_wide;
cristyd9b279b2012-02-11 01:34:21 +000058
cristy1b4c17f2012-02-12 15:48:43 +000059 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);
66 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristy99d6fa02011-09-23 13:09:30 +000067 return(status);
68#endif
69}
70
71static 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
cristyf07b0852011-09-23 21:36:56 +000079 int
cristy1b4c17f2012-02-12 15:48:43 +000080 count;
cristyf07b0852011-09-23 21:36:56 +000081
cristy99d6fa02011-09-23 13:09:30 +000082 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +000083 *mode_wide,
84 *path_wide;
cristy99d6fa02011-09-23 13:09:30 +000085
cristy1b4c17f2012-02-12 15:48:43 +000086 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)
cristyf07b0852011-09-23 21:36:56 +000090 return((FILE *) NULL);
cristy1b4c17f2012-02-12 15:48:43 +000091 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)
cristy99d6fa02011-09-23 13:09:30 +000095 {
cristy1b4c17f2012-02-12 15:48:43 +000096 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristyf07b0852011-09-23 21:36:56 +000097 return((FILE *) NULL);
cristy99d6fa02011-09-23 13:09:30 +000098 }
cristy1b4c17f2012-02-12 15:48:43 +000099 count=MultiByteToWideChar(CP_UTF8,0,mode,-1,mode_wide,count);
100 file=_wfopen(path_wide,mode_wide);
101 mode_wide=(WCHAR *) RelinquishMagickMemory(mode_wide);
102 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristy99d6fa02011-09-23 13:09:30 +0000103 return(file);
104#endif
105}
106
cristy1b4c17f2012-02-12 15:48:43 +0000107#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
108typedef int
109 mode_t;
110#endif
111
cristy9e0719b2011-12-29 03:45:45 +0000112static inline int open_utf8(const char *path,int flags,mode_t mode)
cristy99d6fa02011-09-23 13:09:30 +0000113{
114#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
115 return(open(path,flags,mode));
116#else
117 int
cristy1b4c17f2012-02-12 15:48:43 +0000118 count,
cristy99d6fa02011-09-23 13:09:30 +0000119 status;
120
cristyd9b279b2012-02-11 01:34:21 +0000121 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +0000122 *path_wide;
cristyd9b279b2012-02-11 01:34:21 +0000123
cristy1b4c17f2012-02-12 15:48:43 +0000124 path_wide=(WCHAR *) NULL;
125 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
126 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
127 if (path_wide == (WCHAR *) NULL)
128 return(-1);
129 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
130 status=_wopen(path_wide,flags,mode);
131 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristy99d6fa02011-09-23 13:09:30 +0000132 return(status);
133#endif
134}
135
cristy18c6c272011-09-23 14:40:37 +0000136static inline FILE *popen_utf8(const char *command,const char *type)
137{
138#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
cristy1b4c17f2012-02-12 15:48:43 +0000139 return(popen(command,type));
cristy18c6c272011-09-23 14:40:37 +0000140#else
141 FILE
142 *file;
143
cristyf07b0852011-09-23 21:36:56 +0000144 int
cristy1b4c17f2012-02-12 15:48:43 +0000145 count;
cristyf07b0852011-09-23 21:36:56 +0000146
cristy18c6c272011-09-23 14:40:37 +0000147 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +0000148 *type_wide,
149 *command_wide;
cristy18c6c272011-09-23 14:40:37 +0000150
cristy1b4c17f2012-02-12 15:48:43 +0000151 command_wide=(WCHAR *) NULL;
152 count=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
153 command_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*command_wide));
154 if (command_wide == (WCHAR *) NULL)
cristyf07b0852011-09-23 21:36:56 +0000155 return((FILE *) NULL);
cristy1b4c17f2012-02-12 15:48:43 +0000156 count=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,count);
157 count=MultiByteToWideChar(CP_UTF8,0,type,-1,NULL,0);
158 type_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*type_wide));
159 if (type_wide == (WCHAR *) NULL)
cristy18c6c272011-09-23 14:40:37 +0000160 {
cristy1b4c17f2012-02-12 15:48:43 +0000161 command_wide=(WCHAR *) RelinquishMagickMemory(command_wide);
cristyf07b0852011-09-23 21:36:56 +0000162 return((FILE *) NULL);
cristy18c6c272011-09-23 14:40:37 +0000163 }
cristy1b4c17f2012-02-12 15:48:43 +0000164 count=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,count);
165 file=_wpopen(command_wide,type_wide);
166 type_wide=(WCHAR *) RelinquishMagickMemory(type_wide);
167 command_wide=(WCHAR *) RelinquishMagickMemory(command_wide);
cristy18c6c272011-09-23 14:40:37 +0000168 return(file);
169#endif
170}
171
cristy99d6fa02011-09-23 13:09:30 +0000172static inline int remove_utf8(const char *path)
173{
174#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
175 return(unlink(path));
176#else
177 int
cristy1b4c17f2012-02-12 15:48:43 +0000178 count,
cristy99d6fa02011-09-23 13:09:30 +0000179 status;
180
cristyd9b279b2012-02-11 01:34:21 +0000181 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +0000182 *path_wide;
cristyd9b279b2012-02-11 01:34:21 +0000183
cristy1b4c17f2012-02-12 15:48:43 +0000184 path_wide=(WCHAR *) NULL;
185 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
186 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
187 if (path_wide == (WCHAR *) NULL)
188 return(-1);
189 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
190 status=_wremove(path_wide);
191 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristy99d6fa02011-09-23 13:09:30 +0000192 return(status);
193#endif
194}
195
cristy320684d2011-09-23 14:55:47 +0000196static inline int rename_utf8(const char *source,const char *destination)
197{
198#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
199 return(rename(source,destination));
200#else
cristy320684d2011-09-23 14:55:47 +0000201 int
cristy1b4c17f2012-02-12 15:48:43 +0000202 count,
cristy320684d2011-09-23 14:55:47 +0000203 status;
204
cristyd9b279b2012-02-11 01:34:21 +0000205 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +0000206 *destination_wide,
207 *source_wide;
cristyd9b279b2012-02-11 01:34:21 +0000208
cristy1b4c17f2012-02-12 15:48:43 +0000209 source_wide=(WCHAR *) NULL;
210 count=MultiByteToWideChar(CP_UTF8,0,source,-1,NULL,0);
211 source_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*source_wide));
212 if (source_wide == (WCHAR *) NULL)
213 return(-1);
214 count=MultiByteToWideChar(CP_UTF8,0,source,-1,source_wide,count);
215 count=MultiByteToWideChar(CP_UTF8,0,destination,-1,NULL,0);
216 destination_wide=(WCHAR *) AcquireQuantumMemory(count,
217 sizeof(*destination_wide));
218 if (destination_wide == (WCHAR *) NULL)
cristy320684d2011-09-23 14:55:47 +0000219 {
cristy1b4c17f2012-02-12 15:48:43 +0000220 source_wide=(WCHAR *) RelinquishMagickMemory(source_wide);
221 return(-1);
cristy320684d2011-09-23 14:55:47 +0000222 }
cristy1b4c17f2012-02-12 15:48:43 +0000223 count=MultiByteToWideChar(CP_UTF8,0,destination,-1,destination_wide,count);
224 status=_wrename(source_wide,destination_wide);
225 destination_wide=(WCHAR *) RelinquishMagickMemory(destination_wide);
226 source_wide=(WCHAR *) RelinquishMagickMemory(source_wide);
cristy320684d2011-09-23 14:55:47 +0000227 return(status);
228#endif
229}
230
cristy99d6fa02011-09-23 13:09:30 +0000231static inline int stat_utf8(const char *path,struct stat *attributes)
232{
233#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
234 return(stat(path,attributes));
235#else
236 int
cristy1b4c17f2012-02-12 15:48:43 +0000237 count,
cristy99d6fa02011-09-23 13:09:30 +0000238 status;
239
cristyd9b279b2012-02-11 01:34:21 +0000240 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +0000241 *path_wide;
cristyd9b279b2012-02-11 01:34:21 +0000242
cristy1b4c17f2012-02-12 15:48:43 +0000243 path_wide=(WCHAR *) NULL;
244 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
245 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
246 if (path_wide == (WCHAR *) NULL)
247 return(-1);
248 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
249 status=_wstat64(path_wide,attributes);
250 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristy99d6fa02011-09-23 13:09:30 +0000251 return(status);
252#endif
253}
254
cristyd1dd6e42011-09-04 01:46:08 +0000255#if defined(__cplusplus) || defined(c_plusplus)
256}
257#endif
258
259#endif