]> git.sesse.net Git - vlc/blob - include/vlc_fixups.h
Inline and fix some linking errors
[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 # include <string.h>
30 # include <stdlib.h>
31
32 #ifndef HAVE_STRDUP
33 static inline char *strdup (const char *str)
34 {
35     size_t len = strlen (str) + 1;
36     char *res = malloc (len);
37     if (res) memcpy (res, str, len);
38     return res;
39 }
40 #endif
41
42 #ifndef HAVE_VASPRINTF
43 # include <stdarg.h>
44 static inline int vasprintf (char **strp, const char *fmt, va_list ap)
45 {
46     int len = vsnprintf (NULL, 0, fmt, ap) + 1;
47     char *res = malloc (len);
48     if (res == NULL)
49         return -1;
50     *strp = res;
51     return vsprintf (res, fmt, ap);
52 }
53 #endif
54
55 #ifndef HAVE_ASPRINTF
56 # include <stdarg.h>
57 static inline int asprintf (char **strp, const char *fmt, ...)
58 {
59     va_list ap;
60     int ret;
61     va_start (fmt, ap);
62     ret = vasprintf (strp, fmt, ap);
63     va_end (ap);
64     return ret;
65 }
66 #endif
67
68 #ifndef HAVE_STRNLEN
69 static inline size_t strnlen (const char *str, size_t max)
70 {
71     const char *end = (const char *) memchr (str, 0, max);
72     return end ? (size_t)(end - str) : max;
73 }
74 #endif
75
76 #ifndef HAVE_STRNDUP
77 static inline char *strndup (const char *str, size_t max)
78 {
79     size_t len = strnlen (str, max);
80     char *res = (char *) malloc (len + 1);
81     if (res)
82     {
83         memcpy (res, str, len);
84         res[len] = '\0';
85     }
86     return res;
87 }
88 #endif
89
90 #ifndef HAVE_STRLCPY
91 # define strlcpy vlc_strlcpy
92 #endif
93
94 #ifndef HAVE_STRTOF
95 # define strtof( a, b ) ((float)strtod (a, b))
96 #endif
97
98 #ifndef HAVE_ATOF
99 # define atof( str ) (strtod ((str), (char **)NULL, 10))
100 #endif
101
102 #ifndef HAVE_STRTOLL
103 # define strtoll vlc_strtoll
104 #endif
105
106 #ifndef HAVE_ATOLL
107 # define atoll( str ) (strtoll ((str), (char **)NULL, 10))
108 #endif
109
110 #ifndef HAVE_LLDIV
111 typedef struct {
112     long long quot; /* Quotient. */
113     long long rem;  /* Remainder. */
114 } lldiv_t;
115
116 static inline lldiv_t lldiv (long long numer, long long denom)
117 {
118     lldiv_t d = { .quot = numer / denom, .rem = numer % denom };
119     return d;
120 }
121 #endif
122
123 #ifndef HAVE_SCANDIR
124 # define scandir vlc_scandir
125 # define alphasort vlc_alphasort
126 #endif
127
128 #ifndef HAVE_GETENV
129 static inline getenv (const char *name)
130 {
131     (void)name;
132     return NULL;
133 }
134 #endif
135
136 #ifndef HAVE_STRCASECMP
137 # ifndef HAVE_STRICMP
138 #  include <ctype.h>
139 static inline int strcasecmp (const char *s1, const char *s2)
140 {
141     for (size_t i = 0;; i++)
142     {
143         int d = tolower (s1[i]) - tolower (s2[i]);
144         if (d) return d;
145     }
146     return 0;
147 }
148 # else
149 #  define strcasecmp stricmp
150 # endif
151 #endif
152
153 #ifndef HAVE_STRNCASECMP
154 # ifndef HAVE_STRNICMP
155 #  include <ctype.h>
156 static inline int strncasecmp (const char *s1, const char *s2, size_t n)
157 {
158     for (size_t i = 0; i < n; i++)
159     {
160         int d = tolower (s1[i]) - tolower (s2[i]);
161         if (d) return d;
162     }
163     return 0;
164 }
165 # else
166 #  define strncasecmp strnicmp
167 # endif
168 #endif
169
170 #ifndef HAVE_STRCASESTR
171 # ifndef HAVE_STRISTR
172 #  define strcasestr vlc_strcasestr
173 # else
174 #  define strcasestr stristr
175 # endif
176 #endif
177
178 #ifndef HAVE_LOCALTIME_R
179 /* If localtime_r() is not provided, we assume localtime() uses
180  * thread-specific storage. */
181 # include <time.h>
182 static inline struct tm *localtime_r (const time_t *timep, struct tm *result)
183 {
184     struct tm *s = localtime (timep);
185     if (s == NULL)
186         return NULL;
187
188     *result = *s;
189     return result;
190 }
191 static inline struct tm *gmtime_r (const time_t *timep, struct tm *result)
192 {
193     struct tm *s = gmtime (timep);
194     if (s == NULL)
195         return NULL;
196
197     *result = *s;
198     return result;
199 }
200 #endif
201
202 #ifndef HAVE_USELOCALE
203 typedef void *locale_t;
204 # define newlocale( a, b, c ) ((locale_t)0)
205 # define uselocale( a ) ((locale_t)0)
206 # define freelocale( a ) (void)0
207 #endif
208
209 #ifdef WIN32
210 # include <dirent.h>
211 # define opendir Use_utf8_opendir_or_vlc_wopendir_instead!
212 # define readdir Use_utf8_readdir_or_vlc_wreaddir_instead!
213 # define closedir vlc_wclosedir
214 #endif
215
216 /* libintl support */
217 #define _(str) vlc_gettext (str)
218
219 #if defined (ENABLE_NLS)
220 # include <libintl.h>
221 #else
222 # define dgettext(dom, str) ((char *)(str))
223 #endif
224
225 #define N_(str) gettext_noop (str)
226 #define gettext_noop(str) (str)
227
228 #endif /* !LIBVLC_FIXUPS_H */