blob: c90186c8abea6a8dd136e41ea7f4e8b40cc197dc [file] [log] [blame]
cristyd1dd6e42011-09-04 01:46:08 +00001/*
2 Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization
3 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
25extern MagickPrivate char
26 **GetPathComponents(const char *,size_t *),
27 **ListFiles(const char *,const char *,size_t *);
28
29extern MagickPrivate MagickBooleanType
30 GetExecutionPath(char *,const size_t);
31
32extern MagickPrivate ssize_t
33 GetMagickPageSize(void);
34
35extern MagickPrivate void
36 ChopPathComponents(char *,const size_t),
37 ExpandFilename(char *),
38 MagickDelay(const MagickSizeType);
39
cristy99d6fa02011-09-23 13:09:30 +000040/*
41 Windows UTF8 compatibility methods.
42*/
43
44static inline int access_utf8(const char *path,int mode)
45{
46#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
47 return(access(path,mode));
48#else
49 int
50 count,
51 status;
52
53 WCHAR
54 *path_wide;
55
56 path_wide=(WCHAR *) NULL;
57 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
58 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
59 if (path_wide == (WCHAR *) NULL)
60 return(-1);
61 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
62 status=_waccess(path_wide,mode);
63 path_wide=RelinquishMagickMemory(path_wide);
64 return(status);
65#endif
66}
67
68static inline FILE *fopen_utf8(const char *path,const char *mode)
69{
70#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
71 return(fopen(path,mode));
72#else
73 FILE
74 *file;
75
cristyf07b0852011-09-23 21:36:56 +000076 int
77 count;
78
cristy99d6fa02011-09-23 13:09:30 +000079 WCHAR
80 *mode_wide,
81 *path_wide;
82
83 path_wide=(WCHAR *) NULL;
84 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
85 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
86 if (path_wide == (WCHAR *) NULL)
cristyf07b0852011-09-23 21:36:56 +000087 return((FILE *) NULL);
cristy99d6fa02011-09-23 13:09:30 +000088 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
89 count=MultiByteToWideChar(CP_UTF8,0,mode,-1,NULL,0);
90 mode_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*mode_wide));
91 if (mode_wide == (WCHAR *) NULL)
92 {
93 path_wide=RelinquishMagickMemory(path_wide);
cristyf07b0852011-09-23 21:36:56 +000094 return((FILE *) NULL);
cristy99d6fa02011-09-23 13:09:30 +000095 }
96 count=MultiByteToWideChar(CP_UTF8,0,mode,-1,mode_wide,count);
cristyf07b0852011-09-23 21:36:56 +000097 file=_wfopen(path_wide,mode_wide);
cristy99d6fa02011-09-23 13:09:30 +000098 mode_wide=RelinquishMagickMemory(mode_wide);
99 path_wide=RelinquishMagickMemory(path_wide);
100 return(file);
101#endif
102}
103
104static inline int open_utf8(const char *path,int flags,int mode)
105{
106#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
107 return(open(path,flags,mode));
108#else
109 int
110 count,
111 status;
112
113 WCHAR
114 *path_wide;
115
116 path_wide=(WCHAR *) NULL;
117 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
118 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
119 if (path_wide == (WCHAR *) NULL)
120 return(-1);
121 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
122 status=_wopen(path_wide,flags,mode);
123 path_wide=RelinquishMagickMemory(path_wide);
124 return(status);
125#endif
126}
127
cristy18c6c272011-09-23 14:40:37 +0000128static inline FILE *popen_utf8(const char *command,const char *type)
129{
130#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
131 return(fopen(command,type));
132#else
133 FILE
134 *file;
135
cristyf07b0852011-09-23 21:36:56 +0000136 int
137 count;
138
cristy18c6c272011-09-23 14:40:37 +0000139 WCHAR
140 *type_wide,
141 *command_wide;
142
143 command_wide=(WCHAR *) NULL;
144 count=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
145 command_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*command_wide));
146 if (command_wide == (WCHAR *) NULL)
cristyf07b0852011-09-23 21:36:56 +0000147 return((FILE *) NULL);
cristy18c6c272011-09-23 14:40:37 +0000148 count=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,count);
149 count=MultiByteToWideChar(CP_UTF8,0,type,-1,NULL,0);
150 type_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*type_wide));
151 if (type_wide == (WCHAR *) NULL)
152 {
153 command_wide=RelinquishMagickMemory(command_wide);
cristyf07b0852011-09-23 21:36:56 +0000154 return((FILE *) NULL);
cristy18c6c272011-09-23 14:40:37 +0000155 }
156 count=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,count);
cristyf07b0852011-09-23 21:36:56 +0000157 file=_wpopen(command_wide,type_wide);
cristy18c6c272011-09-23 14:40:37 +0000158 type_wide=RelinquishMagickMemory(type_wide);
cristyf07b0852011-09-23 21:36:56 +0000159 command_wide=RelinquishMagickMemory(command_wide);
cristy18c6c272011-09-23 14:40:37 +0000160 return(file);
161#endif
162}
163
cristy99d6fa02011-09-23 13:09:30 +0000164static inline int remove_utf8(const char *path)
165{
166#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
167 return(unlink(path));
168#else
169 int
170 count,
171 status;
172
173 WCHAR
174 *path_wide;
175
176 path_wide=(WCHAR *) NULL;
177 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
178 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
179 if (path_wide == (WCHAR *) NULL)
180 return(-1);
181 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
182 status=_wremove(path_wide);
183 path_wide=RelinquishMagickMemory(path_wide);
184 return(status);
185#endif
186}
187
cristy320684d2011-09-23 14:55:47 +0000188static inline int rename_utf8(const char *source,const char *destination)
189{
190#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
191 return(rename(source,destination));
192#else
cristy320684d2011-09-23 14:55:47 +0000193 int
cristyf07b0852011-09-23 21:36:56 +0000194 count,
cristy320684d2011-09-23 14:55:47 +0000195 status;
196
197 WCHAR
198 *destination_wide,
199 *source_wide;
200
201 source_wide=(WCHAR *) NULL;
202 count=MultiByteToWideChar(CP_UTF8,0,source,-1,NULL,0);
203 source_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*source_wide));
204 if (source_wide == (WCHAR *) NULL)
205 return(-1);
206 count=MultiByteToWideChar(CP_UTF8,0,source,-1,source_wide,count);
207 count=MultiByteToWideChar(CP_UTF8,0,destination,-1,NULL,0);
208 destination_wide=(WCHAR *) AcquireQuantumMemory(count,
209 sizeof(*destination_wide));
210 if (destination_wide == (WCHAR *) NULL)
211 {
212 source_wide=RelinquishMagickMemory(source_wide);
213 return(-1);
214 }
215 count=MultiByteToWideChar(CP_UTF8,0,destination,-1,destination_wide,count);
cristyf07b0852011-09-23 21:36:56 +0000216 status=_wrename(source_wide,destination_wide);
cristy320684d2011-09-23 14:55:47 +0000217 destination_wide=RelinquishMagickMemory(destination_wide);
cristyf07b0852011-09-23 21:36:56 +0000218 source_wide=RelinquishMagickMemory(source_wide);
cristy320684d2011-09-23 14:55:47 +0000219 return(status);
220#endif
221}
222
cristy99d6fa02011-09-23 13:09:30 +0000223static inline int stat_utf8(const char *path,struct stat *attributes)
224{
225#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
226 return(stat(path,attributes));
227#else
228 int
229 count,
230 status;
231
232 WCHAR
233 *path_wide;
234
235 path_wide=(WCHAR *) NULL;
236 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
237 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
238 if (path_wide == (WCHAR *) NULL)
239 return(-1);
240 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
cristyf07b0852011-09-23 21:36:56 +0000241 status=wstat(path_wide,attributes);
cristy99d6fa02011-09-23 13:09:30 +0000242 path_wide=RelinquishMagickMemory(path_wide);
243 return(status);
244#endif
245}
246
cristyd1dd6e42011-09-04 01:46:08 +0000247#if defined(__cplusplus) || defined(c_plusplus)
248}
249#endif
250
251#endif