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 int ret = vsnprintf (str, size, fmt ? fmt : format, ap);
131 # define vsnprintf vlc_vsnprintf
133 static inline int vlc_printf (const char *format, ...)
137 va_start (ap, format);
138 ret = vprintf (format, ap);
142 # define printf(...) vlc_printf(__VA_ARGS__)
144 static inline int vlc_fprintf (FILE *stream, const char *format, ...)
148 va_start (ap, format);
149 ret = vfprintf (stream, format, ap);
153 # define fprintf vlc_fprintf
155 static inline int vlc_sprintf (char *str, const char *format, ...)
159 va_start (ap, format);
160 ret = vsprintf (str, format, ap);
164 # define sprintf vlc_sprintf
166 static inline int vlc_snprintf (char *str, size_t size, const char *format, ...)
170 va_start (ap, format);
171 ret = vsnprintf (str, size, format, ap);
175 # define snprintf vlc_snprintf
177 /* Make sure we don't use flawed vasprintf or asprintf either */
178 # undef HAVE_VASPRINTF
179 # undef HAVE_ASPRINTF
182 #ifndef HAVE_VASPRINTF
186 static inline int vasprintf (char **strp, const char *fmt, va_list ap)
189 int len = vsnprintf (NULL, 0, fmt, ap) + 1;
190 char *res = (char *)malloc (len);
194 return vsnprintf (res, len, fmt, ap);
196 /* HACK: vsnprintf in the WinCE API behaves like
197 * the one in glibc 2.0 and doesn't return the number of characters
198 * it needed to copy the string.
199 * cf http://msdn.microsoft.com/en-us/library/1kt27hek.aspx
200 * and cf the man page of vsnprintf
202 Guess we need no more than 50 bytes. */
206 if ((res = (char *) malloc (size)) == NULL)
211 n = vsnprintf (res, size, fmt, ap);
213 /* If that worked, return the string. */
214 if (n > -1 && n < size)
220 /* Else try again with more space. */
221 size *= 2; /* twice the old size */
223 if ((np = (char *) realloc (res, size)) == NULL)
234 #endif /* UNDER_CE */
238 #ifndef HAVE_ASPRINTF
241 static inline int asprintf (char **strp, const char *fmt, ...)
246 ret = vasprintf (strp, fmt, ap);
254 static inline size_t strnlen (const char *str, size_t max)
256 const char *end = (const char *) memchr (str, 0, max);
257 return end ? (size_t)(end - str) : max;
264 static inline char *strndup (const char *str, size_t max)
266 size_t len = strnlen (str, max);
267 char *res = (char *) malloc (len + 1);
270 memcpy (res, str, len);
278 # define strlcpy vlc_strlcpy
282 # define strtof( a, b ) ((float)strtod (a, b))
286 # define atof( str ) (strtod ((str), (char **)NULL, 10))
290 # define strtoll vlc_strtoll
294 static inline char *strsep( char **ppsz_string, const char *psz_delimiters )
296 char *psz_string = *ppsz_string;
300 char *p = strpbrk( psz_string, psz_delimiters );
314 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
319 long long quot; /* Quotient. */
320 long long rem; /* Remainder. */
323 static inline lldiv_t lldiv (long long numer, long long denom)
325 lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
331 # define scandir vlc_scandir
332 # define alphasort vlc_alphasort
336 static inline char *getenv (const char *name)
343 #ifndef HAVE_STRCASECMP
344 # ifndef HAVE_STRICMP
346 static inline int strcasecmp (const char *s1, const char *s2)
348 for (size_t i = 0;; i++)
350 int d = tolower (s1[i]) - tolower (s2[i]);
351 if (d || !s1[i]) return d;
356 # define strcasecmp stricmp
360 #ifndef HAVE_STRNCASECMP
361 # ifndef HAVE_STRNICMP
363 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
365 for (size_t i = 0; i < n; i++)
367 int d = tolower (s1[i]) - tolower (s2[i]);
368 if (d || !s1[i]) return d;
373 # define strncasecmp strnicmp
377 #ifndef HAVE_STRCASESTR
378 # ifndef HAVE_STRISTR
379 # define strcasestr vlc_strcasestr
381 # define strcasestr stristr
385 #ifndef HAVE_LOCALTIME_R
386 /* If localtime_r() is not provided, we assume localtime() uses
387 * thread-specific storage. */
389 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
391 struct tm *s = localtime (timep);
398 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
400 struct tm *s = gmtime (timep);
409 /* Alignment of critical static data structures */
410 #ifdef ATTRIBUTE_ALIGNED_MAX
411 # define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
413 # define ATTR_ALIGN(align)
416 #ifndef HAVE_USELOCALE
417 typedef void *locale_t;
418 # define newlocale( a, b, c ) ((locale_t)0)
419 # define uselocale( a ) ((locale_t)0)
420 # define freelocale( a ) (void)0
425 # define opendir Use_utf8_opendir_or_vlc_wopendir_instead!
426 # define readdir Use_utf8_readdir_or_vlc_wreaddir_instead!
427 # define closedir vlc_wclosedir
430 /* libintl support */
431 #define _(str) vlc_gettext (str)
433 #if defined (ENABLE_NLS)
434 # include <libintl.h>
437 #define N_(str) gettext_noop (str)
438 #define gettext_noop(str) (str)
441 static inline void rewind ( FILE *stream )
443 fseek(stream, 0L, SEEK_SET);
448 #endif /* !LIBVLC_FIXUPS_H */