utility-private.h revision 04a6fc8fdc1e2c323984295fb66285e1b02fb094
1/*
2  Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization
3  dedicated to making software imaging solutions freely available.
4
5  You may not use this file except in compliance with the License.
6  obtain a copy of the License at
7
8    http://www.imagemagick.org/script/license.php
9
10  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
25#include "MagickCore/memory_.h"
26#include "MagickCore/nt-feature.h"
27
28extern 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
43/*
44  Windows UTF8 compatibility methods.
45*/
46
47static inline int access_utf8(const char *path,int mode)
48{
49#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
50  return(access(path,mode));
51#else
52   int
53     count,
54     status;
55
56   WCHAR
57     *path_wide;
58
59   path_wide=(WCHAR *) NULL;
60   count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
61   path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
62   if (path_wide == (WCHAR *) NULL)
63     return(-1);
64   count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
65   status=_waccess(path_wide,mode);
66   path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
67   return(status);
68#endif
69}
70
71static inline FILE *fopen_utf8(const char *path,const char *mode)
72{
73#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
74  return(fopen(path,mode));
75#else
76   FILE
77     *file;
78
79   int
80     count;
81
82   WCHAR
83     *mode_wide,
84     *path_wide;
85
86   path_wide=(WCHAR *) NULL;
87   count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
88   path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
89   if (path_wide == (WCHAR *) NULL)
90     return((FILE *) NULL);
91   count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
92   count=MultiByteToWideChar(CP_UTF8,0,mode,-1,NULL,0);
93   mode_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*mode_wide));
94   if (mode_wide == (WCHAR *) NULL)
95     {
96       path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
97       return((FILE *) NULL);
98     }
99   count=MultiByteToWideChar(CP_UTF8,0,mode,-1,mode_wide,count);
100   file=_wfopen(path_wide,mode_wide);
101   mode_wide=(WCHAR *) RelinquishMagickMemory(mode_wide);
102   path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
103   return(file);
104#endif
105}
106
107#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
108typedef int
109  mode_t;
110#endif
111
112static inline int open_utf8(const char *path,int flags,mode_t mode)
113{
114#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
115  return(open(path,flags,mode));
116#else
117   int
118     count,
119     status;
120
121   WCHAR
122     *path_wide;
123
124   path_wide=(WCHAR *) NULL;
125   count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
126   path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
127   if (path_wide == (WCHAR *) NULL)
128     return(-1);
129   count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
130   status=_wopen(path_wide,flags,mode);
131   path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
132   return(status);
133#endif
134}
135
136static inline FILE *popen_utf8(const char *command,const char *type)
137{
138#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
139  return(popen(command,type));
140#else
141   FILE
142     *file;
143
144   int
145     count;
146
147   WCHAR
148     *type_wide,
149     *command_wide;
150
151   command_wide=(WCHAR *) NULL;
152   count=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
153   command_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*command_wide));
154   if (command_wide == (WCHAR *) NULL)
155     return((FILE *) NULL);
156   count=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,count);
157   count=MultiByteToWideChar(CP_UTF8,0,type,-1,NULL,0);
158   type_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*type_wide));
159   if (type_wide == (WCHAR *) NULL)
160     {
161       command_wide=(WCHAR *) RelinquishMagickMemory(command_wide);
162       return((FILE *) NULL);
163     }
164   count=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,count);
165   file=_wpopen(command_wide,type_wide);
166   type_wide=(WCHAR *) RelinquishMagickMemory(type_wide);
167   command_wide=(WCHAR *) RelinquishMagickMemory(command_wide);
168   return(file);
169#endif
170}
171
172static inline int remove_utf8(const char *path)
173{
174#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
175  return(unlink(path));
176#else
177   int
178     count,
179     status;
180
181   WCHAR
182     *path_wide;
183
184   path_wide=(WCHAR *) NULL;
185   count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
186   path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
187   if (path_wide == (WCHAR *) NULL)
188     return(-1);
189   count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
190   status=_wremove(path_wide);
191   path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
192   return(status);
193#endif
194}
195
196static inline int rename_utf8(const char *source,const char *destination)
197{
198#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
199  return(rename(source,destination));
200#else
201   int
202     count,
203     status;
204
205   WCHAR
206     *destination_wide,
207     *source_wide;
208
209   source_wide=(WCHAR *) NULL;
210   count=MultiByteToWideChar(CP_UTF8,0,source,-1,NULL,0);
211   source_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*source_wide));
212   if (source_wide == (WCHAR *) NULL)
213     return(-1);
214   count=MultiByteToWideChar(CP_UTF8,0,source,-1,source_wide,count);
215   count=MultiByteToWideChar(CP_UTF8,0,destination,-1,NULL,0);
216   destination_wide=(WCHAR *) AcquireQuantumMemory(count,
217     sizeof(*destination_wide));
218   if (destination_wide == (WCHAR *) NULL)
219     {
220       source_wide=(WCHAR *) RelinquishMagickMemory(source_wide);
221       return(-1);
222     }
223   count=MultiByteToWideChar(CP_UTF8,0,destination,-1,destination_wide,count);
224   status=_wrename(source_wide,destination_wide);
225   destination_wide=(WCHAR *) RelinquishMagickMemory(destination_wide);
226   source_wide=(WCHAR *) RelinquishMagickMemory(source_wide);
227   return(status);
228#endif
229}
230
231static inline int stat_utf8(const char *path,struct stat *attributes)
232{
233#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
234  return(stat(path,attributes));
235#else
236   int
237     count,
238     status;
239
240   WCHAR
241     *path_wide;
242
243   path_wide=(WCHAR *) NULL;
244   count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
245   path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
246   if (path_wide == (WCHAR *) NULL)
247     return(-1);
248   count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
249   status=_wstat64(path_wide,attributes);
250   path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
251   return(status);
252#endif
253}
254
255#if defined(__cplusplus) || defined(c_plusplus)
256}
257#endif
258
259#endif
260