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
32 static inline char *strdup (const char *str)
34 size_t len = strlen (str) + 1;
35 char *res = (char *)malloc (len);
36 if (res) memcpy (res, str, len);
42 /* Windows' printf doesn't support %z modifiers, thus we need to rewrite
43 * the format string in a wrapper. */
46 static inline char *vlc_fix_format_string (const char *format)
50 const char *src = format, *tmp;
53 while ((tmp = strstr (src, "%z")) != NULL)
61 fmt = (char*)malloc (strlen (format) + n + 1);
67 while ((tmp = strstr (src, "%z")) != NULL)
72 memcpy (dst, "%ll", 3);
79 if (strstr (format, "%z") == NULL)
82 fmt = strdup (format);
86 while ((f = strstr (fmt, "%z")) != NULL)
97 static inline int vlc_vprintf (const char *format, va_list ap)
99 char *fmt = vlc_fix_format_string (format);
100 int ret = vprintf (fmt ? fmt : format, ap);
104 # define vprintf vlc_vprintf
106 static inline int vlc_vfprintf (FILE *stream, const char *format, va_list ap)
108 char *fmt = vlc_fix_format_string (format);
109 int ret = vfprintf (stream, fmt ? fmt : format, ap);
113 # define vfprintf vlc_vfprintf
115 static inline int vlc_vsprintf (char *str, const char *format, va_list ap)
117 char *fmt = vlc_fix_format_string (format);
118 int ret = vsprintf (str, fmt ? fmt : format, ap);
122 # define vsprintf vlc_vsprintf
124 static inline int vlc_vsnprintf (char *str, size_t size, const char *format, va_list ap)
126 char *fmt = vlc_fix_format_string (format);
127 /* traditionally, MSVCRT has provided vsnprintf as _vsnprintf;
128 * to 'aid' portability/standards compliance, mingw provides a
129 * static version of vsnprintf that is buggy. Be sure to use
130 * MSVCRT version, at least it behaves as expected */
131 int ret = _vsnprintf (str, size, fmt ? fmt : format, ap);
135 # define vsnprintf vlc_vsnprintf
137 static inline int vlc_printf (const char *format, ...)
141 va_start (ap, format);
142 ret = vlc_vprintf (format, ap);
146 # define printf(...) vlc_printf(__VA_ARGS__)
148 static inline int vlc_fprintf (FILE *stream, const char *format, ...)
152 va_start (ap, format);
153 ret = vlc_vfprintf (stream, format, ap);
157 # define fprintf vlc_fprintf
160 static inline int vlc_sprintf (char *str, const char *format, ...)
164 va_start (ap, format);
165 ret = vlc_vsprintf (str, format, ap);
169 # define sprintf vlc_sprintf
172 static inline int vlc_snprintf (char *str, size_t size, const char *format, ...)
176 va_start (ap, format);
177 ret = vlc_vsnprintf (str, size, format, ap);
181 /* win32: snprintf must always be vlc_snprintf or _snprintf,
182 * see comment in vlc_vsnprintf */
183 # define snprintf vlc_snprintf
185 /* Make sure we don't use flawed vasprintf or asprintf either */
186 # undef HAVE_VASPRINTF
187 # undef HAVE_ASPRINTF
190 #ifndef HAVE_VASPRINTF
194 static inline int vasprintf (char **strp, const char *fmt, va_list ap)
197 int len = vsnprintf (NULL, 0, fmt, ap) + 1;
198 char *res = (char *)malloc (len);
202 return vsnprintf (res, len, fmt, ap);
204 /* HACK: vsnprintf in the WinCE API behaves like
205 * the one in glibc 2.0 and doesn't return the number of characters
206 * it needed to copy the string.
207 * cf http://msdn.microsoft.com/en-us/library/1kt27hek.aspx
208 * and cf the man page of vsnprintf
210 Guess we need no more than 50 bytes. */
214 if ((res = (char *) malloc (size)) == NULL)
219 n = vsnprintf (res, size, fmt, ap);
221 /* If that worked, return the string. */
222 if (n > -1 && n < size)
228 /* Else try again with more space. */
229 size *= 2; /* twice the old size */
231 if ((np = (char *) realloc (res, size)) == NULL)
242 #endif /* UNDER_CE */
246 #ifndef HAVE_ASPRINTF
249 static inline int asprintf (char **strp, const char *fmt, ...)
254 ret = vasprintf (strp, fmt, ap);
262 static inline size_t strnlen (const char *str, size_t max)
264 const char *end = (const char *) memchr (str, 0, max);
265 return end ? (size_t)(end - str) : max;
272 static inline char *strndup (const char *str, size_t max)
274 size_t len = strnlen (str, max);
275 char *res = (char *) malloc (len + 1);
278 memcpy (res, str, len);
286 # define strlcpy vlc_strlcpy
290 # define strtof( a, b ) ((float)strtod (a, b))
294 # define atof( str ) (strtod ((str), (char **)NULL, 10))
298 # define strtoll vlc_strtoll
302 static inline char *strsep( char **ppsz_string, const char *psz_delimiters )
304 char *psz_string = *ppsz_string;
308 char *p = strpbrk( psz_string, psz_delimiters );
322 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
327 long long quot; /* Quotient. */
328 long long rem; /* Remainder. */
331 static inline lldiv_t lldiv (long long numer, long long denom)
333 lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
339 # define scandir vlc_scandir
340 # define alphasort vlc_alphasort
344 static inline char *getenv (const char *name)
351 #ifndef HAVE_STRCASECMP
352 # ifndef HAVE_STRICMP
354 static inline int strcasecmp (const char *s1, const char *s2)
356 for (size_t i = 0;; i++)
358 int d = tolower (s1[i]) - tolower (s2[i]);
359 if (d || !s1[i]) return d;
364 # define strcasecmp stricmp
368 #ifndef HAVE_STRNCASECMP
369 # ifndef HAVE_STRNICMP
371 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
373 for (size_t i = 0; i < n; i++)
375 int d = tolower (s1[i]) - tolower (s2[i]);
376 if (d || !s1[i]) return d;
381 # define strncasecmp strnicmp
385 #ifndef HAVE_STRCASESTR
386 # ifndef HAVE_STRISTR
387 # define strcasestr vlc_strcasestr
389 # define strcasestr stristr
393 #ifndef HAVE_LOCALTIME_R
394 /* If localtime_r() is not provided, we assume localtime() uses
395 * thread-specific storage. */
397 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
399 struct tm *s = localtime (timep);
406 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
408 struct tm *s = gmtime (timep);
417 /* Alignment of critical static data structures */
418 #ifdef ATTRIBUTE_ALIGNED_MAX
419 # define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
421 # define ATTR_ALIGN(align)
424 #ifndef HAVE_USELOCALE
425 typedef void *locale_t;
426 # define newlocale( a, b, c ) ((locale_t)0)
427 # define uselocale( a ) ((locale_t)0)
428 # define freelocale( a ) (void)0
433 # define opendir Use_utf8_opendir_or_vlc_wopendir_instead!
434 # define readdir Use_utf8_readdir_or_vlc_wreaddir_instead!
435 # define closedir vlc_wclosedir
438 /* libintl support */
439 #define _(str) vlc_gettext (str)
441 #if defined (ENABLE_NLS)
442 # include <libintl.h>
445 #define N_(str) gettext_noop (str)
446 #define gettext_noop(str) (str)
449 static inline void rewind ( FILE *stream )
451 fseek(stream, 0L, SEEK_SET);
456 #endif /* !LIBVLC_FIXUPS_H */