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