]> git.sesse.net Git - vlc/blob - include/vlc_fixups.h
win32: Fix *printf & require mingw32-runtime version > 3.13
[vlc] / include / vlc_fixups.h
1 /*****************************************************************************
2  * fixups.h: portability fixups included from config.h
3  *****************************************************************************
4  * Copyright © 1998-2008 the VideoLAN project
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20
21 /**
22  * \file
23  * This file is a collection of portability fixes
24  */
25
26 #ifndef LIBVLC_FIXUPS_H
27 # define LIBVLC_FIXUPS_H 1
28
29 #ifdef __MINGW32_VERSION
30 # if __MINGW32_MAJOR_VERSION == 3 && __MINGW32_MINOR_VERSION < 14
31 #  error This mingw-runtime is too old, it has a broken vsnprintf
32 # endif
33 /* mingw-runtime provides the whole printf family in a c99 compliant way. */
34 /* the way to enable this is to define __USE_MINGW_ANSI_STDIO, or something
35  * such as _ISOC99_SOURCE; the former is done by configure.ac */
36 /* This isn't done here, since some modules don't include config.h and
37  * therefore this as the first include file */
38 #elif defined UNDER_CE
39 # error Window CE support for *printf needs fixing.
40 #endif
41
42 #ifndef HAVE_STRDUP
43 # include <string.h>
44 # include <stdlib.h>
45 static inline char *strdup (const char *str)
46 {
47     size_t len = strlen (str) + 1;
48     char *res = (char *)malloc (len);
49     if (res) memcpy (res, str, len);
50     return res;
51 }
52 #endif
53
54 #ifdef WIN32
55 /* Windows' printf doesn't support %z modifiers, thus we need to rewrite
56  * the format string in a wrapper. */
57 # include <string.h>
58 # include <stdlib.h>
59 static inline char *vlc_fix_format_string (const char *format)
60 {
61     char *fmt;
62 # ifdef WIN64
63     const char *src = format, *tmp;
64     char *dst;
65     size_t n = 0;
66     while ((tmp = strstr (src, "%z")) != NULL)
67     {
68         n++;
69         src = tmp + 2;
70     }
71     if (n == 0)
72         return NULL;
73
74     fmt = (char*)malloc (strlen (format) + n + 1);
75     if (fmt == NULL)
76         return NULL;
77
78     src = format;
79     dst = fmt;
80     while ((tmp = strstr (src, "%z")) != NULL)
81     {
82         size_t d = tmp - src;
83         memcpy (dst, src, d);
84         dst += d;
85         memcpy (dst, "%ll", 3);
86         dst += 3;
87         src = tmp + 2;
88     }
89     strcpy (dst, src);
90 # else
91     char *f;
92     if (strstr (format, "%z") == NULL)
93         return NULL;
94
95     fmt = strdup (format);
96     if (fmt == NULL)
97         return NULL;
98
99     while ((f = strstr (fmt, "%z")) != NULL)
100     {
101        f[1] = 'l';
102     }
103 # endif
104     return fmt;
105 }
106
107 # include <stdio.h>
108 # include <stdarg.h>
109
110 static inline int vlc_vprintf (const char *format, va_list ap)
111 {
112     char *fmt = vlc_fix_format_string (format);
113     int ret = vprintf (fmt ? fmt : format, ap);
114     free (fmt);
115     return ret;
116 }
117 # define vprintf vlc_vprintf
118
119 static inline int vlc_vfprintf (FILE *stream, const char *format, va_list ap)
120 {
121     char *fmt = vlc_fix_format_string (format);
122     int ret = vfprintf (stream, fmt ? fmt : format, ap);
123     free (fmt);
124     return ret;
125 }
126 # define vfprintf vlc_vfprintf
127
128 static inline int vlc_vsprintf (char *str, const char *format, va_list ap)
129 {
130     char *fmt = vlc_fix_format_string (format);
131     int ret = vsprintf (str, fmt ? fmt : format, ap);
132     free (fmt);
133     return ret;
134 }
135 # define vsprintf vlc_vsprintf
136
137 static inline int vlc_vsnprintf (char *str, size_t size, const char *format, va_list ap)
138 {
139     char *fmt = vlc_fix_format_string (format);
140     int ret = vsnprintf (str, size, fmt ? fmt : format, ap);
141     free (fmt);
142     return ret;
143 }
144 # define vsnprintf vlc_vsnprintf
145
146 static inline int vlc_printf (const char *format, ...)
147 {
148     va_list ap;
149     int ret;
150     va_start (ap, format);
151     ret = vprintf (format, ap);
152     va_end (ap);
153     return ret;
154 }
155 # define printf(...) vlc_printf(__VA_ARGS__)
156
157 static inline int vlc_fprintf (FILE *stream, const char *format, ...)
158 {
159     va_list ap;
160     int ret;
161     va_start (ap, format);
162     ret = vfprintf (stream, format, ap);
163     va_end (ap);
164     return ret;
165 }
166 # define fprintf vlc_fprintf
167
168 #if 0
169 static inline int vlc_sprintf (char *str, const char *format, ...)
170 {
171     va_list ap;
172     int ret;
173     va_start (ap, format);
174     ret = vsprintf (str, format, ap);
175     va_end (ap);
176     return ret;
177 }
178 # define sprintf vlc_sprintf
179 #endif
180
181 static inline int vlc_snprintf (char *str, size_t size, const char *format, ...)
182 {
183     va_list ap;
184     int ret;
185     va_start (ap, format);
186     ret = vsnprintf (str, size, format, ap);
187     va_end (ap);
188     return ret;
189 }
190 # define snprintf vlc_snprintf
191
192 /* Make sure we don't use flawed vasprintf or asprintf either */
193 # undef HAVE_VASPRINTF
194 # undef HAVE_ASPRINTF
195 #endif
196
197 #ifndef HAVE_VASPRINTF
198 # include <stdio.h>
199 # include <stdlib.h>
200 # include <stdarg.h>
201 static inline int vasprintf (char **strp, const char *fmt, va_list ap)
202 {
203 #ifndef UNDER_CE
204     int len = vsnprintf (NULL, 0, fmt, ap) + 1;
205     char *res = (char *)malloc (len);
206     if (res == NULL)
207         return -1;
208     *strp = res;
209     return vsnprintf (res, len, fmt, ap);
210 #else
211     /* HACK: vsnprintf in the WinCE API behaves like
212      * the one in glibc 2.0 and doesn't return the number of characters
213      * it needed to copy the string.
214      * cf http://msdn.microsoft.com/en-us/library/1kt27hek.aspx
215      * and cf the man page of vsnprintf
216      *
217      Guess we need no more than 50 bytes. */
218     int n, size = 50;
219     char *res, *np;
220
221     if ((res = (char *) malloc (size)) == NULL)
222         return -1;
223
224     while (1)
225     {
226         n = vsnprintf (res, size, fmt, ap);
227
228         /* If that worked, return the string. */
229         if (n > -1 && n < size)
230         {
231             *strp = res;
232             return n;
233         }
234
235         /* Else try again with more space. */
236         size *= 2;  /* twice the old size */
237
238         if ((np = (char *) realloc (res, size)) == NULL)
239         {
240             free(res);
241             return -1;
242         }
243         else
244         {
245             res = np;
246         }
247
248     }
249 #endif /* UNDER_CE */
250 }
251 #endif
252
253 #ifndef HAVE_ASPRINTF
254 # include <stdio.h>
255 # include <stdarg.h>
256 static inline int asprintf (char **strp, const char *fmt, ...)
257 {
258     va_list ap;
259     int ret;
260     va_start (ap, fmt);
261     ret = vasprintf (strp, fmt, ap);
262     va_end (ap);
263     return ret;
264 }
265 #endif
266
267 #ifndef HAVE_STRNLEN
268 # include <string.h>
269 static inline size_t strnlen (const char *str, size_t max)
270 {
271     const char *end = (const char *) memchr (str, 0, max);
272     return end ? (size_t)(end - str) : max;
273 }
274 #endif
275
276 #ifndef HAVE_STRNDUP
277 # include <string.h>
278 # include <stdlib.h>
279 static inline char *strndup (const char *str, size_t max)
280 {
281     size_t len = strnlen (str, max);
282     char *res = (char *) malloc (len + 1);
283     if (res)
284     {
285         memcpy (res, str, len);
286         res[len] = '\0';
287     }
288     return res;
289 }
290 #endif
291
292 #ifndef HAVE_STRLCPY
293 # define strlcpy vlc_strlcpy
294 #endif
295
296 #ifndef HAVE_STRTOF
297 # define strtof( a, b ) ((float)strtod (a, b))
298 #endif
299
300 #ifndef HAVE_ATOF
301 # define atof( str ) (strtod ((str), (char **)NULL, 10))
302 #endif
303
304 #ifndef HAVE_STRTOLL
305 # define strtoll vlc_strtoll
306 #endif
307
308 #ifndef HAVE_STRSEP
309 static inline char *strsep( char **ppsz_string, const char *psz_delimiters )
310 {
311     char *psz_string = *ppsz_string;
312     if( !psz_string )
313         return NULL;
314
315     char *p = strpbrk( psz_string, psz_delimiters );
316     if( !p )
317     {
318         *ppsz_string = NULL;
319         return psz_string;
320     }
321     *p++ = '\0';
322
323     *ppsz_string = p;
324     return psz_string;
325 }
326 #endif
327
328 #ifndef HAVE_ATOLL
329 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
330 #endif
331
332 #ifndef HAVE_LLDIV
333 typedef struct {
334     long long quot; /* Quotient. */
335     long long rem;  /* Remainder. */
336 } lldiv_t;
337
338 static inline lldiv_t lldiv (long long numer, long long denom)
339 {
340     lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
341     return d;
342 }
343 #endif
344
345 #ifndef HAVE_SCANDIR
346 # define scandir vlc_scandir
347 # define alphasort vlc_alphasort
348 #endif
349
350 #ifndef HAVE_GETENV
351 static inline char *getenv (const char *name)
352 {
353     (void)name;
354     return NULL;
355 }
356 #endif
357
358 #ifndef HAVE_STRCASECMP
359 # ifndef HAVE_STRICMP
360 #  include <ctype.h>
361 static inline int strcasecmp (const char *s1, const char *s2)
362 {
363     for (size_t i = 0;; i++)
364     {
365         int d = tolower (s1[i]) - tolower (s2[i]);
366         if (d || !s1[i]) return d;
367     }
368     return 0;
369 }
370 # else
371 #  define strcasecmp stricmp
372 # endif
373 #endif
374
375 #ifndef HAVE_STRNCASECMP
376 # ifndef HAVE_STRNICMP
377 #  include <ctype.h>
378 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
379 {
380     for (size_t i = 0; i < n; i++)
381     {
382         int d = tolower (s1[i]) - tolower (s2[i]);
383         if (d || !s1[i]) return d;
384     }
385     return 0;
386 }
387 # else
388 #  define strncasecmp strnicmp
389 # endif
390 #endif
391
392 #ifndef HAVE_STRCASESTR
393 # ifndef HAVE_STRISTR
394 #  define strcasestr vlc_strcasestr
395 # else
396 #  define strcasestr stristr
397 # endif
398 #endif
399
400 #ifndef HAVE_LOCALTIME_R
401 /* If localtime_r() is not provided, we assume localtime() uses
402  * thread-specific storage. */
403 # include <time.h>
404 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
405 {
406     struct tm *s = localtime (timep);
407     if (s == NULL)
408         return NULL;
409
410     *result = *s;
411     return result;
412 }
413 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
414 {
415     struct tm *s = gmtime (timep);
416     if (s == NULL)
417         return NULL;
418
419     *result = *s;
420     return result;
421 }
422 #endif
423
424 /* Alignment of critical static data structures */
425 #ifdef ATTRIBUTE_ALIGNED_MAX
426 #   define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
427 #else
428 #   define ATTR_ALIGN(align)
429 #endif
430
431 #ifndef HAVE_USELOCALE
432 typedef void *locale_t;
433 # define newlocale( a, b, c ) ((locale_t)0)
434 # define uselocale( a ) ((locale_t)0)
435 # define freelocale( a ) (void)0
436 #endif
437
438 #ifdef WIN32
439 # include <dirent.h>
440 # define opendir Use_utf8_opendir_or_vlc_wopendir_instead!
441 # define readdir Use_utf8_readdir_or_vlc_wreaddir_instead!
442 # define closedir vlc_wclosedir
443 #endif
444
445 /* libintl support */
446 #define _(str) vlc_gettext (str)
447
448 #if defined (ENABLE_NLS)
449 # include <libintl.h>
450 #endif
451
452 #define N_(str) gettext_noop (str)
453 #define gettext_noop(str) (str)
454
455 #ifdef UNDER_CE
456 static inline void rewind ( FILE *stream )
457 {
458     fseek(stream, 0L, SEEK_SET);
459     clearerr(stream);
460 }
461 #endif
462
463 #endif /* !LIBVLC_FIXUPS_H */