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
156 static inline int vlc_sprintf (char *str, const char *format, ...)
160 va_start (ap, format);
161 ret = vsprintf (str, format, ap);
165 # define sprintf vlc_sprintf
168 static inline int vlc_snprintf (char *str, size_t size, const char *format, ...)
172 va_start (ap, format);
173 ret = vsnprintf (str, size, format, ap);
177 # define snprintf vlc_snprintf
179 /* Make sure we don't use flawed vasprintf or asprintf either */
180 # undef HAVE_VASPRINTF
181 # undef HAVE_ASPRINTF
184 #ifndef HAVE_VASPRINTF
188 static inline int vasprintf (char **strp, const char *fmt, va_list ap)
191 int len = vsnprintf (NULL, 0, fmt, ap) + 1;
192 char *res = (char *)malloc (len);
196 return vsnprintf (res, len, fmt, ap);
198 /* HACK: vsnprintf in the WinCE API behaves like
199 * the one in glibc 2.0 and doesn't return the number of characters
200 * it needed to copy the string.
201 * cf http://msdn.microsoft.com/en-us/library/1kt27hek.aspx
202 * and cf the man page of vsnprintf
204 Guess we need no more than 50 bytes. */
208 if ((res = (char *) malloc (size)) == NULL)
213 n = vsnprintf (res, size, fmt, ap);
215 /* If that worked, return the string. */
216 if (n > -1 && n < size)
222 /* Else try again with more space. */
223 size *= 2; /* twice the old size */
225 if ((np = (char *) realloc (res, size)) == NULL)
236 #endif /* UNDER_CE */
240 #ifndef HAVE_ASPRINTF
243 static inline int asprintf (char **strp, const char *fmt, ...)
248 ret = vasprintf (strp, fmt, ap);
256 static inline size_t strnlen (const char *str, size_t max)
258 const char *end = (const char *) memchr (str, 0, max);
259 return end ? (size_t)(end - str) : max;
266 static inline char *strndup (const char *str, size_t max)
268 size_t len = strnlen (str, max);
269 char *res = (char *) malloc (len + 1);
272 memcpy (res, str, len);
280 # define strlcpy vlc_strlcpy
284 # define strtof( a, b ) ((float)strtod (a, b))
288 # define atof( str ) (strtod ((str), (char **)NULL, 10))
292 # define strtoll vlc_strtoll
296 static inline char *strsep( char **ppsz_string, const char *psz_delimiters )
298 char *psz_string = *ppsz_string;
302 char *p = strpbrk( psz_string, psz_delimiters );
316 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
321 long long quot; /* Quotient. */
322 long long rem; /* Remainder. */
325 static inline lldiv_t lldiv (long long numer, long long denom)
327 lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
333 # define scandir vlc_scandir
334 # define alphasort vlc_alphasort
338 static inline char *getenv (const char *name)
345 #ifndef HAVE_STRCASECMP
346 # ifndef HAVE_STRICMP
348 static inline int strcasecmp (const char *s1, const char *s2)
350 for (size_t i = 0;; i++)
352 int d = tolower (s1[i]) - tolower (s2[i]);
353 if (d || !s1[i]) return d;
358 # define strcasecmp stricmp
362 #ifndef HAVE_STRNCASECMP
363 # ifndef HAVE_STRNICMP
365 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
367 for (size_t i = 0; i < n; i++)
369 int d = tolower (s1[i]) - tolower (s2[i]);
370 if (d || !s1[i]) return d;
375 # define strncasecmp strnicmp
379 #ifndef HAVE_STRCASESTR
380 # ifndef HAVE_STRISTR
381 # define strcasestr vlc_strcasestr
383 # define strcasestr stristr
387 #ifndef HAVE_LOCALTIME_R
388 /* If localtime_r() is not provided, we assume localtime() uses
389 * thread-specific storage. */
391 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
393 struct tm *s = localtime (timep);
400 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
402 struct tm *s = gmtime (timep);
411 /* Alignment of critical static data structures */
412 #ifdef ATTRIBUTE_ALIGNED_MAX
413 # define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
415 # define ATTR_ALIGN(align)
418 #ifndef HAVE_USELOCALE
419 typedef void *locale_t;
420 # define newlocale( a, b, c ) ((locale_t)0)
421 # define uselocale( a ) ((locale_t)0)
422 # define freelocale( a ) (void)0
427 # define opendir Use_utf8_opendir_or_vlc_wopendir_instead!
428 # define readdir Use_utf8_readdir_or_vlc_wreaddir_instead!
429 # define closedir vlc_wclosedir
432 /* libintl support */
433 #define _(str) vlc_gettext (str)
435 #if defined (ENABLE_NLS)
436 # include <libintl.h>
439 #define N_(str) gettext_noop (str)
440 #define gettext_noop(str) (str)
443 static inline void rewind ( FILE *stream )
445 fseek(stream, 0L, SEEK_SET);
450 #endif /* !LIBVLC_FIXUPS_H */