Don't try to use the non-standard alloca.h
[openal-soft.git] / Alc / helpers.c
blob4b5eceb729eb79ac6e14e7909edadb82828bf81b
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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
39 #ifndef AL_NO_UID_DEFS
40 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
41 #define INITGUID
42 #include <windows.h>
43 #ifdef HAVE_GUIDDEF_H
44 #include <guiddef.h>
45 #else
46 #include <initguid.h>
47 #endif
49 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
50 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
52 DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
54 DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
55 DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
56 DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
57 DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
59 #ifdef HAVE_MMDEVAPI
60 #include <devpropdef.h>
61 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
62 #endif
63 #endif
64 #endif /* AL_NO_UID_DEFS */
66 #ifdef HAVE_DLFCN_H
67 #include <dlfcn.h>
68 #endif
69 #ifdef HAVE_CPUID_H
70 #include <cpuid.h>
71 #endif
72 #ifdef HAVE_SYS_SYSCONF_H
73 #include <sys/sysconf.h>
74 #endif
75 #ifdef HAVE_FLOAT_H
76 #include <float.h>
77 #endif
78 #ifdef HAVE_IEEEFP_H
79 #include <ieeefp.h>
80 #endif
82 #ifdef _WIN32_IE
83 #include <shlobj.h>
84 #endif
86 #include "alMain.h"
87 #include "alu.h"
88 #include "atomic.h"
89 #include "uintmap.h"
90 #include "vector.h"
91 #include "alstring.h"
92 #include "compat.h"
93 #include "threads.h"
96 extern inline ALuint NextPowerOf2(ALuint value);
97 extern inline ALint fastf2i(ALfloat f);
98 extern inline ALuint fastf2u(ALfloat f);
101 ALuint CPUCapFlags = 0;
104 void FillCPUCaps(ALuint capfilter)
106 ALuint caps = 0;
108 /* FIXME: We really should get this for all available CPUs in case different
109 * CPUs have different caps (is that possible on one machine?). */
110 #if defined(HAVE_CPUID_H) && (defined(__i386__) || defined(__x86_64__) || \
111 defined(_M_IX86) || defined(_M_X64))
112 union {
113 unsigned int regs[4];
114 char str[sizeof(unsigned int[4])];
115 } cpuinf[3];
117 if(!__get_cpuid(0, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
118 ERR("Failed to get CPUID\n");
119 else
121 unsigned int maxfunc = cpuinf[0].regs[0];
122 unsigned int maxextfunc = 0;
124 if(__get_cpuid(0x80000000, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
125 maxextfunc = cpuinf[0].regs[0];
126 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
128 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
129 if(maxextfunc >= 0x80000004 &&
130 __get_cpuid(0x80000002, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]) &&
131 __get_cpuid(0x80000003, &cpuinf[1].regs[0], &cpuinf[1].regs[1], &cpuinf[1].regs[2], &cpuinf[1].regs[3]) &&
132 __get_cpuid(0x80000004, &cpuinf[2].regs[0], &cpuinf[2].regs[1], &cpuinf[2].regs[2], &cpuinf[2].regs[3]))
133 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
135 if(maxfunc >= 1 &&
136 __get_cpuid(1, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
138 if((cpuinf[0].regs[3]&(1<<25)))
140 caps |= CPU_CAP_SSE;
141 if((cpuinf[0].regs[3]&(1<<26)))
142 caps |= CPU_CAP_SSE2;
146 #elif defined(HAVE_WINDOWS_H)
147 HMODULE k32 = GetModuleHandleA("kernel32.dll");
148 BOOL (WINAPI*IsProcessorFeaturePresent)(DWORD ProcessorFeature);
149 IsProcessorFeaturePresent = (BOOL(WINAPI*)(DWORD))GetProcAddress(k32, "IsProcessorFeaturePresent");
150 if(!IsProcessorFeaturePresent)
151 ERR("IsProcessorFeaturePresent not available; CPU caps not detected\n");
152 else
154 if(IsProcessorFeaturePresent(PF_XMMI_INSTRUCTIONS_AVAILABLE))
156 caps |= CPU_CAP_SSE;
157 if(IsProcessorFeaturePresent(PF_XMMI64_INSTRUCTIONS_AVAILABLE))
158 caps |= CPU_CAP_SSE2;
161 #endif
162 #ifdef HAVE_NEON
163 /* Assume Neon support if compiled with it */
164 caps |= CPU_CAP_NEON;
165 #endif
167 TRACE("Got caps:%s%s%s%s\n", ((caps&CPU_CAP_SSE)?((capfilter&CPU_CAP_SSE)?" SSE":" (SSE)"):""),
168 ((caps&CPU_CAP_SSE2)?((capfilter&CPU_CAP_SSE2)?" SSE2":" (SSE2)"):""),
169 ((caps&CPU_CAP_NEON)?((capfilter&CPU_CAP_NEON)?" Neon":" (Neon)"):""),
170 ((!caps)?" -none-":""));
171 CPUCapFlags = caps & capfilter;
175 void *al_malloc(size_t alignment, size_t size)
177 #if defined(HAVE_ALIGNED_ALLOC)
178 size = (size+(alignment-1))&~(alignment-1);
179 return aligned_alloc(alignment, size);
180 #elif defined(HAVE_POSIX_MEMALIGN)
181 void *ret;
182 if(posix_memalign(&ret, alignment, size) == 0)
183 return ret;
184 return NULL;
185 #elif defined(HAVE__ALIGNED_MALLOC)
186 return _aligned_malloc(size, alignment);
187 #else
188 char *ret = malloc(size+alignment);
189 if(ret != NULL)
191 *(ret++) = 0x00;
192 while(((ALintptrEXT)ret&(alignment-1)) != 0)
193 *(ret++) = 0x55;
195 return ret;
196 #endif
199 void *al_calloc(size_t alignment, size_t size)
201 void *ret = al_malloc(alignment, size);
202 if(ret) memset(ret, 0, size);
203 return ret;
206 void al_free(void *ptr)
208 #if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
209 free(ptr);
210 #elif defined(HAVE__ALIGNED_MALLOC)
211 _aligned_free(ptr);
212 #else
213 if(ptr != NULL)
215 char *finder = ptr;
216 do {
217 --finder;
218 } while(*finder == 0x55);
219 free(finder);
221 #endif
225 #if (defined(HAVE___CONTROL87_2) || defined(HAVE__CONTROLFP)) && (defined(__x86_64__) || defined(_M_X64))
226 /* Win64 doesn't allow us to set the precision control. */
227 #undef _MCW_PC
228 #define _MCW_PC 0
229 #endif
231 void SetMixerFPUMode(FPUCtl *ctl)
233 #ifdef HAVE_FENV_H
234 fegetenv(STATIC_CAST(fenv_t, ctl));
235 #if defined(__GNUC__) && defined(HAVE_SSE)
236 if((CPUCapFlags&CPU_CAP_SSE))
237 __asm__ __volatile__("stmxcsr %0" : "=m" (*&ctl->sse_state));
238 #endif
240 #ifdef FE_TOWARDZERO
241 fesetround(FE_TOWARDZERO);
242 #endif
243 #if defined(__GNUC__) && defined(HAVE_SSE)
244 if((CPUCapFlags&CPU_CAP_SSE))
246 int sseState = ctl->sse_state;
247 sseState |= 0x6000; /* set round-to-zero */
248 sseState |= 0x8000; /* set flush-to-zero */
249 if((CPUCapFlags&CPU_CAP_SSE2))
250 sseState |= 0x0040; /* set denormals-are-zero */
251 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
253 #endif
255 #elif defined(HAVE___CONTROL87_2)
257 int mode;
258 __control87_2(0, 0, &ctl->state, NULL);
259 __control87_2(_RC_CHOP|_PC_24, _MCW_RC|_MCW_PC, &mode, NULL);
260 #ifdef HAVE_SSE
261 if((CPUCapFlags&CPU_CAP_SSE))
263 __control87_2(0, 0, NULL, &ctl->sse_state);
264 __control87_2(_RC_CHOP|_DN_FLUSH, _MCW_RC|_MCW_DN, NULL, &mode);
266 #endif
268 #elif defined(HAVE__CONTROLFP)
270 ctl->state = _controlfp(0, 0);
271 (void)_controlfp(_RC_CHOP|_PC_24, _MCW_RC|_MCW_PC);
272 #endif
275 void RestoreFPUMode(const FPUCtl *ctl)
277 #ifdef HAVE_FENV_H
278 fesetenv(STATIC_CAST(fenv_t, ctl));
279 #if defined(__GNUC__) && defined(HAVE_SSE)
280 if((CPUCapFlags&CPU_CAP_SSE))
281 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&ctl->sse_state));
282 #endif
284 #elif defined(HAVE___CONTROL87_2)
286 int mode;
287 __control87_2(ctl->state, _MCW_RC|_MCW_PC, &mode, NULL);
288 #ifdef HAVE_SSE
289 if((CPUCapFlags&CPU_CAP_SSE))
290 __control87_2(ctl->sse_state, _MCW_RC|_MCW_DN, NULL, &mode);
291 #endif
293 #elif defined(HAVE__CONTROLFP)
295 _controlfp(ctl->state, _MCW_RC|_MCW_PC);
296 #endif
300 #ifdef _WIN32
302 static WCHAR *FromUTF8(const char *str)
304 WCHAR *out = NULL;
305 int len;
307 if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
309 out = calloc(sizeof(WCHAR), len);
310 MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
312 return out;
316 void *LoadLib(const char *name)
318 HANDLE hdl = NULL;
319 WCHAR *wname;
321 wname = FromUTF8(name);
322 if(!wname)
323 ERR("Failed to convert UTF-8 filename: \"%s\"\n", name);
324 else
326 hdl = LoadLibraryW(wname);
327 free(wname);
329 return hdl;
331 void CloseLib(void *handle)
332 { FreeLibrary((HANDLE)handle); }
333 void *GetSymbol(void *handle, const char *name)
335 void *ret;
337 ret = (void*)GetProcAddress((HANDLE)handle, name);
338 if(ret == NULL)
339 ERR("Failed to load %s\n", name);
340 return ret;
343 WCHAR *strdupW(const WCHAR *str)
345 const WCHAR *n;
346 WCHAR *ret;
347 size_t len;
349 n = str;
350 while(*n) n++;
351 len = n - str;
353 ret = calloc(sizeof(WCHAR), len+1);
354 if(ret != NULL)
355 memcpy(ret, str, sizeof(WCHAR)*len);
356 return ret;
359 FILE *al_fopen(const char *fname, const char *mode)
361 WCHAR *wname=NULL, *wmode=NULL;
362 FILE *file = NULL;
364 wname = FromUTF8(fname);
365 wmode = FromUTF8(mode);
366 if(!wname)
367 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
368 else if(!wmode)
369 ERR("Failed to convert UTF-8 mode: \"%s\"\n", mode);
370 else
371 file = _wfopen(wname, wmode);
373 free(wname);
374 free(wmode);
376 return file;
379 #else
381 #ifdef HAVE_DLFCN_H
383 void *LoadLib(const char *name)
385 const char *err;
386 void *handle;
388 dlerror();
389 handle = dlopen(name, RTLD_NOW);
390 if((err=dlerror()) != NULL)
391 handle = NULL;
392 return handle;
394 void CloseLib(void *handle)
395 { dlclose(handle); }
396 void *GetSymbol(void *handle, const char *name)
398 const char *err;
399 void *sym;
401 dlerror();
402 sym = dlsym(handle, name);
403 if((err=dlerror()) != NULL)
405 WARN("Failed to load %s: %s\n", name, err);
406 sym = NULL;
408 return sym;
411 #endif
412 #endif
415 void al_print(const char *type, const char *func, const char *fmt, ...)
417 va_list ap;
419 va_start(ap, fmt);
420 fprintf(LogFile, "AL lib: %s %s: ", type, func);
421 vfprintf(LogFile, fmt, ap);
422 va_end(ap);
424 fflush(LogFile);
427 #ifdef _WIN32
428 static inline int is_slash(int c)
429 { return (c == '\\' || c == '/'); }
431 FILE *OpenDataFile(const char *fname, const char *subdir)
433 static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
434 WCHAR *wname=NULL, *wsubdir=NULL;
435 int i;
437 /* If the path is absolute, open it directly. */
438 if(fname[0] != '\0' && fname[1] == ':' && is_slash(fname[2]))
440 FILE *f;
441 if((f=al_fopen(fname, "rb")) != NULL)
443 TRACE("Opened %s\n", fname);
444 return f;
446 WARN("Could not open %s\n", fname);
447 return NULL;
450 wname = FromUTF8(fname);
451 wsubdir = FromUTF8(subdir);
452 if(!wname)
453 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
454 else if(!wsubdir)
455 ERR("Failed to convert UTF-8 subdir: \"%s\"\n", subdir);
456 else for(i = 0;i < 2;i++)
458 WCHAR buffer[PATH_MAX];
459 size_t len;
460 FILE *f;
462 if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) == FALSE)
463 continue;
465 len = lstrlenW(buffer);
466 if(len > 0 && is_slash(buffer[len-1]))
467 buffer[--len] = '\0';
468 _snwprintf(buffer+len, PATH_MAX-len, L"/%ls/%ls", wsubdir, wname);
469 len = lstrlenW(buffer);
470 while(len > 0)
472 --len;
473 if(buffer[len] == '/')
474 buffer[len] = '\\';
477 if((f=_wfopen(buffer, L"rb")) != NULL)
479 TRACE("Opened %ls\n", buffer);
480 return f;
482 WARN("Could not open %ls\n", buffer);
484 free(wname);
485 free(wsubdir);
487 return NULL;
489 #else
490 FILE *OpenDataFile(const char *fname, const char *subdir)
492 char buffer[PATH_MAX] = "";
493 const char *str, *next;
494 FILE *f;
496 if(fname[0] == '/')
498 if((f=al_fopen(fname, "rb")) != NULL)
500 TRACE("Opened %s\n", fname);
501 return f;
503 WARN("Could not open %s\n", fname);
504 return NULL;
507 if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
508 snprintf(buffer, sizeof(buffer), "%s/%s/%s", str, subdir, fname);
509 else if((str=getenv("HOME")) != NULL && str[0] != '\0')
510 snprintf(buffer, sizeof(buffer), "%s/.local/share/%s/%s", str, subdir, fname);
511 if(buffer[0])
513 if((f=al_fopen(buffer, "rb")) != NULL)
515 TRACE("Opened %s\n", buffer);
516 return f;
518 WARN("Could not open %s\n", buffer);
521 if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
522 str = "/usr/local/share/:/usr/share/";
524 next = str;
525 while((str=next) != NULL && str[0] != '\0')
527 size_t len;
528 next = strchr(str, ':');
530 if(!next)
531 len = strlen(str);
532 else
534 len = next - str;
535 next++;
538 if(len > sizeof(buffer)-1)
539 len = sizeof(buffer)-1;
540 strncpy(buffer, str, len);
541 buffer[len] = '\0';
542 snprintf(buffer+len, sizeof(buffer)-len, "/%s/%s", subdir, fname);
544 if((f=al_fopen(buffer, "rb")) != NULL)
546 TRACE("Opened %s\n", buffer);
547 return f;
549 WARN("Could not open %s\n", buffer);
552 return NULL;
554 #endif
557 void SetRTPriority(void)
559 ALboolean failed = AL_FALSE;
561 #ifdef _WIN32
562 if(RTPrioLevel > 0)
563 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
564 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
565 if(RTPrioLevel > 0)
567 struct sched_param param;
568 /* Use the minimum real-time priority possible for now (on Linux this
569 * should be 1 for SCHED_RR) */
570 param.sched_priority = sched_get_priority_min(SCHED_RR);
571 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
573 #else
574 /* Real-time priority not available */
575 failed = (RTPrioLevel>0);
576 #endif
577 if(failed)
578 ERR("Failed to set priority level for thread\n");
582 ALboolean vector_reserve(void *ptr, size_t base_size, size_t obj_count, size_t obj_size, ALboolean exact)
584 vector_ *vecptr = ptr;
585 if((size_t)(*vecptr ? (*vecptr)->Capacity : 0) < obj_count)
587 ALsizei old_size = (*vecptr ? (*vecptr)->Size : 0);
588 void *temp;
590 /* Limit vector sizes to the greatest power-of-two value that an
591 * ALsizei can hold. */
592 if(obj_count > (INT_MAX>>1)+1)
593 return AL_FALSE;
595 /* Use the next power-of-2 size if we don't need to allocate the exact
596 * amount. This is preferred when regularly increasing the vector since
597 * it means fewer reallocations. Though it means it also wastes some
598 * memory. */
599 if(exact == AL_FALSE)
600 obj_count = NextPowerOf2((ALuint)obj_count);
602 /* Need to be explicit with the caller type's base size, because it
603 * could have extra padding before the start of the array (that is,
604 * sizeof(*vector_) may not equal base_size). */
605 temp = realloc(*vecptr, base_size + obj_size*obj_count);
606 if(temp == NULL) return AL_FALSE;
608 *vecptr = temp;
609 (*vecptr)->Capacity = (ALsizei)obj_count;
610 (*vecptr)->Size = old_size;
612 return AL_TRUE;
615 ALboolean vector_resize(void *ptr, size_t base_size, size_t obj_count, size_t obj_size)
617 vector_ *vecptr = ptr;
618 if(*vecptr || obj_count > 0)
620 if(!vector_reserve(vecptr, base_size, obj_count, obj_size, AL_TRUE))
621 return AL_FALSE;
622 (*vecptr)->Size = (ALsizei)obj_count;
624 return AL_TRUE;
627 ALboolean vector_insert(void *ptr, size_t base_size, size_t obj_size, void *ins_pos, const void *datstart, const void *datend)
629 vector_ *vecptr = ptr;
630 if(datstart != datend)
632 ptrdiff_t ins_elem = ((char*)ins_pos - ((char*)(*vecptr) + base_size)) / obj_size;
633 ptrdiff_t numins = ((const char*)datend - (const char*)datstart) / obj_size;
634 if(!vector_reserve(vecptr, base_size, VECTOR_SIZE(*vecptr)+numins, obj_size, AL_TRUE))
635 return AL_FALSE;
636 /* NOTE: ins_pos may have been invalidated if *vecptr moved. Use ins_elem instead. */
637 if(ins_elem < (*vecptr)->Size)
639 memmove((char*)(*vecptr) + base_size + ((ins_elem+numins)*obj_size),
640 (char*)(*vecptr) + base_size + ((ins_elem )*obj_size),
641 ((*vecptr)->Size-ins_elem)*obj_size);
643 memcpy((char*)(*vecptr) + base_size + (ins_elem*obj_size),
644 datstart, numins*obj_size);
645 (*vecptr)->Size += (ALsizei)numins;
647 return AL_TRUE;
651 extern inline ALsizei al_string_length(const_al_string str);
652 extern inline ALboolean al_string_empty(const_al_string str);
653 extern inline const al_string_char_type *al_string_get_cstr(const_al_string str);
655 void al_string_clear(al_string *str)
657 /* Reserve one more character than the total size of the string. This is to
658 * ensure we have space to add a null terminator in the string data so it
659 * can be used as a C-style string. */
660 VECTOR_RESERVE(*str, 1);
661 VECTOR_RESIZE(*str, 0);
662 *VECTOR_ITER_END(*str) = 0;
665 static inline int al_string_compare(const al_string_char_type *str1, ALsizei str1len,
666 const al_string_char_type *str2, ALsizei str2len)
668 ALsizei complen = mini(str1len, str2len);
669 int ret = memcmp(str1, str2, complen);
670 if(ret == 0)
672 if(str1len > str2len) return 1;
673 if(str1len < str2len) return -1;
675 return ret;
677 int al_string_cmp(const_al_string str1, const_al_string str2)
679 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
680 &VECTOR_FRONT(str2), al_string_length(str2));
682 int al_string_cmp_cstr(const_al_string str1, const al_string_char_type *str2)
684 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
685 str2, (ALsizei)strlen(str2));
688 void al_string_copy(al_string *str, const_al_string from)
690 ALsizei len = VECTOR_SIZE(from);
691 VECTOR_RESERVE(*str, len+1);
692 VECTOR_RESIZE(*str, 0);
693 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), VECTOR_ITER_BEGIN(from), VECTOR_ITER_BEGIN(from)+len);
694 *VECTOR_ITER_END(*str) = 0;
697 void al_string_copy_cstr(al_string *str, const al_string_char_type *from)
699 size_t len = strlen(from);
700 VECTOR_RESERVE(*str, len+1);
701 VECTOR_RESIZE(*str, 0);
702 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
703 *VECTOR_ITER_END(*str) = 0;
706 void al_string_append_char(al_string *str, const al_string_char_type c)
708 VECTOR_RESERVE(*str, al_string_length(*str)+2);
709 VECTOR_PUSH_BACK(*str, c);
710 *VECTOR_ITER_END(*str) = 0;
713 void al_string_append_cstr(al_string *str, const al_string_char_type *from)
715 size_t len = strlen(from);
716 if(len != 0)
718 VECTOR_RESERVE(*str, al_string_length(*str)+len+1);
719 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
720 *VECTOR_ITER_END(*str) = 0;
724 void al_string_append_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
726 if(to != from)
728 VECTOR_RESERVE(*str, al_string_length(*str)+(to-from)+1);
729 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, to);
730 *VECTOR_ITER_END(*str) = 0;
734 #ifdef _WIN32
735 void al_string_copy_wcstr(al_string *str, const wchar_t *from)
737 int len;
738 if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
740 VECTOR_RESERVE(*str, len);
741 VECTOR_RESIZE(*str, len-1);
742 WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, NULL, NULL);
743 *VECTOR_ITER_END(*str) = 0;
746 #endif