blob: d3f8573bc7d23e4b3b5286a5483a59be4cb5edba [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
cristy99d6fa02011-09-23 13:09:30 +000076 WCHAR
77 *mode_wide,
78 *path_wide;
79
80 path_wide=(WCHAR *) NULL;
81 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
82 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
83 if (path_wide == (WCHAR *) NULL)
84 return(-1);
85 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
86 count=MultiByteToWideChar(CP_UTF8,0,mode,-1,NULL,0);
87 mode_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*mode_wide));
88 if (mode_wide == (WCHAR *) NULL)
89 {
90 path_wide=RelinquishMagickMemory(path_wide);
91 return(-1);
92 }
93 count=MultiByteToWideChar(CP_UTF8,0,mode,-1,mode_wide,count);
94 file=_wfopen(path_wide,mode_width);
95 mode_wide=RelinquishMagickMemory(mode_wide);
96 path_wide=RelinquishMagickMemory(path_wide);
97 return(file);
98#endif
99}
100
101static inline int open_utf8(const char *path,int flags,int mode)
102{
103#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
104 return(open(path,flags,mode));
105#else
106 int
107 count,
108 status;
109
110 WCHAR
111 *path_wide;
112
113 path_wide=(WCHAR *) NULL;
114 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
115 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
116 if (path_wide == (WCHAR *) NULL)
117 return(-1);
118 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
119 status=_wopen(path_wide,flags,mode);
120 path_wide=RelinquishMagickMemory(path_wide);
121 return(status);
122#endif
123}
124
cristy18c6c272011-09-23 14:40:37 +0000125static inline FILE *popen_utf8(const char *command,const char *type)
126{
127#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
128 return(fopen(command,type));
129#else
130 FILE
131 *file;
132
cristy18c6c272011-09-23 14:40:37 +0000133 WCHAR
134 *type_wide,
135 *command_wide;
136
137 command_wide=(WCHAR *) NULL;
138 count=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
139 command_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*command_wide));
140 if (command_wide == (WCHAR *) NULL)
141 return(-1);
142 count=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,count);
143 count=MultiByteToWideChar(CP_UTF8,0,type,-1,NULL,0);
144 type_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*type_wide));
145 if (type_wide == (WCHAR *) NULL)
146 {
147 command_wide=RelinquishMagickMemory(command_wide);
148 return(-1);
149 }
150 count=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,count);
151 file=_wpopen(path_wide,type_width);
152 type_wide=RelinquishMagickMemory(type_wide);
153 path_wide=RelinquishMagickMemory(path_wide);
154 return(file);
155#endif
156}
157
cristy99d6fa02011-09-23 13:09:30 +0000158static inline int remove_utf8(const char *path)
159{
160#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
161 return(unlink(path));
162#else
163 int
164 count,
165 status;
166
167 WCHAR
168 *path_wide;
169
170 path_wide=(WCHAR *) NULL;
171 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
172 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
173 if (path_wide == (WCHAR *) NULL)
174 return(-1);
175 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
176 status=_wremove(path_wide);
177 path_wide=RelinquishMagickMemory(path_wide);
178 return(status);
179#endif
180}
181
cristy320684d2011-09-23 14:55:47 +0000182static inline int rename_utf8(const char *source,const char *destination)
183{
184#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
185 return(rename(source,destination));
186#else
187 FILE
188 *file;
189
190 int
191 status;
192
193 WCHAR
194 *destination_wide,
195 *source_wide;
196
197 source_wide=(WCHAR *) NULL;
198 count=MultiByteToWideChar(CP_UTF8,0,source,-1,NULL,0);
199 source_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*source_wide));
200 if (source_wide == (WCHAR *) NULL)
201 return(-1);
202 count=MultiByteToWideChar(CP_UTF8,0,source,-1,source_wide,count);
203 count=MultiByteToWideChar(CP_UTF8,0,destination,-1,NULL,0);
204 destination_wide=(WCHAR *) AcquireQuantumMemory(count,
205 sizeof(*destination_wide));
206 if (destination_wide == (WCHAR *) NULL)
207 {
208 source_wide=RelinquishMagickMemory(source_wide);
209 return(-1);
210 }
211 count=MultiByteToWideChar(CP_UTF8,0,destination,-1,destination_wide,count);
212 status=_wrename(path_wide,destination_width);
213 destination_wide=RelinquishMagickMemory(destination_wide);
214 path_wide=RelinquishMagickMemory(path_wide);
215 return(status);
216#endif
217}
218
cristy99d6fa02011-09-23 13:09:30 +0000219static inline int stat_utf8(const char *path,struct stat *attributes)
220{
221#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
222 return(stat(path,attributes));
223#else
224 int
225 count,
226 status;
227
228 WCHAR
229 *path_wide;
230
231 path_wide=(WCHAR *) NULL;
232 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
233 path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
234 if (path_wide == (WCHAR *) NULL)
235 return(-1);
236 count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
237 status=_wstat(path_wide,attributes);
238 path_wide=RelinquishMagickMemory(path_wide);
239 return(status);
240#endif
241}
242
cristyd1dd6e42011-09-04 01:46:08 +0000243#if defined(__cplusplus) || defined(c_plusplus)
244}
245#endif
246
247#endif