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)
47 int len = vsnprintf (NULL, 0, fmt, ap) + 1;
48 char *res = (char *)malloc (len);
52 return vsprintf (res, fmt, ap);
59 static inline int asprintf (char **strp, const char *fmt, ...)
64 ret = vasprintf (strp, fmt, ap);
72 static inline size_t strnlen (const char *str, size_t max)
74 const char *end = (const char *) memchr (str, 0, max);
75 return end ? (size_t)(end - str) : max;
82 static inline char *strndup (const char *str, size_t max)
84 size_t len = strnlen (str, max);
85 char *res = (char *) malloc (len + 1);
88 memcpy (res, str, len);
96 # define strlcpy vlc_strlcpy
100 # define strtof( a, b ) ((float)strtod (a, b))
104 # define atof( str ) (strtod ((str), (char **)NULL, 10))
108 # define strtoll vlc_strtoll
112 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
117 long long quot; /* Quotient. */
118 long long rem; /* Remainder. */
121 static inline lldiv_t lldiv (long long numer, long long denom)
123 lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
129 # define scandir vlc_scandir
130 # define alphasort vlc_alphasort
134 static inline getenv (const char *name)
141 #ifndef HAVE_STRCASECMP
142 # ifndef HAVE_STRICMP
144 static inline int strcasecmp (const char *s1, const char *s2)
146 for (size_t i = 0;; i++)
148 int d = tolower (s1[i]) - tolower (s2[i]);
149 if (d || !s1[i]) return d;
154 # define strcasecmp stricmp
158 #ifndef HAVE_STRNCASECMP
159 # ifndef HAVE_STRNICMP
161 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
163 for (size_t i = 0; i < n; i++)
165 int d = tolower (s1[i]) - tolower (s2[i]);
166 if (d || !s1[i]) return d;
171 # define strncasecmp strnicmp
175 #ifndef HAVE_STRCASESTR
176 # ifndef HAVE_STRISTR
177 # define strcasestr vlc_strcasestr
179 # define strcasestr stristr
183 #ifndef HAVE_LOCALTIME_R
184 /* If localtime_r() is not provided, we assume localtime() uses
185 * thread-specific storage. */
187 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
189 struct tm *s = localtime (timep);
196 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
198 struct tm *s = gmtime (timep);
207 /* Alignment of critical static data structures */
208 #ifdef ATTRIBUTE_ALIGNED_MAX
209 # define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
211 # define ATTR_ALIGN(align)
214 #ifndef HAVE_USELOCALE
215 typedef void *locale_t;
216 # define newlocale( a, b, c ) ((locale_t)0)
217 # define uselocale( a ) ((locale_t)0)
218 # define freelocale( a ) (void)0
223 # define opendir Use_utf8_opendir_or_vlc_wopendir_instead!
224 # define readdir Use_utf8_readdir_or_vlc_wreaddir_instead!
225 # define closedir vlc_wclosedir
228 /* libintl support */
229 #define _(str) vlc_gettext (str)
231 #if defined (ENABLE_NLS)
232 # include <libintl.h>
234 # define dgettext(dom, str) ((char *)(str))
237 #define N_(str) gettext_noop (str)
238 #define gettext_noop(str) (str)
240 #endif /* !LIBVLC_FIXUPS_H */