Make the modulator effect multichannel
[openal-soft.git] / Alc / helpers.c
blob9a9273c44d23a86e2991064a918d1f0625c1f99c
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 2011 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #ifdef _WIN32
22 #ifdef __MINGW32__
23 #define _WIN32_IE 0x501
24 #else
25 #define _WIN32_IE 0x400
26 #endif
27 #endif
29 #include "config.h"
31 #include <stdlib.h>
32 #include <time.h>
33 #include <errno.h>
34 #include <stdarg.h>
35 #ifdef HAVE_MALLOC_H
36 #include <malloc.h>
37 #endif
38 #ifdef HAVE_DIRENT_H
39 #include <dirent.h>
40 #endif
42 #ifndef AL_NO_UID_DEFS
43 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
44 #define INITGUID
45 #include <windows.h>
46 #ifdef HAVE_GUIDDEF_H
47 #include <guiddef.h>
48 #else
49 #include <initguid.h>
50 #endif
52 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
53 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
55 DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
57 DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
58 DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
59 DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
60 DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
61 DEFINE_GUID(IID_IAudioCaptureClient, 0xc8adbd64, 0xe71e, 0x48a0, 0xa4,0xde, 0x18,0x5c,0x39,0x5c,0xd3,0x17);
63 #ifdef HAVE_MMDEVAPI
64 #include <devpropdef.h>
65 #include <propkeydef.h>
66 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
67 DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_FormFactor, 0x1da5d803, 0xd492, 0x4edd, 0x8c,0x23, 0xe0,0xc0,0xff,0xee,0x7f,0x0e, 0);
68 #endif
69 #endif
70 #endif /* AL_NO_UID_DEFS */
72 #ifdef HAVE_DLFCN_H
73 #include <dlfcn.h>
74 #endif
75 #ifdef HAVE_INTRIN_H
76 #include <intrin.h>
77 #endif
78 #ifdef HAVE_CPUID_H
79 #include <cpuid.h>
80 #endif
81 #ifdef HAVE_SYS_SYSCONF_H
82 #include <sys/sysconf.h>
83 #endif
84 #ifdef HAVE_FLOAT_H
85 #include <float.h>
86 #endif
87 #ifdef HAVE_IEEEFP_H
88 #include <ieeefp.h>
89 #endif
91 #ifndef _WIN32
92 #include <unistd.h>
93 #elif defined(_WIN32_IE)
94 #include <shlobj.h>
95 #endif
97 #include "alMain.h"
98 #include "alu.h"
99 #include "atomic.h"
100 #include "uintmap.h"
101 #include "vector.h"
102 #include "alstring.h"
103 #include "compat.h"
104 #include "threads.h"
107 extern inline ALuint NextPowerOf2(ALuint value);
108 extern inline ALint fastf2i(ALfloat f);
109 extern inline ALuint fastf2u(ALfloat f);
112 ALuint CPUCapFlags = 0;
115 void FillCPUCaps(ALuint capfilter)
117 ALuint caps = 0;
119 /* FIXME: We really should get this for all available CPUs in case different
120 * CPUs have different caps (is that possible on one machine?). */
121 #if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
122 defined(_M_IX86) || defined(_M_X64))
123 union {
124 unsigned int regs[4];
125 char str[sizeof(unsigned int[4])];
126 } cpuinf[3];
128 if(!__get_cpuid(0, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
129 ERR("Failed to get CPUID\n");
130 else
132 unsigned int maxfunc = cpuinf[0].regs[0];
133 unsigned int maxextfunc = 0;
135 if(__get_cpuid(0x80000000, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
136 maxextfunc = cpuinf[0].regs[0];
137 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
139 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
140 if(maxextfunc >= 0x80000004 &&
141 __get_cpuid(0x80000002, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]) &&
142 __get_cpuid(0x80000003, &cpuinf[1].regs[0], &cpuinf[1].regs[1], &cpuinf[1].regs[2], &cpuinf[1].regs[3]) &&
143 __get_cpuid(0x80000004, &cpuinf[2].regs[0], &cpuinf[2].regs[1], &cpuinf[2].regs[2], &cpuinf[2].regs[3]))
144 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
146 if(maxfunc >= 1 &&
147 __get_cpuid(1, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
149 if((cpuinf[0].regs[3]&(1<<25)))
151 caps |= CPU_CAP_SSE;
152 if((cpuinf[0].regs[3]&(1<<26)))
154 caps |= CPU_CAP_SSE2;
155 if((cpuinf[0].regs[2]&(1<<0)))
157 caps |= CPU_CAP_SSE3;
158 if((cpuinf[0].regs[2]&(1<<19)))
159 caps |= CPU_CAP_SSE4_1;
165 #elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
166 defined(_M_IX86) || defined(_M_X64))
167 union {
168 int regs[4];
169 char str[sizeof(int[4])];
170 } cpuinf[3];
172 (__cpuid)(cpuinf[0].regs, 0);
173 if(cpuinf[0].regs[0] == 0)
174 ERR("Failed to get CPUID\n");
175 else
177 unsigned int maxfunc = cpuinf[0].regs[0];
178 unsigned int maxextfunc;
180 (__cpuid)(cpuinf[0].regs, 0x80000000);
181 maxextfunc = cpuinf[0].regs[0];
183 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
185 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
186 if(maxextfunc >= 0x80000004)
188 (__cpuid)(cpuinf[0].regs, 0x80000002);
189 (__cpuid)(cpuinf[1].regs, 0x80000003);
190 (__cpuid)(cpuinf[2].regs, 0x80000004);
191 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
194 if(maxfunc >= 1)
196 (__cpuid)(cpuinf[0].regs, 1);
197 if((cpuinf[0].regs[3]&(1<<25)))
199 caps |= CPU_CAP_SSE;
200 if((cpuinf[0].regs[3]&(1<<26)))
202 caps |= CPU_CAP_SSE2;
203 if((cpuinf[0].regs[2]&(1<<0)))
205 caps |= CPU_CAP_SSE3;
206 if((cpuinf[0].regs[2]&(1<<19)))
207 caps |= CPU_CAP_SSE4_1;
213 #else
214 /* Assume support for whatever's supported if we can't check for it */
215 #if defined(HAVE_SSE4_1)
216 #warning "Assuming SSE 4.1 run-time support!"
217 caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3 | CPU_CAP_SSE4_1;
218 #elif defined(HAVE_SSE3)
219 #warning "Assuming SSE 3 run-time support!"
220 caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3;
221 #elif defined(HAVE_SSE2)
222 #warning "Assuming SSE 2 run-time support!"
223 caps |= CPU_CAP_SSE | CPU_CAP_SSE2;
224 #elif defined(HAVE_SSE)
225 #warning "Assuming SSE run-time support!"
226 caps |= CPU_CAP_SSE;
227 #endif
228 #endif
229 #ifdef HAVE_NEON
230 /* Assume Neon support if compiled with it */
231 caps |= CPU_CAP_NEON;
232 #endif
234 TRACE("Extensions:%s%s%s%s%s%s\n",
235 ((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
236 ((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
237 ((capfilter&CPU_CAP_SSE3) ? ((caps&CPU_CAP_SSE3) ? " +SSE3" : " -SSE3") : ""),
238 ((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
239 ((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +Neon" : " -Neon") : ""),
240 ((!capfilter) ? " -none-" : "")
242 CPUCapFlags = caps & capfilter;
246 void *al_malloc(size_t alignment, size_t size)
248 #if defined(HAVE_ALIGNED_ALLOC)
249 size = (size+(alignment-1))&~(alignment-1);
250 return aligned_alloc(alignment, size);
251 #elif defined(HAVE_POSIX_MEMALIGN)
252 void *ret;
253 if(posix_memalign(&ret, alignment, size) == 0)
254 return ret;
255 return NULL;
256 #elif defined(HAVE__ALIGNED_MALLOC)
257 return _aligned_malloc(size, alignment);
258 #else
259 char *ret = malloc(size+alignment);
260 if(ret != NULL)
262 *(ret++) = 0x00;
263 while(((ptrdiff_t)ret&(alignment-1)) != 0)
264 *(ret++) = 0x55;
266 return ret;
267 #endif
270 void *al_calloc(size_t alignment, size_t size)
272 void *ret = al_malloc(alignment, size);
273 if(ret) memset(ret, 0, size);
274 return ret;
277 void al_free(void *ptr)
279 #if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
280 free(ptr);
281 #elif defined(HAVE__ALIGNED_MALLOC)
282 _aligned_free(ptr);
283 #else
284 if(ptr != NULL)
286 char *finder = ptr;
287 do {
288 --finder;
289 } while(*finder == 0x55);
290 free(finder);
292 #endif
296 void SetMixerFPUMode(FPUCtl *ctl)
298 #ifdef HAVE_FENV_H
299 fegetenv(STATIC_CAST(fenv_t, ctl));
300 #if defined(__GNUC__) && defined(HAVE_SSE)
301 /* FIXME: Some fegetenv implementations can get the SSE environment too?
302 * How to tell when it does? */
303 if((CPUCapFlags&CPU_CAP_SSE))
304 __asm__ __volatile__("stmxcsr %0" : "=m" (*&ctl->sse_state));
305 #endif
307 #ifdef FE_TOWARDZERO
308 fesetround(FE_TOWARDZERO);
309 #endif
310 #if defined(__GNUC__) && defined(HAVE_SSE)
311 if((CPUCapFlags&CPU_CAP_SSE))
313 int sseState = ctl->sse_state;
314 sseState |= 0x6000; /* set round-to-zero */
315 sseState |= 0x8000; /* set flush-to-zero */
316 if((CPUCapFlags&CPU_CAP_SSE2))
317 sseState |= 0x0040; /* set denormals-are-zero */
318 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
320 #endif
322 #elif defined(HAVE___CONTROL87_2)
324 int mode;
325 __control87_2(0, 0, &ctl->state, NULL);
326 __control87_2(_RC_CHOP, _MCW_RC, &mode, NULL);
327 #ifdef HAVE_SSE
328 if((CPUCapFlags&CPU_CAP_SSE))
330 __control87_2(0, 0, NULL, &ctl->sse_state);
331 __control87_2(_RC_CHOP|_DN_FLUSH, _MCW_RC|_MCW_DN, NULL, &mode);
333 #endif
335 #elif defined(HAVE__CONTROLFP)
337 ctl->state = _controlfp(0, 0);
338 (void)_controlfp(_RC_CHOP, _MCW_RC);
339 #endif
342 void RestoreFPUMode(const FPUCtl *ctl)
344 #ifdef HAVE_FENV_H
345 fesetenv(STATIC_CAST(fenv_t, ctl));
346 #if defined(__GNUC__) && defined(HAVE_SSE)
347 if((CPUCapFlags&CPU_CAP_SSE))
348 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&ctl->sse_state));
349 #endif
351 #elif defined(HAVE___CONTROL87_2)
353 int mode;
354 __control87_2(ctl->state, _MCW_RC, &mode, NULL);
355 #ifdef HAVE_SSE
356 if((CPUCapFlags&CPU_CAP_SSE))
357 __control87_2(ctl->sse_state, _MCW_RC|_MCW_DN, NULL, &mode);
358 #endif
360 #elif defined(HAVE__CONTROLFP)
362 _controlfp(ctl->state, _MCW_RC);
363 #endif
367 static int StringSortCompare(const void *str1, const void *str2)
369 return al_string_cmp(*(const_al_string*)str1, *(const_al_string*)str2);
372 #ifdef _WIN32
374 static WCHAR *FromUTF8(const char *str)
376 WCHAR *out = NULL;
377 int len;
379 if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
381 out = calloc(sizeof(WCHAR), len);
382 MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
384 return out;
388 void *LoadLib(const char *name)
390 HANDLE hdl = NULL;
391 WCHAR *wname;
393 wname = FromUTF8(name);
394 if(!wname)
395 ERR("Failed to convert UTF-8 filename: \"%s\"\n", name);
396 else
398 hdl = LoadLibraryW(wname);
399 free(wname);
401 return hdl;
403 void CloseLib(void *handle)
404 { FreeLibrary((HANDLE)handle); }
405 void *GetSymbol(void *handle, const char *name)
407 void *ret;
409 ret = (void*)GetProcAddress((HANDLE)handle, name);
410 if(ret == NULL)
411 ERR("Failed to load %s\n", name);
412 return ret;
415 WCHAR *strdupW(const WCHAR *str)
417 const WCHAR *n;
418 WCHAR *ret;
419 size_t len;
421 n = str;
422 while(*n) n++;
423 len = n - str;
425 ret = calloc(sizeof(WCHAR), len+1);
426 if(ret != NULL)
427 memcpy(ret, str, sizeof(WCHAR)*len);
428 return ret;
431 FILE *al_fopen(const char *fname, const char *mode)
433 WCHAR *wname=NULL, *wmode=NULL;
434 FILE *file = NULL;
436 wname = FromUTF8(fname);
437 wmode = FromUTF8(mode);
438 if(!wname)
439 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
440 else if(!wmode)
441 ERR("Failed to convert UTF-8 mode: \"%s\"\n", mode);
442 else
443 file = _wfopen(wname, wmode);
445 free(wname);
446 free(wmode);
448 return file;
452 void al_print(const char *type, const char *func, const char *fmt, ...)
454 char str[1024];
455 WCHAR *wstr;
456 va_list ap;
458 va_start(ap, fmt);
459 vsnprintf(str, sizeof(str), fmt, ap);
460 va_end(ap);
462 str[sizeof(str)-1] = 0;
463 wstr = FromUTF8(str);
464 if(!wstr)
465 fprintf(LogFile, "AL lib: %s %s: <UTF-8 error> %s", type, func, str);
466 else
468 fprintf(LogFile, "AL lib: %s %s: %ls", type, func, wstr);
469 free(wstr);
470 wstr = NULL;
472 fflush(LogFile);
476 static inline int is_slash(int c)
477 { return (c == '\\' || c == '/'); }
479 FILE *OpenDataFile(const char *fname, const char *subdir)
481 static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
482 WCHAR *wname=NULL, *wsubdir=NULL;
483 FILE *f;
484 size_t i;
486 wname = FromUTF8(fname);
487 if(!wname)
489 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
490 return NULL;
493 /* If the path is absolute, open it directly. */
494 if(wname[0] != '\0' && wname[1] == ':' && is_slash(wname[2]))
496 f = _wfopen(wname, L"rb");
497 if(f) TRACE("Opened %s\n", fname);
498 else WARN("Could not open %s\n", fname);
499 free(wname);
500 return f;
503 /* Try the current directory first before the data directories. */
504 if((f=_wfopen(wname, L"rb")) != NULL)
506 TRACE("Opened %s\n", fname);
507 free(wname);
508 return f;
511 wsubdir = FromUTF8(subdir);
512 if(!wsubdir)
514 ERR("Failed to convert UTF-8 subdir: \"%s\"\n", subdir);
515 free(wname);
516 return NULL;
519 for(i = 0;i < COUNTOF(ids);i++)
521 WCHAR buffer[PATH_MAX];
522 size_t len;
524 if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) == FALSE)
525 continue;
527 len = lstrlenW(buffer);
528 if(len > 0 && is_slash(buffer[len-1]))
529 buffer[--len] = '\0';
530 _snwprintf(buffer+len, PATH_MAX-len, L"/%ls/%ls", wsubdir, wname);
531 len = lstrlenW(buffer);
532 while(len > 0)
534 --len;
535 if(buffer[len] == '/')
536 buffer[len] = '\\';
539 if((f=_wfopen(buffer, L"rb")) != NULL)
541 al_string filepath = AL_STRING_INIT_STATIC();
542 al_string_copy_wcstr(&filepath, buffer);
543 TRACE("Opened %s\n", al_string_get_cstr(filepath));
544 al_string_deinit(&filepath);
545 break;
548 free(wname);
549 free(wsubdir);
551 if(f == NULL)
552 WARN("Could not open %s\\%s\n", subdir, fname);
553 return f;
557 static size_t strlenW(const WCHAR *str)
559 const WCHAR *end = str;
560 while(*end) ++end;
561 return end-str;
564 static const WCHAR *strchrW(const WCHAR *str, WCHAR ch)
566 for(;*str != 0;++str)
568 if(*str == ch)
569 return str;
571 return NULL;
574 static const WCHAR *strrchrW(const WCHAR *str, WCHAR ch)
576 const WCHAR *ret = NULL;
577 for(;*str != 0;++str)
579 if(*str == ch)
580 ret = str;
582 return ret;
585 static const WCHAR *strstrW(const WCHAR *haystack, const WCHAR *needle)
587 size_t len = strlenW(needle);
588 while(*haystack != 0)
590 if(CompareStringW(GetThreadLocale(), NORM_IGNORECASE,
591 haystack, len, needle, len) == CSTR_EQUAL)
592 return haystack;
594 do {
595 ++haystack;
596 } while(((*haystack)&0xC000) == 0x8000);
598 return NULL;
602 /* Compares the filename in the find data with the match string. The match
603 * string may contain the "%r" marker to signifiy a sample rate (really any
604 * positive integer), "%%" to signify a single '%', or "%s" for a (non-greedy)
605 * string.
607 static int MatchFilter(const WCHAR *match, const WIN32_FIND_DATAW *fdata)
609 const WCHAR *name = fdata->cFileName;
610 int ret = 1;
612 do {
613 const WCHAR *p = strchrW(match, '%');
614 if(!p)
615 ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE,
616 match, -1, name, -1) == CSTR_EQUAL;
617 else
619 int len = p-match;
620 ret = lstrlenW(name) >= len;
621 if(ret)
622 ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE,
623 match, len, name, len) == CSTR_EQUAL;
624 if(ret)
626 match += len;
627 name += len;
629 ++p;
630 if(*p == 'r')
632 unsigned long l = 0;
633 while(*name >= '0' && *name <= '9')
635 l = l*10 + (*name-'0');
636 ++name;
638 ret = l > 0;
639 ++p;
641 else if(*p == 's')
643 const WCHAR *next = p+1;
644 if(*next != '\0' && *next != '%')
646 const WCHAR *next_p = strchrW(next, '%');
647 const WCHAR *m;
649 if(!next_p)
650 m = strstrW(name, next);
651 else
653 WCHAR *tmp = malloc((next_p - next + 1) * 2);
654 memcpy(tmp, next, (next_p - next) * 2);
655 tmp[next_p - next] = 0;
657 m = strstrW(name, tmp);
659 free(tmp);
662 ret = !!m;
663 if(ret)
665 size_t l;
666 if(next_p) l = next_p - next;
667 else l = strlenW(next);
669 name = m + l;
670 next += l;
673 p = next;
678 match = p;
679 } while(ret && match && *match);
681 return ret;
684 static void RecurseDirectorySearch(const char *path, const WCHAR *match, vector_al_string *results)
686 WIN32_FIND_DATAW fdata;
687 const WCHAR *sep, *p;
688 HANDLE hdl;
690 if(!match[0])
691 return;
693 /* Find the last directory separator and the next '%' marker in the match
694 * string. */
695 sep = strrchrW(match, '\\');
696 p = strchrW(match, '%');
698 /* If there's no separator, test the files in the specified path against
699 * the match string, and add the results. */
700 if(!sep)
702 al_string pathstr = AL_STRING_INIT_STATIC();
703 WCHAR *wpath;
705 TRACE("Searching %s for %ls\n", path, match);
707 al_string_append_cstr(&pathstr, path);
708 al_string_append_cstr(&pathstr, "\\*.*");
709 wpath = FromUTF8(al_string_get_cstr(pathstr));
711 hdl = FindFirstFileW(wpath, &fdata);
712 if(hdl != INVALID_HANDLE_VALUE)
714 size_t base = VECTOR_SIZE(*results);
715 do {
716 if(MatchFilter(match, &fdata))
718 al_string str = AL_STRING_INIT_STATIC();
719 al_string_copy_cstr(&str, path);
720 al_string_append_char(&str, '\\');
721 al_string_append_wcstr(&str, fdata.cFileName);
722 TRACE("Got result %s\n", al_string_get_cstr(str));
723 VECTOR_PUSH_BACK(*results, str);
725 } while(FindNextFileW(hdl, &fdata));
726 FindClose(hdl);
728 if(VECTOR_SIZE(*results) > base)
729 qsort(VECTOR_ITER_BEGIN(*results)+base, VECTOR_SIZE(*results)-base,
730 sizeof(VECTOR_FRONT(*results)), StringSortCompare);
733 free(wpath);
734 al_string_deinit(&pathstr);
736 return;
739 /* If there's no '%' marker, or it's after the final separator, append the
740 * remaining directories to the path and recurse into it with the remaining
741 * filename portion. */
742 if(!p || p-sep >= 0)
744 al_string npath = AL_STRING_INIT_STATIC();
745 al_string_append_cstr(&npath, path);
746 al_string_append_char(&npath, '\\');
747 al_string_append_wrange(&npath, match, sep);
749 TRACE("Recursing into %s with %ls\n", al_string_get_cstr(npath), sep+1);
750 RecurseDirectorySearch(al_string_get_cstr(npath), sep+1, results);
752 al_string_deinit(&npath);
753 return;
756 /* Look for the last separator before the '%' marker, and the first
757 * separator after it. */
758 sep = strchrW(match, '\\');
759 if(sep-p >= 0) sep = NULL;
760 for(;;)
762 const WCHAR *next = strchrW(sep?sep+1:match, '\\');
763 if(next-p < 0)
765 al_string npath = AL_STRING_INIT_STATIC();
766 WCHAR *nwpath, *nwmatch;
768 /* Append up to the last directory before the one with a '%'. */
769 al_string_copy_cstr(&npath, path);
770 if(sep)
772 al_string_append_char(&npath, '\\');
773 al_string_append_wrange(&npath, match, sep);
775 al_string_append_cstr(&npath, "\\*.*");
776 nwpath = FromUTF8(al_string_get_cstr(npath));
778 /* Take the directory name containing a '%' as a new string to
779 * match against. */
780 if(!sep)
782 nwmatch = calloc(2, next-match+1);
783 memcpy(nwmatch, match, (next-match)*2);
785 else
787 nwmatch = calloc(2, next-(sep+1)+1);
788 memcpy(nwmatch, sep+1, (next-(sep+1))*2);
791 /* For each matching directory name, recurse into it with the
792 * remaining string. */
793 TRACE("Searching %s for %ls\n", al_string_get_cstr(npath), nwmatch);
794 hdl = FindFirstFileW(nwpath, &fdata);
795 if(hdl != INVALID_HANDLE_VALUE)
797 do {
798 if(MatchFilter(nwmatch, &fdata))
800 al_string ndir = AL_STRING_INIT_STATIC();
801 al_string_copy(&ndir, npath);
802 al_string_append_char(&ndir, '\\');
803 al_string_append_wcstr(&ndir, fdata.cFileName);
804 TRACE("Recursing %s with %ls\n", al_string_get_cstr(ndir), next+1);
805 RecurseDirectorySearch(al_string_get_cstr(ndir), next+1, results);
806 al_string_deinit(&ndir);
808 } while(FindNextFileW(hdl, &fdata));
809 FindClose(hdl);
812 free(nwmatch);
813 free(nwpath);
814 al_string_deinit(&npath);
815 break;
817 sep = next;
821 vector_al_string SearchDataFiles(const char *match, const char *subdir)
823 static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
824 static RefCount search_lock;
825 vector_al_string results = VECTOR_INIT_STATIC();
826 WCHAR *wmatch;
827 size_t i;
829 while(ATOMIC_EXCHANGE(uint, &search_lock, 1) == 1)
830 althrd_yield();
832 wmatch = FromUTF8(match);
833 if(!wmatch)
835 ERR("Failed to convert UTF-8 filename: \"%s\"\n", match);
836 return results;
838 for(i = 0;wmatch[i];++i)
840 if(wmatch[i] == '/')
841 wmatch[i] = '\\';
844 /* If the path is absolute, use it directly. */
845 if(isalpha(wmatch[0]) && wmatch[1] == ':' && is_slash(wmatch[2]))
847 char drv[3] = { (char)wmatch[0], ':', 0 };
848 RecurseDirectorySearch(drv, wmatch+3, &results);
850 else if(wmatch[0] == '\\' && wmatch[1] == '\\' && wmatch[2] == '?' && wmatch[3] == '\\')
851 RecurseDirectorySearch("\\\\?", wmatch+4, &results);
852 else
854 al_string path = AL_STRING_INIT_STATIC();
855 WCHAR *cwdbuf;
857 /* Search the app-local directory. */
858 if((cwdbuf=_wgetenv(L"ALSOFT_LOCAL_PATH")) && *cwdbuf != '\0')
860 al_string_copy_wcstr(&path, cwdbuf);
861 if(is_slash(VECTOR_BACK(path)))
863 VECTOR_POP_BACK(path);
864 *VECTOR_ITER_END(path) = 0;
867 else if(!(cwdbuf=_wgetcwd(NULL, 0)))
868 al_string_copy_cstr(&path, ".");
869 else
871 al_string_copy_wcstr(&path, cwdbuf);
872 if(is_slash(VECTOR_BACK(path)))
874 VECTOR_POP_BACK(path);
875 *VECTOR_ITER_END(path) = 0;
877 free(cwdbuf);
879 #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
880 VECTOR_FOR_EACH(char, path, FIX_SLASH);
881 #undef FIX_SLASH
882 RecurseDirectorySearch(al_string_get_cstr(path), wmatch, &results);
884 /* Search the local and global data dirs. */
885 for(i = 0;i < COUNTOF(ids);i++)
887 WCHAR buffer[PATH_MAX];
888 if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) != FALSE)
890 al_string_copy_wcstr(&path, buffer);
891 if(!is_slash(VECTOR_BACK(path)))
892 al_string_append_char(&path, '\\');
893 al_string_append_cstr(&path, subdir);
894 #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
895 VECTOR_FOR_EACH(char, path, FIX_SLASH);
896 #undef FIX_SLASH
898 RecurseDirectorySearch(al_string_get_cstr(path), wmatch, &results);
902 al_string_deinit(&path);
905 free(wmatch);
906 ATOMIC_STORE(&search_lock, 0);
908 return results;
911 #else
913 #ifdef HAVE_DLFCN_H
915 void *LoadLib(const char *name)
917 const char *err;
918 void *handle;
920 dlerror();
921 handle = dlopen(name, RTLD_NOW);
922 if((err=dlerror()) != NULL)
923 handle = NULL;
924 return handle;
926 void CloseLib(void *handle)
927 { dlclose(handle); }
928 void *GetSymbol(void *handle, const char *name)
930 const char *err;
931 void *sym;
933 dlerror();
934 sym = dlsym(handle, name);
935 if((err=dlerror()) != NULL)
937 WARN("Failed to load %s: %s\n", name, err);
938 sym = NULL;
940 return sym;
943 #endif /* HAVE_DLFCN_H */
945 void al_print(const char *type, const char *func, const char *fmt, ...)
947 va_list ap;
949 va_start(ap, fmt);
950 fprintf(LogFile, "AL lib: %s %s: ", type, func);
951 vfprintf(LogFile, fmt, ap);
952 va_end(ap);
954 fflush(LogFile);
958 FILE *OpenDataFile(const char *fname, const char *subdir)
960 char buffer[PATH_MAX] = "";
961 const char *str, *next;
962 FILE *f;
964 if(fname[0] == '/')
966 if((f=al_fopen(fname, "rb")) != NULL)
968 TRACE("Opened %s\n", fname);
969 return f;
971 WARN("Could not open %s\n", fname);
972 return NULL;
975 if((f=al_fopen(fname, "rb")) != NULL)
977 TRACE("Opened %s\n", fname);
978 return f;
981 if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
982 snprintf(buffer, sizeof(buffer), "%s/%s/%s", str, subdir, fname);
983 else if((str=getenv("HOME")) != NULL && str[0] != '\0')
984 snprintf(buffer, sizeof(buffer), "%s/.local/share/%s/%s", str, subdir, fname);
985 if(buffer[0])
987 if((f=al_fopen(buffer, "rb")) != NULL)
989 TRACE("Opened %s\n", buffer);
990 return f;
994 if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
995 str = "/usr/local/share/:/usr/share/";
997 next = str;
998 while((str=next) != NULL && str[0] != '\0')
1000 size_t len;
1001 next = strchr(str, ':');
1003 if(!next)
1004 len = strlen(str);
1005 else
1007 len = next - str;
1008 next++;
1011 if(len > sizeof(buffer)-1)
1012 len = sizeof(buffer)-1;
1013 strncpy(buffer, str, len);
1014 buffer[len] = '\0';
1015 snprintf(buffer+len, sizeof(buffer)-len, "/%s/%s", subdir, fname);
1017 if((f=al_fopen(buffer, "rb")) != NULL)
1019 TRACE("Opened %s\n", buffer);
1020 return f;
1023 WARN("Could not open %s/%s\n", subdir, fname);
1025 return NULL;
1029 static int MatchFilter(const char *name, const char *match)
1031 int ret = 1;
1033 do {
1034 const char *p = strchr(match, '%');
1035 if(!p)
1036 ret = strcmp(match, name) == 0;
1037 else
1039 size_t len = p-match;
1040 ret = strncmp(match, name, len) == 0;
1041 if(ret)
1043 match += len;
1044 name += len;
1046 ++p;
1047 if(*p == 'r')
1049 char *end;
1050 ret = strtoul(name, &end, 10) > 0;
1051 if(ret) name = end;
1052 ++p;
1054 else if(*p == 's')
1056 const char *next = p+1;
1057 if(*next != '\0' && *next != '%')
1059 const char *next_p = strchr(next, '%');
1060 const char *m;
1062 if(!next_p)
1063 m = strstr(name, next);
1064 else
1066 char *tmp = malloc(next_p - next + 1);
1067 memcpy(tmp, next, next_p - next);
1068 tmp[next_p - next] = 0;
1070 m = strstr(name, tmp);
1072 free(tmp);
1075 ret = !!m;
1076 if(ret)
1078 size_t l;
1079 if(next_p) l = next_p - next;
1080 else l = strlen(next);
1082 name = m + l;
1083 next += l;
1086 p = next;
1091 match = p;
1092 } while(ret && match && *match);
1094 return ret;
1097 static void RecurseDirectorySearch(const char *path, const char *match, vector_al_string *results)
1099 char *sep, *p;
1101 if(!match[0])
1102 return;
1104 sep = strrchr(match, '/');
1105 p = strchr(match, '%');
1107 if(!sep)
1109 DIR *dir;
1111 TRACE("Searching %s for %s\n", path?path:"/", match);
1112 dir = opendir(path?path:"/");
1113 if(dir != NULL)
1115 size_t base = VECTOR_SIZE(*results);
1116 struct dirent *dirent;
1117 while((dirent=readdir(dir)) != NULL)
1119 al_string str;
1120 if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0 ||
1121 !MatchFilter(dirent->d_name, match))
1122 continue;
1124 AL_STRING_INIT(str);
1125 if(path) al_string_copy_cstr(&str, path);
1126 al_string_append_char(&str, '/');
1127 al_string_append_cstr(&str, dirent->d_name);
1128 TRACE("Got result %s\n", al_string_get_cstr(str));
1129 VECTOR_PUSH_BACK(*results, str);
1131 closedir(dir);
1133 if(VECTOR_SIZE(*results) > base)
1134 qsort(VECTOR_ITER_BEGIN(*results)+base, VECTOR_SIZE(*results)-base,
1135 sizeof(VECTOR_FRONT(*results)), StringSortCompare);
1138 return;
1141 if(!p || p-sep >= 0)
1143 al_string npath = AL_STRING_INIT_STATIC();
1144 if(path) al_string_append_cstr(&npath, path);
1145 al_string_append_char(&npath, '/');
1146 al_string_append_range(&npath, match, sep);
1148 TRACE("Recursing into %s with %s\n", al_string_get_cstr(npath), sep+1);
1149 RecurseDirectorySearch(al_string_get_cstr(npath), sep+1, results);
1151 al_string_deinit(&npath);
1152 return;
1155 sep = strchr(match, '/');
1156 if(sep-p >= 0) sep = NULL;
1157 for(;;)
1159 char *next = strchr(sep?sep+1:match, '/');
1160 if(next-p < 0)
1162 al_string npath = AL_STRING_INIT_STATIC();
1163 al_string nmatch = AL_STRING_INIT_STATIC();
1164 const char *tomatch;
1165 DIR *dir;
1167 if(!sep)
1169 al_string_append_cstr(&npath, path?path:"/.");
1170 tomatch = match;
1172 else
1174 if(path) al_string_append_cstr(&npath, path);
1175 al_string_append_char(&npath, '/');
1176 al_string_append_range(&npath, match, sep);
1178 al_string_append_range(&nmatch, sep+1, next);
1179 tomatch = al_string_get_cstr(nmatch);
1182 TRACE("Searching %s for %s\n", al_string_get_cstr(npath), tomatch);
1183 dir = opendir(path?path:"/");
1184 if(dir != NULL)
1186 al_string ndir = AL_STRING_INIT_STATIC();
1187 struct dirent *dirent;
1189 while((dirent=readdir(dir)) != NULL)
1191 if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0 ||
1192 !MatchFilter(dirent->d_name, tomatch))
1193 continue;
1194 al_string_copy(&ndir, npath);
1195 al_string_append_char(&ndir, '/');
1196 al_string_append_cstr(&ndir, dirent->d_name);
1197 TRACE("Recursing %s with %s\n", al_string_get_cstr(ndir), next+1);
1198 RecurseDirectorySearch(al_string_get_cstr(ndir), next+1, results);
1200 closedir(dir);
1202 al_string_deinit(&ndir);
1205 al_string_deinit(&nmatch);
1206 al_string_deinit(&npath);
1207 break;
1210 sep = next;
1214 vector_al_string SearchDataFiles(const char *match, const char *subdir)
1216 static RefCount search_lock;
1217 vector_al_string results = VECTOR_INIT_STATIC();
1219 while(ATOMIC_EXCHANGE(uint, &search_lock, 1) == 1)
1220 althrd_yield();
1222 if(match[0] == '/')
1223 RecurseDirectorySearch(NULL, match+1, &results);
1224 else
1226 al_string path = AL_STRING_INIT_STATIC();
1227 const char *str, *next;
1228 char cwdbuf[PATH_MAX];
1230 /* Search the app-local directory. */
1231 if((str=getenv("ALSOFT_LOCAL_PATH")) && *str != '\0')
1233 strncpy(cwdbuf, str, sizeof(cwdbuf)-1);
1234 cwdbuf[sizeof(cwdbuf)-1] = '\0';
1236 else if(!getcwd(cwdbuf, sizeof(cwdbuf)))
1237 strcpy(cwdbuf, ".");
1238 RecurseDirectorySearch(cwdbuf, match, &results);
1240 // Search local data dir
1241 if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
1243 al_string_append_cstr(&path, str);
1244 al_string_append_char(&path, '/');
1245 al_string_append_cstr(&path, subdir);
1247 else if((str=getenv("HOME")) != NULL && str[0] != '\0')
1249 al_string_append_cstr(&path, str);
1250 al_string_append_cstr(&path, "/.local/share/");
1251 al_string_append_cstr(&path, subdir);
1253 if(!al_string_empty(path))
1254 RecurseDirectorySearch(al_string_get_cstr(path), match, &results);
1256 // Search global data dirs
1257 if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
1258 str = "/usr/local/share/:/usr/share/";
1260 next = str;
1261 while((str=next) != NULL && str[0] != '\0')
1263 next = strchr(str, ':');
1264 if(!next)
1265 al_string_copy_cstr(&path, str);
1266 else
1268 al_string_clear(&path);
1269 al_string_append_range(&path, str, next);
1270 ++next;
1272 if(!al_string_empty(path))
1274 al_string_append_char(&path, '/');
1275 al_string_append_cstr(&path, subdir);
1277 RecurseDirectorySearch(al_string_get_cstr(path), match, &results);
1281 al_string_deinit(&path);
1284 ATOMIC_STORE(&search_lock, 0);
1286 return results;
1289 #endif
1292 void SetRTPriority(void)
1294 ALboolean failed = AL_FALSE;
1296 #ifdef _WIN32
1297 if(RTPrioLevel > 0)
1298 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
1299 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
1300 if(RTPrioLevel > 0)
1302 struct sched_param param;
1303 /* Use the minimum real-time priority possible for now (on Linux this
1304 * should be 1 for SCHED_RR) */
1305 param.sched_priority = sched_get_priority_min(SCHED_RR);
1306 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
1308 #else
1309 /* Real-time priority not available */
1310 failed = (RTPrioLevel>0);
1311 #endif
1312 if(failed)
1313 ERR("Failed to set priority level for thread\n");
1317 ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_size, size_t obj_count, ALboolean exact)
1319 vector_ *vecptr = (vector_*)ptr;
1320 if((*vecptr ? (*vecptr)->Capacity : 0) < obj_count)
1322 size_t old_size = (*vecptr ? (*vecptr)->Size : 0);
1323 void *temp;
1325 /* Use the next power-of-2 size if we don't need to allocate the exact
1326 * amount. This is preferred when regularly increasing the vector since
1327 * it means fewer reallocations. Though it means it also wastes some
1328 * memory. */
1329 if(exact == AL_FALSE && obj_count < INT_MAX)
1330 obj_count = NextPowerOf2((ALuint)obj_count);
1332 /* Need to be explicit with the caller type's base size, because it
1333 * could have extra padding before the start of the array (that is,
1334 * sizeof(*vector_) may not equal base_size). */
1335 temp = realloc(*vecptr, base_size + obj_size*obj_count);
1336 if(temp == NULL) return AL_FALSE;
1338 *vecptr = temp;
1339 (*vecptr)->Capacity = obj_count;
1340 (*vecptr)->Size = old_size;
1342 return AL_TRUE;
1345 ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_size, size_t obj_count)
1347 vector_ *vecptr = (vector_*)ptr;
1348 if(*vecptr || obj_count > 0)
1350 if(!vector_reserve((char*)vecptr, base_size, obj_size, obj_count, AL_TRUE))
1351 return AL_FALSE;
1352 (*vecptr)->Size = obj_count;
1354 return AL_TRUE;
1357 ALboolean vector_insert(char *ptr, size_t base_size, size_t obj_size, void *ins_pos, const void *datstart, const void *datend)
1359 vector_ *vecptr = (vector_*)ptr;
1360 if(datstart != datend)
1362 ptrdiff_t ins_elem = (*vecptr ? ((char*)ins_pos - ((char*)(*vecptr) + base_size)) :
1363 ((char*)ins_pos - (char*)NULL)) /
1364 obj_size;
1365 ptrdiff_t numins = ((const char*)datend - (const char*)datstart) / obj_size;
1367 assert(numins > 0);
1368 if((size_t)numins + VECTOR_SIZE(*vecptr) < (size_t)numins ||
1369 !vector_reserve((char*)vecptr, base_size, obj_size, VECTOR_SIZE(*vecptr)+numins, AL_TRUE))
1370 return AL_FALSE;
1372 /* NOTE: ins_pos may have been invalidated if *vecptr moved. Use ins_elem instead. */
1373 if((size_t)ins_elem < (*vecptr)->Size)
1375 memmove((char*)(*vecptr) + base_size + ((ins_elem+numins)*obj_size),
1376 (char*)(*vecptr) + base_size + ((ins_elem )*obj_size),
1377 ((*vecptr)->Size-ins_elem)*obj_size);
1379 memcpy((char*)(*vecptr) + base_size + (ins_elem*obj_size),
1380 datstart, numins*obj_size);
1381 (*vecptr)->Size += numins;
1383 return AL_TRUE;
1387 extern inline void al_string_deinit(al_string *str);
1388 extern inline size_t al_string_length(const_al_string str);
1389 extern inline ALboolean al_string_empty(const_al_string str);
1390 extern inline const al_string_char_type *al_string_get_cstr(const_al_string str);
1392 void al_string_clear(al_string *str)
1394 /* Reserve one more character than the total size of the string. This is to
1395 * ensure we have space to add a null terminator in the string data so it
1396 * can be used as a C-style string. */
1397 VECTOR_RESERVE(*str, 1);
1398 VECTOR_RESIZE(*str, 0);
1399 *VECTOR_ITER_END(*str) = 0;
1402 static inline int al_string_compare(const al_string_char_type *str1, size_t str1len,
1403 const al_string_char_type *str2, size_t str2len)
1405 size_t complen = (str1len < str2len) ? str1len : str2len;
1406 int ret = memcmp(str1, str2, complen);
1407 if(ret == 0)
1409 if(str1len > str2len) return 1;
1410 if(str1len < str2len) return -1;
1412 return ret;
1414 int al_string_cmp(const_al_string str1, const_al_string str2)
1416 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
1417 &VECTOR_FRONT(str2), al_string_length(str2));
1419 int al_string_cmp_cstr(const_al_string str1, const al_string_char_type *str2)
1421 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
1422 str2, strlen(str2));
1425 void al_string_copy(al_string *str, const_al_string from)
1427 size_t len = al_string_length(from);
1428 VECTOR_RESERVE(*str, len+1);
1429 VECTOR_RESIZE(*str, 0);
1430 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), VECTOR_ITER_BEGIN(from), VECTOR_ITER_BEGIN(from)+len);
1431 *VECTOR_ITER_END(*str) = 0;
1434 void al_string_copy_cstr(al_string *str, const al_string_char_type *from)
1436 size_t len = strlen(from);
1437 VECTOR_RESERVE(*str, len+1);
1438 VECTOR_RESIZE(*str, 0);
1439 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
1440 *VECTOR_ITER_END(*str) = 0;
1443 void al_string_append_char(al_string *str, const al_string_char_type c)
1445 VECTOR_RESERVE(*str, al_string_length(*str)+2);
1446 VECTOR_PUSH_BACK(*str, c);
1447 *VECTOR_ITER_END(*str) = 0;
1450 void al_string_append_cstr(al_string *str, const al_string_char_type *from)
1452 size_t len = strlen(from);
1453 if(len != 0)
1455 VECTOR_RESERVE(*str, al_string_length(*str)+len+1);
1456 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
1457 *VECTOR_ITER_END(*str) = 0;
1461 void al_string_append_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
1463 if(to != from)
1465 VECTOR_RESERVE(*str, al_string_length(*str)+(to-from)+1);
1466 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, to);
1467 *VECTOR_ITER_END(*str) = 0;
1471 #ifdef _WIN32
1472 void al_string_copy_wcstr(al_string *str, const wchar_t *from)
1474 int len;
1475 if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
1477 VECTOR_RESERVE(*str, len);
1478 VECTOR_RESIZE(*str, len-1);
1479 WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, NULL, NULL);
1480 *VECTOR_ITER_END(*str) = 0;
1484 void al_string_append_wcstr(al_string *str, const wchar_t *from)
1486 int len;
1487 if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
1489 size_t strlen = al_string_length(*str);
1490 VECTOR_RESERVE(*str, strlen+len);
1491 VECTOR_RESIZE(*str, strlen+len-1);
1492 WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str) + strlen, len, NULL, NULL);
1493 *VECTOR_ITER_END(*str) = 0;
1497 void al_string_append_wrange(al_string *str, const wchar_t *from, const wchar_t *to)
1499 int len;
1500 if((len=WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), NULL, 0, NULL, NULL)) > 0)
1502 size_t strlen = al_string_length(*str);
1503 VECTOR_RESERVE(*str, strlen+len+1);
1504 VECTOR_RESIZE(*str, strlen+len);
1505 WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), &VECTOR_FRONT(*str) + strlen, len+1, NULL, NULL);
1506 *VECTOR_ITER_END(*str) = 0;
1509 #endif