util.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // Copyright (c) 2011-2017, SDLPAL development team.
  5. // All rights reserved.
  6. //
  7. // This file is part of SDLPAL.
  8. //
  9. // SDLPAL is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation, either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. //
  22. #include "util.h"
  23. #include "input.h"
  24. #include "global.h"
  25. #include "palcfg.h"
  26. #include <errno.h>
  27. #include "midi.h"
  28. #if SDL_VERSION_ATLEAST(2, 0, 0)
  29. #include "SDL_video.h"
  30. #include "SDL_messagebox.h"
  31. #endif
  32. static char internal_buffer[PAL_MAX_GLOBAL_BUFFERS + 1][PAL_GLOBAL_BUFFER_SIZE];
  33. #define INTERNAL_BUFFER_SIZE_ARGS internal_buffer[PAL_MAX_GLOBAL_BUFFERS], PAL_GLOBAL_BUFFER_SIZE
  34. void UTIL_MsgBox(char *string)
  35. {
  36. #if SDL_VERSION_ATLEAST(2, 0, 0)
  37. extern SDL_Window *gpWindow;
  38. char buffer[300];
  39. SDL_MessageBoxButtonData buttons[] = { { 0, 0, "OK" } };
  40. SDL_MessageBoxData mbd = { SDL_MESSAGEBOX_WARNING, gpWindow, "Alert",buffer, 1, buttons, NULL };
  41. int btnid;
  42. sprintf(buffer, "%s\n", string);
  43. SDL_ShowMessageBox(&mbd, &btnid);
  44. #endif
  45. }
  46. long
  47. flength(
  48. FILE *fp
  49. )
  50. {
  51. long old_pos = ftell(fp), length;
  52. if (old_pos == -1) return -1;
  53. if (fseek(fp, 0, SEEK_END) == -1) return -1;
  54. length = ftell(fp); fseek(fp, old_pos, SEEK_SET);
  55. return length;
  56. }
  57. void
  58. trim(
  59. char *str
  60. )
  61. /*++
  62. Purpose:
  63. Remove the leading and trailing spaces in a string.
  64. Parameters:
  65. str - the string to proceed.
  66. Return value:
  67. None.
  68. --*/
  69. {
  70. int pos = 0;
  71. char *dest = str;
  72. //
  73. // skip leading blanks
  74. //
  75. while (str[pos] <= ' ' && str[pos] > 0)
  76. pos++;
  77. while (str[pos])
  78. {
  79. *(dest++) = str[pos];
  80. pos++;
  81. }
  82. *(dest--) = '\0'; // store the null
  83. //
  84. // remove trailing blanks
  85. //
  86. while (dest >= str && *dest <= ' ' && *dest > 0)
  87. *(dest--) = '\0';
  88. }
  89. char *
  90. UTIL_va(
  91. char *buffer,
  92. int buflen,
  93. const char *format,
  94. ...
  95. )
  96. {
  97. if (buflen > 0 && buffer)
  98. {
  99. va_list argptr;
  100. va_start(argptr, format);
  101. vsnprintf(buffer, buflen, format, argptr);
  102. va_end(argptr);
  103. return buffer;
  104. }
  105. else
  106. {
  107. return NULL;
  108. }
  109. }
  110. /*
  111. * RNG code based on RACC by Pierre-Marie Baty.
  112. * http://racc.bots-united.com
  113. *
  114. * Copyright (c) 2004, Pierre-Marie Baty
  115. * All rights reserved.
  116. *
  117. * Redistribution and use in source and binary forms, with or
  118. * without modification, are permitted provided that the following
  119. * conditions are met:
  120. *
  121. * Redistributions of source code must retain the above copyright
  122. * notice, this list of conditions and the following disclaimer.
  123. *
  124. * Redistributions in binary form must reproduce the above copyright
  125. * notice, this list of conditions and the following disclaimer in
  126. * the documentation and/or other materials provided with the
  127. * distribution.
  128. *
  129. * Neither the name of the RACC nor the names of its contributors
  130. * may be used to endorse or promote products derived from this
  131. * software without specific prior written permission.
  132. *
  133. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  134. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  135. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  136. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  137. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  138. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  139. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  140. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  141. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  142. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  143. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  144. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  145. * POSSIBILITY OF SUCH DAMAGE.
  146. */
  147. //
  148. // Our random number generator's seed.
  149. //
  150. static int glSeed = 0;
  151. static void
  152. lsrand(
  153. unsigned int iInitialSeed
  154. )
  155. /*++
  156. Purpose:
  157. This function initializes the random seed based on the initial seed value passed in the
  158. iInitialSeed parameter.
  159. Parameters:
  160. [IN] iInitialSeed - The initial random seed.
  161. Return value:
  162. None.
  163. --*/
  164. {
  165. //
  166. // fill in the initial seed of the random number generator
  167. //
  168. glSeed = 1664525L * iInitialSeed + 1013904223L;
  169. }
  170. static int
  171. lrand(
  172. void
  173. )
  174. /*++
  175. Purpose:
  176. This function is the equivalent of the rand() standard C library function, except that
  177. whereas rand() works only with short integers (i.e. not above 32767), this function is
  178. able to generate 32-bit random numbers.
  179. Parameters:
  180. None.
  181. Return value:
  182. The generated random number.
  183. --*/
  184. {
  185. if (glSeed == 0) // if the random seed isn't initialized...
  186. lsrand((unsigned int)time(NULL)); // initialize it first
  187. glSeed = 1664525L * glSeed + 1013904223L; // do some twisted math (infinite suite)
  188. return ((glSeed >> 1) + 1073741824L); // and return the result.
  189. }
  190. int
  191. RandomLong(
  192. int from,
  193. int to
  194. )
  195. /*++
  196. Purpose:
  197. This function returns a random integer number between (and including) the starting and
  198. ending values passed by parameters from and to.
  199. Parameters:
  200. from - the starting value.
  201. to - the ending value.
  202. Return value:
  203. The generated random number.
  204. --*/
  205. {
  206. if (to <= from)
  207. return from;
  208. return from + lrand() / (INT_MAX / (to - from + 1));
  209. }
  210. float
  211. RandomFloat(
  212. float from,
  213. float to
  214. )
  215. /*++
  216. Purpose:
  217. This function returns a random floating-point number between (and including) the starting
  218. and ending values passed by parameters from and to.
  219. Parameters:
  220. from - the starting value.
  221. to - the ending value.
  222. Return value:
  223. The generated random number.
  224. --*/
  225. {
  226. if (to <= from)
  227. return from;
  228. return from + (float)lrand() / (INT_MAX / (to - from));
  229. }
  230. void
  231. UTIL_Delay(
  232. unsigned int ms
  233. )
  234. {
  235. unsigned int t = SDL_GetTicks() + ms;
  236. PAL_ProcessEvent();
  237. while (!SDL_TICKS_PASSED(SDL_GetTicks(), t))
  238. {
  239. SDL_Delay(1);
  240. PAL_ProcessEvent();
  241. }
  242. }
  243. void
  244. TerminateOnError(
  245. const char *fmt,
  246. ...
  247. )
  248. // This function terminates the game because of an error and
  249. // prints the message string pointed to by fmt both in the
  250. // console and in a messagebox.
  251. {
  252. va_list argptr;
  253. char string[256];
  254. extern VOID PAL_Shutdown(int);
  255. // concatenate all the arguments in one string
  256. va_start(argptr, fmt);
  257. vsnprintf(string, sizeof(string), fmt, argptr);
  258. va_end(argptr);
  259. fprintf(stderr, "\nFATAL ERROR: %s\n", string);
  260. #if SDL_VERSION_ATLEAST(2, 0, 0)
  261. {
  262. extern SDL_Window *gpWindow;
  263. char buffer[300];
  264. SDL_MessageBoxButtonData buttons[2] = { { 0, 0, "Yes" },{ 0, 1, "No" } };
  265. SDL_MessageBoxData mbd = { SDL_MESSAGEBOX_ERROR, gpWindow, "FATAL ERROR", buffer, 2, buttons, NULL };
  266. int btnid;
  267. #if PAL_HAS_CONFIG_PAGE
  268. sprintf(buffer, "%sLaunch setting dialog on next start?\n", string);
  269. if (SDL_ShowMessageBox(&mbd, &btnid) == 0 && btnid == 0)
  270. {
  271. gConfig.fLaunchSetting = TRUE;
  272. PAL_SaveConfig();
  273. }
  274. #else
  275. sprintf(buffer, "%s\n", string);
  276. mbd.numbuttons=1;
  277. SDL_ShowMessageBox(&mbd, &btnid);
  278. #endif
  279. PAL_Shutdown(255);
  280. }
  281. #else
  282. PAL_FATAL_OUTPUT(string);
  283. #endif
  284. #ifdef _DEBUG
  285. assert(!"TerminateOnError()"); // allows jumping to debugger
  286. #endif
  287. PAL_Shutdown(255);
  288. }
  289. void *
  290. UTIL_malloc(
  291. size_t buffer_size
  292. )
  293. {
  294. // handy wrapper for operations we always forget, like checking malloc's returned pointer.
  295. void *buffer;
  296. // first off, check if buffer size is valid
  297. if (buffer_size == 0)
  298. TerminateOnError("UTIL_malloc() called with invalid buffer size: %d\n", buffer_size);
  299. buffer = malloc(buffer_size); // allocate real memory space
  300. // last check, check if malloc call succeeded
  301. if (buffer == NULL)
  302. TerminateOnError("UTIL_malloc() failure for %d bytes (out of memory?)\n", buffer_size);
  303. return buffer; // nothing went wrong, so return buffer pointer
  304. }
  305. void *
  306. UTIL_calloc(
  307. size_t n,
  308. size_t size
  309. )
  310. {
  311. // handy wrapper for operations we always forget, like checking calloc's returned pointer.
  312. void *buffer;
  313. // first off, check if buffer size is valid
  314. if (n == 0 || size == 0)
  315. TerminateOnError ("UTIL_calloc() called with invalid parameters\n");
  316. buffer = calloc(n, size); // allocate real memory space
  317. // last check, check if malloc call succeeded
  318. if (buffer == NULL)
  319. TerminateOnError("UTIL_calloc() failure for %d bytes (out of memory?)\n", size * n);
  320. return buffer; // nothing went wrong, so return buffer pointer
  321. }
  322. FILE *
  323. UTIL_OpenRequiredFile(
  324. LPCSTR lpszFileName
  325. )
  326. /*++
  327. Purpose:
  328. Open a required file. If fails, quit the program.
  329. Parameters:
  330. [IN] lpszFileName - file name to open.
  331. Return value:
  332. Pointer to the file.
  333. --*/
  334. {
  335. return UTIL_OpenRequiredFileForMode(lpszFileName, "rb");
  336. }
  337. FILE *
  338. UTIL_OpenRequiredFileForMode(
  339. LPCSTR lpszFileName,
  340. LPCSTR szMode
  341. )
  342. /*++
  343. Purpose:
  344. Open a required file. If fails, quit the program.
  345. Parameters:
  346. [IN] lpszFileName - file name to open.
  347. [IN] szMode - file open mode.
  348. Return value:
  349. Pointer to the file.
  350. --*/
  351. {
  352. FILE *fp = UTIL_OpenFileForMode(lpszFileName, szMode);
  353. if (fp == NULL)
  354. {
  355. TerminateOnError("File open error(%d): %s!\n", errno, lpszFileName);
  356. }
  357. return fp;
  358. }
  359. FILE *
  360. UTIL_OpenFile(
  361. LPCSTR lpszFileName
  362. )
  363. /*++
  364. Purpose:
  365. Open a file. If fails, return NULL.
  366. Parameters:
  367. [IN] lpszFileName - file name to open.
  368. Return value:
  369. Pointer to the file.
  370. --*/
  371. {
  372. return UTIL_OpenFileForMode(lpszFileName, "rb");
  373. }
  374. FILE *
  375. UTIL_OpenFileForMode(
  376. LPCSTR lpszFileName,
  377. LPCSTR szMode
  378. )
  379. /*++
  380. Purpose:
  381. Open a file. If fails, return NULL.
  382. Parameters:
  383. [IN] lpszFileName - file name to open.
  384. [IN] szMode - file open mode.
  385. Return value:
  386. Pointer to the file.
  387. --*/
  388. {
  389. //
  390. // If lpszFileName is an absolute path, use its last element as filename
  391. //
  392. if (UTIL_IsAbsolutePath(lpszFileName))
  393. {
  394. char *temp = strdup(lpszFileName), *filename = temp;
  395. FILE *fp = NULL;
  396. for (char *next = strpbrk(filename, PAL_PATH_SEPARATORS); next; next = strpbrk(filename = next + 1, PAL_PATH_SEPARATORS));
  397. if (*filename)
  398. {
  399. filename[-1] = '\0';
  400. fp = UTIL_OpenFileAtPathForMode(*temp ? temp : "/", filename, szMode);
  401. }
  402. free(temp);
  403. return fp;
  404. }
  405. return UTIL_OpenFileAtPathForMode(gConfig.pszGamePath, lpszFileName, szMode);
  406. }
  407. FILE *
  408. UTIL_OpenFileAtPath(
  409. LPCSTR lpszPath,
  410. LPCSTR lpszFileName
  411. )
  412. {
  413. return UTIL_OpenFileAtPathForMode(lpszPath, lpszFileName, "rb");
  414. }
  415. FILE *
  416. UTIL_OpenFileAtPathForMode(
  417. LPCSTR lpszPath,
  418. LPCSTR lpszFileName,
  419. LPCSTR szMode
  420. )
  421. {
  422. if (!lpszPath || !lpszFileName || !szMode) return NULL;
  423. //
  424. // Construct full path according to lpszPath and lpszFileName
  425. //
  426. const char *path = UTIL_GetFullPathName(INTERNAL_BUFFER_SIZE_ARGS, lpszPath, lpszFileName);
  427. //
  428. // If no matching path, check the open mode
  429. //
  430. if (path)
  431. {
  432. return fopen(path, szMode);
  433. }
  434. else if (szMode[0] != 'r')
  435. {
  436. return fopen(UTIL_CombinePath(INTERNAL_BUFFER_SIZE_ARGS, 2, lpszPath, lpszFileName), szMode);
  437. }
  438. else
  439. {
  440. return NULL;
  441. }
  442. }
  443. VOID
  444. UTIL_CloseFile(
  445. FILE *fp
  446. )
  447. /*++
  448. Purpose:
  449. Close a file.
  450. Parameters:
  451. [IN] fp - file handle to be closed.
  452. Return value:
  453. None.
  454. --*/
  455. {
  456. if (fp != NULL)
  457. {
  458. fclose(fp);
  459. }
  460. }
  461. const char *
  462. UTIL_GetFullPathName(
  463. char *buffer,
  464. size_t buflen,
  465. const char *basepath,
  466. const char *subpath
  467. )
  468. {
  469. if (!buffer || !basepath || !subpath || buflen == 0) return NULL;
  470. int baselen = strlen(basepath), sublen = strlen(subpath);
  471. if (sublen == 0) return NULL;
  472. char *_base = strdup(basepath), *_sub = strdup(subpath);
  473. const char *result = NULL;
  474. if (access(UTIL_CombinePath(INTERNAL_BUFFER_SIZE_ARGS, 2, _base, _sub), 0) == 0)
  475. {
  476. result = internal_buffer[PAL_MAX_GLOBAL_BUFFERS];
  477. }
  478. #if !defined(PAL_FILESYSTEM_IGNORE_CASE) || !PAL_FILESYSTEM_IGNORE_CASE
  479. if (result == NULL)
  480. {
  481. size_t pos = strspn(_sub, PAL_PATH_SEPARATORS);
  482. if (pos < sublen)
  483. {
  484. char *start = _sub + pos;
  485. char *end = strpbrk(start, PAL_PATH_SEPARATORS);
  486. if (end) *end = '\0';
  487. //
  488. // try to find the matching file in the directory.
  489. //
  490. struct dirent **list;
  491. int n = scandir(_base, &list, 0, alphasort);
  492. while (n-- > 0)
  493. {
  494. if (!result && strcasecmp(list[n]->d_name, start) == 0)
  495. {
  496. result = UTIL_CombinePath(INTERNAL_BUFFER_SIZE_ARGS, 2, _base, list[n]->d_name);
  497. if (end)
  498. result = UTIL_GetFullPathName(INTERNAL_BUFFER_SIZE_ARGS, result, end + 1);
  499. else if (access(result, 0) != 0)
  500. result = NULL;
  501. }
  502. free(list[n]);
  503. }
  504. free(list);
  505. }
  506. }
  507. #endif
  508. if (result != NULL)
  509. {
  510. size_t dstlen = min(buflen - 1, strlen(result));
  511. result = (char *)memmove(buffer, result, dstlen);
  512. buffer[dstlen] = '\0';
  513. }
  514. free(_base);
  515. free(_sub);
  516. return result;
  517. }
  518. const char *
  519. UTIL_CombinePath(
  520. char *buffer,
  521. size_t buflen,
  522. int numentry,
  523. ...
  524. )
  525. {
  526. if (buffer && buflen > 0 && numentry > 0)
  527. {
  528. const char *retval = buffer;
  529. va_list argptr;
  530. va_start(argptr, numentry);
  531. for (int i = 0; i < numentry && buflen > 1; i++)
  532. {
  533. const char *path = va_arg(argptr, const char *);
  534. int path_len = path ? strlen(path) : 0;
  535. int append_delim = (i < numentry - 1 && path_len > 0 && !PAL_IS_PATH_SEPARATOR(path[path_len - 1]));
  536. for (int is_sep = 0, j = 0; j < path_len && buflen > (size_t)append_delim + 1; j++)
  537. {
  538. //
  539. // Skip continuous path separators
  540. //
  541. if (PAL_IS_PATH_SEPARATOR(path[j]))
  542. {
  543. if (is_sep)
  544. continue;
  545. else
  546. is_sep = 1;
  547. }
  548. else
  549. {
  550. is_sep = 0;
  551. }
  552. *buffer++ = path[j];
  553. buflen--;
  554. }
  555. //
  556. // Make sure a path delimeter is append to the destination if this is not the last entry
  557. //
  558. if (append_delim)
  559. {
  560. *buffer++ = PAL_PATH_SEPARATORS[0];
  561. buflen--;
  562. }
  563. }
  564. va_end(argptr);
  565. *buffer = '\0';
  566. return retval;
  567. }
  568. else
  569. {
  570. return NULL;
  571. }
  572. }
  573. char *
  574. UTIL_GlobalBuffer(
  575. int index
  576. )
  577. {
  578. return (index >= 0 && index < PAL_MAX_GLOBAL_BUFFERS) ? internal_buffer[index] : NULL;
  579. }
  580. PALFILE
  581. UTIL_CheckResourceFiles(
  582. const char *path,
  583. const char *msgfile
  584. )
  585. {
  586. const char *common_files[] = {
  587. "abc.mkf", "ball.mkf", "data.mkf", "f.mkf",
  588. "fbp.mkf", "fire.mkf", "gop.mkf", "map.mkf",
  589. "mgo.mkf", "pat.mkf", "rgm.mkf", "rng.mkf",
  590. "sss.mkf"
  591. };
  592. const char *msg_files[][2] = {
  593. { msgfile, "m.msg" },
  594. { msgfile, "word.dat" }
  595. };
  596. const char *sound_files[2] = { "voc.mkf", "sounds.mkf" };
  597. const char *music_files[2] = { "midi.mkf", "mus.mkf" };
  598. int msgidx = !(msgfile && *msgfile);
  599. PALFILE retval = (PALFILE)0;
  600. for (int i = 0; i < sizeof(common_files) / sizeof(common_files[0]); i++)
  601. {
  602. if (!UTIL_GetFullPathName(INTERNAL_BUFFER_SIZE_ARGS, path, common_files[i]))
  603. {
  604. retval |= (PALFILE)(1 << i);
  605. }
  606. }
  607. for (int i = 0; i < sizeof(msg_files[0]) / sizeof(msg_files[0][0]); i++)
  608. {
  609. if (!UTIL_GetFullPathName(INTERNAL_BUFFER_SIZE_ARGS, path, msg_files[i][msgidx]))
  610. {
  611. retval |= (PALFILE)(1 << ((i + 1) * msgidx + 13));
  612. }
  613. }
  614. for (int i = 0; i < sizeof(sound_files) / sizeof(sound_files[0]); i++)
  615. {
  616. if (!UTIL_GetFullPathName(INTERNAL_BUFFER_SIZE_ARGS, path, sound_files[i]))
  617. {
  618. retval |= (PALFILE)(1 << (i + 16));
  619. }
  620. }
  621. for (int i = 0; i < sizeof(music_files) / sizeof(music_files[0]); i++)
  622. {
  623. if (!UTIL_GetFullPathName(INTERNAL_BUFFER_SIZE_ARGS, path, music_files[i]))
  624. {
  625. retval |= (PALFILE)(1 << (i + 18));
  626. }
  627. }
  628. return retval;
  629. }
  630. #if !defined(PAL_HAS_PLATFORM_SPECIFIC_UTILS)
  631. BOOL
  632. UTIL_GetScreenSize(
  633. DWORD *pdwScreenWidth,
  634. DWORD *pdwScreenHeight
  635. )
  636. {
  637. return FALSE;
  638. }
  639. BOOL
  640. UTIL_IsAbsolutePath(
  641. LPCSTR lpszFileName
  642. )
  643. {
  644. return FALSE;
  645. }
  646. INT
  647. UTIL_Platform_Init(
  648. int argc,
  649. char* argv[]
  650. )
  651. {
  652. gConfig.fLaunchSetting = FALSE;
  653. return 0;
  654. }
  655. VOID
  656. UTIL_Platform_Quit(
  657. VOID
  658. )
  659. {
  660. }
  661. #endif
  662. /*
  663. * Logging utilities
  664. */
  665. #ifndef PAL_LOG_BUFFER_SIZE
  666. # define PAL_LOG_BUFFER_SIZE 4096
  667. #endif
  668. #define PAL_LOG_BUFFER_EXTRA_SIZE 32
  669. static LOGCALLBACK _log_callbacks[PAL_LOG_MAX_OUTPUTS];
  670. static LOGLEVEL _log_callback_levels[PAL_LOG_MAX_OUTPUTS];
  671. static char _log_buffer[PAL_LOG_BUFFER_SIZE + PAL_LOG_BUFFER_EXTRA_SIZE];
  672. static const char * const _loglevel_str[] = {
  673. "[VERBOSE]",
  674. " [DEBUG]",
  675. " [INFO]",
  676. "[WARNING]",
  677. " [ERROR]",
  678. " [FATAL]",
  679. };
  680. int
  681. UTIL_LogAddOutputCallback(
  682. LOGCALLBACK callback,
  683. LOGLEVEL loglevel
  684. )
  685. {
  686. if (!callback) return -1;
  687. // De-duplication
  688. for (int i = 0; i < PAL_LOG_MAX_OUTPUTS; i++)
  689. {
  690. if (!_log_callbacks[i])
  691. {
  692. _log_callbacks[i] = callback;
  693. }
  694. if (_log_callbacks[i] == callback)
  695. {
  696. _log_callback_levels[i] = loglevel;
  697. return i;
  698. }
  699. }
  700. return -1;
  701. }
  702. void
  703. UTIL_LogRemoveOutputCallback(
  704. int id
  705. )
  706. {
  707. if (id < 0 || id >= PAL_LOG_MAX_OUTPUTS) return;
  708. while (id < PAL_LOG_MAX_OUTPUTS - 1)
  709. {
  710. _log_callbacks[id] = _log_callbacks[id + 1];
  711. _log_callback_levels[id] = _log_callback_levels[id + 1];
  712. id++;
  713. }
  714. _log_callbacks[id] = NULL;
  715. _log_callback_levels[id] = LOGLEVEL_MIN;
  716. }
  717. void
  718. UTIL_LogOutput(
  719. LOGLEVEL level,
  720. const char *fmt,
  721. ...
  722. )
  723. {
  724. va_list va;
  725. time_t tv = time(NULL);
  726. struct tm *tmval = localtime(&tv);
  727. int id, n;
  728. if (level < gConfig.iLogLevel || !_log_callbacks[0]) return;
  729. if (level > LOGLEVEL_MAX) level = LOGLEVEL_MAX;
  730. snprintf(_log_buffer, PAL_LOG_BUFFER_EXTRA_SIZE,
  731. "%04d-%02d-%02d %02d:%02d:%02d %s: ",
  732. tmval->tm_year + 1900, tmval->tm_mon, tmval->tm_mday,
  733. tmval->tm_hour, tmval->tm_min, tmval->tm_sec,
  734. _loglevel_str[level]);
  735. va_start(va, fmt);
  736. n = vsnprintf(_log_buffer + PAL_LOG_BUFFER_EXTRA_SIZE - 1, PAL_LOG_BUFFER_SIZE, fmt, va);
  737. va_end(va);
  738. n = (n == -1) ? PAL_LOG_BUFFER_EXTRA_SIZE + PAL_LOG_BUFFER_SIZE - 1 : n + PAL_LOG_BUFFER_EXTRA_SIZE;
  739. _log_buffer[n--] = '\0';
  740. if (_log_buffer[n] != '\n') _log_buffer[n] = '\n';
  741. for(id = 0; id < PAL_LOG_MAX_OUTPUTS && _log_callbacks[id]; id++)
  742. {
  743. if (level >= _log_callback_levels[id])
  744. {
  745. _log_callbacks[id](level, _log_buffer, _log_buffer + PAL_LOG_BUFFER_EXTRA_SIZE - 1);
  746. }
  747. }
  748. }
  749. void
  750. UTIL_LogSetLevel(
  751. LOGLEVEL minlevel
  752. )
  753. {
  754. if (minlevel < LOGLEVEL_MIN)
  755. gConfig.iLogLevel = LOGLEVEL_MIN;
  756. else if (minlevel > LOGLEVEL_MAX)
  757. gConfig.iLogLevel = LOGLEVEL_MAX;
  758. else
  759. gConfig.iLogLevel = minlevel;
  760. }
  761. void
  762. UTIL_LogToFile(
  763. LOGLEVEL _,
  764. const char *string,
  765. const char *__
  766. )
  767. {
  768. FILE *fp = fopen(gConfig.pszLogFile, "a");
  769. if (fp)
  770. {
  771. fputs(string, fp);
  772. fclose(fp);
  773. }
  774. }