blob: e1314a0b025738699532446767f410c139766d4a [file] [log] [blame]
cristyd1dd6e42011-09-04 01:46:08 +00001/*
cristyfe676ee2013-11-18 13:03:38 +00002 Copyright 1999-2014 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
cristy59864562013-04-18 11:47:41 +000034 GetExecutionPath(char *,const size_t),
35 ShredFile(const char *);
cristyd1dd6e42011-09-04 01:46:08 +000036
37extern MagickPrivate ssize_t
38 GetMagickPageSize(void);
39
40extern MagickPrivate void
41 ChopPathComponents(char *,const size_t),
42 ExpandFilename(char *),
43 MagickDelay(const MagickSizeType);
44
cristy99d6fa02011-09-23 13:09:30 +000045/*
46 Windows UTF8 compatibility methods.
47*/
48
cristye42f6582012-02-11 17:59:50 +000049static inline int access_utf8(const char *path,int mode)
cristy99d6fa02011-09-23 13:09:30 +000050{
cristy07a3cca2012-12-10 13:09:10 +000051#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
cristy99d6fa02011-09-23 13:09:30 +000052 return(access(path,mode));
53#else
54 int
cristy1b4c17f2012-02-12 15:48:43 +000055 count,
cristy99d6fa02011-09-23 13:09:30 +000056 status;
57
cristyd9b279b2012-02-11 01:34:21 +000058 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +000059 *path_wide;
cristyd9b279b2012-02-11 01:34:21 +000060
cristy1b4c17f2012-02-12 15:48:43 +000061 path_wide=(WCHAR *) NULL;
62 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
63 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
64 if (path_wide == (WCHAR *) NULL)
65 return(-1);
66 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
67 status=_waccess(path_wide,mode);
68 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristy99d6fa02011-09-23 13:09:30 +000069 return(status);
70#endif
71}
72
73static inline FILE *fopen_utf8(const char *path,const char *mode)
74{
cristy07a3cca2012-12-10 13:09:10 +000075#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
cristy99d6fa02011-09-23 13:09:30 +000076 return(fopen(path,mode));
77#else
78 FILE
79 *file;
80
cristyf07b0852011-09-23 21:36:56 +000081 int
cristy1b4c17f2012-02-12 15:48:43 +000082 count;
cristyf07b0852011-09-23 21:36:56 +000083
cristy99d6fa02011-09-23 13:09:30 +000084 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +000085 *mode_wide,
86 *path_wide;
cristy99d6fa02011-09-23 13:09:30 +000087
cristy1b4c17f2012-02-12 15:48:43 +000088 path_wide=(WCHAR *) NULL;
89 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
90 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
91 if (path_wide == (WCHAR *) NULL)
cristyf07b0852011-09-23 21:36:56 +000092 return((FILE *) NULL);
cristy1b4c17f2012-02-12 15:48:43 +000093 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
94 count=MultiByteToWideChar(CP_UTF8,0,mode,-1,NULL,0);
95 mode_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*mode_wide));
96 if (mode_wide == (WCHAR *) NULL)
cristy99d6fa02011-09-23 13:09:30 +000097 {
cristy1b4c17f2012-02-12 15:48:43 +000098 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristyf07b0852011-09-23 21:36:56 +000099 return((FILE *) NULL);
cristy99d6fa02011-09-23 13:09:30 +0000100 }
cristy1b4c17f2012-02-12 15:48:43 +0000101 count=MultiByteToWideChar(CP_UTF8,0,mode,-1,mode_wide,count);
102 file=_wfopen(path_wide,mode_wide);
103 mode_wide=(WCHAR *) RelinquishMagickMemory(mode_wide);
104 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristy99d6fa02011-09-23 13:09:30 +0000105 return(file);
106#endif
107}
108
cristy277f3a22012-12-10 13:18:45 +0000109#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__)
cristy1b4c17f2012-02-12 15:48:43 +0000110typedef int
111 mode_t;
112#endif
113
cristy9e0719b2011-12-29 03:45:45 +0000114static inline int open_utf8(const char *path,int flags,mode_t mode)
cristy99d6fa02011-09-23 13:09:30 +0000115{
cristy07a3cca2012-12-10 13:09:10 +0000116#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
cristy99d6fa02011-09-23 13:09:30 +0000117 return(open(path,flags,mode));
118#else
119 int
cristy1b4c17f2012-02-12 15:48:43 +0000120 count,
cristy99d6fa02011-09-23 13:09:30 +0000121 status;
122
cristyd9b279b2012-02-11 01:34:21 +0000123 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +0000124 *path_wide;
cristyd9b279b2012-02-11 01:34:21 +0000125
cristy1b4c17f2012-02-12 15:48:43 +0000126 path_wide=(WCHAR *) NULL;
127 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
128 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
129 if (path_wide == (WCHAR *) NULL)
130 return(-1);
131 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
132 status=_wopen(path_wide,flags,mode);
133 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristy99d6fa02011-09-23 13:09:30 +0000134 return(status);
135#endif
136}
137
cristy18c6c272011-09-23 14:40:37 +0000138static inline FILE *popen_utf8(const char *command,const char *type)
139{
cristy07a3cca2012-12-10 13:09:10 +0000140#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
cristy1b4c17f2012-02-12 15:48:43 +0000141 return(popen(command,type));
cristy18c6c272011-09-23 14:40:37 +0000142#else
143 FILE
144 *file;
145
cristyf07b0852011-09-23 21:36:56 +0000146 int
cristy1b4c17f2012-02-12 15:48:43 +0000147 count;
cristyf07b0852011-09-23 21:36:56 +0000148
cristy18c6c272011-09-23 14:40:37 +0000149 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +0000150 *type_wide,
151 *command_wide;
cristy18c6c272011-09-23 14:40:37 +0000152
cristy1b4c17f2012-02-12 15:48:43 +0000153 command_wide=(WCHAR *) NULL;
154 count=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
155 command_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*command_wide));
156 if (command_wide == (WCHAR *) NULL)
cristyf07b0852011-09-23 21:36:56 +0000157 return((FILE *) NULL);
cristy1b4c17f2012-02-12 15:48:43 +0000158 count=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,count);
159 count=MultiByteToWideChar(CP_UTF8,0,type,-1,NULL,0);
160 type_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*type_wide));
161 if (type_wide == (WCHAR *) NULL)
cristy18c6c272011-09-23 14:40:37 +0000162 {
cristy1b4c17f2012-02-12 15:48:43 +0000163 command_wide=(WCHAR *) RelinquishMagickMemory(command_wide);
cristyf07b0852011-09-23 21:36:56 +0000164 return((FILE *) NULL);
cristy18c6c272011-09-23 14:40:37 +0000165 }
cristy1b4c17f2012-02-12 15:48:43 +0000166 count=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,count);
167 file=_wpopen(command_wide,type_wide);
168 type_wide=(WCHAR *) RelinquishMagickMemory(type_wide);
169 command_wide=(WCHAR *) RelinquishMagickMemory(command_wide);
cristy18c6c272011-09-23 14:40:37 +0000170 return(file);
171#endif
172}
173
cristy99d6fa02011-09-23 13:09:30 +0000174static inline int remove_utf8(const char *path)
175{
cristy07a3cca2012-12-10 13:09:10 +0000176#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
cristy99d6fa02011-09-23 13:09:30 +0000177 return(unlink(path));
178#else
179 int
cristy1b4c17f2012-02-12 15:48:43 +0000180 count,
cristy99d6fa02011-09-23 13:09:30 +0000181 status;
182
cristyd9b279b2012-02-11 01:34:21 +0000183 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +0000184 *path_wide;
cristyd9b279b2012-02-11 01:34:21 +0000185
cristy1b4c17f2012-02-12 15:48:43 +0000186 path_wide=(WCHAR *) NULL;
187 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
188 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
189 if (path_wide == (WCHAR *) NULL)
190 return(-1);
191 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
192 status=_wremove(path_wide);
193 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristy99d6fa02011-09-23 13:09:30 +0000194 return(status);
195#endif
196}
197
cristy320684d2011-09-23 14:55:47 +0000198static inline int rename_utf8(const char *source,const char *destination)
199{
cristy07a3cca2012-12-10 13:09:10 +0000200#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
cristy320684d2011-09-23 14:55:47 +0000201 return(rename(source,destination));
202#else
cristy320684d2011-09-23 14:55:47 +0000203 int
cristy1b4c17f2012-02-12 15:48:43 +0000204 count,
cristy320684d2011-09-23 14:55:47 +0000205 status;
206
cristyd9b279b2012-02-11 01:34:21 +0000207 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +0000208 *destination_wide,
209 *source_wide;
cristyd9b279b2012-02-11 01:34:21 +0000210
cristy1b4c17f2012-02-12 15:48:43 +0000211 source_wide=(WCHAR *) NULL;
212 count=MultiByteToWideChar(CP_UTF8,0,source,-1,NULL,0);
213 source_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*source_wide));
214 if (source_wide == (WCHAR *) NULL)
215 return(-1);
216 count=MultiByteToWideChar(CP_UTF8,0,source,-1,source_wide,count);
217 count=MultiByteToWideChar(CP_UTF8,0,destination,-1,NULL,0);
218 destination_wide=(WCHAR *) AcquireQuantumMemory(count,
219 sizeof(*destination_wide));
220 if (destination_wide == (WCHAR *) NULL)
cristy320684d2011-09-23 14:55:47 +0000221 {
cristy1b4c17f2012-02-12 15:48:43 +0000222 source_wide=(WCHAR *) RelinquishMagickMemory(source_wide);
223 return(-1);
cristy320684d2011-09-23 14:55:47 +0000224 }
cristy1b4c17f2012-02-12 15:48:43 +0000225 count=MultiByteToWideChar(CP_UTF8,0,destination,-1,destination_wide,count);
226 status=_wrename(source_wide,destination_wide);
227 destination_wide=(WCHAR *) RelinquishMagickMemory(destination_wide);
228 source_wide=(WCHAR *) RelinquishMagickMemory(source_wide);
cristy320684d2011-09-23 14:55:47 +0000229 return(status);
230#endif
231}
232
cristy99d6fa02011-09-23 13:09:30 +0000233static inline int stat_utf8(const char *path,struct stat *attributes)
234{
cristy07a3cca2012-12-10 13:09:10 +0000235#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
cristy99d6fa02011-09-23 13:09:30 +0000236 return(stat(path,attributes));
237#else
238 int
cristy1b4c17f2012-02-12 15:48:43 +0000239 count,
cristy99d6fa02011-09-23 13:09:30 +0000240 status;
241
cristyd9b279b2012-02-11 01:34:21 +0000242 WCHAR
cristy1b4c17f2012-02-12 15:48:43 +0000243 *path_wide;
cristyd9b279b2012-02-11 01:34:21 +0000244
cristy1b4c17f2012-02-12 15:48:43 +0000245 path_wide=(WCHAR *) NULL;
246 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
247 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
248 if (path_wide == (WCHAR *) NULL)
249 return(-1);
250 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
dirk94041a92013-09-06 04:56:30 +0000251 status=wstat(path_wide,attributes);
cristy1b4c17f2012-02-12 15:48:43 +0000252 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
cristy99d6fa02011-09-23 13:09:30 +0000253 return(status);
254#endif
255}
256
cristyd1dd6e42011-09-04 01:46:08 +0000257#if defined(__cplusplus) || defined(c_plusplus)
258}
259#endif
260
261#endif