~ubuntu-branches/debian/sid/audacious-plugins/sid

« back to all changes in this revision

Viewing changes to src/mpg123/libmpg123/compat.c

  • Committer: Bazaar Package Importer
  • Author(s): Bilal Akhtar
  • Date: 2011-04-10 18:56:21 UTC
  • mfrom: (1.1.14 upstream) (2.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110410185621-7eg1k5a7v3f4aqrn
Tags: 2.4.4-1
* New upstream release.
  - Fix FTBFS with GCC 4.5 (Closes: #621989)
* Upload to unstable.
* debian/control:
  - Update versioned dependencies according to upstream changes.
  - Bump Standards-Version to 3.9.2 (no changes needed).
  - Since the package will be building against the new FFMpeg libs,
    fix the problem of depending on old uninstallable libav*
    packages (Closes: #617603)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
        compat: Some compatibility functions.
 
3
 
 
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;-)
 
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
 
9
        initially written by Thomas Orgis, Windows Unicode stuff by JonY.
 
10
*/
 
11
 
 
12
#include "config.h"
 
13
#include "compat.h"
 
14
 
 
15
#ifdef _MSC_VER
 
16
#include <io.h>
 
17
#else
 
18
#include <fcntl.h>
 
19
#endif
 
20
 
 
21
#ifdef WANT_WIN32_UNICODE
 
22
#include <wchar.h>
 
23
#include <windows.h>
 
24
#include <winnls.h>
 
25
#endif
 
26
 
 
27
#include "debug.h"
 
28
 
 
29
/* A safe realloc also for very old systems where realloc(NULL, size) returns NULL. */
 
30
void *safe_realloc(void *ptr, size_t size)
 
31
{
 
32
        if(ptr == NULL) return malloc(size);
 
33
        else return realloc(ptr, size);
 
34
}
 
35
 
 
36
#ifndef HAVE_STRERROR
 
37
const char *strerror(int errnum)
 
38
{
 
39
        extern int sys_nerr;
 
40
        extern char *sys_errlist[];
 
41
 
 
42
        return (errnum < sys_nerr) ?  sys_errlist[errnum]  :  "";
 
43
}
 
44
#endif
 
45
 
 
46
#ifndef HAVE_STRDUP
 
47
char *strdup(const char *src)
 
48
{
 
49
        char *dest;
 
50
 
 
51
        if (!(dest = (char *) malloc(strlen(src)+1)))
 
52
        return NULL;
 
53
        else
 
54
        return strcpy(dest, src);
 
55
}
 
56
#endif
 
57
 
 
58
int compat_open(const char *filename, int mode)
 
59
{
 
60
        int ret;
 
61
#if defined (WANT_WIN32_UNICODE)
 
62
        const wchar_t *frag = NULL;
 
63
 
 
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 */
 
66
 
 
67
        ret = _wopen(frag, mode); /*Try _wopen */
 
68
        if (ret != -1 ) goto open_ok; /* msdn says -1 means failure */
 
69
 
 
70
fallback:
 
71
#endif
 
72
 
 
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 */
 
75
#else
 
76
        ret = open (filename, mode);
 
77
#endif
 
78
 
 
79
#if defined (WANT_WIN32_UNICODE)
 
80
open_ok:
 
81
        free ((void *)frag); /* Freeing a NULL should be OK */
 
82
#endif
 
83
 
 
84
        return ret;
 
85
}
 
86
 
 
87
int compat_close(int infd)
 
88
{
 
89
#ifdef __MSVCRT__ /* MSDN says POSIX function is deprecated beginning in Visual C++ 2005 */
 
90
        return _close(infd);
 
91
#else
 
92
        return close(infd);
 
93
#endif
 
94
}
 
95
 
 
96
/* Windows Unicode stuff */
 
97
 
 
98
#ifdef WANT_WIN32_UNICODE
 
99
int win32_wide_utf8(const wchar_t * const wptr, const char **const mbptr, size_t * const buflen)
 
100
{
 
101
        size_t len;
 
102
        char *buf;
 
103
        int ret;
 
104
 
 
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);
 
108
 
 
109
        if(buf != NULL)
 
110
        {
 
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. */
 
114
 
 
115
                return ret;
 
116
        }
 
117
        else
 
118
        {
 
119
                if(buflen != NULL) *buflen = 0;
 
120
 
 
121
                return 0;
 
122
        }
 
123
}
 
124
 
 
125
int win32_utf8_wide(const char *const mbptr, const wchar_t ** const wptr, size_t * const buflen)
 
126
{
 
127
        size_t len;
 
128
        wchar_t *buf;
 
129
        int ret;
 
130
 
 
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);
 
134
 
 
135
        if(buf != NULL)
 
136
        {
 
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. */
 
140
 
 
141
                return ret;
 
142
        }
 
143
        else
 
144
        {
 
145
                if (buflen != NULL) *buflen = 0;
 
146
 
 
147
                return 0;
 
148
        }
 
149
}
 
150
#endif