utility-private.h revision 45ef08fd6a09813e4a8f5ddadf85ba9e0ec2cdc7
1/*
2  Copyright 1999-2013 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-base.h"
27#include "MagickCore/nt-base-private.h"
28
29extern MagickPrivate char
30  **GetPathComponents(const char *,size_t *),
31  **ListFiles(const char *,const char *,size_t *);
32
33extern MagickPrivate MagickBooleanType
34  GetExecutionPath(char *,const size_t);
35
36extern MagickPrivate ssize_t
37  GetMagickPageSize(void);
38
39extern MagickPrivate void
40  ChopPathComponents(char *,const size_t),
41  ExpandFilename(char *),
42  MagickDelay(const MagickSizeType);
43
44/*
45  Windows UTF8 compatibility methods.
46*/
47
48static inline int access_utf8(const char *path,int mode)
49{
50#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
51  return(access(path,mode));
52#else
53   int
54     count,
55     status;
56
57   WCHAR
58     *path_wide;
59
60   path_wide=(WCHAR *) NULL;
61   count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
62   path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
63   if (path_wide == (WCHAR *) NULL)
64     return(-1);
65   count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
66   status=_waccess(path_wide,mode);
67   path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
68   return(status);
69#endif
70}
71
72static inline FILE *fopen_utf8(const char *path,const char *mode)
73{
74#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
75  return(fopen(path,mode));
76#else
77   FILE
78     *file;
79
80   int
81     count;
82
83   WCHAR
84     *mode_wide,
85     *path_wide;
86
87   path_wide=(WCHAR *) NULL;
88   count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
89   path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
90   if (path_wide == (WCHAR *) NULL)
91     return((FILE *) NULL);
92   count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
93   count=MultiByteToWideChar(CP_UTF8,0,mode,-1,NULL,0);
94   mode_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*mode_wide));
95   if (mode_wide == (WCHAR *) NULL)
96     {
97       path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
98       return((FILE *) NULL);
99     }
100   count=MultiByteToWideChar(CP_UTF8,0,mode,-1,mode_wide,count);
101   file=_wfopen(path_wide,mode_wide);
102   mode_wide=(WCHAR *) RelinquishMagickMemory(mode_wide);
103   path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
104   return(file);
105#endif
106}
107
108#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
109typedef int
110  mode_t;
111#endif
112
113static inline int open_utf8(const char *path,int flags,mode_t mode)
114{
115#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
116  return(open(path,flags,mode));
117#else
118   int
119     count,
120     status;
121
122   WCHAR
123     *path_wide;
124
125   path_wide=(WCHAR *) NULL;
126   count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
127   path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
128   if (path_wide == (WCHAR *) NULL)
129     return(-1);
130   count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
131   status=_wopen(path_wide,flags,mode);
132   path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
133   return(status);
134#endif
135}
136
137static inline FILE *popen_utf8(const char *command,const char *type)
138{
139#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
140  return(popen(command,type));
141#else
142   FILE
143     *file;
144
145   int
146     count;
147
148   WCHAR
149     *type_wide,
150     *command_wide;
151
152   command_wide=(WCHAR *) NULL;
153   count=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
154   command_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*command_wide));
155   if (command_wide == (WCHAR *) NULL)
156     return((FILE *) NULL);
157   count=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,count);
158   count=MultiByteToWideChar(CP_UTF8,0,type,-1,NULL,0);
159   type_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*type_wide));
160   if (type_wide == (WCHAR *) NULL)
161     {
162       command_wide=(WCHAR *) RelinquishMagickMemory(command_wide);
163       return((FILE *) NULL);
164     }
165   count=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,count);
166   file=_wpopen(command_wide,type_wide);
167   type_wide=(WCHAR *) RelinquishMagickMemory(type_wide);
168   command_wide=(WCHAR *) RelinquishMagickMemory(command_wide);
169   return(file);
170#endif
171}
172
173static inline int remove_utf8(const char *path)
174{
175#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
176  return(unlink(path));
177#else
178   int
179     count,
180     status;
181
182   WCHAR
183     *path_wide;
184
185   path_wide=(WCHAR *) NULL;
186   count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
187   path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
188   if (path_wide == (WCHAR *) NULL)
189     return(-1);
190   count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
191   status=_wremove(path_wide);
192   path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
193   return(status);
194#endif
195}
196
197static inline int rename_utf8(const char *source,const char *destination)
198{
199#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
200  return(rename(source,destination));
201#else
202   int
203     count,
204     status;
205
206   WCHAR
207     *destination_wide,
208     *source_wide;
209
210   source_wide=(WCHAR *) NULL;
211   count=MultiByteToWideChar(CP_UTF8,0,source,-1,NULL,0);
212   source_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*source_wide));
213   if (source_wide == (WCHAR *) NULL)
214     return(-1);
215   count=MultiByteToWideChar(CP_UTF8,0,source,-1,source_wide,count);
216   count=MultiByteToWideChar(CP_UTF8,0,destination,-1,NULL,0);
217   destination_wide=(WCHAR *) AcquireQuantumMemory(count,
218     sizeof(*destination_wide));
219   if (destination_wide == (WCHAR *) NULL)
220     {
221       source_wide=(WCHAR *) RelinquishMagickMemory(source_wide);
222       return(-1);
223     }
224   count=MultiByteToWideChar(CP_UTF8,0,destination,-1,destination_wide,count);
225   status=_wrename(source_wide,destination_wide);
226   destination_wide=(WCHAR *) RelinquishMagickMemory(destination_wide);
227   source_wide=(WCHAR *) RelinquishMagickMemory(source_wide);
228   return(status);
229#endif
230}
231
232static inline int stat_utf8(const char *path,struct stat *attributes)
233{
234#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
235  return(stat(path,attributes));
236#else
237   int
238     count,
239     status;
240
241   WCHAR
242     *path_wide;
243
244   path_wide=(WCHAR *) NULL;
245   count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
246   path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
247   if (path_wide == (WCHAR *) NULL)
248     return(-1);
249   count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
250   status=_wstat64(path_wide,attributes);
251   path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
252   return(status);
253#endif
254}
255
256#if defined(__cplusplus) || defined(c_plusplus)
257}
258#endif
259
260#endif
261