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