Qt4: Sprefs, add sub-margin.
[vlc/davidf-public.git] / include / vlc_fixups.h
blobc74069f67b14176c05612126555ebeb132552f8d
1 /*****************************************************************************
2 * fixups.h: portability fixups included from config.h
3 *****************************************************************************
4 * Copyright © 1998-2008 the VideoLAN project
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.
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.
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 *****************************************************************************/
21 /**
22 * \file
23 * This file is a collection of portability fixes
26 #ifndef LIBVLC_FIXUPS_H
27 # define LIBVLC_FIXUPS_H 1
29 #ifndef HAVE_STRDUP
30 # include <string.h>
31 # include <stdlib.h>
32 static inline char *strdup (const char *str)
34 size_t len = strlen (str) + 1;
35 char *res = (char *)malloc (len);
36 if (res) memcpy (res, str, len);
37 return res;
39 #endif
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)
47 int len = vsnprintf (NULL, 0, fmt, ap) + 1;
48 char *res = (char *)malloc (len);
49 if (res == NULL)
50 return -1;
51 *strp = res;
52 return vsprintf (res, fmt, ap);
54 #endif
56 #ifndef HAVE_ASPRINTF
57 # include <stdio.h>
58 # include <stdarg.h>
59 static inline int asprintf (char **strp, const char *fmt, ...)
61 va_list ap;
62 int ret;
63 va_start (ap, fmt);
64 ret = vasprintf (strp, fmt, ap);
65 va_end (ap);
66 return ret;
68 #endif
70 #ifndef HAVE_STRNLEN
71 # include <string.h>
72 static inline size_t strnlen (const char *str, size_t max)
74 const char *end = (const char *) memchr (str, 0, max);
75 return end ? (size_t)(end - str) : max;
77 #endif
79 #ifndef HAVE_STRNDUP
80 # include <string.h>
81 # include <stdlib.h>
82 static inline char *strndup (const char *str, size_t max)
84 size_t len = strnlen (str, max);
85 char *res = (char *) malloc (len + 1);
86 if (res)
88 memcpy (res, str, len);
89 res[len] = '\0';
91 return res;
93 #endif
95 #ifndef HAVE_STRLCPY
96 # define strlcpy vlc_strlcpy
97 #endif
99 #ifndef HAVE_STRTOF
100 # define strtof( a, b ) ((float)strtod (a, b))
101 #endif
103 #ifndef HAVE_ATOF
104 # define atof( str ) (strtod ((str), (char **)NULL, 10))
105 #endif
107 #ifndef HAVE_STRTOLL
108 # define strtoll vlc_strtoll
109 #endif
111 #ifndef HAVE_STRSEP
112 # define strsep vlc_strsep
113 #endif
115 #ifndef HAVE_ATOLL
116 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
117 #endif
119 #ifndef HAVE_LLDIV
120 typedef struct {
121 long long quot; /* Quotient. */
122 long long rem; /* Remainder. */
123 } lldiv_t;
125 static inline lldiv_t lldiv (long long numer, long long denom)
127 lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
128 return d;
130 #endif
132 #ifndef HAVE_SCANDIR
133 # define scandir vlc_scandir
134 # define alphasort vlc_alphasort
135 #endif
137 #ifndef HAVE_GETENV
138 static inline char *getenv (const char *name)
140 (void)name;
141 return NULL;
143 #endif
145 #ifndef HAVE_STRCASECMP
146 # ifndef HAVE_STRICMP
147 # include <ctype.h>
148 static inline int strcasecmp (const char *s1, const char *s2)
150 for (size_t i = 0;; i++)
152 int d = tolower (s1[i]) - tolower (s2[i]);
153 if (d || !s1[i]) return d;
155 return 0;
157 # else
158 # define strcasecmp stricmp
159 # endif
160 #endif
162 #ifndef HAVE_STRNCASECMP
163 # ifndef HAVE_STRNICMP
164 # include <ctype.h>
165 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
167 for (size_t i = 0; i < n; i++)
169 int d = tolower (s1[i]) - tolower (s2[i]);
170 if (d || !s1[i]) return d;
172 return 0;
174 # else
175 # define strncasecmp strnicmp
176 # endif
177 #endif
179 #ifndef HAVE_STRCASESTR
180 # ifndef HAVE_STRISTR
181 # define strcasestr vlc_strcasestr
182 # else
183 # define strcasestr stristr
184 # endif
185 #endif
187 #ifndef HAVE_LOCALTIME_R
188 /* If localtime_r() is not provided, we assume localtime() uses
189 * thread-specific storage. */
190 # include <time.h>
191 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
193 struct tm *s = localtime (timep);
194 if (s == NULL)
195 return NULL;
197 *result = *s;
198 return result;
200 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
202 struct tm *s = gmtime (timep);
203 if (s == NULL)
204 return NULL;
206 *result = *s;
207 return result;
209 #endif
211 /* Alignment of critical static data structures */
212 #ifdef ATTRIBUTE_ALIGNED_MAX
213 # define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
214 #else
215 # define ATTR_ALIGN(align)
216 #endif
218 #ifndef HAVE_USELOCALE
219 typedef void *locale_t;
220 # define newlocale( a, b, c ) ((locale_t)0)
221 # define uselocale( a ) ((locale_t)0)
222 # define freelocale( a ) (void)0
223 #endif
225 #ifdef WIN32
226 # include <dirent.h>
227 # define opendir Use_utf8_opendir_or_vlc_wopendir_instead!
228 # define readdir Use_utf8_readdir_or_vlc_wreaddir_instead!
229 # define closedir vlc_wclosedir
230 #endif
232 /* libintl support */
233 #define _(str) vlc_gettext (str)
235 #if defined (ENABLE_NLS)
236 # include <libintl.h>
237 #endif
239 #define N_(str) gettext_noop (str)
240 #define gettext_noop(str) (str)
242 #endif /* !LIBVLC_FIXUPS_H */