Subversion Repositories Kolibri OS

Rev

Rev 1905 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1905 serge 1
/*
3960 Serge 2
	compat: Some compatibility functions.
1905 serge 3
 
4
	The mpg123 code is determined to keep it's legacy. A legacy of old, old UNIX.
3960 Serge 5
	So anything possibly somewhat advanced should be considered to be put here, with proper #ifdef;-)
1905 serge 6
 
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
3960 Serge 9
	initially written by Thomas Orgis, Windows Unicode stuff by JonY.
1905 serge 10
*/
11
 
12
#include "config.h"
13
#include "compat.h"
14
 
3960 Serge 15
#ifdef _MSC_VER
16
#include 
17
#else
18
#include 
19
#endif
20
#include 
21
 
22
#include "debug.h"
23
 
24
int _EXFUN(open, (const char *, int, ...));
25
 
1905 serge 26
/* A safe realloc also for very old systems where realloc(NULL, size) returns NULL. */
27
void *safe_realloc(void *ptr, size_t size)
28
{
29
	if(ptr == NULL) return malloc(size);
30
	else return realloc(ptr, size);
31
}
32
 
33
#ifndef HAVE_STRERROR
34
const char *strerror(int errnum)
35
{
36
	extern int sys_nerr;
37
	extern char *sys_errlist[];
38
 
39
	return (errnum < sys_nerr) ?  sys_errlist[errnum]  :  "";
40
}
41
#endif
42
 
43
#ifndef HAVE_STRDUP
44
char *strdup(const char *src)
45
{
46
	char *dest;
47
 
48
	if (!(dest = (char *) malloc(strlen(src)+1)))
49
	return NULL;
50
	else
51
	return strcpy(dest, src);
52
}
53
#endif
3960 Serge 54
 
55
int compat_open(const char *filename, int flags)
56
{
57
	int ret;
58
#if defined (WANT_WIN32_UNICODE)
59
	wchar_t *frag = NULL;
60
 
61
	ret = win32_utf8_wide(filename, &frag, NULL);
62
	if ((frag == NULL) || (ret == 0)) goto fallback; /* Fallback to plain open when ucs-2 conversion fails */
63
 
64
	ret = _wopen(frag, flags); /*Try _wopen */
65
	if (ret != -1 ) goto open_ok; /* msdn says -1 means failure */
66
 
67
fallback:
68
#endif
69
 
70
#if (defined(WIN32) && !defined (__CYGWIN__)) /* MSDN says POSIX function is deprecated beginning in Visual C++ 2005 */
71
    ret = _open(filename, flags); /* Try plain old _open(), if it fails, do nothing */
72
#else
73
	/* On UNIX, we always add a default permission mask in case flags|O_CREAT. */
74
	ret = open(filename, flags, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
75
#endif
76
 
77
#if defined (WANT_WIN32_UNICODE)
78
open_ok:
79
	free ((void *)frag); /* Freeing a NULL should be OK */
80
#endif
81
 
82
	return ret;
83
}
84
 
85
int compat_close(int infd)
86
{
87
#if (defined(WIN32) && !defined (__CYGWIN__)) /* MSDN says POSIX function is deprecated beginning in Visual C++ 2005 */
88
	return _close(infd);
89
#else
90
	return close(infd);
91
#endif
92
}
93
 
94
/* Windows Unicode stuff */
95
 
96
#ifdef WANT_WIN32_UNICODE
97
int win32_wide_utf8(const wchar_t * const wptr, char **mbptr, size_t * buflen)
98
{
99
  size_t len;
100
  char *buf;
101
  int ret = 0;
102
 
103
  len = WideCharToMultiByte(CP_UTF8, 0, wptr, -1, NULL, 0, NULL, NULL); /* Get utf-8 string length */
104
  buf = calloc(len + 1, sizeof (char)); /* Can we assume sizeof char always = 1? */
105
 
106
  if(!buf) len = 0;
107
  else {
108
    if (len != 0) ret = WideCharToMultiByte(CP_UTF8, 0, wptr, -1, buf, len, NULL, NULL); /*Do actual conversion*/
109
    buf[len] = '0'; /* Must terminate */
110
  }
111
  *mbptr = buf; /* Set string pointer to allocated buffer */
112
  if(buflen != NULL) *buflen = (len) * sizeof (char); /* Give length of allocated memory if needed. */
113
  return ret;
114
}
115
 
116
int win32_utf8_wide(const char *const mbptr, wchar_t **wptr, size_t *buflen)
117
{
118
  size_t len;
119
  wchar_t *buf;
120
  int ret = 0;
121
 
122
  len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mbptr, -1, NULL, 0); /* Get converted size */
123
  buf = calloc(len + 1, sizeof (wchar_t)); /* Allocate memory accordingly */
124
 
125
  if(!buf) len = 0;
126
  else {
127
    if (len != 0) ret = MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS, mbptr, -1, buf, len); /* Do conversion */
128
    buf[len] = L'0'; /* Must terminate */
129
  }
130
  *wptr = buf; /* Set string pointer to allocated buffer */
131
  if (buflen != NULL) *buflen = len * sizeof (wchar_t); /* Give length of allocated memory if needed. */
132
  return ret; /* Number of characters written */
133
}
134
#endif