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