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