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