blob: df0160d838acfca94f8fb846e974a5949ed93cd5 [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"
cristyd2d11ec2012-03-28 13:53:49 +000027#include "MagickCore/nt-base-private.h"
cristy0740a982011-10-13 15:01:01 +000028
cristyd1dd6e42011-09-04 01:46:08 +000029extern MagickPrivate char
30 **GetPathComponents(const char *,size_t *),
31 **ListFiles(const char *,const char *,size_t *);
32
33extern MagickPrivate MagickBooleanType
34 GetExecutionPath(char *,const size_t);
35
36extern MagickPrivate ssize_t
37 GetMagickPageSize(void);
38
39extern MagickPrivate void
40 ChopPathComponents(char *,const size_t),
41 ExpandFilename(char *),
42 MagickDelay(const MagickSizeType);
43
cristy99d6fa02011-09-23 13:09:30 +000044/*
45 Windows UTF8 compatibility methods.
46*/
47
cristye42f6582012-02-11 17:59:50 +000048static inline int access_utf8(const char *path,int mode)
cristy99d6fa02011-09-23 13:09:30 +000049{
50#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
51 return(access(path,mode));
52#else
53 int
cristy1b4c17f2012-02-12 15:48:43 +000054 count,
cristy99d6fa02011-09-23 13:09:30 +000055 status;
56
cristyd9b279b2012-02-11 01:34:21 +000057 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +000058 *path_wide;
cristyd9b279b2012-02-11 01:34:21 +000059
cristy1b4c17f2012-02-12 15:48:43 +000060 path_wide=(WCHAR *) NULL;
61 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
62 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
63 if (path_wide == (WCHAR *) NULL)
64 return(-1);
65 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
66 status=_waccess(path_wide,mode);
67 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristy99d6fa02011-09-23 13:09:30 +000068 return(status);
69#endif
70}
71
72static inline FILE *fopen_utf8(const char *path,const char *mode)
73{
74#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
75 return(fopen(path,mode));
76#else
77 FILE
78 *file;
79
cristyf07b0852011-09-23 21:36:56 +000080 int
cristy1b4c17f2012-02-12 15:48:43 +000081 count;
cristyf07b0852011-09-23 21:36:56 +000082
cristy99d6fa02011-09-23 13:09:30 +000083 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +000084 *mode_wide,
85 *path_wide;
cristy99d6fa02011-09-23 13:09:30 +000086
cristy1b4c17f2012-02-12 15:48:43 +000087 path_wide=(WCHAR *) NULL;
88 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
89 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
90 if (path_wide == (WCHAR *) NULL)
cristyf07b0852011-09-23 21:36:56 +000091 return((FILE *) NULL);
cristy1b4c17f2012-02-12 15:48:43 +000092 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
93 count=MultiByteToWideChar(CP_UTF8,0,mode,-1,NULL,0);
94 mode_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*mode_wide));
95 if (mode_wide == (WCHAR *) NULL)
cristy99d6fa02011-09-23 13:09:30 +000096 {
cristy1b4c17f2012-02-12 15:48:43 +000097 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristyf07b0852011-09-23 21:36:56 +000098 return((FILE *) NULL);
cristy99d6fa02011-09-23 13:09:30 +000099 }
cristy1b4c17f2012-02-12 15:48:43 +0000100 count=MultiByteToWideChar(CP_UTF8,0,mode,-1,mode_wide,count);
101 file=_wfopen(path_wide,mode_wide);
102 mode_wide=(WCHAR *) RelinquishMagickMemory(mode_wide);
103 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristy99d6fa02011-09-23 13:09:30 +0000104 return(file);
105#endif
106}
107
cristy1b4c17f2012-02-12 15:48:43 +0000108#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
109typedef int
110 mode_t;
111#endif
112
cristy9e0719b2011-12-29 03:45:45 +0000113static inline int open_utf8(const char *path,int flags,mode_t mode)
cristy99d6fa02011-09-23 13:09:30 +0000114{
115#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
116 return(open(path,flags,mode));
117#else
118 int
cristy1b4c17f2012-02-12 15:48:43 +0000119 count,
cristy99d6fa02011-09-23 13:09:30 +0000120 status;
121
cristyd9b279b2012-02-11 01:34:21 +0000122 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +0000123 *path_wide;
cristyd9b279b2012-02-11 01:34:21 +0000124
cristy1b4c17f2012-02-12 15:48:43 +0000125 path_wide=(WCHAR *) NULL;
126 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
127 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
128 if (path_wide == (WCHAR *) NULL)
129 return(-1);
130 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
131 status=_wopen(path_wide,flags,mode);
132 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristy99d6fa02011-09-23 13:09:30 +0000133 return(status);
134#endif
135}
136
cristy18c6c272011-09-23 14:40:37 +0000137static inline FILE *popen_utf8(const char *command,const char *type)
138{
139#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
cristy1b4c17f2012-02-12 15:48:43 +0000140 return(popen(command,type));
cristy18c6c272011-09-23 14:40:37 +0000141#else
142 FILE
143 *file;
144
cristyf07b0852011-09-23 21:36:56 +0000145 int
cristy1b4c17f2012-02-12 15:48:43 +0000146 count;
cristyf07b0852011-09-23 21:36:56 +0000147
cristy18c6c272011-09-23 14:40:37 +0000148 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +0000149 *type_wide,
150 *command_wide;
cristy18c6c272011-09-23 14:40:37 +0000151
cristy1b4c17f2012-02-12 15:48:43 +0000152 command_wide=(WCHAR *) NULL;
153 count=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
154 command_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*command_wide));
155 if (command_wide == (WCHAR *) NULL)
cristyf07b0852011-09-23 21:36:56 +0000156 return((FILE *) NULL);
cristy1b4c17f2012-02-12 15:48:43 +0000157 count=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,count);
158 count=MultiByteToWideChar(CP_UTF8,0,type,-1,NULL,0);
159 type_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*type_wide));
160 if (type_wide == (WCHAR *) NULL)
cristy18c6c272011-09-23 14:40:37 +0000161 {
cristy1b4c17f2012-02-12 15:48:43 +0000162 command_wide=(WCHAR *) RelinquishMagickMemory(command_wide);
cristyf07b0852011-09-23 21:36:56 +0000163 return((FILE *) NULL);
cristy18c6c272011-09-23 14:40:37 +0000164 }
cristy1b4c17f2012-02-12 15:48:43 +0000165 count=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,count);
166 file=_wpopen(command_wide,type_wide);
167 type_wide=(WCHAR *) RelinquishMagickMemory(type_wide);
168 command_wide=(WCHAR *) RelinquishMagickMemory(command_wide);
cristy18c6c272011-09-23 14:40:37 +0000169 return(file);
170#endif
171}
172
cristy99d6fa02011-09-23 13:09:30 +0000173static inline int remove_utf8(const char *path)
174{
175#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
176 return(unlink(path));
177#else
178 int
cristy1b4c17f2012-02-12 15:48:43 +0000179 count,
cristy99d6fa02011-09-23 13:09:30 +0000180 status;
181
cristyd9b279b2012-02-11 01:34:21 +0000182 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +0000183 *path_wide;
cristyd9b279b2012-02-11 01:34:21 +0000184
cristy1b4c17f2012-02-12 15:48:43 +0000185 path_wide=(WCHAR *) NULL;
186 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
187 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
188 if (path_wide == (WCHAR *) NULL)
189 return(-1);
190 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
191 status=_wremove(path_wide);
192 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristy99d6fa02011-09-23 13:09:30 +0000193 return(status);
194#endif
195}
196
cristy320684d2011-09-23 14:55:47 +0000197static inline int rename_utf8(const char *source,const char *destination)
198{
199#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
200 return(rename(source,destination));
201#else
cristy320684d2011-09-23 14:55:47 +0000202 int
cristy1b4c17f2012-02-12 15:48:43 +0000203 count,
cristy320684d2011-09-23 14:55:47 +0000204 status;
205
cristyd9b279b2012-02-11 01:34:21 +0000206 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +0000207 *destination_wide,
208 *source_wide;
cristyd9b279b2012-02-11 01:34:21 +0000209
cristy1b4c17f2012-02-12 15:48:43 +0000210 source_wide=(WCHAR *) NULL;
211 count=MultiByteToWideChar(CP_UTF8,0,source,-1,NULL,0);
212 source_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*source_wide));
213 if (source_wide == (WCHAR *) NULL)
214 return(-1);
215 count=MultiByteToWideChar(CP_UTF8,0,source,-1,source_wide,count);
216 count=MultiByteToWideChar(CP_UTF8,0,destination,-1,NULL,0);
217 destination_wide=(WCHAR *) AcquireQuantumMemory(count,
218 sizeof(*destination_wide));
219 if (destination_wide == (WCHAR *) NULL)
cristy320684d2011-09-23 14:55:47 +0000220 {
cristy1b4c17f2012-02-12 15:48:43 +0000221 source_wide=(WCHAR *) RelinquishMagickMemory(source_wide);
222 return(-1);
cristy320684d2011-09-23 14:55:47 +0000223 }
cristy1b4c17f2012-02-12 15:48:43 +0000224 count=MultiByteToWideChar(CP_UTF8,0,destination,-1,destination_wide,count);
225 status=_wrename(source_wide,destination_wide);
226 destination_wide=(WCHAR *) RelinquishMagickMemory(destination_wide);
227 source_wide=(WCHAR *) RelinquishMagickMemory(source_wide);
cristy320684d2011-09-23 14:55:47 +0000228 return(status);
229#endif
230}
231
cristy99d6fa02011-09-23 13:09:30 +0000232static inline int stat_utf8(const char *path,struct stat *attributes)
233{
234#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
235 return(stat(path,attributes));
236#else
237 int
cristy1b4c17f2012-02-12 15:48:43 +0000238 count,
cristy99d6fa02011-09-23 13:09:30 +0000239 status;
240
cristyd9b279b2012-02-11 01:34:21 +0000241 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +0000242 *path_wide;
cristyd9b279b2012-02-11 01:34:21 +0000243
cristy1b4c17f2012-02-12 15:48:43 +0000244 path_wide=(WCHAR *) NULL;
245 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
246 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
247 if (path_wide == (WCHAR *) NULL)
248 return(-1);
249 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
250 status=_wstat64(path_wide,attributes);
251 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristy99d6fa02011-09-23 13:09:30 +0000252 return(status);
253#endif
254}
255
cristyd1dd6e42011-09-04 01:46:08 +0000256#if defined(__cplusplus) || defined(c_plusplus)
257}
258#endif
259
260#endif