Print CPU extensions that are unavailable but requested
[openal-soft.git] / Alc / helpers.c
blobe1220fd4b71d1fba261741f020ae3260133783cb
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)))
143 caps |= CPU_CAP_SSE2;
144 if((cpuinf[0].regs[2]&(1<<19)))
145 caps |= CPU_CAP_SSE4_1;
150 #elif defined(HAVE_WINDOWS_H)
151 HMODULE k32 = GetModuleHandleA("kernel32.dll");
152 BOOL (WINAPI*IsProcessorFeaturePresent)(DWORD ProcessorFeature);
153 IsProcessorFeaturePresent = (BOOL(WINAPI*)(DWORD))GetProcAddress(k32, "IsProcessorFeaturePresent");
154 if(!IsProcessorFeaturePresent)
155 ERR("IsProcessorFeaturePresent not available; CPU caps not detected\n");
156 else
158 if(IsProcessorFeaturePresent(PF_XMMI_INSTRUCTIONS_AVAILABLE))
160 caps |= CPU_CAP_SSE;
161 if(IsProcessorFeaturePresent(PF_XMMI64_INSTRUCTIONS_AVAILABLE))
162 caps |= CPU_CAP_SSE2;
165 #endif
166 #ifdef HAVE_NEON
167 /* Assume Neon support if compiled with it */
168 caps |= CPU_CAP_NEON;
169 #endif
171 TRACE("Extensions:%s%s%s%s%s\n",
172 ((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
173 ((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
174 ((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
175 ((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +Neon" : " -Neon") : ""),
176 ((!capfilter) ? " -none-" : "")
178 CPUCapFlags = caps & capfilter;
182 void *al_malloc(size_t alignment, size_t size)
184 #if defined(HAVE_ALIGNED_ALLOC)
185 size = (size+(alignment-1))&~(alignment-1);
186 return aligned_alloc(alignment, size);
187 #elif defined(HAVE_POSIX_MEMALIGN)
188 void *ret;
189 if(posix_memalign(&ret, alignment, size) == 0)
190 return ret;
191 return NULL;
192 #elif defined(HAVE__ALIGNED_MALLOC)
193 return _aligned_malloc(size, alignment);
194 #else
195 char *ret = malloc(size+alignment);
196 if(ret != NULL)
198 *(ret++) = 0x00;
199 while(((ALintptrEXT)ret&(alignment-1)) != 0)
200 *(ret++) = 0x55;
202 return ret;
203 #endif
206 void *al_calloc(size_t alignment, size_t size)
208 void *ret = al_malloc(alignment, size);
209 if(ret) memset(ret, 0, size);
210 return ret;
213 void al_free(void *ptr)
215 #if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
216 free(ptr);
217 #elif defined(HAVE__ALIGNED_MALLOC)
218 _aligned_free(ptr);
219 #else
220 if(ptr != NULL)
222 char *finder = ptr;
223 do {
224 --finder;
225 } while(*finder == 0x55);
226 free(finder);
228 #endif
232 #if (defined(HAVE___CONTROL87_2) || defined(HAVE__CONTROLFP)) && (defined(__x86_64__) || defined(_M_X64))
233 /* Win64 doesn't allow us to set the precision control. */
234 #undef _MCW_PC
235 #define _MCW_PC 0
236 #endif
238 void SetMixerFPUMode(FPUCtl *ctl)
240 #ifdef HAVE_FENV_H
241 fegetenv(STATIC_CAST(fenv_t, ctl));
242 #if defined(__GNUC__) && defined(HAVE_SSE)
243 if((CPUCapFlags&CPU_CAP_SSE))
244 __asm__ __volatile__("stmxcsr %0" : "=m" (*&ctl->sse_state));
245 #endif
247 #ifdef FE_TOWARDZERO
248 fesetround(FE_TOWARDZERO);
249 #endif
250 #if defined(__GNUC__) && defined(HAVE_SSE)
251 if((CPUCapFlags&CPU_CAP_SSE))
253 int sseState = ctl->sse_state;
254 sseState |= 0x6000; /* set round-to-zero */
255 sseState |= 0x8000; /* set flush-to-zero */
256 if((CPUCapFlags&CPU_CAP_SSE2))
257 sseState |= 0x0040; /* set denormals-are-zero */
258 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
260 #endif
262 #elif defined(HAVE___CONTROL87_2)
264 int mode;
265 __control87_2(0, 0, &ctl->state, NULL);
266 __control87_2(_RC_CHOP|_PC_24, _MCW_RC|_MCW_PC, &mode, NULL);
267 #ifdef HAVE_SSE
268 if((CPUCapFlags&CPU_CAP_SSE))
270 __control87_2(0, 0, NULL, &ctl->sse_state);
271 __control87_2(_RC_CHOP|_DN_FLUSH, _MCW_RC|_MCW_DN, NULL, &mode);
273 #endif
275 #elif defined(HAVE__CONTROLFP)
277 ctl->state = _controlfp(0, 0);
278 (void)_controlfp(_RC_CHOP|_PC_24, _MCW_RC|_MCW_PC);
279 #endif
282 void RestoreFPUMode(const FPUCtl *ctl)
284 #ifdef HAVE_FENV_H
285 fesetenv(STATIC_CAST(fenv_t, ctl));
286 #if defined(__GNUC__) && defined(HAVE_SSE)
287 if((CPUCapFlags&CPU_CAP_SSE))
288 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&ctl->sse_state));
289 #endif
291 #elif defined(HAVE___CONTROL87_2)
293 int mode;
294 __control87_2(ctl->state, _MCW_RC|_MCW_PC, &mode, NULL);
295 #ifdef HAVE_SSE
296 if((CPUCapFlags&CPU_CAP_SSE))
297 __control87_2(ctl->sse_state, _MCW_RC|_MCW_DN, NULL, &mode);
298 #endif
300 #elif defined(HAVE__CONTROLFP)
302 _controlfp(ctl->state, _MCW_RC|_MCW_PC);
303 #endif
307 #ifdef _WIN32
309 static WCHAR *FromUTF8(const char *str)
311 WCHAR *out = NULL;
312 int len;
314 if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
316 out = calloc(sizeof(WCHAR), len);
317 MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
319 return out;
323 void *LoadLib(const char *name)
325 HANDLE hdl = NULL;
326 WCHAR *wname;
328 wname = FromUTF8(name);
329 if(!wname)
330 ERR("Failed to convert UTF-8 filename: \"%s\"\n", name);
331 else
333 hdl = LoadLibraryW(wname);
334 free(wname);
336 return hdl;
338 void CloseLib(void *handle)
339 { FreeLibrary((HANDLE)handle); }
340 void *GetSymbol(void *handle, const char *name)
342 void *ret;
344 ret = (void*)GetProcAddress((HANDLE)handle, name);
345 if(ret == NULL)
346 ERR("Failed to load %s\n", name);
347 return ret;
350 WCHAR *strdupW(const WCHAR *str)
352 const WCHAR *n;
353 WCHAR *ret;
354 size_t len;
356 n = str;
357 while(*n) n++;
358 len = n - str;
360 ret = calloc(sizeof(WCHAR), len+1);
361 if(ret != NULL)
362 memcpy(ret, str, sizeof(WCHAR)*len);
363 return ret;
366 FILE *al_fopen(const char *fname, const char *mode)
368 WCHAR *wname=NULL, *wmode=NULL;
369 FILE *file = NULL;
371 wname = FromUTF8(fname);
372 wmode = FromUTF8(mode);
373 if(!wname)
374 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
375 else if(!wmode)
376 ERR("Failed to convert UTF-8 mode: \"%s\"\n", mode);
377 else
378 file = _wfopen(wname, wmode);
380 free(wname);
381 free(wmode);
383 return file;
386 #else
388 #ifdef HAVE_DLFCN_H
390 void *LoadLib(const char *name)
392 const char *err;
393 void *handle;
395 dlerror();
396 handle = dlopen(name, RTLD_NOW);
397 if((err=dlerror()) != NULL)
398 handle = NULL;
399 return handle;
401 void CloseLib(void *handle)
402 { dlclose(handle); }
403 void *GetSymbol(void *handle, const char *name)
405 const char *err;
406 void *sym;
408 dlerror();
409 sym = dlsym(handle, name);
410 if((err=dlerror()) != NULL)
412 WARN("Failed to load %s: %s\n", name, err);
413 sym = NULL;
415 return sym;
418 #endif
419 #endif
422 void al_print(const char *type, const char *func, const char *fmt, ...)
424 va_list ap;
426 va_start(ap, fmt);
427 fprintf(LogFile, "AL lib: %s %s: ", type, func);
428 vfprintf(LogFile, fmt, ap);
429 va_end(ap);
431 fflush(LogFile);
434 #ifdef _WIN32
435 static inline int is_slash(int c)
436 { return (c == '\\' || c == '/'); }
438 FILE *OpenDataFile(const char *fname, const char *subdir)
440 static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
441 WCHAR *wname=NULL, *wsubdir=NULL;
442 int i;
444 /* If the path is absolute, open it directly. */
445 if(fname[0] != '\0' && fname[1] == ':' && is_slash(fname[2]))
447 FILE *f;
448 if((f=al_fopen(fname, "rb")) != NULL)
450 TRACE("Opened %s\n", fname);
451 return f;
453 WARN("Could not open %s\n", fname);
454 return NULL;
457 wname = FromUTF8(fname);
458 wsubdir = FromUTF8(subdir);
459 if(!wname)
460 ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
461 else if(!wsubdir)
462 ERR("Failed to convert UTF-8 subdir: \"%s\"\n", subdir);
463 else for(i = 0;i < 2;i++)
465 WCHAR buffer[PATH_MAX];
466 size_t len;
467 FILE *f;
469 if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) == FALSE)
470 continue;
472 len = lstrlenW(buffer);
473 if(len > 0 && is_slash(buffer[len-1]))
474 buffer[--len] = '\0';
475 _snwprintf(buffer+len, PATH_MAX-len, L"/%ls/%ls", wsubdir, wname);
476 len = lstrlenW(buffer);
477 while(len > 0)
479 --len;
480 if(buffer[len] == '/')
481 buffer[len] = '\\';
484 if((f=_wfopen(buffer, L"rb")) != NULL)
486 TRACE("Opened %ls\n", buffer);
487 return f;
489 WARN("Could not open %ls\n", buffer);
491 free(wname);
492 free(wsubdir);
494 return NULL;
496 #else
497 FILE *OpenDataFile(const char *fname, const char *subdir)
499 char buffer[PATH_MAX] = "";
500 const char *str, *next;
501 FILE *f;
503 if(fname[0] == '/')
505 if((f=al_fopen(fname, "rb")) != NULL)
507 TRACE("Opened %s\n", fname);
508 return f;
510 WARN("Could not open %s\n", fname);
511 return NULL;
514 if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
515 snprintf(buffer, sizeof(buffer), "%s/%s/%s", str, subdir, fname);
516 else if((str=getenv("HOME")) != NULL && str[0] != '\0')
517 snprintf(buffer, sizeof(buffer), "%s/.local/share/%s/%s", str, subdir, fname);
518 if(buffer[0])
520 if((f=al_fopen(buffer, "rb")) != NULL)
522 TRACE("Opened %s\n", buffer);
523 return f;
525 WARN("Could not open %s\n", buffer);
528 if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
529 str = "/usr/local/share/:/usr/share/";
531 next = str;
532 while((str=next) != NULL && str[0] != '\0')
534 size_t len;
535 next = strchr(str, ':');
537 if(!next)
538 len = strlen(str);
539 else
541 len = next - str;
542 next++;
545 if(len > sizeof(buffer)-1)
546 len = sizeof(buffer)-1;
547 strncpy(buffer, str, len);
548 buffer[len] = '\0';
549 snprintf(buffer+len, sizeof(buffer)-len, "/%s/%s", subdir, fname);
551 if((f=al_fopen(buffer, "rb")) != NULL)
553 TRACE("Opened %s\n", buffer);
554 return f;
556 WARN("Could not open %s\n", buffer);
559 return NULL;
561 #endif
564 void SetRTPriority(void)
566 ALboolean failed = AL_FALSE;
568 #ifdef _WIN32
569 if(RTPrioLevel > 0)
570 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
571 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
572 if(RTPrioLevel > 0)
574 struct sched_param param;
575 /* Use the minimum real-time priority possible for now (on Linux this
576 * should be 1 for SCHED_RR) */
577 param.sched_priority = sched_get_priority_min(SCHED_RR);
578 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
580 #else
581 /* Real-time priority not available */
582 failed = (RTPrioLevel>0);
583 #endif
584 if(failed)
585 ERR("Failed to set priority level for thread\n");
589 ALboolean vector_reserve(void *ptr, size_t base_size, size_t obj_count, size_t obj_size, ALboolean exact)
591 vector_ *vecptr = ptr;
592 if((size_t)(*vecptr ? (*vecptr)->Capacity : 0) < obj_count)
594 ALsizei old_size = (*vecptr ? (*vecptr)->Size : 0);
595 void *temp;
597 /* Limit vector sizes to the greatest power-of-two value that an
598 * ALsizei can hold. */
599 if(obj_count > (INT_MAX>>1)+1)
600 return AL_FALSE;
602 /* Use the next power-of-2 size if we don't need to allocate the exact
603 * amount. This is preferred when regularly increasing the vector since
604 * it means fewer reallocations. Though it means it also wastes some
605 * memory. */
606 if(exact == AL_FALSE)
607 obj_count = NextPowerOf2((ALuint)obj_count);
609 /* Need to be explicit with the caller type's base size, because it
610 * could have extra padding before the start of the array (that is,
611 * sizeof(*vector_) may not equal base_size). */
612 temp = realloc(*vecptr, base_size + obj_size*obj_count);
613 if(temp == NULL) return AL_FALSE;
615 *vecptr = temp;
616 (*vecptr)->Capacity = (ALsizei)obj_count;
617 (*vecptr)->Size = old_size;
619 return AL_TRUE;
622 ALboolean vector_resize(void *ptr, size_t base_size, size_t obj_count, size_t obj_size)
624 vector_ *vecptr = ptr;
625 if(*vecptr || obj_count > 0)
627 if(!vector_reserve(vecptr, base_size, obj_count, obj_size, AL_TRUE))
628 return AL_FALSE;
629 (*vecptr)->Size = (ALsizei)obj_count;
631 return AL_TRUE;
634 ALboolean vector_insert(void *ptr, size_t base_size, size_t obj_size, void *ins_pos, const void *datstart, const void *datend)
636 vector_ *vecptr = ptr;
637 if(datstart != datend)
639 ptrdiff_t ins_elem = ((char*)ins_pos - ((char*)(*vecptr) + base_size)) / obj_size;
640 ptrdiff_t numins = ((const char*)datend - (const char*)datstart) / obj_size;
642 assert(numins > 0);
643 if(INT_MAX-VECTOR_SIZE(*vecptr) <= numins ||
644 !vector_reserve(vecptr, base_size, VECTOR_SIZE(*vecptr)+numins, obj_size, AL_TRUE))
645 return AL_FALSE;
647 /* NOTE: ins_pos may have been invalidated if *vecptr moved. Use ins_elem instead. */
648 if(ins_elem < (*vecptr)->Size)
650 memmove((char*)(*vecptr) + base_size + ((ins_elem+numins)*obj_size),
651 (char*)(*vecptr) + base_size + ((ins_elem )*obj_size),
652 ((*vecptr)->Size-ins_elem)*obj_size);
654 memcpy((char*)(*vecptr) + base_size + (ins_elem*obj_size),
655 datstart, numins*obj_size);
656 (*vecptr)->Size += (ALsizei)numins;
658 return AL_TRUE;
662 extern inline ALsizei al_string_length(const_al_string str);
663 extern inline ALboolean al_string_empty(const_al_string str);
664 extern inline const al_string_char_type *al_string_get_cstr(const_al_string str);
666 void al_string_clear(al_string *str)
668 /* Reserve one more character than the total size of the string. This is to
669 * ensure we have space to add a null terminator in the string data so it
670 * can be used as a C-style string. */
671 VECTOR_RESERVE(*str, 1);
672 VECTOR_RESIZE(*str, 0);
673 *VECTOR_ITER_END(*str) = 0;
676 static inline int al_string_compare(const al_string_char_type *str1, ALsizei str1len,
677 const al_string_char_type *str2, ALsizei str2len)
679 ALsizei complen = mini(str1len, str2len);
680 int ret = memcmp(str1, str2, complen);
681 if(ret == 0)
683 if(str1len > str2len) return 1;
684 if(str1len < str2len) return -1;
686 return ret;
688 int al_string_cmp(const_al_string str1, const_al_string str2)
690 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
691 &VECTOR_FRONT(str2), al_string_length(str2));
693 int al_string_cmp_cstr(const_al_string str1, const al_string_char_type *str2)
695 return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
696 str2, (ALsizei)strlen(str2));
699 void al_string_copy(al_string *str, const_al_string from)
701 ALsizei len = VECTOR_SIZE(from);
702 VECTOR_RESERVE(*str, len+1);
703 VECTOR_RESIZE(*str, 0);
704 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), VECTOR_ITER_BEGIN(from), VECTOR_ITER_BEGIN(from)+len);
705 *VECTOR_ITER_END(*str) = 0;
708 void al_string_copy_cstr(al_string *str, const al_string_char_type *from)
710 size_t len = strlen(from);
711 VECTOR_RESERVE(*str, len+1);
712 VECTOR_RESIZE(*str, 0);
713 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
714 *VECTOR_ITER_END(*str) = 0;
717 void al_string_append_char(al_string *str, const al_string_char_type c)
719 VECTOR_RESERVE(*str, al_string_length(*str)+2);
720 VECTOR_PUSH_BACK(*str, c);
721 *VECTOR_ITER_END(*str) = 0;
724 void al_string_append_cstr(al_string *str, const al_string_char_type *from)
726 size_t len = strlen(from);
727 if(len != 0)
729 VECTOR_RESERVE(*str, al_string_length(*str)+len+1);
730 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
731 *VECTOR_ITER_END(*str) = 0;
735 void al_string_append_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
737 if(to != from)
739 VECTOR_RESERVE(*str, al_string_length(*str)+(to-from)+1);
740 VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, to);
741 *VECTOR_ITER_END(*str) = 0;
745 #ifdef _WIN32
746 void al_string_copy_wcstr(al_string *str, const wchar_t *from)
748 int len;
749 if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
751 VECTOR_RESERVE(*str, len);
752 VECTOR_RESIZE(*str, len-1);
753 WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, NULL, NULL);
754 *VECTOR_ITER_END(*str) = 0;
757 #endif