MagickCore  6.8.5
utility-private.h
Go to the documentation of this file.
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)
22 extern "C" {
23 #endif
24 
25 #include "magick/memory_.h"
26 #include "magick/nt-base.h"
27 
29  ShredFile(const char *);
30 
31 /*
32  Windows UTF8 compatibility methods.
33 */
34 
35 static inline int access_utf8(const char *path,int mode)
36 {
37 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
38  return(access(path,mode));
39 #else
40  int
41  count,
42  status;
43 
44  WCHAR
45  *path_wide;
46 
47  path_wide=(WCHAR *) NULL;
48  count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
49  path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
50  if (path_wide == (WCHAR *) NULL)
51  return(-1);
52  count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
53  status=_waccess(path_wide,mode);
54  path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
55  return(status);
56 #endif
57 }
58 
59 static inline FILE *fopen_utf8(const char *path,const char *mode)
60 {
61 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
62  return(fopen(path,mode));
63 #else
64  FILE
65  *file;
66 
67  int
68  count;
69 
70  WCHAR
71  *mode_wide,
72  *path_wide;
73 
74  path_wide=(WCHAR *) NULL;
75  count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
76  path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
77  if (path_wide == (WCHAR *) NULL)
78  return((FILE *) NULL);
79  count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
80  count=MultiByteToWideChar(CP_UTF8,0,mode,-1,NULL,0);
81  mode_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*mode_wide));
82  if (mode_wide == (WCHAR *) NULL)
83  {
84  path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
85  return((FILE *) NULL);
86  }
87  count=MultiByteToWideChar(CP_UTF8,0,mode,-1,mode_wide,count);
88  file=_wfopen(path_wide,mode_wide);
89  mode_wide=(WCHAR *) RelinquishMagickMemory(mode_wide);
90  path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
91  return(file);
92 #endif
93 }
94 
95 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__MINGW64__)
96 typedef int
97  mode_t;
98 #endif
99 
100 static inline int open_utf8(const char *path,int flags,mode_t mode)
101 {
102 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
103  return(open(path,flags,mode));
104 #else
105  int
106  count,
107  status;
108 
109  WCHAR
110  *path_wide;
111 
112  path_wide=(WCHAR *) NULL;
113  count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
114  path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
115  if (path_wide == (WCHAR *) NULL)
116  return(-1);
117  count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
118  status=_wopen(path_wide,flags,mode);
119  path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
120  return(status);
121 #endif
122 }
123 
124 static inline FILE *popen_utf8(const char *command,const char *type)
125 {
126 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
127  return(popen(command,type));
128 #else
129  FILE
130  *file;
131 
132  int
133  count;
134 
135  WCHAR
136  *type_wide,
137  *command_wide;
138 
139  command_wide=(WCHAR *) NULL;
140  count=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
141  command_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*command_wide));
142  if (command_wide == (WCHAR *) NULL)
143  return((FILE *) NULL);
144  count=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,count);
145  count=MultiByteToWideChar(CP_UTF8,0,type,-1,NULL,0);
146  type_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*type_wide));
147  if (type_wide == (WCHAR *) NULL)
148  {
149  command_wide=(WCHAR *) RelinquishMagickMemory(command_wide);
150  return((FILE *) NULL);
151  }
152  count=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,count);
153  file=_wpopen(command_wide,type_wide);
154  type_wide=(WCHAR *) RelinquishMagickMemory(type_wide);
155  command_wide=(WCHAR *) RelinquishMagickMemory(command_wide);
156  return(file);
157 #endif
158 }
159 
160 static inline int remove_utf8(const char *path)
161 {
162 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
163  return(unlink(path));
164 #else
165  int
166  count,
167  status;
168 
169  WCHAR
170  *path_wide;
171 
172  path_wide=(WCHAR *) NULL;
173  count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
174  path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
175  if (path_wide == (WCHAR *) NULL)
176  return(-1);
177  count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
178  status=_wremove(path_wide);
179  path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
180  return(status);
181 #endif
182 }
183 
184 static inline int rename_utf8(const char *source,const char *destination)
185 {
186 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
187  return(rename(source,destination));
188 #else
189  int
190  count,
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=(WCHAR *) RelinquishMagickMemory(source_wide);
209  return(-1);
210  }
211  count=MultiByteToWideChar(CP_UTF8,0,destination,-1,destination_wide,count);
212  status=_wrename(source_wide,destination_wide);
213  destination_wide=(WCHAR *) RelinquishMagickMemory(destination_wide);
214  source_wide=(WCHAR *) RelinquishMagickMemory(source_wide);
215  return(status);
216 #endif
217 }
218 
219 static inline int stat_utf8(const char *path,struct stat *attributes)
220 {
221 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
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=_wstat64(path_wide,attributes);
238  path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
239  return(status);
240 #endif
241 }
242 
243 #if defined(__cplusplus) || defined(c_plusplus)
244 }
245 #endif
246 
247 #endif