helpers.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  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. #include <ctype.h>
  33. #ifdef HAVE_MALLOC_H
  34. #include <malloc.h>
  35. #endif
  36. #ifdef HAVE_DIRENT_H
  37. #include <dirent.h>
  38. #endif
  39. #ifdef HAVE_PROC_PIDPATH
  40. #include <libproc.h>
  41. #endif
  42. #ifdef __FreeBSD__
  43. #include <sys/types.h>
  44. #include <sys/sysctl.h>
  45. #endif
  46. #ifndef AL_NO_UID_DEFS
  47. #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
  48. #define INITGUID
  49. #include <windows.h>
  50. #ifdef HAVE_GUIDDEF_H
  51. #include <guiddef.h>
  52. #else
  53. #include <initguid.h>
  54. #endif
  55. DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
  56. DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
  57. DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
  58. DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
  59. DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
  60. DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
  61. DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
  62. DEFINE_GUID(IID_IAudioCaptureClient, 0xc8adbd64, 0xe71e, 0x48a0, 0xa4,0xde, 0x18,0x5c,0x39,0x5c,0xd3,0x17);
  63. #ifdef HAVE_WASAPI
  64. #include <wtypes.h>
  65. #include <devpropdef.h>
  66. #include <propkeydef.h>
  67. DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
  68. DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_FormFactor, 0x1da5d803, 0xd492, 0x4edd, 0x8c,0x23, 0xe0,0xc0,0xff,0xee,0x7f,0x0e, 0);
  69. DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_GUID, 0x1da5d803, 0xd492, 0x4edd, 0x8c, 0x23,0xe0, 0xc0,0xff,0xee,0x7f,0x0e, 4 );
  70. #endif
  71. #endif
  72. #endif /* AL_NO_UID_DEFS */
  73. #ifdef HAVE_DLFCN_H
  74. #include <dlfcn.h>
  75. #endif
  76. #ifdef HAVE_INTRIN_H
  77. #include <intrin.h>
  78. #endif
  79. #ifdef HAVE_CPUID_H
  80. #include <cpuid.h>
  81. #endif
  82. #ifdef HAVE_SYS_SYSCONF_H
  83. #include <sys/sysconf.h>
  84. #endif
  85. #ifdef HAVE_FLOAT_H
  86. #include <float.h>
  87. #endif
  88. #ifdef HAVE_IEEEFP_H
  89. #include <ieeefp.h>
  90. #endif
  91. #ifndef _WIN32
  92. #include <sys/types.h>
  93. #include <sys/stat.h>
  94. #include <sys/mman.h>
  95. #include <fcntl.h>
  96. #include <unistd.h>
  97. #elif defined(_WIN32_IE)
  98. #include <shlobj.h>
  99. #endif
  100. #include "alMain.h"
  101. #include "alu.h"
  102. #include "cpu_caps.h"
  103. #include "fpu_modes.h"
  104. #include "atomic.h"
  105. #include "uintmap.h"
  106. #include "vector.h"
  107. #include "alstring.h"
  108. #include "compat.h"
  109. #include "threads.h"
  110. extern inline ALuint NextPowerOf2(ALuint value);
  111. extern inline size_t RoundUp(size_t value, size_t r);
  112. extern inline ALint fastf2i(ALfloat f);
  113. #ifndef __GNUC__
  114. #if defined(HAVE_BITSCANFORWARD64_INTRINSIC)
  115. extern inline int msvc64_ctz64(ALuint64 v);
  116. #elif defined(HAVE_BITSCANFORWARD_INTRINSIC)
  117. extern inline int msvc_ctz64(ALuint64 v);
  118. #else
  119. extern inline int fallback_popcnt64(ALuint64 v);
  120. extern inline int fallback_ctz64(ALuint64 value);
  121. #endif
  122. #endif
  123. int CPUCapFlags = 0;
  124. void FillCPUCaps(int capfilter)
  125. {
  126. int caps = 0;
  127. /* FIXME: We really should get this for all available CPUs in case different
  128. * CPUs have different caps (is that possible on one machine?). */
  129. #if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
  130. defined(_M_IX86) || defined(_M_X64))
  131. union {
  132. unsigned int regs[4];
  133. char str[sizeof(unsigned int[4])];
  134. } cpuinf[3];
  135. if(!__get_cpuid(0, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
  136. ERR("Failed to get CPUID\n");
  137. else
  138. {
  139. unsigned int maxfunc = cpuinf[0].regs[0];
  140. unsigned int maxextfunc = 0;
  141. if(__get_cpuid(0x80000000, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
  142. maxextfunc = cpuinf[0].regs[0];
  143. TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
  144. TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
  145. if(maxextfunc >= 0x80000004 &&
  146. __get_cpuid(0x80000002, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]) &&
  147. __get_cpuid(0x80000003, &cpuinf[1].regs[0], &cpuinf[1].regs[1], &cpuinf[1].regs[2], &cpuinf[1].regs[3]) &&
  148. __get_cpuid(0x80000004, &cpuinf[2].regs[0], &cpuinf[2].regs[1], &cpuinf[2].regs[2], &cpuinf[2].regs[3]))
  149. TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
  150. if(maxfunc >= 1 &&
  151. __get_cpuid(1, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
  152. {
  153. if((cpuinf[0].regs[3]&(1<<25)))
  154. {
  155. caps |= CPU_CAP_SSE;
  156. if((cpuinf[0].regs[3]&(1<<26)))
  157. {
  158. caps |= CPU_CAP_SSE2;
  159. if((cpuinf[0].regs[2]&(1<<0)))
  160. {
  161. caps |= CPU_CAP_SSE3;
  162. if((cpuinf[0].regs[2]&(1<<19)))
  163. caps |= CPU_CAP_SSE4_1;
  164. }
  165. }
  166. }
  167. }
  168. }
  169. #elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
  170. defined(_M_IX86) || defined(_M_X64))
  171. union {
  172. int regs[4];
  173. char str[sizeof(int[4])];
  174. } cpuinf[3];
  175. (__cpuid)(cpuinf[0].regs, 0);
  176. if(cpuinf[0].regs[0] == 0)
  177. ERR("Failed to get CPUID\n");
  178. else
  179. {
  180. unsigned int maxfunc = cpuinf[0].regs[0];
  181. unsigned int maxextfunc;
  182. (__cpuid)(cpuinf[0].regs, 0x80000000);
  183. maxextfunc = cpuinf[0].regs[0];
  184. 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)
  187. {
  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);
  192. }
  193. if(maxfunc >= 1)
  194. {
  195. (__cpuid)(cpuinf[0].regs, 1);
  196. if((cpuinf[0].regs[3]&(1<<25)))
  197. {
  198. caps |= CPU_CAP_SSE;
  199. if((cpuinf[0].regs[3]&(1<<26)))
  200. {
  201. caps |= CPU_CAP_SSE2;
  202. if((cpuinf[0].regs[2]&(1<<0)))
  203. {
  204. caps |= CPU_CAP_SSE3;
  205. if((cpuinf[0].regs[2]&(1<<19)))
  206. caps |= CPU_CAP_SSE4_1;
  207. }
  208. }
  209. }
  210. }
  211. }
  212. #else
  213. /* Assume support for whatever's supported if we can't check for it */
  214. #if defined(HAVE_SSE4_1)
  215. #warning "Assuming SSE 4.1 run-time support!"
  216. caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3 | CPU_CAP_SSE4_1;
  217. #elif defined(HAVE_SSE3)
  218. #warning "Assuming SSE 3 run-time support!"
  219. caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3;
  220. #elif defined(HAVE_SSE2)
  221. #warning "Assuming SSE 2 run-time support!"
  222. caps |= CPU_CAP_SSE | CPU_CAP_SSE2;
  223. #elif defined(HAVE_SSE)
  224. #warning "Assuming SSE run-time support!"
  225. caps |= CPU_CAP_SSE;
  226. #endif
  227. #endif
  228. #ifdef HAVE_NEON
  229. FILE *file = fopen("/proc/cpuinfo", "rt");
  230. if(!file)
  231. ERR("Failed to open /proc/cpuinfo, cannot check for NEON support\n");
  232. else
  233. {
  234. char buf[256];
  235. while(fgets(buf, sizeof(buf), file) != NULL)
  236. {
  237. size_t len;
  238. char *str;
  239. if(strncmp(buf, "Features\t:", 10) != 0)
  240. continue;
  241. len = strlen(buf);
  242. while(len > 0 && isspace(buf[len-1]))
  243. buf[--len] = 0;
  244. TRACE("Got features string:%s\n", buf+10);
  245. str = buf;
  246. while((str=strstr(str, "neon")) != NULL)
  247. {
  248. if(isspace(*(str-1)) && (str[4] == 0 || isspace(str[4])))
  249. {
  250. caps |= CPU_CAP_NEON;
  251. break;
  252. }
  253. str++;
  254. }
  255. break;
  256. }
  257. fclose(file);
  258. file = NULL;
  259. }
  260. #endif
  261. TRACE("Extensions:%s%s%s%s%s%s\n",
  262. ((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
  263. ((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
  264. ((capfilter&CPU_CAP_SSE3) ? ((caps&CPU_CAP_SSE3) ? " +SSE3" : " -SSE3") : ""),
  265. ((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
  266. ((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +NEON" : " -NEON") : ""),
  267. ((!capfilter) ? " -none-" : "")
  268. );
  269. CPUCapFlags = caps & capfilter;
  270. }
  271. void SetMixerFPUMode(FPUCtl *ctl)
  272. {
  273. #ifdef HAVE_FENV_H
  274. fegetenv(&ctl->flt_env);
  275. #ifdef _WIN32
  276. /* HACK: A nasty bug in MinGW-W64 causes fegetenv and fesetenv to not save
  277. * and restore the FPU rounding mode, so we have to do it manually. Don't
  278. * know if this also applies to MSVC.
  279. */
  280. ctl->round_mode = fegetround();
  281. #endif
  282. #if defined(__GNUC__) && defined(HAVE_SSE)
  283. /* FIXME: Some fegetenv implementations can get the SSE environment too?
  284. * How to tell when it does? */
  285. if((CPUCapFlags&CPU_CAP_SSE))
  286. __asm__ __volatile__("stmxcsr %0" : "=m" (*&ctl->sse_state));
  287. #endif
  288. #ifdef FE_TOWARDZERO
  289. fesetround(FE_TOWARDZERO);
  290. #endif
  291. #if defined(__GNUC__) && defined(HAVE_SSE)
  292. if((CPUCapFlags&CPU_CAP_SSE))
  293. {
  294. int sseState = ctl->sse_state;
  295. sseState |= 0x6000; /* set round-to-zero */
  296. sseState |= 0x8000; /* set flush-to-zero */
  297. if((CPUCapFlags&CPU_CAP_SSE2))
  298. sseState |= 0x0040; /* set denormals-are-zero */
  299. __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
  300. }
  301. #endif
  302. #elif defined(HAVE___CONTROL87_2)
  303. int mode;
  304. __control87_2(0, 0, &ctl->state, NULL);
  305. __control87_2(_RC_CHOP, _MCW_RC, &mode, NULL);
  306. #ifdef HAVE_SSE
  307. if((CPUCapFlags&CPU_CAP_SSE))
  308. {
  309. __control87_2(0, 0, NULL, &ctl->sse_state);
  310. __control87_2(_RC_CHOP|_DN_FLUSH, _MCW_RC|_MCW_DN, NULL, &mode);
  311. }
  312. #endif
  313. #elif defined(HAVE__CONTROLFP)
  314. ctl->state = _controlfp(0, 0);
  315. (void)_controlfp(_RC_CHOP, _MCW_RC);
  316. #endif
  317. }
  318. void RestoreFPUMode(const FPUCtl *ctl)
  319. {
  320. #ifdef HAVE_FENV_H
  321. fesetenv(&ctl->flt_env);
  322. #ifdef _WIN32
  323. fesetround(ctl->round_mode);
  324. #endif
  325. #if defined(__GNUC__) && defined(HAVE_SSE)
  326. if((CPUCapFlags&CPU_CAP_SSE))
  327. __asm__ __volatile__("ldmxcsr %0" : : "m" (*&ctl->sse_state));
  328. #endif
  329. #elif defined(HAVE___CONTROL87_2)
  330. int mode;
  331. __control87_2(ctl->state, _MCW_RC, &mode, NULL);
  332. #ifdef HAVE_SSE
  333. if((CPUCapFlags&CPU_CAP_SSE))
  334. __control87_2(ctl->sse_state, _MCW_RC|_MCW_DN, NULL, &mode);
  335. #endif
  336. #elif defined(HAVE__CONTROLFP)
  337. _controlfp(ctl->state, _MCW_RC);
  338. #endif
  339. }
  340. static int StringSortCompare(const void *str1, const void *str2)
  341. {
  342. return alstr_cmp(*(const_al_string*)str1, *(const_al_string*)str2);
  343. }
  344. #ifdef _WIN32
  345. static WCHAR *strrchrW(WCHAR *str, WCHAR ch)
  346. {
  347. WCHAR *ret = NULL;
  348. while(*str)
  349. {
  350. if(*str == ch)
  351. ret = str;
  352. ++str;
  353. }
  354. return ret;
  355. }
  356. void GetProcBinary(al_string *path, al_string *fname)
  357. {
  358. WCHAR *pathname, *sep;
  359. DWORD pathlen;
  360. DWORD len;
  361. pathlen = 256;
  362. pathname = malloc(pathlen * sizeof(pathname[0]));
  363. while(pathlen > 0 && (len=GetModuleFileNameW(NULL, pathname, pathlen)) == pathlen)
  364. {
  365. free(pathname);
  366. pathlen <<= 1;
  367. pathname = malloc(pathlen * sizeof(pathname[0]));
  368. }
  369. if(len == 0)
  370. {
  371. free(pathname);
  372. ERR("Failed to get process name: error %lu\n", GetLastError());
  373. return;
  374. }
  375. pathname[len] = 0;
  376. if((sep=strrchrW(pathname, '\\')) != NULL)
  377. {
  378. WCHAR *sep2 = strrchrW(sep+1, '/');
  379. if(sep2) sep = sep2;
  380. }
  381. else
  382. sep = strrchrW(pathname, '/');
  383. if(sep)
  384. {
  385. if(path) alstr_copy_wrange(path, pathname, sep);
  386. if(fname) alstr_copy_wcstr(fname, sep+1);
  387. }
  388. else
  389. {
  390. if(path) alstr_clear(path);
  391. if(fname) alstr_copy_wcstr(fname, pathname);
  392. }
  393. free(pathname);
  394. if(path && fname)
  395. TRACE("Got: %s, %s\n", alstr_get_cstr(*path), alstr_get_cstr(*fname));
  396. else if(path) TRACE("Got path: %s\n", alstr_get_cstr(*path));
  397. else if(fname) TRACE("Got filename: %s\n", alstr_get_cstr(*fname));
  398. }
  399. static WCHAR *FromUTF8(const char *str)
  400. {
  401. WCHAR *out = NULL;
  402. int len;
  403. if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
  404. {
  405. out = calloc(sizeof(WCHAR), len);
  406. MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
  407. }
  408. return out;
  409. }
  410. void *LoadLib(const char *name)
  411. {
  412. HANDLE hdl = NULL;
  413. WCHAR *wname;
  414. wname = FromUTF8(name);
  415. if(!wname)
  416. ERR("Failed to convert UTF-8 filename: \"%s\"\n", name);
  417. else
  418. {
  419. hdl = LoadLibraryW(wname);
  420. free(wname);
  421. }
  422. return hdl;
  423. }
  424. void CloseLib(void *handle)
  425. { FreeLibrary((HANDLE)handle); }
  426. void *GetSymbol(void *handle, const char *name)
  427. {
  428. void *ret;
  429. ret = (void*)GetProcAddress((HANDLE)handle, name);
  430. if(ret == NULL)
  431. ERR("Failed to load %s\n", name);
  432. return ret;
  433. }
  434. WCHAR *strdupW(const WCHAR *str)
  435. {
  436. const WCHAR *n;
  437. WCHAR *ret;
  438. size_t len;
  439. n = str;
  440. while(*n) n++;
  441. len = n - str;
  442. ret = calloc(sizeof(WCHAR), len+1);
  443. if(ret != NULL)
  444. memcpy(ret, str, sizeof(WCHAR)*len);
  445. return ret;
  446. }
  447. FILE *al_fopen(const char *fname, const char *mode)
  448. {
  449. WCHAR *wname=NULL, *wmode=NULL;
  450. FILE *file = NULL;
  451. wname = FromUTF8(fname);
  452. wmode = FromUTF8(mode);
  453. if(!wname)
  454. ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname);
  455. else if(!wmode)
  456. ERR("Failed to convert UTF-8 mode: \"%s\"\n", mode);
  457. else
  458. file = _wfopen(wname, wmode);
  459. free(wname);
  460. free(wmode);
  461. return file;
  462. }
  463. void al_print(const char *type, const char *func, const char *fmt, ...)
  464. {
  465. char str[1024];
  466. WCHAR *wstr;
  467. va_list ap;
  468. va_start(ap, fmt);
  469. vsnprintf(str, sizeof(str), fmt, ap);
  470. va_end(ap);
  471. str[sizeof(str)-1] = 0;
  472. wstr = FromUTF8(str);
  473. if(!wstr)
  474. fprintf(LogFile, "AL lib: %s %s: <UTF-8 error> %s", type, func, str);
  475. else
  476. {
  477. fprintf(LogFile, "AL lib: %s %s: %ls", type, func, wstr);
  478. free(wstr);
  479. wstr = NULL;
  480. }
  481. fflush(LogFile);
  482. }
  483. static inline int is_slash(int c)
  484. { return (c == '\\' || c == '/'); }
  485. static void DirectorySearch(const char *path, const char *ext, vector_al_string *results)
  486. {
  487. al_string pathstr = AL_STRING_INIT_STATIC();
  488. WIN32_FIND_DATAW fdata;
  489. WCHAR *wpath;
  490. HANDLE hdl;
  491. alstr_copy_cstr(&pathstr, path);
  492. alstr_append_cstr(&pathstr, "\\*");
  493. alstr_append_cstr(&pathstr, ext);
  494. TRACE("Searching %s\n", alstr_get_cstr(pathstr));
  495. wpath = FromUTF8(alstr_get_cstr(pathstr));
  496. hdl = FindFirstFileW(wpath, &fdata);
  497. if(hdl != INVALID_HANDLE_VALUE)
  498. {
  499. size_t base = VECTOR_SIZE(*results);
  500. do {
  501. al_string str = AL_STRING_INIT_STATIC();
  502. alstr_copy_cstr(&str, path);
  503. alstr_append_char(&str, '\\');
  504. alstr_append_wcstr(&str, fdata.cFileName);
  505. TRACE("Got result %s\n", alstr_get_cstr(str));
  506. VECTOR_PUSH_BACK(*results, str);
  507. } while(FindNextFileW(hdl, &fdata));
  508. FindClose(hdl);
  509. if(VECTOR_SIZE(*results) > base)
  510. qsort(VECTOR_BEGIN(*results)+base, VECTOR_SIZE(*results)-base,
  511. sizeof(VECTOR_FRONT(*results)), StringSortCompare);
  512. }
  513. free(wpath);
  514. alstr_reset(&pathstr);
  515. }
  516. vector_al_string SearchDataFiles(const char *ext, const char *subdir)
  517. {
  518. static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
  519. static RefCount search_lock;
  520. vector_al_string results = VECTOR_INIT_STATIC();
  521. size_t i;
  522. while(ATOMIC_EXCHANGE_SEQ(&search_lock, 1) == 1)
  523. althrd_yield();
  524. /* If the path is absolute, use it directly. */
  525. if(isalpha(subdir[0]) && subdir[1] == ':' && is_slash(subdir[2]))
  526. {
  527. al_string path = AL_STRING_INIT_STATIC();
  528. alstr_copy_cstr(&path, subdir);
  529. #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
  530. VECTOR_FOR_EACH(char, path, FIX_SLASH);
  531. #undef FIX_SLASH
  532. DirectorySearch(alstr_get_cstr(path), ext, &results);
  533. alstr_reset(&path);
  534. }
  535. else if(subdir[0] == '\\' && subdir[1] == '\\' && subdir[2] == '?' && subdir[3] == '\\')
  536. DirectorySearch(subdir, ext, &results);
  537. else
  538. {
  539. al_string path = AL_STRING_INIT_STATIC();
  540. WCHAR *cwdbuf;
  541. /* Search the app-local directory. */
  542. if((cwdbuf=_wgetenv(L"ALSOFT_LOCAL_PATH")) && *cwdbuf != '\0')
  543. {
  544. alstr_copy_wcstr(&path, cwdbuf);
  545. if(is_slash(VECTOR_BACK(path)))
  546. {
  547. VECTOR_POP_BACK(path);
  548. *VECTOR_END(path) = 0;
  549. }
  550. }
  551. else if(!(cwdbuf=_wgetcwd(NULL, 0)))
  552. alstr_copy_cstr(&path, ".");
  553. else
  554. {
  555. alstr_copy_wcstr(&path, cwdbuf);
  556. if(is_slash(VECTOR_BACK(path)))
  557. {
  558. VECTOR_POP_BACK(path);
  559. *VECTOR_END(path) = 0;
  560. }
  561. free(cwdbuf);
  562. }
  563. #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
  564. VECTOR_FOR_EACH(char, path, FIX_SLASH);
  565. #undef FIX_SLASH
  566. DirectorySearch(alstr_get_cstr(path), ext, &results);
  567. /* Search the local and global data dirs. */
  568. for(i = 0;i < COUNTOF(ids);i++)
  569. {
  570. WCHAR buffer[MAX_PATH];
  571. if(SHGetSpecialFolderPathW(NULL, buffer, ids[i], FALSE) != FALSE)
  572. {
  573. alstr_copy_wcstr(&path, buffer);
  574. if(!is_slash(VECTOR_BACK(path)))
  575. alstr_append_char(&path, '\\');
  576. alstr_append_cstr(&path, subdir);
  577. #define FIX_SLASH(i) do { if(*(i) == '/') *(i) = '\\'; } while(0)
  578. VECTOR_FOR_EACH(char, path, FIX_SLASH);
  579. #undef FIX_SLASH
  580. DirectorySearch(alstr_get_cstr(path), ext, &results);
  581. }
  582. }
  583. alstr_reset(&path);
  584. }
  585. ATOMIC_STORE_SEQ(&search_lock, 0);
  586. return results;
  587. }
  588. struct FileMapping MapFileToMem(const char *fname)
  589. {
  590. struct FileMapping ret = { NULL, NULL, NULL, 0 };
  591. MEMORY_BASIC_INFORMATION meminfo;
  592. HANDLE file, fmap;
  593. WCHAR *wname;
  594. void *ptr;
  595. wname = FromUTF8(fname);
  596. file = CreateFileW(wname, GENERIC_READ, FILE_SHARE_READ, NULL,
  597. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  598. if(file == INVALID_HANDLE_VALUE)
  599. {
  600. ERR("Failed to open %s: %lu\n", fname, GetLastError());
  601. free(wname);
  602. return ret;
  603. }
  604. free(wname);
  605. wname = NULL;
  606. fmap = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
  607. if(!fmap)
  608. {
  609. ERR("Failed to create map for %s: %lu\n", fname, GetLastError());
  610. CloseHandle(file);
  611. return ret;
  612. }
  613. ptr = MapViewOfFile(fmap, FILE_MAP_READ, 0, 0, 0);
  614. if(!ptr)
  615. {
  616. ERR("Failed to map %s: %lu\n", fname, GetLastError());
  617. CloseHandle(fmap);
  618. CloseHandle(file);
  619. return ret;
  620. }
  621. if(VirtualQuery(ptr, &meminfo, sizeof(meminfo)) != sizeof(meminfo))
  622. {
  623. ERR("Failed to get map size for %s: %lu\n", fname, GetLastError());
  624. UnmapViewOfFile(ptr);
  625. CloseHandle(fmap);
  626. CloseHandle(file);
  627. return ret;
  628. }
  629. ret.file = file;
  630. ret.fmap = fmap;
  631. ret.ptr = ptr;
  632. ret.len = meminfo.RegionSize;
  633. return ret;
  634. }
  635. void UnmapFileMem(const struct FileMapping *mapping)
  636. {
  637. UnmapViewOfFile(mapping->ptr);
  638. CloseHandle(mapping->fmap);
  639. CloseHandle(mapping->file);
  640. }
  641. #else
  642. void GetProcBinary(al_string *path, al_string *fname)
  643. {
  644. char *pathname = NULL;
  645. size_t pathlen;
  646. #ifdef __FreeBSD__
  647. int mib[4] = { CTL_KERN, KERN_PROC_ARGS, getpid() };
  648. if(sysctl(mib, 3, NULL, &pathlen, NULL, 0) == -1)
  649. WARN("Failed to sysctl kern.procargs.%d: %s\n", mib[2], strerror(errno));
  650. else
  651. {
  652. pathname = malloc(pathlen + 1);
  653. sysctl(mib, 3, (void*)pathname, &pathlen, NULL, 0);
  654. pathname[pathlen] = 0;
  655. }
  656. #endif
  657. #ifdef HAVE_PROC_PIDPATH
  658. if(!pathname)
  659. {
  660. const pid_t pid = getpid();
  661. char procpath[PROC_PIDPATHINFO_MAXSIZE];
  662. int ret;
  663. ret = proc_pidpath(pid, procpath, sizeof(procpath));
  664. if(ret < 1)
  665. {
  666. WARN("proc_pidpath(%d, ...) failed: %s\n", pid, strerror(errno));
  667. free(pathname);
  668. pathname = NULL;
  669. }
  670. else
  671. {
  672. pathlen = strlen(procpath);
  673. pathname = strdup(procpath);
  674. }
  675. }
  676. #endif
  677. if(!pathname)
  678. {
  679. const char *selfname;
  680. ssize_t len;
  681. pathlen = 256;
  682. pathname = malloc(pathlen);
  683. selfname = "/proc/self/exe";
  684. len = readlink(selfname, pathname, pathlen);
  685. if(len == -1 && errno == ENOENT)
  686. {
  687. selfname = "/proc/self/file";
  688. len = readlink(selfname, pathname, pathlen);
  689. }
  690. if(len == -1 && errno == ENOENT)
  691. {
  692. selfname = "/proc/curproc/exe";
  693. len = readlink(selfname, pathname, pathlen);
  694. }
  695. if(len == -1 && errno == ENOENT)
  696. {
  697. selfname = "/proc/curproc/file";
  698. len = readlink(selfname, pathname, pathlen);
  699. }
  700. while(len > 0 && (size_t)len == pathlen)
  701. {
  702. free(pathname);
  703. pathlen <<= 1;
  704. pathname = malloc(pathlen);
  705. len = readlink(selfname, pathname, pathlen);
  706. }
  707. if(len <= 0)
  708. {
  709. free(pathname);
  710. WARN("Failed to readlink %s: %s\n", selfname, strerror(errno));
  711. return;
  712. }
  713. pathname[len] = 0;
  714. }
  715. char *sep = strrchr(pathname, '/');
  716. if(sep)
  717. {
  718. if(path) alstr_copy_range(path, pathname, sep);
  719. if(fname) alstr_copy_cstr(fname, sep+1);
  720. }
  721. else
  722. {
  723. if(path) alstr_clear(path);
  724. if(fname) alstr_copy_cstr(fname, pathname);
  725. }
  726. free(pathname);
  727. if(path && fname)
  728. TRACE("Got: %s, %s\n", alstr_get_cstr(*path), alstr_get_cstr(*fname));
  729. else if(path) TRACE("Got path: %s\n", alstr_get_cstr(*path));
  730. else if(fname) TRACE("Got filename: %s\n", alstr_get_cstr(*fname));
  731. }
  732. #ifdef HAVE_DLFCN_H
  733. void *LoadLib(const char *name)
  734. {
  735. const char *err;
  736. void *handle;
  737. dlerror();
  738. handle = dlopen(name, RTLD_NOW);
  739. if((err=dlerror()) != NULL)
  740. handle = NULL;
  741. return handle;
  742. }
  743. void CloseLib(void *handle)
  744. { dlclose(handle); }
  745. void *GetSymbol(void *handle, const char *name)
  746. {
  747. const char *err;
  748. void *sym;
  749. dlerror();
  750. sym = dlsym(handle, name);
  751. if((err=dlerror()) != NULL)
  752. {
  753. WARN("Failed to load %s: %s\n", name, err);
  754. sym = NULL;
  755. }
  756. return sym;
  757. }
  758. #endif /* HAVE_DLFCN_H */
  759. void al_print(const char *type, const char *func, const char *fmt, ...)
  760. {
  761. va_list ap;
  762. va_start(ap, fmt);
  763. fprintf(LogFile, "AL lib: %s %s: ", type, func);
  764. vfprintf(LogFile, fmt, ap);
  765. va_end(ap);
  766. fflush(LogFile);
  767. }
  768. static void DirectorySearch(const char *path, const char *ext, vector_al_string *results)
  769. {
  770. size_t extlen = strlen(ext);
  771. DIR *dir;
  772. TRACE("Searching %s for *%s\n", path, ext);
  773. dir = opendir(path);
  774. if(dir != NULL)
  775. {
  776. size_t base = VECTOR_SIZE(*results);
  777. struct dirent *dirent;
  778. while((dirent=readdir(dir)) != NULL)
  779. {
  780. al_string str;
  781. size_t len;
  782. if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0)
  783. continue;
  784. len = strlen(dirent->d_name);
  785. if(!(len > extlen))
  786. continue;
  787. if(strcasecmp(dirent->d_name+len-extlen, ext) != 0)
  788. continue;
  789. AL_STRING_INIT(str);
  790. alstr_copy_cstr(&str, path);
  791. if(VECTOR_BACK(str) != '/')
  792. alstr_append_char(&str, '/');
  793. alstr_append_cstr(&str, dirent->d_name);
  794. TRACE("Got result %s\n", alstr_get_cstr(str));
  795. VECTOR_PUSH_BACK(*results, str);
  796. }
  797. closedir(dir);
  798. if(VECTOR_SIZE(*results) > base)
  799. qsort(VECTOR_BEGIN(*results)+base, VECTOR_SIZE(*results)-base,
  800. sizeof(VECTOR_FRONT(*results)), StringSortCompare);
  801. }
  802. }
  803. vector_al_string SearchDataFiles(const char *ext, const char *subdir)
  804. {
  805. static RefCount search_lock;
  806. vector_al_string results = VECTOR_INIT_STATIC();
  807. while(ATOMIC_EXCHANGE_SEQ(&search_lock, 1) == 1)
  808. althrd_yield();
  809. if(subdir[0] == '/')
  810. DirectorySearch(subdir, ext, &results);
  811. else
  812. {
  813. al_string path = AL_STRING_INIT_STATIC();
  814. const char *str, *next;
  815. /* Search the app-local directory. */
  816. if((str=getenv("ALSOFT_LOCAL_PATH")) && *str != '\0')
  817. DirectorySearch(str, ext, &results);
  818. else
  819. {
  820. size_t cwdlen = 256;
  821. char *cwdbuf = malloc(cwdlen);
  822. while(!getcwd(cwdbuf, cwdlen))
  823. {
  824. free(cwdbuf);
  825. cwdbuf = NULL;
  826. if(errno != ERANGE)
  827. break;
  828. cwdlen <<= 1;
  829. cwdbuf = malloc(cwdlen);
  830. }
  831. if(!cwdbuf)
  832. DirectorySearch(".", ext, &results);
  833. else
  834. {
  835. DirectorySearch(cwdbuf, ext, &results);
  836. free(cwdbuf);
  837. cwdbuf = NULL;
  838. }
  839. }
  840. // Search local data dir
  841. if((str=getenv("XDG_DATA_HOME")) != NULL && str[0] != '\0')
  842. {
  843. alstr_copy_cstr(&path, str);
  844. if(VECTOR_BACK(path) != '/')
  845. alstr_append_char(&path, '/');
  846. alstr_append_cstr(&path, subdir);
  847. DirectorySearch(alstr_get_cstr(path), ext, &results);
  848. }
  849. else if((str=getenv("HOME")) != NULL && str[0] != '\0')
  850. {
  851. alstr_copy_cstr(&path, str);
  852. if(VECTOR_BACK(path) == '/')
  853. {
  854. VECTOR_POP_BACK(path);
  855. *VECTOR_END(path) = 0;
  856. }
  857. alstr_append_cstr(&path, "/.local/share/");
  858. alstr_append_cstr(&path, subdir);
  859. DirectorySearch(alstr_get_cstr(path), ext, &results);
  860. }
  861. // Search global data dirs
  862. if((str=getenv("XDG_DATA_DIRS")) == NULL || str[0] == '\0')
  863. str = "/usr/local/share/:/usr/share/";
  864. next = str;
  865. while((str=next) != NULL && str[0] != '\0')
  866. {
  867. next = strchr(str, ':');
  868. if(!next)
  869. alstr_copy_cstr(&path, str);
  870. else
  871. {
  872. alstr_copy_range(&path, str, next);
  873. ++next;
  874. }
  875. if(!alstr_empty(path))
  876. {
  877. if(VECTOR_BACK(path) != '/')
  878. alstr_append_char(&path, '/');
  879. alstr_append_cstr(&path, subdir);
  880. DirectorySearch(alstr_get_cstr(path), ext, &results);
  881. }
  882. }
  883. alstr_reset(&path);
  884. }
  885. ATOMIC_STORE_SEQ(&search_lock, 0);
  886. return results;
  887. }
  888. struct FileMapping MapFileToMem(const char *fname)
  889. {
  890. struct FileMapping ret = { -1, NULL, 0 };
  891. struct stat sbuf;
  892. void *ptr;
  893. int fd;
  894. fd = open(fname, O_RDONLY, 0);
  895. if(fd == -1)
  896. {
  897. ERR("Failed to open %s: (%d) %s\n", fname, errno, strerror(errno));
  898. return ret;
  899. }
  900. if(fstat(fd, &sbuf) == -1)
  901. {
  902. ERR("Failed to stat %s: (%d) %s\n", fname, errno, strerror(errno));
  903. close(fd);
  904. return ret;
  905. }
  906. ptr = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  907. if(ptr == MAP_FAILED)
  908. {
  909. ERR("Failed to map %s: (%d) %s\n", fname, errno, strerror(errno));
  910. close(fd);
  911. return ret;
  912. }
  913. ret.fd = fd;
  914. ret.ptr = ptr;
  915. ret.len = sbuf.st_size;
  916. return ret;
  917. }
  918. void UnmapFileMem(const struct FileMapping *mapping)
  919. {
  920. munmap(mapping->ptr, mapping->len);
  921. close(mapping->fd);
  922. }
  923. #endif
  924. void SetRTPriority(void)
  925. {
  926. ALboolean failed = AL_FALSE;
  927. #ifdef _WIN32
  928. if(RTPrioLevel > 0)
  929. failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
  930. #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
  931. if(RTPrioLevel > 0)
  932. {
  933. struct sched_param param;
  934. /* Use the minimum real-time priority possible for now (on Linux this
  935. * should be 1 for SCHED_RR) */
  936. param.sched_priority = sched_get_priority_min(SCHED_RR);
  937. failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
  938. }
  939. #else
  940. /* Real-time priority not available */
  941. failed = (RTPrioLevel>0);
  942. #endif
  943. if(failed)
  944. ERR("Failed to set priority level for thread\n");
  945. }
  946. extern inline void alstr_reset(al_string *str);
  947. extern inline size_t alstr_length(const_al_string str);
  948. extern inline ALboolean alstr_empty(const_al_string str);
  949. extern inline const al_string_char_type *alstr_get_cstr(const_al_string str);
  950. void alstr_clear(al_string *str)
  951. {
  952. if(!alstr_empty(*str))
  953. {
  954. /* Reserve one more character than the total size of the string. This
  955. * is to ensure we have space to add a null terminator in the string
  956. * data so it can be used as a C-style string.
  957. */
  958. VECTOR_RESIZE(*str, 0, 1);
  959. VECTOR_ELEM(*str, 0) = 0;
  960. }
  961. }
  962. static inline int alstr_compare(const al_string_char_type *str1, size_t str1len,
  963. const al_string_char_type *str2, size_t str2len)
  964. {
  965. size_t complen = (str1len < str2len) ? str1len : str2len;
  966. int ret = memcmp(str1, str2, complen);
  967. if(ret == 0)
  968. {
  969. if(str1len > str2len) return 1;
  970. if(str1len < str2len) return -1;
  971. }
  972. return ret;
  973. }
  974. int alstr_cmp(const_al_string str1, const_al_string str2)
  975. {
  976. return alstr_compare(&VECTOR_FRONT(str1), alstr_length(str1),
  977. &VECTOR_FRONT(str2), alstr_length(str2));
  978. }
  979. int alstr_cmp_cstr(const_al_string str1, const al_string_char_type *str2)
  980. {
  981. return alstr_compare(&VECTOR_FRONT(str1), alstr_length(str1),
  982. str2, strlen(str2));
  983. }
  984. void alstr_copy(al_string *str, const_al_string from)
  985. {
  986. size_t len = alstr_length(from);
  987. size_t i;
  988. VECTOR_RESIZE(*str, len, len+1);
  989. for(i = 0;i < len;i++)
  990. VECTOR_ELEM(*str, i) = VECTOR_ELEM(from, i);
  991. VECTOR_ELEM(*str, i) = 0;
  992. }
  993. void alstr_copy_cstr(al_string *str, const al_string_char_type *from)
  994. {
  995. size_t len = strlen(from);
  996. size_t i;
  997. VECTOR_RESIZE(*str, len, len+1);
  998. for(i = 0;i < len;i++)
  999. VECTOR_ELEM(*str, i) = from[i];
  1000. VECTOR_ELEM(*str, i) = 0;
  1001. }
  1002. void alstr_copy_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
  1003. {
  1004. size_t len = to - from;
  1005. size_t i;
  1006. VECTOR_RESIZE(*str, len, len+1);
  1007. for(i = 0;i < len;i++)
  1008. VECTOR_ELEM(*str, i) = from[i];
  1009. VECTOR_ELEM(*str, i) = 0;
  1010. }
  1011. void alstr_append_char(al_string *str, const al_string_char_type c)
  1012. {
  1013. size_t len = alstr_length(*str);
  1014. VECTOR_RESIZE(*str, len, len+2);
  1015. VECTOR_PUSH_BACK(*str, c);
  1016. VECTOR_ELEM(*str, len+1) = 0;
  1017. }
  1018. void alstr_append_cstr(al_string *str, const al_string_char_type *from)
  1019. {
  1020. size_t len = strlen(from);
  1021. if(len != 0)
  1022. {
  1023. size_t base = alstr_length(*str);
  1024. size_t i;
  1025. VECTOR_RESIZE(*str, base+len, base+len+1);
  1026. for(i = 0;i < len;i++)
  1027. VECTOR_ELEM(*str, base+i) = from[i];
  1028. VECTOR_ELEM(*str, base+i) = 0;
  1029. }
  1030. }
  1031. void alstr_append_range(al_string *str, const al_string_char_type *from, const al_string_char_type *to)
  1032. {
  1033. size_t len = to - from;
  1034. if(len != 0)
  1035. {
  1036. size_t base = alstr_length(*str);
  1037. size_t i;
  1038. VECTOR_RESIZE(*str, base+len, base+len+1);
  1039. for(i = 0;i < len;i++)
  1040. VECTOR_ELEM(*str, base+i) = from[i];
  1041. VECTOR_ELEM(*str, base+i) = 0;
  1042. }
  1043. }
  1044. #ifdef _WIN32
  1045. void alstr_copy_wcstr(al_string *str, const wchar_t *from)
  1046. {
  1047. int len;
  1048. if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
  1049. {
  1050. VECTOR_RESIZE(*str, len-1, len);
  1051. WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_FRONT(*str), len, NULL, NULL);
  1052. VECTOR_ELEM(*str, len-1) = 0;
  1053. }
  1054. }
  1055. void alstr_append_wcstr(al_string *str, const wchar_t *from)
  1056. {
  1057. int len;
  1058. if((len=WideCharToMultiByte(CP_UTF8, 0, from, -1, NULL, 0, NULL, NULL)) > 0)
  1059. {
  1060. size_t base = alstr_length(*str);
  1061. VECTOR_RESIZE(*str, base+len-1, base+len);
  1062. WideCharToMultiByte(CP_UTF8, 0, from, -1, &VECTOR_ELEM(*str, base), len, NULL, NULL);
  1063. VECTOR_ELEM(*str, base+len-1) = 0;
  1064. }
  1065. }
  1066. void alstr_copy_wrange(al_string *str, const wchar_t *from, const wchar_t *to)
  1067. {
  1068. int len;
  1069. if((len=WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), NULL, 0, NULL, NULL)) > 0)
  1070. {
  1071. VECTOR_RESIZE(*str, len, len+1);
  1072. WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), &VECTOR_FRONT(*str), len+1, NULL, NULL);
  1073. VECTOR_ELEM(*str, len) = 0;
  1074. }
  1075. }
  1076. void alstr_append_wrange(al_string *str, const wchar_t *from, const wchar_t *to)
  1077. {
  1078. int len;
  1079. if((len=WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), NULL, 0, NULL, NULL)) > 0)
  1080. {
  1081. size_t base = alstr_length(*str);
  1082. VECTOR_RESIZE(*str, base+len, base+len+1);
  1083. WideCharToMultiByte(CP_UTF8, 0, from, (int)(to-from), &VECTOR_ELEM(*str, base), len+1, NULL, NULL);
  1084. VECTOR_ELEM(*str, base+len) = 0;
  1085. }
  1086. }
  1087. #endif