2011-08-20 10:59:46 +00:00
|
|
|
/**
|
|
|
|
* OpenAL cross platform audio library
|
|
|
|
* Copyright (C) 2011 by authors.
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2014-08-18 12:11:03 +00:00
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2011-08-20 10:59:46 +00:00
|
|
|
* Or go to http://www.gnu.org/copyleft/lgpl.html
|
|
|
|
*/
|
|
|
|
|
2014-02-27 09:49:23 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
#define _WIN32_IE 0x501
|
|
|
|
#else
|
|
|
|
#define _WIN32_IE 0x400
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2011-08-20 10:59:46 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-11-05 06:36:34 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
2012-11-26 03:20:35 +00:00
|
|
|
#ifdef HAVE_MALLOC_H
|
2012-11-25 10:33:20 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
2012-11-05 06:36:34 +00:00
|
|
|
|
2013-10-07 13:36:58 +00:00
|
|
|
#ifndef AL_NO_UID_DEFS
|
2011-08-20 11:55:11 +00:00
|
|
|
#if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
|
|
|
|
#define INITGUID
|
|
|
|
#include <windows.h>
|
|
|
|
#ifdef HAVE_GUIDDEF_H
|
|
|
|
#include <guiddef.h>
|
|
|
|
#else
|
|
|
|
#include <initguid.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
|
|
|
|
DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
|
|
|
|
|
|
|
|
DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
|
|
|
|
|
|
|
|
DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
|
|
|
|
DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
|
|
|
|
DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
|
|
|
|
DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
|
|
|
|
|
2012-02-17 01:00:41 +00:00
|
|
|
#ifdef HAVE_MMDEVAPI
|
|
|
|
#include <devpropdef.h>
|
|
|
|
DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
|
|
|
|
#endif
|
2011-08-20 11:55:11 +00:00
|
|
|
#endif
|
2013-10-07 13:36:58 +00:00
|
|
|
#endif /* AL_NO_UID_DEFS */
|
|
|
|
|
2012-10-07 04:30:03 +00:00
|
|
|
#ifdef HAVE_DLFCN_H
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
2014-08-11 21:19:15 +00:00
|
|
|
#ifdef HAVE_INTRIN_H
|
|
|
|
#include <intrin.h>
|
|
|
|
#endif
|
2012-10-07 04:30:03 +00:00
|
|
|
#ifdef HAVE_CPUID_H
|
|
|
|
#include <cpuid.h>
|
|
|
|
#endif
|
2014-01-19 03:16:46 +00:00
|
|
|
#ifdef HAVE_SYS_SYSCONF_H
|
|
|
|
#include <sys/sysconf.h>
|
|
|
|
#endif
|
2012-10-07 04:30:03 +00:00
|
|
|
#ifdef HAVE_FLOAT_H
|
|
|
|
#include <float.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_IEEEFP_H
|
|
|
|
#include <ieeefp.h>
|
|
|
|
#endif
|
|
|
|
|
2014-02-27 09:49:23 +00:00
|
|
|
#ifdef _WIN32_IE
|
|
|
|
#include <shlobj.h>
|
|
|
|
#endif
|
|
|
|
|
2011-08-20 10:59:46 +00:00
|
|
|
#include "alMain.h"
|
2014-04-03 20:46:09 +00:00
|
|
|
#include "alu.h"
|
2013-11-04 20:12:31 +00:00
|
|
|
#include "atomic.h"
|
|
|
|
#include "uintmap.h"
|
2014-03-21 07:54:37 +00:00
|
|
|
#include "vector.h"
|
2014-03-28 12:40:24 +00:00
|
|
|
#include "alstring.h"
|
2013-10-28 18:26:26 +00:00
|
|
|
#include "compat.h"
|
2014-04-16 08:39:11 +00:00
|
|
|
#include "threads.h"
|
2011-08-20 10:59:46 +00:00
|
|
|
|
2013-11-04 20:12:31 +00:00
|
|
|
|
2013-11-04 21:44:46 +00:00
|
|
|
extern inline ALuint NextPowerOf2(ALuint value);
|
|
|
|
extern inline ALint fastf2i(ALfloat f);
|
|
|
|
extern inline ALuint fastf2u(ALfloat f);
|
|
|
|
|
2013-11-04 20:12:31 +00:00
|
|
|
|
2012-08-13 15:53:36 +00:00
|
|
|
ALuint CPUCapFlags = 0;
|
|
|
|
|
|
|
|
|
2012-08-13 17:37:49 +00:00
|
|
|
void FillCPUCaps(ALuint capfilter)
|
2012-08-13 15:53:36 +00:00
|
|
|
{
|
2012-08-13 17:37:49 +00:00
|
|
|
ALuint caps = 0;
|
|
|
|
|
2012-08-13 15:53:36 +00:00
|
|
|
/* FIXME: We really should get this for all available CPUs in case different
|
|
|
|
* CPUs have different caps (is that possible on one machine?). */
|
2014-08-11 20:38:43 +00:00
|
|
|
#if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
|
|
|
|
defined(_M_IX86) || defined(_M_X64))
|
2012-08-13 15:53:36 +00:00
|
|
|
union {
|
|
|
|
unsigned int regs[4];
|
|
|
|
char str[sizeof(unsigned int[4])];
|
|
|
|
} cpuinf[3];
|
|
|
|
|
|
|
|
if(!__get_cpuid(0, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
|
|
|
|
ERR("Failed to get CPUID\n");
|
|
|
|
else
|
|
|
|
{
|
2012-08-15 13:28:45 +00:00
|
|
|
unsigned int maxfunc = cpuinf[0].regs[0];
|
|
|
|
unsigned int maxextfunc = 0;
|
|
|
|
|
|
|
|
if(__get_cpuid(0x80000000, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
|
|
|
|
maxextfunc = cpuinf[0].regs[0];
|
|
|
|
TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
|
|
|
|
|
2012-08-13 15:53:36 +00:00
|
|
|
TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
|
2012-08-15 13:28:45 +00:00
|
|
|
if(maxextfunc >= 0x80000004 &&
|
|
|
|
__get_cpuid(0x80000002, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]) &&
|
2012-08-13 15:53:36 +00:00
|
|
|
__get_cpuid(0x80000003, &cpuinf[1].regs[0], &cpuinf[1].regs[1], &cpuinf[1].regs[2], &cpuinf[1].regs[3]) &&
|
|
|
|
__get_cpuid(0x80000004, &cpuinf[2].regs[0], &cpuinf[2].regs[1], &cpuinf[2].regs[2], &cpuinf[2].regs[3]))
|
|
|
|
TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
|
|
|
|
|
2012-08-15 13:28:45 +00:00
|
|
|
if(maxfunc >= 1 &&
|
|
|
|
__get_cpuid(1, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
|
2012-08-13 15:53:36 +00:00
|
|
|
{
|
2013-06-07 02:30:55 +00:00
|
|
|
if((cpuinf[0].regs[3]&(1<<25)))
|
2013-05-22 23:59:20 +00:00
|
|
|
{
|
2012-08-13 17:37:49 +00:00
|
|
|
caps |= CPU_CAP_SSE;
|
2013-06-07 02:30:55 +00:00
|
|
|
if((cpuinf[0].regs[3]&(1<<26)))
|
2014-06-04 09:57:13 +00:00
|
|
|
{
|
2013-05-22 23:59:20 +00:00
|
|
|
caps |= CPU_CAP_SSE2;
|
2014-06-04 09:57:13 +00:00
|
|
|
if((cpuinf[0].regs[2]&(1<<19)))
|
|
|
|
caps |= CPU_CAP_SSE4_1;
|
|
|
|
}
|
2013-05-22 23:59:20 +00:00
|
|
|
}
|
2012-08-13 15:53:36 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-11 21:19:15 +00:00
|
|
|
#elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
|
|
|
|
defined(_M_IX86) || defined(_M_X64))
|
|
|
|
union {
|
|
|
|
int regs[4];
|
|
|
|
char str[sizeof(int[4])];
|
|
|
|
} cpuinf[3];
|
|
|
|
|
|
|
|
(__cpuid)(cpuinf[0].regs, 0);
|
|
|
|
if(cpuinf[0].regs[0] == 0)
|
|
|
|
ERR("Failed to get CPUID\n");
|
2012-10-07 15:18:22 +00:00
|
|
|
else
|
|
|
|
{
|
2014-08-11 21:19:15 +00:00
|
|
|
unsigned int maxfunc = cpuinf[0].regs[0];
|
|
|
|
unsigned int maxextfunc;
|
|
|
|
|
|
|
|
(__cpuid)(cpuinf[0].regs, 0x80000000);
|
|
|
|
maxextfunc = cpuinf[0].regs[0];
|
|
|
|
|
|
|
|
TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
|
|
|
|
|
|
|
|
TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
|
|
|
|
if(maxextfunc >= 0x80000004)
|
2013-05-22 23:59:20 +00:00
|
|
|
{
|
2014-08-11 21:19:15 +00:00
|
|
|
(__cpuid)(cpuinf[0].regs, 0x80000002);
|
|
|
|
(__cpuid)(cpuinf[1].regs, 0x80000003);
|
|
|
|
(__cpuid)(cpuinf[2].regs, 0x80000004);
|
|
|
|
TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(maxfunc >= 1)
|
|
|
|
{
|
|
|
|
(__cpuid)(cpuinf[0].regs, 1);
|
|
|
|
if((cpuinf[0].regs[3]&(1<<25)))
|
|
|
|
{
|
|
|
|
caps |= CPU_CAP_SSE;
|
|
|
|
if((cpuinf[0].regs[3]&(1<<26)))
|
|
|
|
{
|
|
|
|
caps |= CPU_CAP_SSE2;
|
|
|
|
if((cpuinf[0].regs[2]&(1<<19)))
|
|
|
|
caps |= CPU_CAP_SSE4_1;
|
|
|
|
}
|
|
|
|
}
|
2013-05-22 23:59:20 +00:00
|
|
|
}
|
2012-10-07 15:18:22 +00:00
|
|
|
}
|
2014-08-11 21:45:28 +00:00
|
|
|
#else
|
|
|
|
/* Assume support for whatever's supported if we can't check for it */
|
|
|
|
#if defined(HAVE_SSE4_1)
|
|
|
|
#warning "Assuming SSE 4.1 run-time support!"
|
|
|
|
capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE4_1;
|
|
|
|
#elif defined(HAVE_SSE2)
|
|
|
|
#warning "Assuming SSE 2 run-time support!"
|
|
|
|
capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2;
|
|
|
|
#elif defined(HAVE_SSE)
|
|
|
|
#warning "Assuming SSE run-time support!"
|
|
|
|
capfilter |= CPU_CAP_SSE;
|
|
|
|
#endif
|
2012-08-13 15:53:36 +00:00
|
|
|
#endif
|
2012-08-15 08:32:17 +00:00
|
|
|
#ifdef HAVE_NEON
|
2012-08-13 15:53:36 +00:00
|
|
|
/* Assume Neon support if compiled with it */
|
2012-08-13 17:37:49 +00:00
|
|
|
caps |= CPU_CAP_NEON;
|
2012-08-13 15:53:36 +00:00
|
|
|
#endif
|
|
|
|
|
2014-06-08 13:31:36 +00:00
|
|
|
TRACE("Extensions:%s%s%s%s%s\n",
|
|
|
|
((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
|
|
|
|
((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
|
|
|
|
((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
|
|
|
|
((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +Neon" : " -Neon") : ""),
|
|
|
|
((!capfilter) ? " -none-" : "")
|
2014-06-04 09:57:13 +00:00
|
|
|
);
|
2012-08-13 17:37:49 +00:00
|
|
|
CPUCapFlags = caps & capfilter;
|
2012-08-13 15:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-15 12:50:40 +00:00
|
|
|
void *al_malloc(size_t alignment, size_t size)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_ALIGNED_ALLOC)
|
|
|
|
size = (size+(alignment-1))&~(alignment-1);
|
|
|
|
return aligned_alloc(alignment, size);
|
|
|
|
#elif defined(HAVE_POSIX_MEMALIGN)
|
|
|
|
void *ret;
|
|
|
|
if(posix_memalign(&ret, alignment, size) == 0)
|
|
|
|
return ret;
|
|
|
|
return NULL;
|
|
|
|
#elif defined(HAVE__ALIGNED_MALLOC)
|
|
|
|
return _aligned_malloc(size, alignment);
|
|
|
|
#else
|
|
|
|
char *ret = malloc(size+alignment);
|
|
|
|
if(ret != NULL)
|
|
|
|
{
|
|
|
|
*(ret++) = 0x00;
|
2014-09-05 06:23:48 +00:00
|
|
|
while(((ptrdiff_t)ret&(alignment-1)) != 0)
|
2012-08-16 17:30:13 +00:00
|
|
|
*(ret++) = 0x55;
|
2012-08-15 12:50:40 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void *al_calloc(size_t alignment, size_t size)
|
|
|
|
{
|
|
|
|
void *ret = al_malloc(alignment, size);
|
|
|
|
if(ret) memset(ret, 0, size);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void al_free(void *ptr)
|
|
|
|
{
|
2012-08-25 00:45:16 +00:00
|
|
|
#if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
|
2012-08-15 12:50:40 +00:00
|
|
|
free(ptr);
|
2012-08-25 00:45:16 +00:00
|
|
|
#elif defined(HAVE__ALIGNED_MALLOC)
|
|
|
|
_aligned_free(ptr);
|
2012-08-15 12:50:40 +00:00
|
|
|
#else
|
|
|
|
if(ptr != NULL)
|
|
|
|
{
|
|
|
|
char *finder = ptr;
|
|
|
|
do {
|
|
|
|
--finder;
|
2012-08-16 17:30:13 +00:00
|
|
|
} while(*finder == 0x55);
|
2012-08-15 12:50:40 +00:00
|
|
|
free(finder);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-09-14 08:10:19 +00:00
|
|
|
|
2012-09-16 08:35:16 +00:00
|
|
|
void SetMixerFPUMode(FPUCtl *ctl)
|
2012-09-14 08:10:19 +00:00
|
|
|
{
|
2013-05-23 06:05:26 +00:00
|
|
|
#ifdef HAVE_FENV_H
|
|
|
|
fegetenv(STATIC_CAST(fenv_t, ctl));
|
2013-05-23 05:21:00 +00:00
|
|
|
#if defined(__GNUC__) && defined(HAVE_SSE)
|
|
|
|
if((CPUCapFlags&CPU_CAP_SSE))
|
|
|
|
__asm__ __volatile__("stmxcsr %0" : "=m" (*&ctl->sse_state));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef FE_TOWARDZERO
|
|
|
|
fesetround(FE_TOWARDZERO);
|
|
|
|
#endif
|
|
|
|
#if defined(__GNUC__) && defined(HAVE_SSE)
|
2012-09-14 08:10:19 +00:00
|
|
|
if((CPUCapFlags&CPU_CAP_SSE))
|
2012-09-16 10:19:53 +00:00
|
|
|
{
|
2013-05-23 05:21:00 +00:00
|
|
|
int sseState = ctl->sse_state;
|
2013-05-22 23:31:43 +00:00
|
|
|
sseState |= 0x6000; /* set round-to-zero */
|
2012-09-18 22:08:36 +00:00
|
|
|
sseState |= 0x8000; /* set flush-to-zero */
|
2013-05-22 23:59:20 +00:00
|
|
|
if((CPUCapFlags&CPU_CAP_SSE2))
|
|
|
|
sseState |= 0x0040; /* set denormals-are-zero */
|
2012-09-16 10:19:53 +00:00
|
|
|
__asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
|
|
|
|
}
|
|
|
|
#endif
|
2013-05-23 05:21:00 +00:00
|
|
|
|
2012-09-16 08:44:45 +00:00
|
|
|
#elif defined(HAVE___CONTROL87_2)
|
2013-05-23 05:21:00 +00:00
|
|
|
|
2012-09-16 08:44:45 +00:00
|
|
|
int mode;
|
2012-11-13 23:20:36 +00:00
|
|
|
__control87_2(0, 0, &ctl->state, NULL);
|
2014-06-09 06:39:52 +00:00
|
|
|
__control87_2(_RC_CHOP, _MCW_RC, &mode, NULL);
|
2012-09-16 10:19:53 +00:00
|
|
|
#ifdef HAVE_SSE
|
|
|
|
if((CPUCapFlags&CPU_CAP_SSE))
|
2012-11-13 23:20:36 +00:00
|
|
|
{
|
|
|
|
__control87_2(0, 0, NULL, &ctl->sse_state);
|
2012-09-18 22:08:36 +00:00
|
|
|
__control87_2(_RC_CHOP|_DN_FLUSH, _MCW_RC|_MCW_DN, NULL, &mode);
|
2012-11-13 23:20:36 +00:00
|
|
|
}
|
2012-09-16 10:19:53 +00:00
|
|
|
#endif
|
2013-05-23 05:21:00 +00:00
|
|
|
|
2012-09-16 08:35:16 +00:00
|
|
|
#elif defined(HAVE__CONTROLFP)
|
2013-05-23 05:21:00 +00:00
|
|
|
|
2012-09-16 08:35:16 +00:00
|
|
|
ctl->state = _controlfp(0, 0);
|
2014-06-09 06:39:52 +00:00
|
|
|
(void)_controlfp(_RC_CHOP, _MCW_RC);
|
2012-09-14 08:10:19 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-09-16 08:35:16 +00:00
|
|
|
void RestoreFPUMode(const FPUCtl *ctl)
|
2012-09-14 08:10:19 +00:00
|
|
|
{
|
2013-05-23 06:05:26 +00:00
|
|
|
#ifdef HAVE_FENV_H
|
|
|
|
fesetenv(STATIC_CAST(fenv_t, ctl));
|
2013-05-23 05:21:00 +00:00
|
|
|
#if defined(__GNUC__) && defined(HAVE_SSE)
|
2012-09-16 10:19:53 +00:00
|
|
|
if((CPUCapFlags&CPU_CAP_SSE))
|
|
|
|
__asm__ __volatile__("ldmxcsr %0" : : "m" (*&ctl->sse_state));
|
|
|
|
#endif
|
2013-05-23 05:21:00 +00:00
|
|
|
|
2012-09-16 08:44:45 +00:00
|
|
|
#elif defined(HAVE___CONTROL87_2)
|
2013-05-23 05:21:00 +00:00
|
|
|
|
2012-09-16 08:44:45 +00:00
|
|
|
int mode;
|
2014-06-09 06:39:52 +00:00
|
|
|
__control87_2(ctl->state, _MCW_RC, &mode, NULL);
|
2012-09-16 10:19:53 +00:00
|
|
|
#ifdef HAVE_SSE
|
|
|
|
if((CPUCapFlags&CPU_CAP_SSE))
|
2012-09-18 22:08:36 +00:00
|
|
|
__control87_2(ctl->sse_state, _MCW_RC|_MCW_DN, NULL, &mode);
|
2012-09-16 10:19:53 +00:00
|
|
|
#endif
|
2013-05-23 05:21:00 +00:00
|
|
|
|
2012-09-14 08:10:19 +00:00
|
|
|
#elif defined(HAVE__CONTROLFP)
|
2013-05-23 05:21:00 +00:00
|
|
|
|
2014-06-09 06:39:52 +00:00
|
|
|
_controlfp(ctl->state, _MCW_RC);
|
2012-09-14 08:10:19 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-20 10:59:46 +00:00
|
|
|
#ifdef _WIN32
|
2014-04-16 12:19:34 +00:00
|
|
|
|
2014-04-18 04:17:48 +00:00
|
|
|
static WCHAR *FromUTF8(const char *str)
|
|
|
|
{
|
|
|
|
WCHAR *out = NULL;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
|
|
|
|
{
|
|
|
|
out = calloc(sizeof(WCHAR), len);
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-20 10:59:46 +00:00
|
|
|
void *LoadLib(const char *name)
|
2014-04-18 04:17:48 +00:00
|
|
|
{
|
|
|
|
HANDLE hdl = NULL;
|
|
|
|
WCHAR *wname;
|
|
|
|
|
|
|
|
wname = FromUTF8(name);
|
|
|
|
if(!wname)
|
|
|
|
ERR("Failed to convert UTF-8 filename: \"%s\"\n", name);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hdl = LoadLibraryW(wname);
|
|
|
|
free(wname);
|
|
|
|
}
|
|
|
|
return hdl;
|
|
|
|
}
|
2011-08-20 10:59:46 +00:00
|
|
|
void CloseLib(void *handle)
|
|
|
|
{ FreeLibrary((HANDLE)handle); }
|
|
|
|
void *GetSymbol(void *handle, const char *name)
|
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
|
|
|
|
ret = (void*)GetProcAddress((HANDLE)handle, name);
|
|
|
|
if(ret == NULL)
|
|
|
|
ERR("Failed to load %s\n", name);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-01 14:07:00 +00:00
|
|
|
WCHAR *strdupW(const WCHAR *str)
|
|
|
|
{
|
|
|
|
const WCHAR *n;
|
|
|
|
WCHAR *ret;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
n = str;
|
|
|
|
while(*n) n++;
|
|
|
|
len = n - str;
|
|
|
|
|
|
|
|
ret = calloc(sizeof(WCHAR), len+1);
|
|
|
|
if(ret != NULL)
|
|
|
|
memcpy(ret, str, sizeof(WCHAR)*len);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-03-28 08:21:05 +00:00
|
|
|
FILE *al_fopen(const char *fname, const char *mode)
|
|
|
|
{
|
|
|
|
WCHAR *wname=NULL, *wmode=NULL;
|
|
|
|
FILE *file = NULL;
|
|
|
|
|
|
|
|
wname = FromUTF8(fname);
|
|
|
|
wmode = FromUTF8(mode);
|
2014-03-28 07:37:42 +00:00
|
|
|
if(!wname)
|
|
|
|
ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
|
|
|
|
else if(!wmode)
|
|
|
|
ERR("Failed to convert UTF-8 mode: \"%s\"\n", mode);
|
|
|
|
else
|
|
|
|
file = _wfopen(wname, wmode);
|
|
|
|
|
|
|
|
free(wname);
|
|
|
|
free(wmode);
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2011-08-20 10:59:46 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
#ifdef HAVE_DLFCN_H
|
|
|
|
|
|
|
|
void *LoadLib(const char *name)
|
|
|
|
{
|
|
|
|
const char *err;
|
|
|
|
void *handle;
|
|
|
|
|
|
|
|
dlerror();
|
|
|
|
handle = dlopen(name, RTLD_NOW);
|
|
|
|
if((err=dlerror()) != NULL)
|
|
|
|
handle = NULL;
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
void CloseLib(void *handle)
|
|
|
|
{ dlclose(handle); }
|
|
|
|
void *GetSymbol(void *handle, const char *name)
|
|
|
|
{
|
|
|
|
const char *err;
|
|
|
|
void *sym;
|
|
|
|
|
|
|
|
dlerror();
|
|
|
|
sym = dlsym(handle, name);
|
|
|
|
if((err=dlerror()) != NULL)
|
|
|
|
{
|
2011-09-24 21:52:54 +00:00
|
|
|
WARN("Failed to load %s: %s\n", name, err);
|
2011-08-20 10:59:46 +00:00
|
|
|
sym = NULL;
|
|
|
|
}
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-10-07 11:31:31 +00:00
|
|
|
void al_print(const char *type, const char *func, const char *fmt, ...)
|
2011-08-20 10:59:46 +00:00
|
|
|
{
|
2013-05-22 10:02:39 +00:00
|
|
|
va_list ap;
|
2011-08-20 10:59:46 +00:00
|
|
|
|
2013-05-22 10:02:39 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
fprintf(LogFile, "AL lib: %s %s: ", type, func);
|
|
|
|
vfprintf(LogFile, fmt, ap);
|
|
|
|
va_end(ap);
|
2011-08-20 10:59:46 +00:00
|
|
|
|
|
|
|
fflush(LogFile);
|
|
|
|
}
|
|
|
|
|
2014-03-28 08:21:05 +00:00
|
|
|
#ifdef _WIN32
|
2014-04-14 04:44:13 +00:00
|
|
|
static inline int is_slash(int c)
|
|
|
|
{ return (c == '\\' || c == '/'); }
|
|
|
|
|
2014-02-27 09:49:23 +00:00
|
|
|
FILE *OpenDataFile(const char *fname, const char *subdir)
|
|
|
|
{
|
|
|
|
static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
|
2014-03-28 08:21:05 +00:00
|
|
|
WCHAR *wname=NULL, *wsubdir=NULL;
|
2014-07-21 11:42:45 +00:00
|
|
|
FILE *f;
|
2014-02-27 09:49:23 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* If the path is absolute, open it directly. */
|
2014-04-14 04:44:13 +00:00
|
|
|
if(fname[0] != '\0' && fname[1] == ':' && is_slash(fname[2]))
|
2014-02-27 09:49:23 +00:00
|
|
|
{
|
2014-03-28 07:37:42 +00:00
|
|
|
if((f=al_fopen(fname, "rb")) != NULL)
|
2014-02-27 09:49:23 +00:00
|
|
|
{
|
|
|
|
TRACE("Opened %s\n", fname);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
WARN("Could not open %s\n", fname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-21 11:42:45 +00:00
|
|
|
/* If it's relative, try the current directory first before the data directories. */
|
|
|
|
if((f=al_fopen(fname, "rb")) != NULL)
|
|
|
|
{
|
|
|
|
TRACE("Opened %s\n", fname);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2014-03-28 08:21:05 +00:00
|
|
|
wname = FromUTF8(fname);
|
|
|
|
wsubdir = FromUTF8(subdir);
|
|
|
|
if(!wname)
|
|
|
|
ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
|
|
|
|
else if(!wsubdir)
|
|
|
|
ERR("Failed to convert UTF-8 subdir: \"%s\"\n", subdir);
|
|
|
|
else for(i = 0;i < 2;i++)
|
2014-02-27 09:49:23 +00:00
|
|
|
{
|
2014-03-28 08:21:05 +00:00
|
|
|
WCHAR buffer[PATH_MAX];
|
2014-02-27 09:49:23 +00:00
|
|
|
size_t len;
|
|
|
|
|
2014-03-28 08:21:05 +00:00
|
|
|
if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) == FALSE)
|
2014-02-27 09:49:23 +00:00
|
|
|
continue;
|
|
|
|
|
2014-03-28 08:21:05 +00:00
|
|
|
len = lstrlenW(buffer);
|
2014-04-14 04:44:13 +00:00
|
|
|
if(len > 0 && is_slash(buffer[len-1]))
|
2014-02-27 09:49:23 +00:00
|
|
|
buffer[--len] = '\0';
|
2014-03-28 08:21:05 +00:00
|
|
|
_snwprintf(buffer+len, PATH_MAX-len, L"/%ls/%ls", wsubdir, wname);
|
|
|
|
len = lstrlenW(buffer);
|
2014-02-27 09:49:23 +00:00
|
|
|
while(len > 0)
|
|
|
|
{
|
|
|
|
--len;
|
|
|
|
if(buffer[len] == '/')
|
|
|
|
buffer[len] = '\\';
|
|
|
|
}
|
|
|
|
|
2014-03-28 08:21:05 +00:00
|
|
|
if((f=_wfopen(buffer, L"rb")) != NULL)
|
2014-02-27 09:49:23 +00:00
|
|
|
{
|
2014-03-28 08:21:05 +00:00
|
|
|
TRACE("Opened %ls\n", buffer);
|
2014-02-27 09:49:23 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
}
|
2014-11-19 21:07:37 +00:00
|
|
|
WARN("Could not open %ls\\%ls\n", wsubdir, wname);
|
2014-03-28 08:21:05 +00:00
|
|
|
free(wname);
|
|
|
|
free(wsubdir);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-27 09:49:23 +00:00
|
|
|
#else
|
2014-03-28 08:21:05 +00:00
|
|
|
FILE *OpenDataFile(const char *fname, const char *subdir)
|
|
|
|
{
|
|
|
|
char buffer[PATH_MAX] = "";
|
2014-02-27 09:49:23 +00:00
|
|
|
const char *str, *next;
|
2014-03-28 08:21:05 +00:00
|
|
|
FILE *f;
|
2014-02-27 09:49:23 +00:00
|
|
|
|
|
|
|
if(fname[0] == '/')
|
|
|
|
{
|
2014-03-28 07:37:42 +00:00
|
|
|
if((f=al_fopen(fname, "rb")) != NULL)
|
2014-02-27 09:49:23 +00:00
|
|
|
{
|
|
|
|
TRACE("Opened %s\n", fname);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
WARN("Could not open %s\n", fname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-21 11:42:45 +00:00
|
|
|
if((f=al_fopen(fname, "rb")) != NULL)
|
|
|
|
{
|
|
|
|
TRACE("Opened %s\n", fname);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2014-02-27 09:49:23 +00:00
|
|
|
if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s/%s/%s", str, subdir, fname);
|
|
|
|
else if((str=getenv("HOME")) != NULL && str[0] != '\0')
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s/.local/share/%s/%s", str, subdir, fname);
|
|
|
|
if(buffer[0])
|
|
|
|
{
|
2014-03-28 07:37:42 +00:00
|
|
|
if((f=al_fopen(buffer, "rb")) != NULL)
|
2014-02-27 09:49:23 +00:00
|
|
|
{
|
|
|
|
TRACE("Opened %s\n", buffer);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
|
2014-04-07 20:24:51 +00:00
|
|
|
str = "/usr/local/share/:/usr/share/";
|
2014-02-27 09:49:23 +00:00
|
|
|
|
|
|
|
next = str;
|
|
|
|
while((str=next) != NULL && str[0] != '\0')
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
next = strchr(str, ':');
|
|
|
|
|
|
|
|
if(!next)
|
|
|
|
len = strlen(str);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = next - str;
|
|
|
|
next++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(len > sizeof(buffer)-1)
|
|
|
|
len = sizeof(buffer)-1;
|
|
|
|
strncpy(buffer, str, len);
|
|
|
|
buffer[len] = '\0';
|
|
|
|
snprintf(buffer+len, sizeof(buffer)-len, "/%s/%s", subdir, fname);
|
|
|
|
|
2014-03-28 07:37:42 +00:00
|
|
|
if((f=al_fopen(buffer, "rb")) != NULL)
|
2014-02-27 09:49:23 +00:00
|
|
|
{
|
|
|
|
TRACE("Opened %s\n", buffer);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
}
|
2014-11-19 21:07:37 +00:00
|
|
|
WARN("Could not open %s/%s\n", subdir, fname);
|
2014-02-27 09:49:23 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-03-28 08:21:05 +00:00
|
|
|
#endif
|
2014-02-27 09:49:23 +00:00
|
|
|
|
|
|
|
|
2011-08-20 10:59:46 +00:00
|
|
|
void SetRTPriority(void)
|
|
|
|
{
|
2011-08-31 08:05:01 +00:00
|
|
|
ALboolean failed = AL_FALSE;
|
2011-08-20 10:59:46 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if(RTPrioLevel > 0)
|
|
|
|
failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
|
|
|
|
#elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
|
|
|
|
if(RTPrioLevel > 0)
|
|
|
|
{
|
2011-08-31 08:05:01 +00:00
|
|
|
struct sched_param param;
|
2011-08-20 10:59:46 +00:00
|
|
|
/* Use the minimum real-time priority possible for now (on Linux this
|
|
|
|
* should be 1 for SCHED_RR) */
|
|
|
|
param.sched_priority = sched_get_priority_min(SCHED_RR);
|
|
|
|
failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, ¶m);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* Real-time priority not available */
|
|
|
|
failed = (RTPrioLevel>0);
|
|
|
|
#endif
|
|
|
|
if(failed)
|
|
|
|
ERR("Failed to set priority level for thread\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-01 04:47:22 +00:00
|
|
|
ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_size, size_t obj_count, ALboolean exact)
|
2014-03-21 07:54:37 +00:00
|
|
|
{
|
2014-08-10 15:46:55 +00:00
|
|
|
vector_ *vecptr = (vector_*)ptr;
|
2014-08-10 17:52:08 +00:00
|
|
|
if((*vecptr ? (*vecptr)->Capacity : 0) < obj_count)
|
2014-03-21 07:54:37 +00:00
|
|
|
{
|
2014-10-13 14:00:43 +00:00
|
|
|
size_t old_size = (*vecptr ? (*vecptr)->Size : 0);
|
2014-03-21 07:54:37 +00:00
|
|
|
void *temp;
|
|
|
|
|
2014-03-21 23:35:38 +00:00
|
|
|
/* Use the next power-of-2 size if we don't need to allocate the exact
|
|
|
|
* amount. This is preferred when regularly increasing the vector since
|
|
|
|
* it means fewer reallocations. Though it means it also wastes some
|
|
|
|
* memory. */
|
2014-10-01 04:47:22 +00:00
|
|
|
if(exact == AL_FALSE && obj_count < INT_MAX)
|
2014-03-23 22:22:37 +00:00
|
|
|
obj_count = NextPowerOf2((ALuint)obj_count);
|
2014-03-21 23:35:38 +00:00
|
|
|
|
2014-03-21 07:54:37 +00:00
|
|
|
/* Need to be explicit with the caller type's base size, because it
|
2014-03-21 21:03:26 +00:00
|
|
|
* could have extra padding before the start of the array (that is,
|
2014-03-21 07:54:37 +00:00
|
|
|
* sizeof(*vector_) may not equal base_size). */
|
|
|
|
temp = realloc(*vecptr, base_size + obj_size*obj_count);
|
|
|
|
if(temp == NULL) return AL_FALSE;
|
|
|
|
|
|
|
|
*vecptr = temp;
|
2014-08-10 17:52:08 +00:00
|
|
|
(*vecptr)->Capacity = obj_count;
|
2014-04-03 18:13:12 +00:00
|
|
|
(*vecptr)->Size = old_size;
|
2014-03-21 07:54:37 +00:00
|
|
|
}
|
|
|
|
return AL_TRUE;
|
|
|
|
}
|
|
|
|
|
2014-10-01 04:47:22 +00:00
|
|
|
ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_size, size_t obj_count)
|
2014-03-28 12:40:24 +00:00
|
|
|
{
|
2014-08-10 15:46:55 +00:00
|
|
|
vector_ *vecptr = (vector_*)ptr;
|
2014-04-03 18:13:12 +00:00
|
|
|
if(*vecptr || obj_count > 0)
|
|
|
|
{
|
2014-08-10 17:52:08 +00:00
|
|
|
if(!vector_reserve((char*)vecptr, base_size, obj_size, obj_count, AL_TRUE))
|
2014-04-03 18:13:12 +00:00
|
|
|
return AL_FALSE;
|
2014-08-10 17:52:08 +00:00
|
|
|
(*vecptr)->Size = obj_count;
|
2014-04-03 18:13:12 +00:00
|
|
|
}
|
2014-03-28 12:40:24 +00:00
|
|
|
return AL_TRUE;
|
|
|
|
}
|
|
|
|
|
2014-08-10 15:46:55 +00:00
|
|
|
ALboolean vector_insert(char *ptr, size_t base_size, size_t obj_size, void *ins_pos, const void *datstart, const void *datend)
|
2014-04-10 05:50:28 +00:00
|
|
|
{
|
2014-08-10 15:46:55 +00:00
|
|
|
vector_ *vecptr = (vector_*)ptr;
|
2014-04-14 00:06:18 +00:00
|
|
|
if(datstart != datend)
|
2014-04-10 05:50:28 +00:00
|
|
|
{
|
2014-08-10 16:12:08 +00:00
|
|
|
ptrdiff_t ins_elem = (*vecptr ? ((char*)ins_pos - ((char*)(*vecptr) + base_size)) :
|
|
|
|
((char*)ins_pos - (char*)NULL)) /
|
|
|
|
obj_size;
|
2014-04-14 00:06:18 +00:00
|
|
|
ptrdiff_t numins = ((const char*)datend - (const char*)datstart) / obj_size;
|
2014-05-22 19:16:22 +00:00
|
|
|
|
|
|
|
assert(numins > 0);
|
2014-10-01 04:47:22 +00:00
|
|
|
if((size_t)numins + VECTOR_SIZE(*vecptr) < (size_t)numins ||
|
2014-08-10 17:52:08 +00:00
|
|
|
!vector_reserve((char*)vecptr, base_size, obj_size, VECTOR_SIZE(*vecptr)+numins, AL_TRUE))
|
2014-04-10 05:50:28 +00:00
|
|
|
return AL_FALSE;
|
2014-05-22 19:16:22 +00:00
|
|
|
|
2014-04-14 01:42:41 +00:00
|
|
|
/* NOTE: ins_pos may have been invalidated if *vecptr moved. Use ins_elem instead. */
|
2014-10-01 04:47:22 +00:00
|
|
|
if((size_t)ins_elem < (*vecptr)->Size)
|
2014-04-10 05:50:28 +00:00
|
|
|
{
|
|
|
|
memmove((char*)(*vecptr) + base_size + ((ins_elem+numins)*obj_size),
|
|
|
|
(char*)(*vecptr) + base_size + ((ins_elem )*obj_size),
|
|
|
|
((*vecptr)->Size-ins_elem)*obj_size);
|
|
|
|
}
|
|
|
|
memcpy((char*)(*vecptr) + base_size + (ins_elem*obj_size),
|
|
|
|
datstart, numins*obj_size);
|
2014-10-01 04:47:22 +00:00
|
|
|
(*vecptr)->Size += numins;
|
2014-04-10 05:50:28 +00:00
|
|
|
}
|
|
|
|
return AL_TRUE;
|
|
|
|
}
|
|
|
|
|
2014-03-28 12:40:24 +00:00
|
|
|
|
2014-07-06 10:27:39 +00:00
|
|
|
extern inline void al_string_deinit(al_string *str);
|
2014-10-01 04:47:22 +00:00
|
|
|
extern inline size_t al_string_length(const_al_string str);
|
2014-04-30 19:53:52 +00:00
|
|
|
extern inline ALboolean al_string_empty(const_al_string str);
|
2014-03-28 12:40:24 +00:00
|
|
|
extern inline const al_string_char_type *al_string_get_cstr(const_al_string str);
|
|
|
|
|
|
|
|
void al_string_clear(al_string *str)
|
|
|
|
{
|
|
|
|
/* Reserve one more character than the total size of the string. This is to
|
|
|
|
* ensure we have space to add a null terminator in the string data so it
|
|
|
|
* can be used as a C-style string. */
|
|
|
|
VECTOR_RESERVE(*str, 1);
|
|
|
|
VECTOR_RESIZE(*str, 0);
|
|
|
|
*VECTOR_ITER_END(*str) = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-01 04:47:22 +00:00
|
|
|
static inline int al_string_compare(const al_string_char_type *str1, size_t str1len,
|
|
|
|
const al_string_char_type *str2, size_t str2len)
|
2014-04-03 20:46:09 +00:00
|
|
|
{
|
2014-10-13 14:04:36 +00:00
|
|
|
size_t complen = (str1len < str2len) ? str1len : str2len;
|
2014-04-03 20:46:09 +00:00
|
|
|
int ret = memcmp(str1, str2, complen);
|
|
|
|
if(ret == 0)
|
|
|
|
{
|
|
|
|
if(str1len > str2len) return 1;
|
|
|
|
if(str1len < str2len) return -1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
int al_string_cmp(const_al_string str1, const_al_string str2)
|
|
|
|
{
|
|
|
|
return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
|
|
|
|
&VECTOR_FRONT(str2), al_string_length(str2));
|
|
|
|
}
|
|
|
|
int al_string_cmp_cstr(const_al_string str1, const al_string_char_type *str2)
|
|
|
|
{
|
|
|
|
return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
|
2014-10-13 14:00:43 +00:00
|
|
|
str2, strlen(str2));
|
2014-04-03 20:46:09 +00:00
|
|
|
}
|
|
|
|
|
2014-03-28 12:40:24 +00:00
|
|
|
void al_string_copy(al_string *str, const_al_string from)
|
|
|
|
{
|
2014-10-13 14:00:43 +00:00
|
|
|
size_t len = al_string_length(from);
|
2014-03-28 12:40:24 +00:00
|
|
|
VECTOR_RESERVE(*str, len+1);
|
2014-04-14 03:54:12 +00:00
|
|
|
VECTOR_RESIZE(*str, 0);
|
|
|
|
VECTOR_INSERT(*str, VECTOR_ITER_END(*str), VECTOR_ITER_BEGIN(from), VECTOR_ITER_BEGIN(from)+len);
|
2014-03-28 12:40:24 +00:00
|
|
|
*VECTOR_ITER_END(*str) = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void al_string_copy_cstr(al_string *str, const al_string_char_type *from)
|
|
|
|
{
|
2014-03-28 15:19:45 +00:00
|
|
|
size_t len = strlen(from);
|
2014-03-28 12:40:24 +00:00
|
|
|
VECTOR_RESERVE(*str, len+1);
|
2014-04-14 03:54:12 +00:00
|
|
|
VECTOR_RESIZE(*str, 0);
|
|
|
|
VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
|
2014-03-28 12:40:24 +00:00
|
|
|
*VECTOR_ITER_END(*str) = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void al_string_append_char(al_string *str, const al_string_char_type c)
|
|
|
|
{
|
|
|
|
VECTOR_RESERVE(*str, al_string_length(*str)+2);
|
|
|
|
VECTOR_PUSH_BACK(*str, c);
|
|
|
|
*VECTOR_ITER_END(*str) = 0;
|
|
|
|
}
|
|
|
|
|
2014-03-28 14:59:47 +00:00
|
|
|
void al_string_append_cstr(al_string *str, const al_string_char_type *from)
|
|
|
|
{
|
|
|
|
size_t len = strlen(from);
|
|
|
|
if(len != 0)
|
|
|
|
{
|
|
|
|
VECTOR_RESERVE(*str, al_string_length(*str)+len+1);
|
2014-04-14 03:54:12 +00:00
|
|
|
VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
|
2014-03-28 14:59:47 +00:00
|
|
|
*VECTOR_ITER_END(*str) = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 12:40:24 +00:00
|
|
|
void al_string_append_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
|
|
|
|
{
|
2014-04-14 03:54:12 +00:00
|
|
|
if(to != from)
|
2014-03-28 12:40:24 +00:00
|
|
|
{
|
2014-04-14 03:54:12 +00:00
|
|
|
VECTOR_RESERVE(*str, al_string_length(*str)+(to-from)+1);
|
|
|
|
VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, to);
|
2014-03-28 12:40:24 +00:00
|
|
|
*VECTOR_ITER_END(*str) = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 14:59:47 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
void al_string_copy_wcstr(al_string *str, const wchar_t *from)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
|
|
|
|
{
|
|
|
|
VECTOR_RESERVE(*str, len);
|
|
|
|
VECTOR_RESIZE(*str, len-1);
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, NULL, NULL);
|
|
|
|
*VECTOR_ITER_END(*str) = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|