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);
41 #ifndef HAVE_VASPRINTF
45 static inline int vasprintf (char **strp, const char *fmt, va_list ap)
48 int len = vsnprintf (NULL, 0, fmt, ap) + 1;
49 char *res = (char *)malloc (len);
53 return vsprintf (res, fmt, ap);
55 /* HACK: vsnprintf in the WinCE API behaves like
56 * the one in glibc 2.0 and doesn't return the number of characters
57 * it needed to copy the string.
58 * cf http://msdn.microsoft.com/en-us/library/1kt27hek.aspx
59 * and cf the man page of vsnprintf
61 Guess we need no more than 50 bytes. */
65 if ((res = (char *) malloc (size)) == NULL)
70 n = vsnprintf (res, size, fmt, ap);
72 /* If that worked, return the string. */
73 if (n > -1 && n < size)
79 /* Else try again with more space. */
81 size *= 2; /* twice the old size */
83 if ((np = (char *) realloc (res, size)) == NULL)
101 static inline int asprintf (char **strp, const char *fmt, ...)
106 ret = vasprintf (strp, fmt, ap);
114 static inline size_t strnlen (const char *str, size_t max)
116 const char *end = (const char *) memchr (str, 0, max);
117 return end ? (size_t)(end - str) : max;
124 static inline char *strndup (const char *str, size_t max)
126 size_t len = strnlen (str, max);
127 char *res = (char *) malloc (len + 1);
130 memcpy (res, str, len);
138 # define strlcpy vlc_strlcpy
142 # define strtof( a, b ) ((float)strtod (a, b))
146 # define atof( str ) (strtod ((str), (char **)NULL, 10))
150 # define strtoll vlc_strtoll
154 # define strsep vlc_strsep
158 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
163 long long quot; /* Quotient. */
164 long long rem; /* Remainder. */
167 static inline lldiv_t lldiv (long long numer, long long denom)
169 lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
175 # define scandir vlc_scandir
176 # define alphasort vlc_alphasort
180 static inline char *getenv (const char *name)
187 #ifndef HAVE_STRCASECMP
188 # ifndef HAVE_STRICMP
190 static inline int strcasecmp (const char *s1, const char *s2)
192 for (size_t i = 0;; i++)
194 int d = tolower (s1[i]) - tolower (s2[i]);
195 if (d || !s1[i]) return d;
200 # define strcasecmp stricmp
204 #ifndef HAVE_STRNCASECMP
205 # ifndef HAVE_STRNICMP
207 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
209 for (size_t i = 0; i < n; i++)
211 int d = tolower (s1[i]) - tolower (s2[i]);
212 if (d || !s1[i]) return d;
217 # define strncasecmp strnicmp
221 #ifndef HAVE_STRCASESTR
222 # ifndef HAVE_STRISTR
223 # define strcasestr vlc_strcasestr
225 # define strcasestr stristr
229 #ifndef HAVE_LOCALTIME_R
230 /* If localtime_r() is not provided, we assume localtime() uses
231 * thread-specific storage. */
233 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
235 struct tm *s = localtime (timep);
242 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
244 struct tm *s = gmtime (timep);
253 /* Alignment of critical static data structures */
254 #ifdef ATTRIBUTE_ALIGNED_MAX
255 # define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
257 # define ATTR_ALIGN(align)
260 #ifndef HAVE_USELOCALE
261 typedef void *locale_t;
262 # define newlocale( a, b, c ) ((locale_t)0)
263 # define uselocale( a ) ((locale_t)0)
264 # define freelocale( a ) (void)0
269 # define opendir Use_utf8_opendir_or_vlc_wopendir_instead!
270 # define readdir Use_utf8_readdir_or_vlc_wreaddir_instead!
271 # define closedir vlc_wclosedir
274 /* libintl support */
275 #define _(str) vlc_gettext (str)
277 #if defined (ENABLE_NLS)
278 # include <libintl.h>
281 #define N_(str) gettext_noop (str)
282 #define gettext_noop(str) (str)
285 static inline void rewind ( FILE *stream )
287 fseek(stream, 0L, SEEK_SET);
292 #endif /* !LIBVLC_FIXUPS_H */