[LIBMPG123]
[reactos.git] / reactos / sdk / include / reactos / libs / libmpg123 / compat.h
1 /*
2 compat: Some compatibility functions and header inclusions.
3 Basic standard C stuff, that may barely be above/around C89.
4
5 The mpg123 code is determined to keep it's legacy. A legacy of old, old UNIX.
6 It is envisioned to include this compat header instead of any of the "standard" headers, to catch compatibility issues.
7 So, don't include stdlib.h or string.h ... include compat.h.
8
9 copyright 2007-8 by the mpg123 project - free software under the terms of the LGPL 2.1
10 see COPYING and AUTHORS files in distribution or http://mpg123.org
11 initially written by Thomas Orgis
12 */
13
14 #ifndef MPG123_COMPAT_H
15 #define MPG123_COMPAT_H
16
17 #include "config.h"
18
19 /* Needed for strdup(), in strict mode ... */
20 #ifndef _XOPEN_SOURCE
21 #define _XOPEN_SOURCE 500
22 #endif
23
24 #include <errno.h>
25
26 #ifdef HAVE_STDLIB_H
27 /* realloc, size_t */
28 #include <stdlib.h>
29 #endif
30
31 #include <stdio.h>
32 #include <math.h>
33
34 #ifdef HAVE_SIGNAL_H
35 #include <signal.h>
36 #else
37 #ifdef HAVE_SYS_SIGNAL_H
38 #include <sys/signal.h>
39 #endif
40 #endif
41
42 #ifdef HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif
45
46 /* Types, types, types. */
47 /* Do we actually need these two in addition to sys/types.h? As replacement? */
48 #ifdef HAVE_SYS_TYPES_H
49 #include <sys/types.h>
50 #endif
51 #ifdef HAVE_INTTYPES_H
52 #include <inttypes.h>
53 #endif
54 #ifdef HAVE_STDINT_H
55 #include <stdint.h>
56 #endif
57 /* We want SIZE_MAX, etc. */
58 #ifdef HAVE_LIMITS_H
59 #include <limits.h>
60 #endif
61
62 #ifndef SIZE_MAX
63 #define SIZE_MAX ((size_t)-1)
64 #endif
65 #ifndef ULONG_MAX
66 #define ULONG_MAX ((unsigned long)-1)
67 #endif
68
69 #ifdef HAVE_STRING_H
70 #include <string.h>
71 #endif
72 #ifdef HAVE_STRINGS_H
73 #include <strings.h>
74 #endif
75
76 #ifdef OS2
77 #include <float.h>
78 #endif
79
80 #ifdef HAVE_SYS_TIME_H
81 #include <sys/time.h>
82 #endif
83 /* For select(), I need select.h according to POSIX 2001, else: sys/time.h sys/types.h unistd.h */
84 #ifdef HAVE_SYS_SELECT_H
85 #include <sys/select.h>
86 #endif
87
88 /* compat_open makes little sense without */
89 #include <fcntl.h>
90
91 /* To parse big numbers... */
92 #ifdef HAVE_ATOLL
93 #define atobigint atoll
94 #else
95 #define atobigint atol
96 #endif
97
98 typedef unsigned char byte;
99
100 #ifndef __REACTOS__
101 #ifdef _MSC_VER
102 typedef long ssize_t;
103 #endif
104 #endif /* __REACTOS__ */
105
106 /* A safe realloc also for very old systems where realloc(NULL, size) returns NULL. */
107 void *safe_realloc(void *ptr, size_t size);
108 #ifndef HAVE_STRERROR
109 const char *strerror(int errnum);
110 #endif
111
112 #ifndef HAVE_STRDUP
113 char *strdup(const char *s);
114 #endif
115
116 /* If we have the size checks enabled, try to derive some sane printfs.
117 Simple start: Use max integer type and format if long is not big enough.
118 I am hesitating to use %ll without making sure that it's there... */
119 #if !(defined PLAIN_C89) && (defined SIZEOF_OFF_T) && (SIZEOF_OFF_T > SIZEOF_LONG) && (defined PRIiMAX)
120 # define OFF_P PRIiMAX
121 typedef intmax_t off_p;
122 #else
123 # define OFF_P "li"
124 typedef long off_p;
125 #endif
126
127 #if !(defined PLAIN_C89) && (defined SIZEOF_SIZE_T) && (SIZEOF_SIZE_T > SIZEOF_LONG) && (defined PRIuMAX)
128 # define SIZE_P PRIuMAX
129 typedef uintmax_t size_p;
130 #else
131 # define SIZE_P "lu"
132 typedef unsigned long size_p;
133 #endif
134
135 #if !(defined PLAIN_C89) && (defined SIZEOF_SSIZE_T) && (SIZEOF_SSIZE_T > SIZEOF_LONG) && (defined PRIiMAX)
136 # define SSIZE_P PRIuMAX
137 typedef intmax_t ssize_p;
138 #else
139 # define SSIZE_P "li"
140 typedef long ssize_p;
141 #endif
142
143 /**
144 * Opening a file handle can be different.
145 * This function here is defined to take a path in native encoding (ISO8859 / UTF-8 / ...), or, when MS Windows Unicode support is enabled, an UTF-8 string that will be converted back to native UCS-2 (wide character) before calling the system's open function.
146 * @param[in] wptr Pointer to wide string.
147 * @param[in] mbptr Pointer to multibyte string.
148 * @return file descriptor (>=0) or error code.
149 */
150 int compat_open(const char *filename, int flags);
151 FILE* compat_fopen(const char *filename, const char *mode);
152
153 /**
154 * Closing a file handle can be platform specific.
155 * This function takes a file descriptor that is to be closed.
156 * @param[in] infd File descriptor to be closed.
157 * @return 0 if the file was successfully closed. A return value of -1 indicates an error.
158 */
159 int compat_close(int infd);
160 int compat_fclose(FILE* stream);
161
162 /* Those do make sense in a separate file, but I chose to include them in compat.c because that's the one source whose object is shared between mpg123 and libmpg123 -- and both need the functionality internally. */
163
164 #ifdef WANT_WIN32_UNICODE
165 /**
166 * win32_uni2mbc
167 * Converts a null terminated UCS-2 string to a multibyte (UTF-8) equivalent.
168 * Caller is supposed to free allocated buffer.
169 * @param[in] wptr Pointer to wide string.
170 * @param[out] mbptr Pointer to multibyte string.
171 * @param[out] buflen Optional parameter for length of allocated buffer.
172 * @return status of WideCharToMultiByte conversion.
173 *
174 * WideCharToMultiByte - http://msdn.microsoft.com/en-us/library/dd374130(VS.85).aspx
175 */
176 int win32_wide_utf8(const wchar_t * const wptr, char **mbptr, size_t * buflen);
177
178 /**
179 * win32_mbc2uni
180 * Converts a null terminated UTF-8 string to a UCS-2 equivalent.
181 * Caller is supposed to free allocated buffer.
182 * @param[out] mbptr Pointer to multibyte string.
183 * @param[in] wptr Pointer to wide string.
184 * @param[out] buflen Optional parameter for length of allocated buffer.
185 * @return status of WideCharToMultiByte conversion.
186 *
187 * MultiByteToWideChar - http://msdn.microsoft.com/en-us/library/dd319072(VS.85).aspx
188 */
189
190 int win32_utf8_wide(const char *const mbptr, wchar_t **wptr, size_t *buflen);
191 #endif
192
193 /* Blocking write/read of data with signal resilience.
194 Both continue after being interrupted by signals and always return the
195 amount of processed data (shortage indicating actual problem or EOF). */
196 size_t unintr_write(int fd, void const *buffer, size_t bytes);
197 size_t unintr_read (int fd, void *buffer, size_t bytes);
198
199 /* That one comes from Tellie on OS/2, needed in resolver. */
200 #ifdef __KLIBC__
201 typedef int socklen_t;
202 #endif
203
204 /* OSX SDK defines an enum with "normal" as value. That clashes with
205 optimize.h */
206 #ifdef __APPLE__
207 #define normal mpg123_normal
208 #endif
209
210 #include "true.h"
211
212 #if (!defined(WIN32) || defined (__CYGWIN__)) && defined(HAVE_SIGNAL_H)
213 void (*catchsignal(int signum, void(*handler)()))();
214 #endif
215
216 #endif