]> git.sesse.net Git - vlc/blob - include/vlc_fixups.h
Remove unused scandir check
[vlc] / include / vlc_fixups.h
1 /*****************************************************************************
2  * fixups.h: portability fixups included from config.h
3  *****************************************************************************
4  * Copyright © 1998-2008 the VideoLAN project
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 /**
22  * \file
23  * This file is a collection of portability fixes
24  */
25
26 #ifndef LIBVLC_FIXUPS_H
27 # define LIBVLC_FIXUPS_H 1
28
29 #ifdef __MINGW32_VERSION
30 # if __MINGW32_MAJOR_VERSION == 3 && __MINGW32_MINOR_VERSION < 14
31 #  error This mingw-runtime is too old, it has a broken vsnprintf
32 # endif
33 /* mingw-runtime provides the whole printf family in a c99 compliant way. */
34 /* the way to enable this is to define __USE_MINGW_ANSI_STDIO, or something
35  * such as _ISOC99_SOURCE; the former is done by configure.ac */
36 /* This isn't done here, since some modules don't include config.h and
37  * therefore this as the first include file */
38 #elif defined UNDER_CE
39 # error Window CE support for *printf needs fixing.
40 #endif
41
42 #ifndef HAVE_STRDUP
43 # include <string.h>
44 # include <stdlib.h>
45 static inline char *strdup (const char *str)
46 {
47     size_t len = strlen (str) + 1;
48     char *res = (char *)malloc (len);
49     if (res) memcpy (res, str, len);
50     return res;
51 }
52 #endif
53
54 #ifndef HAVE_VASPRINTF
55 # include <stdio.h>
56 # include <stdlib.h>
57 # include <stdarg.h>
58 static inline int vasprintf (char **strp, const char *fmt, va_list ap)
59 {
60     int len = vsnprintf (NULL, 0, fmt, ap) + 1;
61     char *res = (char *)malloc (len);
62     if (res == NULL)
63         return -1;
64     *strp = res;
65     return vsnprintf (res, len, fmt, ap);
66 }
67 #endif
68
69 #ifndef HAVE_ASPRINTF
70 # include <stdio.h>
71 # include <stdarg.h>
72 static inline int asprintf (char **strp, const char *fmt, ...)
73 {
74     va_list ap;
75     int ret;
76     va_start (ap, fmt);
77     ret = vasprintf (strp, fmt, ap);
78     va_end (ap);
79     return ret;
80 }
81 #endif
82
83 #ifndef HAVE_STRNLEN
84 # include <string.h>
85 static inline size_t strnlen (const char *str, size_t max)
86 {
87     const char *end = (const char *) memchr (str, 0, max);
88     return end ? (size_t)(end - str) : max;
89 }
90 #endif
91
92 #ifndef HAVE_STRNDUP
93 # include <string.h>
94 # include <stdlib.h>
95 static inline char *strndup (const char *str, size_t max)
96 {
97     size_t len = strnlen (str, max);
98     char *res = (char *) malloc (len + 1);
99     if (res)
100     {
101         memcpy (res, str, len);
102         res[len] = '\0';
103     }
104     return res;
105 }
106 #endif
107
108 #ifndef HAVE_STRLCPY
109 # define strlcpy vlc_strlcpy
110 #endif
111
112 #ifndef HAVE_STRTOF
113 # define strtof( a, b ) ((float)strtod (a, b))
114 #endif
115
116 #ifndef HAVE_ATOF
117 # define atof( str ) (strtod ((str), (char **)NULL, 10))
118 #endif
119
120 #ifndef HAVE_STRTOLL
121 # define strtoll vlc_strtoll
122 #endif
123
124 #ifndef HAVE_STRSEP
125 static inline char *strsep( char **ppsz_string, const char *psz_delimiters )
126 {
127     char *psz_string = *ppsz_string;
128     if( !psz_string )
129         return NULL;
130
131     char *p = strpbrk( psz_string, psz_delimiters );
132     if( !p )
133     {
134         *ppsz_string = NULL;
135         return psz_string;
136     }
137     *p++ = '\0';
138
139     *ppsz_string = p;
140     return psz_string;
141 }
142 #endif
143
144 #ifndef HAVE_ATOLL
145 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
146 #endif
147
148 #ifndef HAVE_LLDIV
149 typedef struct {
150     long long quot; /* Quotient. */
151     long long rem;  /* Remainder. */
152 } lldiv_t;
153
154 static inline lldiv_t lldiv (long long numer, long long denom)
155 {
156     lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
157     return d;
158 }
159 #endif
160
161 #ifndef HAVE_GETENV
162 static inline char *getenv (const char *name)
163 {
164     (void)name;
165     return NULL;
166 }
167 #endif
168
169 #ifndef HAVE_STRCASECMP
170 # ifndef HAVE_STRICMP
171 #  include <ctype.h>
172 static inline int strcasecmp (const char *s1, const char *s2)
173 {
174     for (size_t i = 0;; i++)
175     {
176         int d = tolower (s1[i]) - tolower (s2[i]);
177         if (d || !s1[i]) return d;
178     }
179     return 0;
180 }
181 # else
182 #  define strcasecmp stricmp
183 # endif
184 #endif
185
186 #ifndef HAVE_STRNCASECMP
187 # ifndef HAVE_STRNICMP
188 #  include <ctype.h>
189 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
190 {
191     for (size_t i = 0; i < n; i++)
192     {
193         int d = tolower (s1[i]) - tolower (s2[i]);
194         if (d || !s1[i]) return d;
195     }
196     return 0;
197 }
198 # else
199 #  define strncasecmp strnicmp
200 # endif
201 #endif
202
203 #ifndef HAVE_STRCASESTR
204 # ifndef HAVE_STRISTR
205 #  define strcasestr vlc_strcasestr
206 # else
207 #  define strcasestr stristr
208 # endif
209 #endif
210
211 #ifndef HAVE_LOCALTIME_R
212 /* If localtime_r() is not provided, we assume localtime() uses
213  * thread-specific storage. */
214 # include <time.h>
215 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
216 {
217     struct tm *s = localtime (timep);
218     if (s == NULL)
219         return NULL;
220
221     *result = *s;
222     return result;
223 }
224 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
225 {
226     struct tm *s = gmtime (timep);
227     if (s == NULL)
228         return NULL;
229
230     *result = *s;
231     return result;
232 }
233 #endif
234
235 /* Alignment of critical static data structures */
236 #ifdef ATTRIBUTE_ALIGNED_MAX
237 #   define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
238 #else
239 #   define ATTR_ALIGN(align)
240 #endif
241
242 #ifndef HAVE_USELOCALE
243 typedef void *locale_t;
244 # define newlocale( a, b, c ) ((locale_t)0)
245 # define uselocale( a ) ((locale_t)0)
246 # define freelocale( a ) (void)0
247 #endif
248
249 #ifdef WIN32
250 # include <dirent.h>
251 # define opendir Use_utf8_opendir_or_vlc_wopendir_instead!
252 # define readdir Use_utf8_readdir_or_vlc_wreaddir_instead!
253 # define closedir vlc_wclosedir
254 #endif
255
256 /* libintl support */
257 #define _(str) vlc_gettext (str)
258
259 #if defined (ENABLE_NLS)
260 # include <libintl.h>
261 #endif
262
263 #define N_(str) gettext_noop (str)
264 #define gettext_noop(str) (str)
265
266 #ifdef UNDER_CE
267 static inline void rewind ( FILE *stream )
268 {
269     fseek(stream, 0L, SEEK_SET);
270     clearerr(stream);
271 }
272 #endif
273
274 #endif /* !LIBVLC_FIXUPS_H */