helpers.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  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.
  8. *
  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.
  13. *
  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
  19. */
  20. #ifdef _WIN32
  21. #ifdef __MINGW32__
  22. #define _WIN32_IE 0x501
  23. #else
  24. #define _WIN32_IE 0x400
  25. #endif
  26. #endif
  27. #include "config.h"
  28. #include <stdlib.h>
  29. #include <time.h>
  30. #include <errno.h>
  31. #include <stdarg.h>
  32. #ifdef HAVE_MALLOC_H
  33. #include <malloc.h>
  34. #endif
  35. #ifdef HAVE_DIRENT_H
  36. #include <dirent.h>
  37. #endif
  38. #ifndef AL_NO_UID_DEFS
  39. #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
  40. #define INITGUID
  41. #include <windows.h>
  42. #ifdef HAVE_GUIDDEF_H
  43. #include <guiddef.h>
  44. #else
  45. #include <initguid.h>
  46. #endif
  47. DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
  48. DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
  49. DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
  50. DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
  51. DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
  52. DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
  53. DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
  54. DEFINE_GUID(IID_IAudioCaptureClient, 0xc8adbd64, 0xe71e, 0x48a0, 0xa4,0xde, 0x18,0x5c,0x39,0x5c,0xd3,0x17);
  55. #ifdef HAVE_MMDEVAPI
  56. #include <devpropdef.h>
  57. #include <propkeydef.h>
  58. DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
  59. DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_FormFactor, 0x1da5d803, 0xd492, 0x4edd, 0x8c,0x23, 0xe0,0xc0,0xff,0xee,0x7f,0x0e, 0);
  60. #endif
  61. #endif
  62. #endif /* AL_NO_UID_DEFS */
  63. #ifdef HAVE_DLFCN_H
  64. #include <dlfcn.h>
  65. #endif
  66. #ifdef HAVE_INTRIN_H
  67. #include <intrin.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
  81. #ifndef _WIN32
  82. #include <unistd.h>
  83. #elif defined(_WIN32_IE)
  84. #include <shlobj.h>
  85. #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"
  94. extern inline ALuint NextPowerOf2(ALuint value);
  95. extern inline ALint fastf2i(ALfloat f);
  96. extern inline ALuint fastf2u(ALfloat f);
  97. ALuint CPUCapFlags = 0;
  98. void FillCPUCaps(ALuint capfilter)
  99. {
  100. ALuint caps = 0;
  101. /* FIXME: We really should get this for all available CPUs in case different
  102. * CPUs have different caps (is that possible on one machine?). */
  103. #if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
  104. defined(_M_IX86) || defined(_M_X64))
  105. union {
  106. unsigned int regs[4];
  107. char str[sizeof(unsigned int[4])];
  108. } cpuinf[3];
  109. if(!__get_cpuid(0, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
  110. ERR("Failed to get CPUID\n");
  111. else
  112. {
  113. unsigned int maxfunc = cpuinf[0].regs[0];
  114. unsigned int maxextfunc = 0;
  115. if(__get_cpuid(0x80000000, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
  116. maxextfunc = cpuinf[0].regs[0];
  117. TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
  118. TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
  119. if(maxextfunc >= 0x80000004 &&
  120. __get_cpuid(0x80000002, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]) &&
  121. __get_cpuid(0x80000003, &cpuinf[1].regs[0], &cpuinf[1].regs[1], &cpuinf[1].regs[2], &cpuinf[1].regs[3]) &&
  122. __get_cpuid(0x80000004, &cpuinf[2].regs[0], &cpuinf[2].regs[1], &cpuinf[2].regs[2], &cpuinf[2].regs[3]))
  123. TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
  124. if(maxfunc >= 1 &&
  125. __get_cpuid(1, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
  126. {
  127. if((cpuinf[0].regs[3]&(1<<25)))
  128. {
  129. caps |= CPU_CAP_SSE;
  130. if((cpuinf[0].regs[3]&(1<<26)))
  131. {
  132. caps |= CPU_CAP_SSE2;
  133. if((cpuinf[0].regs[2]&(1<<0)))
  134. {
  135. caps |= CPU_CAP_SSE3;
  136. if((cpuinf[0].regs[2]&(1<<19)))
  137. caps |= CPU_CAP_SSE4_1;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. #elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
  144. defined(_M_IX86) || defined(_M_X64))
  145. union {
  146. int regs[4];
  147. char str[sizeof(int[4])];
  148. } cpuinf[3];
  149. (__cpuid)(cpuinf[0].regs, 0);
  150. if(cpuinf[0].regs[0] == 0)
  151. ERR("Failed to get CPUID\n");
  152. else
  153. {
  154. unsigned int maxfunc = cpuinf[0].regs[0];
  155. unsigned int maxextfunc;
  156. (__cpuid)(cpuinf[0].regs, 0x80000000);
  157. maxextfunc = cpuinf[0].regs[0];
  158. TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
  159. TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
  160. if(maxextfunc >= 0x80000004)
  161. {
  162. (__cpuid)(cpuinf[0].regs, 0x80000002);
  163. (__cpuid)(cpuinf[1].regs, 0x80000003);
  164. (__cpuid)(cpuinf[2].regs, 0x80000004);
  165. TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
  166. }
  167. if(maxfunc >= 1)
  168. {
  169. (__cpuid)(cpuinf[0].regs, 1);
  170. if((cpuinf[0].regs[3]&(1<<25)))
  171. {
  172. caps |= CPU_CAP_SSE;
  173. if((cpuinf[0].regs[3]&(1<<26)))
  174. {
  175. caps |= CPU_CAP_SSE2;
  176. if((cpuinf[0].regs[2]&(1<<0)))
  177. {
  178. caps |= CPU_CAP_SSE3;
  179. if((cpuinf[0].regs[2]&(1<<19)))
  180. caps |= CPU_CAP_SSE4_1;
  181. }
  182. }
  183. }
  184. }
  185. }
  186. #else
  187. /* Assume support for whatever's supported if we can't check for it */
  188. #if defined(HAVE_SSE4_1)
  189. #warning "Assuming SSE 4.1 run-time support!"
  190. caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3 | CPU_CAP_SSE4_1;
  191. #elif defined(HAVE_SSE3)
  192. #warning "Assuming SSE 3 run-time support!"
  193. caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3;
  194. #elif defined(HAVE_SSE2)
  195. #warning "Assuming SSE 2 run-time support!"
  196. caps |= CPU_CAP_SSE | CPU_CAP_SSE2;
  197. #elif defined(HAVE_SSE)
  198. #warning "Assuming SSE run-time support!"
  199. caps |= CPU_CAP_SSE;
  200. #endif
  201. #endif
  202. #ifdef HAVE_NEON
  203. /* Assume Neon support if compiled with it */
  204. caps |= CPU_CAP_NEON;
  205. #endif
  206. TRACE("Extensions:%s%s%s%s%s%s\n",
  207. ((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
  208. ((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
  209. ((capfilter&CPU_CAP_SSE3) ? ((caps&CPU_CAP_SSE3) ? " +SSE3" : " -SSE3") : ""),
  210. ((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
  211. ((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +Neon" : " -Neon") : ""),
  212. ((!capfilter) ? " -none-" : "")
  213. );
  214. CPUCapFlags = caps & capfilter;
  215. }
  216. void *al_malloc(size_t alignment, size_t size)
  217. {
  218. #if defined(HAVE_ALIGNED_ALLOC)
  219. size = (size+(alignment-1))&~(alignment-1);
  220. return aligned_alloc(alignment, size);
  221. #elif defined(HAVE_POSIX_MEMALIGN)
  222. void *ret;
  223. if(posix_memalign(&ret, alignment, size) == 0)
  224. return ret;
  225. return NULL;
  226. #elif defined(HAVE__ALIGNED_MALLOC)
  227. return _aligned_malloc(size, alignment);
  228. #else
  229. char *ret = malloc(size+alignment);
  230. if(ret != NULL)
  231. {
  232. *(ret++) = 0x00;
  233. while(((ptrdiff_t)ret&(alignment-1)) != 0)
  234. *(ret++) = 0x55;
  235. }
  236. return ret;
  237. #endif
  238. }
  239. void *al_calloc(size_t alignment, size_t size)
  240. {
  241. void *ret = al_malloc(alignment, size);
  242. if(ret) memset(ret, 0, size);
  243. return ret;
  244. }
  245. void al_free(void *ptr)
  246. {
  247. #if defined(HAVE_ALIGNED_ALLOC) || defined(HAVE_POSIX_MEMALIGN)
  248. free(ptr);
  249. #elif defined(HAVE__ALIGNED_MALLOC)
  250. _aligned_free(ptr);
  251. #else
  252. if(ptr != NULL)
  253. {
  254. char *finder = ptr;
  255. do {
  256. --finder;
  257. } while(*finder == 0x55);
  258. free(finder);
  259. }
  260. #endif
  261. }
  262. void SetMixerFPUMode(FPUCtl *ctl)
  263. {
  264. #ifdef HAVE_FENV_H
  265. fegetenv(STATIC_CAST(fenv_t, ctl));
  266. #if defined(__GNUC__) && defined(HAVE_SSE)
  267. /* FIXME: Some fegetenv implementations can get the SSE environment too?
  268. * How to tell when it does? */
  269. if((CPUCapFlags&CPU_CAP_SSE))
  270. __asm__ __volatile__("stmxcsr %0" : "=m" (*&ctl->sse_state));
  271. #endif
  272. #ifdef FE_TOWARDZERO
  273. fesetround(FE_TOWARDZERO);
  274. #endif
  275. #if defined(__GNUC__) && defined(HAVE_SSE)
  276. if((CPUCapFlags&CPU_CAP_SSE))
  277. {
  278. int sseState = ctl->sse_state;
  279. sseState |= 0x6000; /* set round-to-zero */
  280. sseState |= 0x8000; /* set flush-to-zero */
  281. if((CPUCapFlags&CPU_CAP_SSE2))
  282. sseState |= 0x0040; /* set denormals-are-zero */
  283. __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
  284. }
  285. #endif
  286. #elif defined(HAVE___CONTROL87_2)
  287. int mode;
  288. __control87_2(0, 0, &ctl->state, NULL);
  289. __control87_2(_RC_CHOP, _MCW_RC, &mode, NULL);
  290. #ifdef HAVE_SSE
  291. if((CPUCapFlags&CPU_CAP_SSE))
  292. {
  293. __control87_2(0, 0, NULL, &ctl->sse_state);
  294. __control87_2(_RC_CHOP|_DN_FLUSH, _MCW_RC|_MCW_DN, NULL, &mode);
  295. }
  296. #endif
  297. #elif defined(HAVE__CONTROLFP)
  298. ctl->state = _controlfp(0, 0);
  299. (void)_controlfp(_RC_CHOP, _MCW_RC);
  300. #endif
  301. }
  302. void RestoreFPUMode(const FPUCtl *ctl)
  303. {
  304. #ifdef HAVE_FENV_H
  305. fesetenv(STATIC_CAST(fenv_t, ctl));
  306. #if defined(__GNUC__) && defined(HAVE_SSE)
  307. if((CPUCapFlags&CPU_CAP_SSE))
  308. __asm__ __volatile__("ldmxcsr %0" : : "m" (*&ctl->sse_state));
  309. #endif
  310. #elif defined(HAVE___CONTROL87_2)
  311. int mode;
  312. __control87_2(ctl->state, _MCW_RC, &mode, NULL);
  313. #ifdef HAVE_SSE
  314. if((CPUCapFlags&CPU_CAP_SSE))
  315. __control87_2(ctl->sse_state, _MCW_RC|_MCW_DN, NULL, &mode);
  316. #endif
  317. #elif defined(HAVE__CONTROLFP)
  318. _controlfp(ctl->state, _MCW_RC);
  319. #endif
  320. }
  321. #ifdef _WIN32
  322. static WCHAR *FromUTF8(const char *str)
  323. {
  324. WCHAR *out = NULL;
  325. int len;
  326. if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
  327. {
  328. out = calloc(sizeof(WCHAR), len);
  329. MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
  330. }
  331. return out;
  332. }
  333. void *LoadLib(const char *name)
  334. {
  335. HANDLE hdl = NULL;
  336. WCHAR *wname;
  337. wname = FromUTF8(name);
  338. if(!wname)
  339. ERR("Failed to convert UTF-8 filename: \"%s\"\n", name);
  340. else
  341. {
  342. hdl = LoadLibraryW(wname);
  343. free(wname);
  344. }
  345. return hdl;
  346. }
  347. void CloseLib(void *handle)
  348. { FreeLibrary((HANDLE)handle); }
  349. void *GetSymbol(void *handle, const char *name)
  350. {
  351. void *ret;
  352. ret = (void*)GetProcAddress((HANDLE)handle, name);
  353. if(ret == NULL)
  354. ERR("Failed to load %s\n", name);
  355. return ret;
  356. }
  357. WCHAR *strdupW(const WCHAR *str)
  358. {
  359. const WCHAR *n;
  360. WCHAR *ret;
  361. size_t len;
  362. n = str;
  363. while(*n) n++;
  364. len = n - str;
  365. ret = calloc(sizeof(WCHAR), len+1);
  366. if(ret != NULL)
  367. memcpy(ret, str, sizeof(WCHAR)*len);
  368. return ret;
  369. }
  370. FILE *al_fopen(const char *fname, const char *mode)
  371. {
  372. WCHAR *wname=NULL, *wmode=NULL;
  373. FILE *file = NULL;
  374. wname = FromUTF8(fname);
  375. wmode = FromUTF8(mode);
  376. if(!wname)
  377. ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
  378. else if(!wmode)
  379. ERR("Failed to convert UTF-8 mode: \"%s\"\n", mode);
  380. else
  381. file = _wfopen(wname, wmode);
  382. free(wname);
  383. free(wmode);
  384. return file;
  385. }
  386. void al_print(const char *type, const char *func, const char *fmt, ...)
  387. {
  388. char str[1024];
  389. WCHAR *wstr;
  390. va_list ap;
  391. va_start(ap, fmt);
  392. vsnprintf(str, sizeof(str), fmt, ap);
  393. va_end(ap);
  394. str[sizeof(str)-1] = 0;
  395. wstr = FromUTF8(str);
  396. if(!wstr)
  397. fprintf(LogFile, "AL lib: %s %s: <UTF-8 error> %s", type, func, str);
  398. else
  399. {
  400. fprintf(LogFile, "AL lib: %s %s: %ls", type, func, wstr);
  401. free(wstr);
  402. wstr = NULL;
  403. }
  404. fflush(LogFile);
  405. }
  406. static inline int is_slash(int c)
  407. { return (c == '\\' || c == '/'); }
  408. FILE *OpenDataFile(const char *fname, const char *subdir)
  409. {
  410. static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
  411. WCHAR *wname=NULL, *wsubdir=NULL;
  412. FILE *f;
  413. size_t i;
  414. wname = FromUTF8(fname);
  415. if(!wname)
  416. {
  417. ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
  418. return NULL;
  419. }
  420. /* If the path is absolute, open it directly. */
  421. if(wname[0] != '\0' && wname[1] == ':' && is_slash(wname[2]))
  422. {
  423. f = _wfopen(wname, L"rb");
  424. if(f) TRACE("Opened %s\n", fname);
  425. else WARN("Could not open %s\n", fname);
  426. free(wname);
  427. return f;
  428. }
  429. /* Try the current directory first before the data directories. */
  430. if((f=_wfopen(wname, L"rb")) != NULL)
  431. {
  432. TRACE("Opened %s\n", fname);
  433. free(wname);
  434. return f;
  435. }
  436. wsubdir = FromUTF8(subdir);
  437. if(!wsubdir)
  438. {
  439. ERR("Failed to convert UTF-8 subdir: \"%s\"\n", subdir);
  440. free(wname);
  441. return NULL;
  442. }
  443. for(i = 0;i < COUNTOF(ids);i++)
  444. {
  445. WCHAR buffer[PATH_MAX];
  446. size_t len;
  447. if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) == FALSE)
  448. continue;
  449. len = lstrlenW(buffer);
  450. if(len > 0 && is_slash(buffer[len-1]))
  451. buffer[--len] = '\0';
  452. _snwprintf(buffer+len, PATH_MAX-len, L"/%ls/%ls", wsubdir, wname);
  453. len = lstrlenW(buffer);
  454. while(len > 0)
  455. {
  456. --len;
  457. if(buffer[len] == '/')
  458. buffer[len] = '\\';
  459. }
  460. if((f=_wfopen(buffer, L"rb")) != NULL)
  461. {
  462. al_string filepath = AL_STRING_INIT_STATIC();
  463. al_string_copy_wcstr(&filepath, buffer);
  464. TRACE("Opened %s\n", al_string_get_cstr(filepath));
  465. al_string_deinit(&filepath);
  466. break;
  467. }
  468. }
  469. free(wname);
  470. free(wsubdir);
  471. if(f == NULL)
  472. WARN("Could not open %s\\%s\n", subdir, fname);
  473. return f;
  474. }
  475. static const WCHAR *strchrW(const WCHAR *str, WCHAR ch)
  476. {
  477. for(;*str != 0;++str)
  478. {
  479. if(*str == ch)
  480. return str;
  481. }
  482. return NULL;
  483. }
  484. static const WCHAR *strrchrW(const WCHAR *str, WCHAR ch)
  485. {
  486. const WCHAR *ret = NULL;
  487. for(;*str != 0;++str)
  488. {
  489. if(*str == ch)
  490. ret = str;
  491. }
  492. return ret;
  493. }
  494. /* Compares the filename in the find data with the match string. The match
  495. * string may contain the "%r" marker to signifiy a sample rate (really any
  496. * positive integer), or "%%" to signify a single '%'.
  497. */
  498. static int MatchFilter(const WCHAR *match, const WIN32_FIND_DATAW *fdata)
  499. {
  500. const WCHAR *name = fdata->cFileName;
  501. int ret = 1;
  502. do {
  503. const WCHAR *p = strchrW(match, '%');
  504. if(!p)
  505. ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE,
  506. match, -1, name, -1) == CSTR_EQUAL;
  507. else
  508. {
  509. int len = p-match;
  510. ret = lstrlenW(name) >= len;
  511. if(ret)
  512. ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE,
  513. match, len, name, len) == CSTR_EQUAL;
  514. if(ret)
  515. {
  516. match += len;
  517. name += len;
  518. ++p;
  519. if(*p == 'r')
  520. {
  521. unsigned long l = 0;
  522. while(*name >= '0' && *name <= '9')
  523. {
  524. l = l*10 + (*name-'0');
  525. ++name;
  526. }
  527. ret = l > 0;
  528. ++p;
  529. }
  530. }
  531. }
  532. match = p;
  533. } while(ret && match && *match);
  534. return ret;
  535. }
  536. static void RecurseDirectorySearch(const char *path, const WCHAR *match, vector_al_string *results)
  537. {
  538. WIN32_FIND_DATAW fdata;
  539. const WCHAR *sep, *p;
  540. HANDLE hdl;
  541. if(!match[0])
  542. return;
  543. /* Find the last directory separator and the next '%' marker in the match
  544. * string. */
  545. sep = strrchrW(match, '\\');
  546. p = strchrW(match, '%');
  547. /* If there's no separator, test the files in the specified path against
  548. * the match string, and add the results. */
  549. if(!sep)
  550. {
  551. al_string pathstr = AL_STRING_INIT_STATIC();
  552. WCHAR *wpath;
  553. TRACE("Searching %s for %ls\n", path, match);
  554. al_string_append_cstr(&pathstr, path);
  555. al_string_append_cstr(&pathstr, "\\*.*");
  556. wpath = FromUTF8(al_string_get_cstr(pathstr));
  557. hdl = FindFirstFileW(wpath, &fdata);
  558. if(hdl != INVALID_HANDLE_VALUE)
  559. {
  560. do {
  561. if(MatchFilter(match, &fdata))
  562. {
  563. al_string str = AL_STRING_INIT_STATIC();
  564. al_string_copy_cstr(&str, path);
  565. al_string_append_char(&str, '\\');
  566. al_string_append_wcstr(&str, fdata.cFileName);
  567. TRACE("Got result %s\n", al_string_get_cstr(str));
  568. VECTOR_PUSH_BACK(*results, str);
  569. }
  570. } while(FindNextFileW(hdl, &fdata));
  571. FindClose(hdl);
  572. }
  573. free(wpath);
  574. al_string_deinit(&pathstr);
  575. return;
  576. }
  577. /* If there's no '%' marker, or it's after the final separator, append the
  578. * remaining directories to the path and recurse into it with the remaining
  579. * filename portion. */
  580. if(!p || p-sep >= 0)
  581. {
  582. al_string npath = AL_STRING_INIT_STATIC();
  583. al_string_append_cstr(&npath, path);
  584. al_string_append_char(&npath, '\\');
  585. al_string_append_wrange(&npath, match, sep);
  586. TRACE("Recursing into %s with %ls\n", al_string_get_cstr(npath), sep+1);
  587. RecurseDirectorySearch(al_string_get_cstr(npath), sep+1, results);
  588. al_string_deinit(&npath);
  589. return;
  590. }
  591. /* Look for the last separator before the '%' marker, and the first
  592. * separator after it. */
  593. sep = strchrW(match, '\\');
  594. if(sep-p >= 0) sep = NULL;
  595. for(;;)
  596. {
  597. const WCHAR *next = strchrW(sep?sep+1:match, '\\');
  598. if(next-p < 0)
  599. {
  600. al_string npath = AL_STRING_INIT_STATIC();
  601. WCHAR *nwpath, *nwmatch;
  602. /* Append up to the last directory before the one with a '%'. */
  603. al_string_copy_cstr(&npath, path);
  604. if(sep)
  605. {
  606. al_string_append_char(&npath, '\\');
  607. al_string_append_wrange(&npath, match, sep);
  608. }
  609. al_string_append_cstr(&npath, "\\*.*");
  610. nwpath = FromUTF8(al_string_get_cstr(npath));
  611. /* Take the directory name containing a '%' as a new string to
  612. * match against. */
  613. if(!sep)
  614. {
  615. nwmatch = calloc(2, next-match+1);
  616. memcpy(nwmatch, match, (next-match)*2);
  617. }
  618. else
  619. {
  620. nwmatch = calloc(2, next-(sep+1)+1);
  621. memcpy(nwmatch, sep+1, (next-(sep+1))*2);
  622. }
  623. /* For each matching directory name, recurse into it with the
  624. * remaining string. */
  625. TRACE("Searching %s for %ls\n", al_string_get_cstr(npath), nwmatch);
  626. hdl = FindFirstFileW(nwpath, &fdata);
  627. if(hdl != INVALID_HANDLE_VALUE)
  628. {
  629. do {
  630. if(MatchFilter(nwmatch, &fdata))
  631. {
  632. al_string ndir = AL_STRING_INIT_STATIC();
  633. al_string_copy(&ndir, npath);
  634. al_string_append_char(&ndir, '\\');
  635. al_string_append_wcstr(&ndir, fdata.cFileName);
  636. TRACE("Recursing %s with %ls\n", al_string_get_cstr(ndir), next+1);
  637. RecurseDirectorySearch(al_string_get_cstr(ndir), next+1, results);
  638. al_string_deinit(&ndir);
  639. }
  640. } while(FindNextFileW(hdl, &fdata));
  641. FindClose(hdl);
  642. }
  643. free(nwmatch);
  644. free(nwpath);
  645. al_string_deinit(&npath);
  646. break;
  647. }
  648. sep = next;
  649. }
  650. }
  651. vector_al_string SearchDataFiles(const char *match, const char *subdir)
  652. {
  653. static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
  654. static RefCount search_lock;
  655. vector_al_string results = VECTOR_INIT_STATIC();
  656. WCHAR *wmatch;
  657. size_t i;
  658. while(ATOMIC_EXCHANGE(uint, &search_lock, 1) == 1)
  659. althrd_yield();
  660. wmatch = FromUTF8(match);
  661. if(!wmatch)
  662. {
  663. ERR("Failed to convert UTF-8 filename: \"%s\"\n", match);
  664. return results;
  665. }
  666. for(i = 0;wmatch[i];++i)
  667. {
  668. if(wmatch[i] == '/')
  669. wmatch[i] = '\\';
  670. }
  671. /* If the path is absolute, use it directly. */
  672. if(isalpha(wmatch[0]) && wmatch[1] == ':' && is_slash(wmatch[2]))
  673. {
  674. char drv[3] = { (char)wmatch[0], ':', 0 };
  675. RecurseDirectorySearch(drv, wmatch+3, &results);
  676. }
  677. else if(wmatch[0] == '\\' && wmatch[1] == '\\' && wmatch[2] == '?' && wmatch[3] == '\\')
  678. RecurseDirectorySearch("\\\\?", wmatch+4, &results);
  679. else
  680. {
  681. al_string path = AL_STRING_INIT_STATIC();
  682. WCHAR *cwdbuf;
  683. /* Search the CWD. */
  684. if(!(cwdbuf=_wgetcwd(NULL, 0)))
  685. al_string_copy_cstr(&path, ".");
  686. else
  687. {
  688. al_string_copy_wcstr(&path, cwdbuf);
  689. if(is_slash(VECTOR_BACK(path)))
  690. {
  691. VECTOR_POP_BACK(path);
  692. *VECTOR_ITER_END(path) = 0;
  693. }
  694. free(cwdbuf);
  695. }
  696. RecurseDirectorySearch(al_string_get_cstr(path), wmatch, &results);
  697. /* Search the local and global data dirs. */
  698. for(i = 0;i < COUNTOF(ids);i++)
  699. {
  700. WCHAR buffer[PATH_MAX];
  701. if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) != FALSE)
  702. {
  703. al_string_copy_wcstr(&path, buffer);
  704. if(!is_slash(VECTOR_BACK(path)))
  705. al_string_append_char(&path, '\\');
  706. al_string_append_cstr(&path, subdir);
  707. #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
  708. VECTOR_FOR_EACH(char, path, FIX_SLASH);
  709. #undef FIX_SLASH
  710. RecurseDirectorySearch(al_string_get_cstr(path), wmatch, &results);
  711. }
  712. }
  713. al_string_deinit(&path);
  714. }
  715. free(wmatch);
  716. ATOMIC_STORE(&search_lock, 0);
  717. return results;
  718. }
  719. #else
  720. #ifdef HAVE_DLFCN_H
  721. void *LoadLib(const char *name)
  722. {
  723. const char *err;
  724. void *handle;
  725. dlerror();
  726. handle = dlopen(name, RTLD_NOW);
  727. if((err=dlerror()) != NULL)
  728. handle = NULL;
  729. return handle;
  730. }
  731. void CloseLib(void *handle)
  732. { dlclose(handle); }
  733. void *GetSymbol(void *handle, const char *name)
  734. {
  735. const char *err;
  736. void *sym;
  737. dlerror();
  738. sym = dlsym(handle, name);
  739. if((err=dlerror()) != NULL)
  740. {
  741. WARN("Failed to load %s: %s\n", name, err);
  742. sym = NULL;
  743. }
  744. return sym;
  745. }
  746. #endif /* HAVE_DLFCN_H */
  747. void al_print(const char *type, const char *func, const char *fmt, ...)
  748. {
  749. va_list ap;
  750. va_start(ap, fmt);
  751. fprintf(LogFile, "AL lib: %s %s: ", type, func);
  752. vfprintf(LogFile, fmt, ap);
  753. va_end(ap);
  754. fflush(LogFile);
  755. }
  756. FILE *OpenDataFile(const char *fname, const char *subdir)
  757. {
  758. char buffer[PATH_MAX] = "";
  759. const char *str, *next;
  760. FILE *f;
  761. if(fname[0] == '/')
  762. {
  763. if((f=al_fopen(fname, "rb")) != NULL)
  764. {
  765. TRACE("Opened %s\n", fname);
  766. return f;
  767. }
  768. WARN("Could not open %s\n", fname);
  769. return NULL;
  770. }
  771. if((f=al_fopen(fname, "rb")) != NULL)
  772. {
  773. TRACE("Opened %s\n", fname);
  774. return f;
  775. }
  776. if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
  777. snprintf(buffer, sizeof(buffer), "%s/%s/%s", str, subdir, fname);
  778. else if((str=getenv("HOME")) != NULL && str[0] != '\0')
  779. snprintf(buffer, sizeof(buffer), "%s/.local/share/%s/%s", str, subdir, fname);
  780. if(buffer[0])
  781. {
  782. if((f=al_fopen(buffer, "rb")) != NULL)
  783. {
  784. TRACE("Opened %s\n", buffer);
  785. return f;
  786. }
  787. }
  788. if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
  789. str = "/usr/local/share/:/usr/share/";
  790. next = str;
  791. while((str=next) != NULL && str[0] != '\0')
  792. {
  793. size_t len;
  794. next = strchr(str, ':');
  795. if(!next)
  796. len = strlen(str);
  797. else
  798. {
  799. len = next - str;
  800. next++;
  801. }
  802. if(len > sizeof(buffer)-1)
  803. len = sizeof(buffer)-1;
  804. strncpy(buffer, str, len);
  805. buffer[len] = '\0';
  806. snprintf(buffer+len, sizeof(buffer)-len, "/%s/%s", subdir, fname);
  807. if((f=al_fopen(buffer, "rb")) != NULL)
  808. {
  809. TRACE("Opened %s\n", buffer);
  810. return f;
  811. }
  812. }
  813. WARN("Could not open %s/%s\n", subdir, fname);
  814. return NULL;
  815. }
  816. static const char *MatchString;
  817. static int MatchFilter(const struct dirent *dir)
  818. {
  819. const char *match = MatchString;
  820. const char *name = dir->d_name;
  821. int ret = 1;
  822. do {
  823. const char *p = strchr(match, '%');
  824. if(!p)
  825. ret = strcmp(match, name) == 0;
  826. else
  827. {
  828. size_t len = p-match;
  829. ret = strncmp(match, name, len) == 0;
  830. if(ret)
  831. {
  832. match += len;
  833. name += len;
  834. ++p;
  835. if(*p == 'r')
  836. {
  837. char *end;
  838. ret = strtoul(name, &end, 10) > 0;
  839. if(ret) name = end;
  840. ++p;
  841. }
  842. }
  843. }
  844. match = p;
  845. } while(ret && match && *match);
  846. return ret;
  847. }
  848. static void RecurseDirectorySearch(const char *path, const char *match, vector_al_string *results)
  849. {
  850. struct dirent **namelist;
  851. char *sep, *p;
  852. int n, i;
  853. if(!match[0])
  854. return;
  855. sep = strrchr(match, '/');
  856. p = strchr(match, '%');
  857. if(!sep)
  858. {
  859. MatchString = match;
  860. TRACE("Searching %s for %s\n", path?path:"/", match);
  861. n = scandir(path?path:"/", &namelist, MatchFilter, alphasort);
  862. if(n >= 0)
  863. {
  864. for(i = 0;i < n;++i)
  865. {
  866. al_string str = AL_STRING_INIT_STATIC();
  867. if(path) al_string_copy_cstr(&str, path);
  868. al_string_append_char(&str, '/');
  869. al_string_append_cstr(&str, namelist[i]->d_name);
  870. TRACE("Got result %s\n", al_string_get_cstr(str));
  871. VECTOR_PUSH_BACK(*results, str);
  872. free(namelist[i]);
  873. }
  874. free(namelist);
  875. }
  876. return;
  877. }
  878. if(!p || p-sep >= 0)
  879. {
  880. al_string npath = AL_STRING_INIT_STATIC();
  881. if(path) al_string_append_cstr(&npath, path);
  882. al_string_append_char(&npath, '/');
  883. al_string_append_range(&npath, match, sep);
  884. TRACE("Recursing into %s with %s\n", al_string_get_cstr(npath), sep+1);
  885. RecurseDirectorySearch(al_string_get_cstr(npath), sep+1, results);
  886. al_string_deinit(&npath);
  887. return;
  888. }
  889. sep = strchr(match, '/');
  890. if(sep-p >= 0) sep = NULL;
  891. for(;;)
  892. {
  893. char *next = strchr(sep?sep+1:match, '/');
  894. if(next-p < 0)
  895. {
  896. al_string npath = AL_STRING_INIT_STATIC();
  897. al_string nmatch = AL_STRING_INIT_STATIC();
  898. if(!sep)
  899. {
  900. al_string_append_cstr(&npath, path?path:"/.");
  901. MatchString = match;
  902. }
  903. else
  904. {
  905. if(path) al_string_append_cstr(&npath, path);
  906. al_string_append_char(&npath, '/');
  907. al_string_append_range(&npath, match, sep);
  908. al_string_append_range(&nmatch, sep+1, next);
  909. MatchString = al_string_get_cstr(nmatch);
  910. }
  911. TRACE("Searching %s for %s\n", al_string_get_cstr(npath), MatchString);
  912. n = scandir(al_string_get_cstr(npath), &namelist, MatchFilter, alphasort);
  913. if(n >= 0)
  914. {
  915. al_string ndir = AL_STRING_INIT_STATIC();
  916. for(i = 0;i < n;++i)
  917. {
  918. al_string_copy(&ndir, npath);
  919. al_string_append_char(&ndir, '/');
  920. al_string_append_cstr(&ndir, namelist[i]->d_name);
  921. free(namelist[i]);
  922. TRACE("Recursing %s with %s\n", al_string_get_cstr(ndir), next+1);
  923. RecurseDirectorySearch(al_string_get_cstr(ndir), next+1, results);
  924. }
  925. al_string_deinit(&ndir);
  926. free(namelist);
  927. }
  928. al_string_deinit(&nmatch);
  929. al_string_deinit(&npath);
  930. break;
  931. }
  932. sep = next;
  933. }
  934. }
  935. vector_al_string SearchDataFiles(const char *match, const char *subdir)
  936. {
  937. static RefCount search_lock;
  938. vector_al_string results = VECTOR_INIT_STATIC();
  939. while(ATOMIC_EXCHANGE(uint, &search_lock, 1) == 1)
  940. althrd_yield();
  941. if(match[0] == '/')
  942. RecurseDirectorySearch(NULL, match+1, &results);
  943. else
  944. {
  945. al_string path = AL_STRING_INIT_STATIC();
  946. const char *str, *next;
  947. char cwdbuf[PATH_MAX];
  948. // Search CWD
  949. if(!getcwd(cwdbuf, sizeof(cwdbuf)))
  950. strcpy(cwdbuf, ".");
  951. RecurseDirectorySearch(cwdbuf, match, &results);
  952. // Search local data dir
  953. if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
  954. {
  955. al_string_append_cstr(&path, str);
  956. al_string_append_char(&path, '/');
  957. al_string_append_cstr(&path, subdir);
  958. }
  959. else if((str=getenv("HOME")) != NULL && str[0] != '\0')
  960. {
  961. al_string_append_cstr(&path, str);
  962. al_string_append_cstr(&path, "/.local/share/");
  963. al_string_append_cstr(&path, subdir);
  964. }
  965. if(!al_string_empty(path))
  966. RecurseDirectorySearch(al_string_get_cstr(path), match, &results);
  967. // Search global data dirs
  968. if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
  969. str = "/usr/local/share/:/usr/share/";
  970. next = str;
  971. while((str=next) != NULL && str[0] != '\0')
  972. {
  973. next = strchr(str, ':');
  974. if(!next)
  975. al_string_copy_cstr(&path, str);
  976. else
  977. {
  978. al_string_clear(&path);
  979. al_string_append_range(&path, str, next);
  980. ++next;
  981. }
  982. if(!al_string_empty(path))
  983. {
  984. al_string_append_char(&path, '/');
  985. al_string_append_cstr(&path, subdir);
  986. RecurseDirectorySearch(al_string_get_cstr(path), match, &results);
  987. }
  988. }
  989. al_string_deinit(&path);
  990. }
  991. ATOMIC_STORE(&search_lock, 0);
  992. return results;
  993. }
  994. #endif
  995. void SetRTPriority(void)
  996. {
  997. ALboolean failed = AL_FALSE;
  998. #ifdef _WIN32
  999. if(RTPrioLevel > 0)
  1000. failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
  1001. #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
  1002. if(RTPrioLevel > 0)
  1003. {
  1004. struct sched_param param;
  1005. /* Use the minimum real-time priority possible for now (on Linux this
  1006. * should be 1 for SCHED_RR) */
  1007. param.sched_priority = sched_get_priority_min(SCHED_RR);
  1008. failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
  1009. }
  1010. #else
  1011. /* Real-time priority not available */
  1012. failed = (RTPrioLevel>0);
  1013. #endif
  1014. if(failed)
  1015. ERR("Failed to set priority level for thread\n");
  1016. }
  1017. ALboolean vector_reserve(char *ptr, size_t base_size, size_t obj_size, size_t obj_count, ALboolean exact)
  1018. {
  1019. vector_ *vecptr = (vector_*)ptr;
  1020. if((*vecptr ? (*vecptr)->Capacity : 0) < obj_count)
  1021. {
  1022. size_t old_size = (*vecptr ? (*vecptr)->Size : 0);
  1023. void *temp;
  1024. /* Use the next power-of-2 size if we don't need to allocate the exact
  1025. * amount. This is preferred when regularly increasing the vector since
  1026. * it means fewer reallocations. Though it means it also wastes some
  1027. * memory. */
  1028. if(exact == AL_FALSE && obj_count < INT_MAX)
  1029. obj_count = NextPowerOf2((ALuint)obj_count);
  1030. /* Need to be explicit with the caller type's base size, because it
  1031. * could have extra padding before the start of the array (that is,
  1032. * sizeof(*vector_) may not equal base_size). */
  1033. temp = realloc(*vecptr, base_size + obj_size*obj_count);
  1034. if(temp == NULL) return AL_FALSE;
  1035. *vecptr = temp;
  1036. (*vecptr)->Capacity = obj_count;
  1037. (*vecptr)->Size = old_size;
  1038. }
  1039. return AL_TRUE;
  1040. }
  1041. ALboolean vector_resize(char *ptr, size_t base_size, size_t obj_size, size_t obj_count)
  1042. {
  1043. vector_ *vecptr = (vector_*)ptr;
  1044. if(*vecptr || obj_count > 0)
  1045. {
  1046. if(!vector_reserve((char*)vecptr, base_size, obj_size, obj_count, AL_TRUE))
  1047. return AL_FALSE;
  1048. (*vecptr)->Size = obj_count;
  1049. }
  1050. return AL_TRUE;
  1051. }
  1052. ALboolean vector_insert(char *ptr, size_t base_size, size_t obj_size, void *ins_pos, const void *datstart, const void *datend)
  1053. {
  1054. vector_ *vecptr = (vector_*)ptr;
  1055. if(datstart != datend)
  1056. {
  1057. ptrdiff_t ins_elem = (*vecptr ? ((char*)ins_pos - ((char*)(*vecptr) + base_size)) :
  1058. ((char*)ins_pos - (char*)NULL)) /
  1059. obj_size;
  1060. ptrdiff_t numins = ((const char*)datend - (const char*)datstart) / obj_size;
  1061. assert(numins > 0);
  1062. if((size_t)numins + VECTOR_SIZE(*vecptr) < (size_t)numins ||
  1063. !vector_reserve((char*)vecptr, base_size, obj_size, VECTOR_SIZE(*vecptr)+numins, AL_TRUE))
  1064. return AL_FALSE;
  1065. /* NOTE: ins_pos may have been invalidated if *vecptr moved. Use ins_elem instead. */
  1066. if((size_t)ins_elem < (*vecptr)->Size)
  1067. {
  1068. memmove((char*)(*vecptr) + base_size + ((ins_elem+numins)*obj_size),
  1069. (char*)(*vecptr) + base_size + ((ins_elem )*obj_size),
  1070. ((*vecptr)->Size-ins_elem)*obj_size);
  1071. }
  1072. memcpy((char*)(*vecptr) + base_size + (ins_elem*obj_size),
  1073. datstart, numins*obj_size);
  1074. (*vecptr)->Size += numins;
  1075. }
  1076. return AL_TRUE;
  1077. }
  1078. extern inline void al_string_deinit(al_string *str);
  1079. extern inline size_t al_string_length(const_al_string str);
  1080. extern inline ALboolean al_string_empty(const_al_string str);
  1081. extern inline const al_string_char_type *al_string_get_cstr(const_al_string str);
  1082. void al_string_clear(al_string *str)
  1083. {
  1084. /* Reserve one more character than the total size of the string. This is to
  1085. * ensure we have space to add a null terminator in the string data so it
  1086. * can be used as a C-style string. */
  1087. VECTOR_RESERVE(*str, 1);
  1088. VECTOR_RESIZE(*str, 0);
  1089. *VECTOR_ITER_END(*str) = 0;
  1090. }
  1091. static inline int al_string_compare(const al_string_char_type *str1, size_t str1len,
  1092. const al_string_char_type *str2, size_t str2len)
  1093. {
  1094. size_t complen = (str1len < str2len) ? str1len : str2len;
  1095. int ret = memcmp(str1, str2, complen);
  1096. if(ret == 0)
  1097. {
  1098. if(str1len > str2len) return 1;
  1099. if(str1len < str2len) return -1;
  1100. }
  1101. return ret;
  1102. }
  1103. int al_string_cmp(const_al_string str1, const_al_string str2)
  1104. {
  1105. return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
  1106. &VECTOR_FRONT(str2), al_string_length(str2));
  1107. }
  1108. int al_string_cmp_cstr(const_al_string str1, const al_string_char_type *str2)
  1109. {
  1110. return al_string_compare(&VECTOR_FRONT(str1), al_string_length(str1),
  1111. str2, strlen(str2));
  1112. }
  1113. void al_string_copy(al_string *str, const_al_string from)
  1114. {
  1115. size_t len = al_string_length(from);
  1116. VECTOR_RESERVE(*str, len+1);
  1117. VECTOR_RESIZE(*str, 0);
  1118. VECTOR_INSERT(*str, VECTOR_ITER_END(*str), VECTOR_ITER_BEGIN(from), VECTOR_ITER_BEGIN(from)+len);
  1119. *VECTOR_ITER_END(*str) = 0;
  1120. }
  1121. void al_string_copy_cstr(al_string *str, const al_string_char_type *from)
  1122. {
  1123. size_t len = strlen(from);
  1124. VECTOR_RESERVE(*str, len+1);
  1125. VECTOR_RESIZE(*str, 0);
  1126. VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
  1127. *VECTOR_ITER_END(*str) = 0;
  1128. }
  1129. void al_string_append_char(al_string *str, const al_string_char_type c)
  1130. {
  1131. VECTOR_RESERVE(*str, al_string_length(*str)+2);
  1132. VECTOR_PUSH_BACK(*str, c);
  1133. *VECTOR_ITER_END(*str) = 0;
  1134. }
  1135. void al_string_append_cstr(al_string *str, const al_string_char_type *from)
  1136. {
  1137. size_t len = strlen(from);
  1138. if(len != 0)
  1139. {
  1140. VECTOR_RESERVE(*str, al_string_length(*str)+len+1);
  1141. VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, from+len);
  1142. *VECTOR_ITER_END(*str) = 0;
  1143. }
  1144. }
  1145. void al_string_append_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
  1146. {
  1147. if(to != from)
  1148. {
  1149. VECTOR_RESERVE(*str, al_string_length(*str)+(to-from)+1);
  1150. VECTOR_INSERT(*str, VECTOR_ITER_END(*str), from, to);
  1151. *VECTOR_ITER_END(*str) = 0;
  1152. }
  1153. }
  1154. #ifdef _WIN32
  1155. void al_string_copy_wcstr(al_string *str, const wchar_t *from)
  1156. {
  1157. int len;
  1158. if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
  1159. {
  1160. VECTOR_RESERVE(*str, len);
  1161. VECTOR_RESIZE(*str, len-1);
  1162. WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, NULL, NULL);
  1163. *VECTOR_ITER_END(*str) = 0;
  1164. }
  1165. }
  1166. void al_string_append_wcstr(al_string *str, const wchar_t *from)
  1167. {
  1168. int len;
  1169. if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
  1170. {
  1171. size_t strlen = al_string_length(*str);
  1172. VECTOR_RESERVE(*str, strlen+len);
  1173. VECTOR_RESIZE(*str, strlen+len-1);
  1174. WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str) + strlen, len, NULL, NULL);
  1175. *VECTOR_ITER_END(*str) = 0;
  1176. }
  1177. }
  1178. void al_string_append_wrange(al_string *str, const wchar_t *from, const wchar_t *to)
  1179. {
  1180. int len;
  1181. if((len=WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), NULL, 0, NULL, NULL)) > 0)
  1182. {
  1183. size_t strlen = al_string_length(*str);
  1184. VECTOR_RESERVE(*str, strlen+len+1);
  1185. VECTOR_RESIZE(*str, strlen+len);
  1186. WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), &VECTOR_FRONT(*str) + strlen, len+1, NULL, NULL);
  1187. *VECTOR_ITER_END(*str) = 0;
  1188. }
  1189. }
  1190. #endif