blob: 7d5380286406693a47665493cb3e10128f78e3ee [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__)
48static inline int MultiByteToWideCharacter(const char *string,
49 WCHAR **wide_string,size_t *extent)
50{
51 size_t
52 length;
53
54 *extent=0;
55 if (wide_string == (WCHAR **) NULL)
56 return(0);
57 *wide_string=(WCHAR *) NULL;
58 if (string == (const char *) NULL)
59 return(0);
60 length=strlen(string)+1;
61 *wide_string=(WCHAR *) AcquireQuantumMemory(length,sizeof(*wide_string));
62 if (*wide_string == (WCHAR *) NULL)
63 return(-1);
64 return(mbstowcs_s(extent,*wide_string,length,string,_TRUNCATE));
65}
66#endif
67
cristye42f6582012-02-11 17:59:50 +000068static inline int access_utf8(const char *path,int mode)
cristy99d6fa02011-09-23 13:09:30 +000069{
70#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
71 return(access(path,mode));
72#else
73 int
cristy99d6fa02011-09-23 13:09:30 +000074 status;
75
cristyd9b279b2012-02-11 01:34:21 +000076 ssize_t
77 extent;
cristy99d6fa02011-09-23 13:09:30 +000078
cristyd9b279b2012-02-11 01:34:21 +000079 WCHAR
80 *wide_path;
81
82 status=MultiByteToWideCharacter(path,&wide_path,&extent);
83 if (status != 0)
84 return(status);
85 status=_waccess(wide_path,mode);
86 wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
cristy99d6fa02011-09-23 13:09:30 +000087 return(status);
88#endif
89}
90
91static inline FILE *fopen_utf8(const char *path,const char *mode)
92{
93#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
94 return(fopen(path,mode));
95#else
96 FILE
97 *file;
98
cristyf07b0852011-09-23 21:36:56 +000099 int
cristyd9b279b2012-02-11 01:34:21 +0000100 status;
101
102 ssize_t
103 extent;
cristyf07b0852011-09-23 21:36:56 +0000104
cristy99d6fa02011-09-23 13:09:30 +0000105 WCHAR
cristyd9b279b2012-02-11 01:34:21 +0000106 *wide_mode,
107 *wide_path;
cristy99d6fa02011-09-23 13:09:30 +0000108
cristyd9b279b2012-02-11 01:34:21 +0000109 status=MultiByteToWideCharacter(path,&wide_path,&extent);
110 if (status != 0)
cristyf07b0852011-09-23 21:36:56 +0000111 return((FILE *) NULL);
cristyd9b279b2012-02-11 01:34:21 +0000112 status=MultiByteToWideCharacter(mode,&wide_mode,&extent);
113 if (status != 0)
cristy99d6fa02011-09-23 13:09:30 +0000114 {
cristyd9b279b2012-02-11 01:34:21 +0000115 wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
cristyf07b0852011-09-23 21:36:56 +0000116 return((FILE *) NULL);
cristy99d6fa02011-09-23 13:09:30 +0000117 }
cristyd9b279b2012-02-11 01:34:21 +0000118 file=_wfopen(wide_path,wide_mode);
119 wide_mode=(WCHAR *) RelinquishMagickMemory(wide_mode);
120 wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
cristy99d6fa02011-09-23 13:09:30 +0000121 return(file);
122#endif
123}
124
cristy9e0719b2011-12-29 03:45:45 +0000125static inline int open_utf8(const char *path,int flags,mode_t mode)
cristy99d6fa02011-09-23 13:09:30 +0000126{
127#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
128 return(open(path,flags,mode));
129#else
130 int
cristy99d6fa02011-09-23 13:09:30 +0000131 status;
132
cristyd9b279b2012-02-11 01:34:21 +0000133 ssize_t
134 extent;
cristy99d6fa02011-09-23 13:09:30 +0000135
cristyd9b279b2012-02-11 01:34:21 +0000136 WCHAR
137 *wide_path;
138
139 status=MultiByteToWideCharacter(path,&wide_path,&extent);
140 if (status != 0)
141 return(status);
142 status=_wopen(wide_path,flags,mode);
143 wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
cristy99d6fa02011-09-23 13:09:30 +0000144 return(status);
145#endif
146}
147
cristy18c6c272011-09-23 14:40:37 +0000148static inline FILE *popen_utf8(const char *command,const char *type)
149{
150#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
151 return(fopen(command,type));
152#else
153 FILE
154 *file;
155
cristyf07b0852011-09-23 21:36:56 +0000156 int
cristyd9b279b2012-02-11 01:34:21 +0000157 status;
158
159 ssize_t
160 extent;
cristyf07b0852011-09-23 21:36:56 +0000161
cristy18c6c272011-09-23 14:40:37 +0000162 WCHAR
cristyd9b279b2012-02-11 01:34:21 +0000163 *wide_type,
164 *wide_command;
cristy18c6c272011-09-23 14:40:37 +0000165
cristyd9b279b2012-02-11 01:34:21 +0000166 status=MultiByteToWideCharacter(command,&wide_command,&extent);
167 if (status != 0)
cristyf07b0852011-09-23 21:36:56 +0000168 return((FILE *) NULL);
cristyd9b279b2012-02-11 01:34:21 +0000169 status=MultiByteToWideCharacter(type,&wide_type,&extent);
170 if (status != 0)
cristy18c6c272011-09-23 14:40:37 +0000171 {
cristyd9b279b2012-02-11 01:34:21 +0000172 wide_command=(WCHAR *) RelinquishMagickMemory(wide_command);
cristyf07b0852011-09-23 21:36:56 +0000173 return((FILE *) NULL);
cristy18c6c272011-09-23 14:40:37 +0000174 }
cristyd9b279b2012-02-11 01:34:21 +0000175 file=_wpopen(wide_command,wide_type);
176 wide_type=(WCHAR *) RelinquishMagickMemory(wide_type);
177 wide_command=(WCHAR *) RelinquishMagickMemory(wide_command);
cristy18c6c272011-09-23 14:40:37 +0000178 return(file);
179#endif
180}
181
cristy99d6fa02011-09-23 13:09:30 +0000182static inline int remove_utf8(const char *path)
183{
184#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
185 return(unlink(path));
186#else
187 int
cristy99d6fa02011-09-23 13:09:30 +0000188 status;
189
cristyd9b279b2012-02-11 01:34:21 +0000190 ssize_t
191 extent;
cristy99d6fa02011-09-23 13:09:30 +0000192
cristyd9b279b2012-02-11 01:34:21 +0000193 WCHAR
194 *wide_path;
195
196 status=MultiByteToWideCharacter(path,&wide_path,&extent);
197 if (status != 0)
198 return(status);
199 status=_wremove(wide_path);
200 wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
cristy99d6fa02011-09-23 13:09:30 +0000201 return(status);
202#endif
203}
204
cristy320684d2011-09-23 14:55:47 +0000205static inline int rename_utf8(const char *source,const char *destination)
206{
207#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
208 return(rename(source,destination));
209#else
cristy320684d2011-09-23 14:55:47 +0000210 int
211 status;
212
cristyd9b279b2012-02-11 01:34:21 +0000213 ssize_t
214 extent;
cristy320684d2011-09-23 14:55:47 +0000215
cristyd9b279b2012-02-11 01:34:21 +0000216 WCHAR
217 *wide_destination,
218 *wide_source;
219
220 status=MultiByteToWideCharacter(source,&wide_source,&extent);
221 if (status != 0)
222 return(status);
223 status=MultiByteToWideCharacter(destination,&wide_destination,&extent);
224 if (status != 0)
cristy320684d2011-09-23 14:55:47 +0000225 {
cristyd9b279b2012-02-11 01:34:21 +0000226 wide_source=(WCHAR *) RelinquishMagickMemory(wide_source);
227 return(status);
cristy320684d2011-09-23 14:55:47 +0000228 }
cristyd9b279b2012-02-11 01:34:21 +0000229 status=_wrename(wide_source,wide_destination);
230 wide_destination=(WCHAR *) RelinquishMagickMemory(wide_destination);
231 wide_source=(WCHAR *) RelinquishMagickMemory(wide_source);
cristy320684d2011-09-23 14:55:47 +0000232 return(status);
233#endif
234}
235
cristy99d6fa02011-09-23 13:09:30 +0000236static inline int stat_utf8(const char *path,struct stat *attributes)
237{
238#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
239 return(stat(path,attributes));
240#else
241 int
cristy99d6fa02011-09-23 13:09:30 +0000242 status;
243
cristyd9b279b2012-02-11 01:34:21 +0000244 ssize_t
245 extent;
cristy99d6fa02011-09-23 13:09:30 +0000246
cristyd9b279b2012-02-11 01:34:21 +0000247 WCHAR
248 *wide_path;
249
250 status=MultiByteToWideCharacter(path,&wide_path,&extent);
251 if (status != 0)
252 return(status);
253 status=_wstat64(wide_path,attributes);
254 wide_path=(WCHAR *) RelinquishMagickMemory(wide_path);
cristy99d6fa02011-09-23 13:09:30 +0000255 return(status);
256#endif
257}
258
cristyd1dd6e42011-09-04 01:46:08 +0000259#if defined(__cplusplus) || defined(c_plusplus)
260}
261#endif
262
263#endif