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