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