2
compat: Some compatibility functions.
4
The mpg123 code is determined to keep it's legacy. A legacy of old, old UNIX.
5
So anything possibly somewhat advanced should be considered to be put here, with proper #ifdef;-)
7
copyright 2007-8 by the mpg123 project - free software under the terms of the LGPL 2.1
8
see COPYING and AUTHORS files in distribution or http://mpg123.org
9
initially written by Thomas Orgis, Windows Unicode stuff by JonY.
21
#ifdef WANT_WIN32_UNICODE
29
/* A safe realloc also for very old systems where realloc(NULL, size) returns NULL. */
30
void *safe_realloc(void *ptr, size_t size)
32
if(ptr == NULL) return malloc(size);
33
else return realloc(ptr, size);
37
const char *strerror(int errnum)
40
extern char *sys_errlist[];
42
return (errnum < sys_nerr) ? sys_errlist[errnum] : "";
47
char *strdup(const char *src)
51
if (!(dest = (char *) malloc(strlen(src)+1)))
54
return strcpy(dest, src);
58
int compat_open(const char *filename, int mode)
61
#if defined (WANT_WIN32_UNICODE)
62
const wchar_t *frag = NULL;
64
ret = win32_utf8_wide(filename, &frag, NULL);
65
if ((frag == NULL) || (ret == 0)) goto fallback; /* Fallback to plain open when ucs-2 conversion fails */
67
ret = _wopen(frag, mode); /*Try _wopen */
68
if (ret != -1 ) goto open_ok; /* msdn says -1 means failure */
73
#ifdef __MSVCRT__ /* MSDN says POSIX function is deprecated beginning in Visual C++ 2005 */
74
ret = _open (filename, mode); /* Try plain old _open(), if it fails, do nothing */
76
ret = open (filename, mode);
79
#if defined (WANT_WIN32_UNICODE)
81
free ((void *)frag); /* Freeing a NULL should be OK */
87
int compat_close(int infd)
89
#ifdef __MSVCRT__ /* MSDN says POSIX function is deprecated beginning in Visual C++ 2005 */
96
/* Windows Unicode stuff */
98
#ifdef WANT_WIN32_UNICODE
99
int win32_wide_utf8(const wchar_t * const wptr, const char **const mbptr, size_t * const buflen)
105
len = WideCharToMultiByte(CP_UTF8, 0, wptr, -1, NULL, 0, NULL, NULL); /* Get utf-8 string length */
106
buf = calloc(len, sizeof (char)); /* Can we assume sizeof char always = 1? */
107
debug2("win32_wide_utf8 allocated %u bytes at %p", len, buf);
111
ret = WideCharToMultiByte(CP_UTF8, 0, wptr, -1, buf, len, NULL, NULL); /*Do actual conversion*/
112
*mbptr = buf; /* Set string pointer to allocated buffer */
113
if(buflen != NULL) *buflen = len * sizeof (char); /* Give length of allocated memory if needed. */
119
if(buflen != NULL) *buflen = 0;
125
int win32_utf8_wide(const char *const mbptr, const wchar_t ** const wptr, size_t * const buflen)
131
len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mbptr, -1, NULL, 0); /* Get converted size */
132
buf = calloc(len, sizeof (wchar_t)); /* Allocate memory accordingly */
133
debug2("win32_utf8_wide allocated %u bytes at %p", len, buf);
137
ret = MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS, mbptr, -1, buf, len); /* Do conversion */
138
*wptr = buf; /* Set string pointer to allocated buffer */
139
if (buflen != NULL) *buflen = len * sizeof (wchar_t); /* Give length of allocated memory if needed. */
145
if (buflen != NULL) *buflen = 0;