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