]> git.sesse.net Git - vlc/blob - include/vlc_fixups.h
typo
[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 #ifndef HAVE_VASPRINTF
42 # include <stdio.h>
43 # include <stdlib.h>
44 # include <stdarg.h>
45 static inline int vasprintf (char **strp, const char *fmt, va_list ap)
46 {
47 #ifndef UNDER_CE
48     int len = vsnprintf (NULL, 0, fmt, ap) + 1;
49     char *res = (char *)malloc (len);
50     if (res == NULL)
51         return -1;
52     *strp = res;
53     return vsprintf (res, fmt, ap);
54 #else
55     /* HACK: vsnprintf in the WinCE API behaves like
56      * the one in glibc 2.0 and doesn't return the number of characters
57      * it needed to copy the string.
58      * cf http://msdn.microsoft.com/en-us/library/1kt27hek.aspx
59      * and cf the man page of vsnprintf
60      *
61      Guess we need no more than 50 bytes. */
62     int n, size = 50;
63     char *res, *np;
64
65     if ((res = (char *) malloc (size)) == NULL)
66         return -1;
67
68     while (1)
69     {
70         n = vsnprintf (res, size, fmt, ap);
71
72         /* If that worked, return the string. */
73         if (n > -1 && n < size)
74         {
75             *strp = res;
76             return n;
77         }
78
79         /* Else try again with more space. */
80         if (n == -1)
81             size *= 2;  /* twice the old size */
82
83         if ((np = (char *) realloc (res, size)) == NULL)
84         {
85             free(res);
86             return -1;
87         }
88         else
89         {
90             res = np;
91         }
92
93     }
94 #endif /* UNDER_CE */
95 }
96 #endif
97
98 #ifndef HAVE_ASPRINTF
99 # include <stdio.h>
100 # include <stdarg.h>
101 static inline int asprintf (char **strp, const char *fmt, ...)
102 {
103     va_list ap;
104     int ret;
105     va_start (ap, fmt);
106     ret = vasprintf (strp, fmt, ap);
107     va_end (ap);
108     return ret;
109 }
110 #endif
111
112 #ifndef HAVE_STRNLEN
113 # include <string.h>
114 static inline size_t strnlen (const char *str, size_t max)
115 {
116     const char *end = (const char *) memchr (str, 0, max);
117     return end ? (size_t)(end - str) : max;
118 }
119 #endif
120
121 #ifndef HAVE_STRNDUP
122 # include <string.h>
123 # include <stdlib.h>
124 static inline char *strndup (const char *str, size_t max)
125 {
126     size_t len = strnlen (str, max);
127     char *res = (char *) malloc (len + 1);
128     if (res)
129     {
130         memcpy (res, str, len);
131         res[len] = '\0';
132     }
133     return res;
134 }
135 #endif
136
137 #ifndef HAVE_STRLCPY
138 # define strlcpy vlc_strlcpy
139 #endif
140
141 #ifndef HAVE_STRTOF
142 # define strtof( a, b ) ((float)strtod (a, b))
143 #endif
144
145 #ifndef HAVE_ATOF
146 # define atof( str ) (strtod ((str), (char **)NULL, 10))
147 #endif
148
149 #ifndef HAVE_STRTOLL
150 # define strtoll vlc_strtoll
151 #endif
152
153 #ifndef HAVE_STRSEP
154 static inline char *strsep( char **ppsz_string, const char *psz_delimiters )
155 {
156     char *psz_string = *ppsz_string;
157     if( !psz_string )
158         return NULL;
159
160     char *p = strpbrk( psz_string, psz_delimiters );
161     if( !p )
162     {
163         *ppsz_string = NULL;
164         return psz_string;
165     }
166     *p++ = '\0';
167
168     *ppsz_string = p;
169     return psz_string;
170 }
171 #endif
172
173 #ifndef HAVE_ATOLL
174 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
175 #endif
176
177 #ifndef HAVE_LLDIV
178 typedef struct {
179     long long quot; /* Quotient. */
180     long long rem;  /* Remainder. */
181 } lldiv_t;
182
183 static inline lldiv_t lldiv (long long numer, long long denom)
184 {
185     lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
186     return d;
187 }
188 #endif
189
190 #ifndef HAVE_SCANDIR
191 # define scandir vlc_scandir
192 # define alphasort vlc_alphasort
193 #endif
194
195 #ifndef HAVE_GETENV
196 static inline char *getenv (const char *name)
197 {
198     (void)name;
199     return NULL;
200 }
201 #endif
202
203 #ifndef HAVE_STRCASECMP
204 # ifndef HAVE_STRICMP
205 #  include <ctype.h>
206 static inline int strcasecmp (const char *s1, const char *s2)
207 {
208     for (size_t i = 0;; i++)
209     {
210         int d = tolower (s1[i]) - tolower (s2[i]);
211         if (d || !s1[i]) return d;
212     }
213     return 0;
214 }
215 # else
216 #  define strcasecmp stricmp
217 # endif
218 #endif
219
220 #ifndef HAVE_STRNCASECMP
221 # ifndef HAVE_STRNICMP
222 #  include <ctype.h>
223 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
224 {
225     for (size_t i = 0; i < n; i++)
226     {
227         int d = tolower (s1[i]) - tolower (s2[i]);
228         if (d || !s1[i]) return d;
229     }
230     return 0;
231 }
232 # else
233 #  define strncasecmp strnicmp
234 # endif
235 #endif
236
237 #ifndef HAVE_STRCASESTR
238 # ifndef HAVE_STRISTR
239 #  define strcasestr vlc_strcasestr
240 # else
241 #  define strcasestr stristr
242 # endif
243 #endif
244
245 #ifndef HAVE_LOCALTIME_R
246 /* If localtime_r() is not provided, we assume localtime() uses
247  * thread-specific storage. */
248 # include <time.h>
249 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
250 {
251     struct tm *s = localtime (timep);
252     if (s == NULL)
253         return NULL;
254
255     *result = *s;
256     return result;
257 }
258 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
259 {
260     struct tm *s = gmtime (timep);
261     if (s == NULL)
262         return NULL;
263
264     *result = *s;
265     return result;
266 }
267 #endif
268
269 /* Alignment of critical static data structures */
270 #ifdef ATTRIBUTE_ALIGNED_MAX
271 #   define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
272 #else
273 #   define ATTR_ALIGN(align)
274 #endif
275
276 #ifndef HAVE_USELOCALE
277 typedef void *locale_t;
278 # define newlocale( a, b, c ) ((locale_t)0)
279 # define uselocale( a ) ((locale_t)0)
280 # define freelocale( a ) (void)0
281 #endif
282
283 #ifdef WIN32
284 # include <dirent.h>
285 # define opendir Use_utf8_opendir_or_vlc_wopendir_instead!
286 # define readdir Use_utf8_readdir_or_vlc_wreaddir_instead!
287 # define closedir vlc_wclosedir
288 #endif
289
290 /* libintl support */
291 #define _(str) vlc_gettext (str)
292
293 #if defined (ENABLE_NLS)
294 # include <libintl.h>
295 #endif
296
297 #define N_(str) gettext_noop (str)
298 #define gettext_noop(str) (str)
299
300 #ifdef UNDER_CE
301 static inline void rewind ( FILE *stream )
302 {
303     fseek(stream, 0L, SEEK_SET);
304     clearerr(stream);
305 }
306 #endif
307
308 #endif /* !LIBVLC_FIXUPS_H */