]> git.sesse.net Git - vlc/blob - include/vlc_fixups.h
Win64: properly work around %z modifiers
[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 static inline int vlc_sprintf (char *str, const char *format, ...)
156 {
157     va_list ap;
158     int ret;
159     va_start (ap, format);
160     ret = vsprintf (str, format, ap);
161     va_end (ap);
162     return ret;
163 }
164 # define sprintf vlc_sprintf
165
166 static inline int vlc_snprintf (char *str, size_t size, const char *format, ...)
167 {
168     va_list ap;
169     int ret;
170     va_start (ap, format);
171     ret = vsnprintf (str, size, format, ap);
172     va_end (ap);
173     return ret;
174 }
175 # define snprintf vlc_snprintf
176
177 /* Make sure we don't use flawed vasprintf or asprintf either */
178 # undef HAVE_VASPRINTF
179 # undef HAVE_ASPRINTF
180 #endif
181
182 #ifndef HAVE_VASPRINTF
183 # include <stdio.h>
184 # include <stdlib.h>
185 # include <stdarg.h>
186 static inline int vasprintf (char **strp, const char *fmt, va_list ap)
187 {
188 #ifndef UNDER_CE
189     int len = vsnprintf (NULL, 0, fmt, ap) + 1;
190     char *res = (char *)malloc (len);
191     if (res == NULL)
192         return -1;
193     *strp = res;
194     return vsnprintf (res, len, fmt, ap);
195 #else
196     /* HACK: vsnprintf in the WinCE API behaves like
197      * the one in glibc 2.0 and doesn't return the number of characters
198      * it needed to copy the string.
199      * cf http://msdn.microsoft.com/en-us/library/1kt27hek.aspx
200      * and cf the man page of vsnprintf
201      *
202      Guess we need no more than 50 bytes. */
203     int n, size = 50;
204     char *res, *np;
205
206     if ((res = (char *) malloc (size)) == NULL)
207         return -1;
208
209     while (1)
210     {
211         n = vsnprintf (res, size, fmt, ap);
212
213         /* If that worked, return the string. */
214         if (n > -1 && n < size)
215         {
216             *strp = res;
217             return n;
218         }
219
220         /* Else try again with more space. */
221         size *= 2;  /* twice the old size */
222
223         if ((np = (char *) realloc (res, size)) == NULL)
224         {
225             free(res);
226             return -1;
227         }
228         else
229         {
230             res = np;
231         }
232
233     }
234 #endif /* UNDER_CE */
235 }
236 #endif
237
238 #ifndef HAVE_ASPRINTF
239 # include <stdio.h>
240 # include <stdarg.h>
241 static inline int asprintf (char **strp, const char *fmt, ...)
242 {
243     va_list ap;
244     int ret;
245     va_start (ap, fmt);
246     ret = vasprintf (strp, fmt, ap);
247     va_end (ap);
248     return ret;
249 }
250 #endif
251
252 #ifndef HAVE_STRNLEN
253 # include <string.h>
254 static inline size_t strnlen (const char *str, size_t max)
255 {
256     const char *end = (const char *) memchr (str, 0, max);
257     return end ? (size_t)(end - str) : max;
258 }
259 #endif
260
261 #ifndef HAVE_STRNDUP
262 # include <string.h>
263 # include <stdlib.h>
264 static inline char *strndup (const char *str, size_t max)
265 {
266     size_t len = strnlen (str, max);
267     char *res = (char *) malloc (len + 1);
268     if (res)
269     {
270         memcpy (res, str, len);
271         res[len] = '\0';
272     }
273     return res;
274 }
275 #endif
276
277 #ifndef HAVE_STRLCPY
278 # define strlcpy vlc_strlcpy
279 #endif
280
281 #ifndef HAVE_STRTOF
282 # define strtof( a, b ) ((float)strtod (a, b))
283 #endif
284
285 #ifndef HAVE_ATOF
286 # define atof( str ) (strtod ((str), (char **)NULL, 10))
287 #endif
288
289 #ifndef HAVE_STRTOLL
290 # define strtoll vlc_strtoll
291 #endif
292
293 #ifndef HAVE_STRSEP
294 static inline char *strsep( char **ppsz_string, const char *psz_delimiters )
295 {
296     char *psz_string = *ppsz_string;
297     if( !psz_string )
298         return NULL;
299
300     char *p = strpbrk( psz_string, psz_delimiters );
301     if( !p )
302     {
303         *ppsz_string = NULL;
304         return psz_string;
305     }
306     *p++ = '\0';
307
308     *ppsz_string = p;
309     return psz_string;
310 }
311 #endif
312
313 #ifndef HAVE_ATOLL
314 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
315 #endif
316
317 #ifndef HAVE_LLDIV
318 typedef struct {
319     long long quot; /* Quotient. */
320     long long rem;  /* Remainder. */
321 } lldiv_t;
322
323 static inline lldiv_t lldiv (long long numer, long long denom)
324 {
325     lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
326     return d;
327 }
328 #endif
329
330 #ifndef HAVE_SCANDIR
331 # define scandir vlc_scandir
332 # define alphasort vlc_alphasort
333 #endif
334
335 #ifndef HAVE_GETENV
336 static inline char *getenv (const char *name)
337 {
338     (void)name;
339     return NULL;
340 }
341 #endif
342
343 #ifndef HAVE_STRCASECMP
344 # ifndef HAVE_STRICMP
345 #  include <ctype.h>
346 static inline int strcasecmp (const char *s1, const char *s2)
347 {
348     for (size_t i = 0;; i++)
349     {
350         int d = tolower (s1[i]) - tolower (s2[i]);
351         if (d || !s1[i]) return d;
352     }
353     return 0;
354 }
355 # else
356 #  define strcasecmp stricmp
357 # endif
358 #endif
359
360 #ifndef HAVE_STRNCASECMP
361 # ifndef HAVE_STRNICMP
362 #  include <ctype.h>
363 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
364 {
365     for (size_t i = 0; i < n; i++)
366     {
367         int d = tolower (s1[i]) - tolower (s2[i]);
368         if (d || !s1[i]) return d;
369     }
370     return 0;
371 }
372 # else
373 #  define strncasecmp strnicmp
374 # endif
375 #endif
376
377 #ifndef HAVE_STRCASESTR
378 # ifndef HAVE_STRISTR
379 #  define strcasestr vlc_strcasestr
380 # else
381 #  define strcasestr stristr
382 # endif
383 #endif
384
385 #ifndef HAVE_LOCALTIME_R
386 /* If localtime_r() is not provided, we assume localtime() uses
387  * thread-specific storage. */
388 # include <time.h>
389 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
390 {
391     struct tm *s = localtime (timep);
392     if (s == NULL)
393         return NULL;
394
395     *result = *s;
396     return result;
397 }
398 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
399 {
400     struct tm *s = gmtime (timep);
401     if (s == NULL)
402         return NULL;
403
404     *result = *s;
405     return result;
406 }
407 #endif
408
409 /* Alignment of critical static data structures */
410 #ifdef ATTRIBUTE_ALIGNED_MAX
411 #   define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
412 #else
413 #   define ATTR_ALIGN(align)
414 #endif
415
416 #ifndef HAVE_USELOCALE
417 typedef void *locale_t;
418 # define newlocale( a, b, c ) ((locale_t)0)
419 # define uselocale( a ) ((locale_t)0)
420 # define freelocale( a ) (void)0
421 #endif
422
423 #ifdef WIN32
424 # include <dirent.h>
425 # define opendir Use_utf8_opendir_or_vlc_wopendir_instead!
426 # define readdir Use_utf8_readdir_or_vlc_wreaddir_instead!
427 # define closedir vlc_wclosedir
428 #endif
429
430 /* libintl support */
431 #define _(str) vlc_gettext (str)
432
433 #if defined (ENABLE_NLS)
434 # include <libintl.h>
435 #endif
436
437 #define N_(str) gettext_noop (str)
438 #define gettext_noop(str) (str)
439
440 #ifdef UNDER_CE
441 static inline void rewind ( FILE *stream )
442 {
443     fseek(stream, 0L, SEEK_SET);
444     clearerr(stream);
445 }
446 #endif
447
448 #endif /* !LIBVLC_FIXUPS_H */