blob: 67b5d3b273ed24686b79426360628aab42534775 [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"
26#include "MagickCore/nt-base-private.h"
27
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
cristyd9b279b2012-02-11 01:34:21 +000047#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
cristyfe2d5fb2012-02-11 22:19:02 +000048typedef int
49 mode_t;
50
cristyd9b279b2012-02-11 01:34:21 +000051static inline int MultiByteToWideCharacter(const char *string,
52 WCHAR **wide_string,size_t *extent)
53{
54 size_t
55 length;
56
57 *extent=0;
58 if (wide_string == (WCHAR **) NULL)
59 return(0);
60 *wide_string=(WCHAR *) NULL;
61 if (string == (const char *) NULL)
62 return(0);
63 length=strlen(string)+1;
64 *wide_string=(WCHAR *) AcquireQuantumMemory(length,sizeof(*wide_string));
65 if (*wide_string == (WCHAR *) NULL)
66 return(-1);
67 return(mbstowcs_s(extent,*wide_string,length,string,_TRUNCATE));
68}
69#endif
70
cristye42f6582012-02-11 17:59:50 +000071static inline int access_utf8(const char *path,int mode)
cristy99d6fa02011-09-23 13:09:30 +000072{
73#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
74 return(access(path,mode));
75#else
76 int
cristy99d6fa02011-09-23 13:09:30 +000077 status;
78
cristyd9b279b2012-02-11 01:34:21 +000079 ssize_t
80 extent;
cristy99d6fa02011-09-23 13:09:30 +000081
cristyd9b279b2012-02-11 01:34:21 +000082 WCHAR
83 *wide_path;
84
85 status=MultiByteToWideCharacter(path,&wide_path,&extent);
86 if (status != 0)
87 return(status);
88 status=_waccess(wide_path,mode);
89 wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
cristy99d6fa02011-09-23 13:09:30 +000090 return(status);
91#endif
92}
93
94static inline FILE *fopen_utf8(const char *path,const char *mode)
95{
96#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
97 return(fopen(path,mode));
98#else
99 FILE
100 *file;
101
cristyf07b0852011-09-23 21:36:56 +0000102 int
cristyd9b279b2012-02-11 01:34:21 +0000103 status;
104
105 ssize_t
106 extent;
cristyf07b0852011-09-23 21:36:56 +0000107
cristy99d6fa02011-09-23 13:09:30 +0000108 WCHAR
cristyd9b279b2012-02-11 01:34:21 +0000109 *wide_mode,
110 *wide_path;
cristy99d6fa02011-09-23 13:09:30 +0000111
cristyd9b279b2012-02-11 01:34:21 +0000112 status=MultiByteToWideCharacter(path,&wide_path,&extent);
113 if (status != 0)
cristyf07b0852011-09-23 21:36:56 +0000114 return((FILE *) NULL);
cristyd9b279b2012-02-11 01:34:21 +0000115 status=MultiByteToWideCharacter(mode,&wide_mode,&extent);
116 if (status != 0)
cristy99d6fa02011-09-23 13:09:30 +0000117 {
cristyd9b279b2012-02-11 01:34:21 +0000118 wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
cristyf07b0852011-09-23 21:36:56 +0000119 return((FILE *) NULL);
cristy99d6fa02011-09-23 13:09:30 +0000120 }
cristyd9b279b2012-02-11 01:34:21 +0000121 file=_wfopen(wide_path,wide_mode);
122 wide_mode=(WCHAR *) RelinquishMagickMemory(wide_mode);
123 wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
cristy99d6fa02011-09-23 13:09:30 +0000124 return(file);
125#endif
126}
127
cristy9e0719b2011-12-29 03:45:45 +0000128static inline int open_utf8(const char *path,int flags,mode_t mode)
cristy99d6fa02011-09-23 13:09:30 +0000129{
130#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
131 return(open(path,flags,mode));
132#else
133 int
cristy99d6fa02011-09-23 13:09:30 +0000134 status;
135
cristyd9b279b2012-02-11 01:34:21 +0000136 ssize_t
137 extent;
cristy99d6fa02011-09-23 13:09:30 +0000138
cristyd9b279b2012-02-11 01:34:21 +0000139 WCHAR
140 *wide_path;
141
142 status=MultiByteToWideCharacter(path,&wide_path,&extent);
143 if (status != 0)
144 return(status);
145 status=_wopen(wide_path,flags,mode);
146 wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
cristy99d6fa02011-09-23 13:09:30 +0000147 return(status);
148#endif
149}
150
cristy18c6c272011-09-23 14:40:37 +0000151static inline FILE *popen_utf8(const char *command,const char *type)
152{
153#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
154 return(fopen(command,type));
155#else
156 FILE
157 *file;
158
cristyf07b0852011-09-23 21:36:56 +0000159 int
cristyd9b279b2012-02-11 01:34:21 +0000160 status;
161
162 ssize_t
163 extent;
cristyf07b0852011-09-23 21:36:56 +0000164
cristy18c6c272011-09-23 14:40:37 +0000165 WCHAR
cristyd9b279b2012-02-11 01:34:21 +0000166 *wide_type,
167 *wide_command;
cristy18c6c272011-09-23 14:40:37 +0000168
cristyd9b279b2012-02-11 01:34:21 +0000169 status=MultiByteToWideCharacter(command,&wide_command,&extent);
170 if (status != 0)
cristyf07b0852011-09-23 21:36:56 +0000171 return((FILE *) NULL);
cristyd9b279b2012-02-11 01:34:21 +0000172 status=MultiByteToWideCharacter(type,&wide_type,&extent);
173 if (status != 0)
cristy18c6c272011-09-23 14:40:37 +0000174 {
cristyd9b279b2012-02-11 01:34:21 +0000175 wide_command=(WCHAR *) RelinquishMagickMemory(wide_command);
cristyf07b0852011-09-23 21:36:56 +0000176 return((FILE *) NULL);
cristy18c6c272011-09-23 14:40:37 +0000177 }
cristyd9b279b2012-02-11 01:34:21 +0000178 file=_wpopen(wide_command,wide_type);
179 wide_type=(WCHAR *) RelinquishMagickMemory(wide_type);
180 wide_command=(WCHAR *) RelinquishMagickMemory(wide_command);
cristy18c6c272011-09-23 14:40:37 +0000181 return(file);
182#endif
183}
184
cristy99d6fa02011-09-23 13:09:30 +0000185static inline int remove_utf8(const char *path)
186{
187#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
188 return(unlink(path));
189#else
190 int
cristy99d6fa02011-09-23 13:09:30 +0000191 status;
192
cristyd9b279b2012-02-11 01:34:21 +0000193 ssize_t
194 extent;
cristy99d6fa02011-09-23 13:09:30 +0000195
cristyd9b279b2012-02-11 01:34:21 +0000196 WCHAR
197 *wide_path;
198
199 status=MultiByteToWideCharacter(path,&wide_path,&extent);
200 if (status != 0)
201 return(status);
202 status=_wremove(wide_path);
203 wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
cristy99d6fa02011-09-23 13:09:30 +0000204 return(status);
205#endif
206}
207
cristy320684d2011-09-23 14:55:47 +0000208static inline int rename_utf8(const char *source,const char *destination)
209{
210#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
211 return(rename(source,destination));
212#else
cristy320684d2011-09-23 14:55:47 +0000213 int
214 status;
215
cristyd9b279b2012-02-11 01:34:21 +0000216 ssize_t
217 extent;
cristy320684d2011-09-23 14:55:47 +0000218
cristyd9b279b2012-02-11 01:34:21 +0000219 WCHAR
220 *wide_destination,
221 *wide_source;
222
223 status=MultiByteToWideCharacter(source,&wide_source,&extent);
224 if (status != 0)
225 return(status);
226 status=MultiByteToWideCharacter(destination,&wide_destination,&extent);
227 if (status != 0)
cristy320684d2011-09-23 14:55:47 +0000228 {
cristyd9b279b2012-02-11 01:34:21 +0000229 wide_source=(WCHAR *) RelinquishMagickMemory(wide_source);
230 return(status);
cristy320684d2011-09-23 14:55:47 +0000231 }
cristyd9b279b2012-02-11 01:34:21 +0000232 status=_wrename(wide_source,wide_destination);
233 wide_destination=(WCHAR *) RelinquishMagickMemory(wide_destination);
234 wide_source=(WCHAR *) RelinquishMagickMemory(wide_source);
cristy320684d2011-09-23 14:55:47 +0000235 return(status);
236#endif
237}
238
cristy99d6fa02011-09-23 13:09:30 +0000239static inline int stat_utf8(const char *path,struct stat *attributes)
240{
241#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
242 return(stat(path,attributes));
243#else
244 int
cristy99d6fa02011-09-23 13:09:30 +0000245 status;
246
cristyd9b279b2012-02-11 01:34:21 +0000247 ssize_t
248 extent;
cristy99d6fa02011-09-23 13:09:30 +0000249
cristyd9b279b2012-02-11 01:34:21 +0000250 WCHAR
251 *wide_path;
252
253 status=MultiByteToWideCharacter(path,&wide_path,&extent);
254 if (status != 0)
255 return(status);
256 status=_wstat64(wide_path,attributes);
257 wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
cristy99d6fa02011-09-23 13:09:30 +0000258 return(status);
259#endif
260}
261
cristyd1dd6e42011-09-04 01:46:08 +0000262#if defined(__cplusplus) || defined(c_plusplus)
263}
264#endif
265
266#endif