1 /*****************************************************************************
2 * fixups.h: portability fixups included from config.h
3 *****************************************************************************
4 * Copyright © 1998-2008 the VideoLAN project
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.
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.
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 *****************************************************************************/
23 * This file is a collection of portability fixes
26 #ifndef LIBVLC_FIXUPS_H
27 # define LIBVLC_FIXUPS_H 1
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
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.
45 static inline char *strdup (const char *str)
47 size_t len = strlen (str) + 1;
48 char *res = (char *)malloc (len);
49 if (res) memcpy (res, str, len);
54 #ifndef HAVE_VASPRINTF
58 static inline int vasprintf (char **strp, const char *fmt, va_list ap)
60 int len = vsnprintf (NULL, 0, fmt, ap) + 1;
61 char *res = (char *)malloc (len);
65 return vsnprintf (res, len, fmt, ap);
72 static inline int asprintf (char **strp, const char *fmt, ...)
77 ret = vasprintf (strp, fmt, ap);
85 static inline size_t strnlen (const char *str, size_t max)
87 const char *end = (const char *) memchr (str, 0, max);
88 return end ? (size_t)(end - str) : max;
95 static inline char *strndup (const char *str, size_t max)
97 size_t len = strnlen (str, max);
98 char *res = (char *) malloc (len + 1);
101 memcpy (res, str, len);
109 # define strlcpy vlc_strlcpy
113 # define strtof( a, b ) ((float)strtod (a, b))
117 # define atof( str ) (strtod ((str), (char **)NULL, 10))
121 # define strtoll vlc_strtoll
125 static inline char *strsep( char **ppsz_string, const char *psz_delimiters )
127 char *psz_string = *ppsz_string;
131 char *p = strpbrk( psz_string, psz_delimiters );
145 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
150 long long quot; /* Quotient. */
151 long long rem; /* Remainder. */
154 static inline lldiv_t lldiv (long long numer, long long denom)
156 lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
162 static inline char *getenv (const char *name)
169 #ifndef HAVE_STRCASECMP
170 # ifndef HAVE_STRICMP
172 static inline int strcasecmp (const char *s1, const char *s2)
174 for (size_t i = 0;; i++)
176 int d = tolower (s1[i]) - tolower (s2[i]);
177 if (d || !s1[i]) return d;
182 # define strcasecmp stricmp
186 #ifndef HAVE_STRNCASECMP
187 # ifndef HAVE_STRNICMP
189 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
191 for (size_t i = 0; i < n; i++)
193 int d = tolower (s1[i]) - tolower (s2[i]);
194 if (d || !s1[i]) return d;
199 # define strncasecmp strnicmp
203 #ifndef HAVE_STRCASESTR
204 # ifndef HAVE_STRISTR
205 # define strcasestr vlc_strcasestr
207 # define strcasestr stristr
211 #ifndef HAVE_LOCALTIME_R
212 /* If localtime_r() is not provided, we assume localtime() uses
213 * thread-specific storage. */
215 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
217 struct tm *s = localtime (timep);
224 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
226 struct tm *s = gmtime (timep);
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)))
239 # define ATTR_ALIGN(align)
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
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
256 /* libintl support */
257 #define _(str) vlc_gettext (str)
259 #if defined (ENABLE_NLS)
260 # include <libintl.h>
263 #define N_(str) gettext_noop (str)
264 #define gettext_noop(str) (str)
267 static inline void rewind ( FILE *stream )
269 fseek(stream, 0L, SEEK_SET);
274 #endif /* !LIBVLC_FIXUPS_H */