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