Subversion Repositories Kolibri OS

Rev

Rev 1905 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1905 Rev 3960
Line 1... Line 1...
1
/*
1
/*
2
	compat: Some compatibility functions. Basic standard C stuff, that may barely be above/around C89.
2
	compat: Some compatibility functions.
Line 3... Line 3...
3
 
3
 
-
 
4
	The mpg123 code is determined to keep it's legacy. A legacy of old, old UNIX.
Line 4... Line 5...
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;-)
5
 
6
 
6
	copyright 2007-8 by the mpg123 project - free software under the terms of the LGPL 2.1
7
	copyright 2007-8 by the mpg123 project - free software under the terms of the LGPL 2.1
7
	see COPYING and AUTHORS files in distribution or http://mpg123.org
8
	see COPYING and AUTHORS files in distribution or http://mpg123.org
Line 8... Line 9...
8
	initially written by Thomas Orgis
9
	initially written by Thomas Orgis, Windows Unicode stuff by JonY.
9
*/
10
*/
Line -... Line 11...
-
 
11
 
-
 
12
#include "config.h"
-
 
13
#include "compat.h"
-
 
14
 
-
 
15
#ifdef _MSC_VER
-
 
16
#include 
-
 
17
#else
-
 
18
#include 
-
 
19
#endif
-
 
20
#include 
-
 
21
 
10
 
22
#include "debug.h"
11
#include "config.h"
23
 
12
#include "compat.h"
24
int _EXFUN(open, (const char *, int, ...));
13
 
25
 
14
/* A safe realloc also for very old systems where realloc(NULL, size) returns NULL. */
26
/* A safe realloc also for very old systems where realloc(NULL, size) returns NULL. */
Line 37... Line 49...
37
	return NULL;
49
	return NULL;
38
	else
50
	else
39
	return strcpy(dest, src);
51
	return strcpy(dest, src);
40
}
52
}
41
#endif
53
#endif
-
 
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