utility-private.h revision 99d6fa0654883b97e5ba16e5fd94fea06c107b5d
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)
22extern "C" {
23#endif
24
25extern MagickPrivate char
26  **GetPathComponents(const char *,size_t *),
27  **ListFiles(const char *,const char *,size_t *);
28
29extern MagickPrivate MagickBooleanType
30  GetExecutionPath(char *,const size_t);
31
32extern MagickPrivate ssize_t
33  GetMagickPageSize(void);
34
35extern MagickPrivate void
36  ChopPathComponents(char *,const size_t),
37  ExpandFilename(char *),
38  MagickDelay(const MagickSizeType);
39
40/*
41  Windows UTF8 compatibility methods.
42*/
43
44static 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
68static 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     status;
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(-1);
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(-1);
95     }
96   count=MultiByteToWideChar(CP_UTF8,0,mode,-1,mode_wide,count);
97   file=_wfopen(path_wide,mode_width);
98   mode_wide=RelinquishMagickMemory(mode_wide);
99   path_wide=RelinquishMagickMemory(path_wide);
100   return(file);
101#endif
102}
103
104static 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
128static inline int remove_utf8(const char *path)
129{
130#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
131  return(unlink(path));
132#else
133   int
134     count,
135     status;
136
137   WCHAR
138     *path_wide;
139
140   path_wide=(WCHAR *) NULL;
141   count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
142   path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
143   if (path_wide == (WCHAR *) NULL)
144     return(-1);
145   count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
146   status=_wremove(path_wide);
147   path_wide=RelinquishMagickMemory(path_wide);
148   return(status);
149#endif
150}
151
152static inline int stat_utf8(const char *path,struct stat *attributes)
153{
154#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
155  return(stat(path,attributes));
156#else
157   int
158     count,
159     status;
160
161   WCHAR
162     *path_wide;
163
164   path_wide=(WCHAR *) NULL;
165   count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
166   path_wide=(WCHAR *) AcquireQuantumMemory(count,sizeof(*path_wide));
167   if (path_wide == (WCHAR *) NULL)
168     return(-1);
169   count=MultiByteToWideChar(CP_UTF8,0,path,-1,path_wide,count);
170   status=_wstat(path_wide,attributes);
171   path_wide=RelinquishMagickMemory(path_wide);
172   return(status);
173#endif
174}
175
176#if defined(__cplusplus) || defined(c_plusplus)
177}
178#endif
179
180#endif
181