util.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #ifndef UTIL_H
  23. #define UTIL_H
  24. #include "common.h"
  25. PAL_C_LINKAGE_BEGIN
  26. void
  27. UTIL_MsgBox(
  28. char *string
  29. );
  30. long
  31. flength(
  32. FILE *fp
  33. );
  34. void
  35. trim(
  36. char *str
  37. );
  38. char *
  39. UTIL_GlobalBuffer(
  40. int index
  41. );
  42. /*++
  43. Purpose:
  44. Does a varargs printf into the user-supplied buffer,
  45. so we don't need to have varargs versions of all text functions.
  46. Parameters:
  47. buffer - user-supplied buffer.
  48. buflen - size of the buffer, including null-terminator.
  49. format - the format string.
  50. Return value:
  51. The value of buffer if buffer is non-NULL and buflen > 0, otherwise NULL.
  52. --*/
  53. char *
  54. UTIL_va(
  55. char *buffer,
  56. int buflen,
  57. const char *format,
  58. ...
  59. );
  60. #define PAL_va(i, fmt, ...) UTIL_va(UTIL_GlobalBuffer(i), PAL_GLOBAL_BUFFER_SIZE, fmt, __VA_ARGS__)
  61. int
  62. RandomLong(
  63. int from,
  64. int to
  65. );
  66. float
  67. RandomFloat(
  68. float from,
  69. float to
  70. );
  71. void
  72. UTIL_Delay(
  73. unsigned int ms
  74. );
  75. void
  76. TerminateOnError(
  77. const char *fmt,
  78. ...
  79. );
  80. void *
  81. UTIL_malloc(
  82. size_t buffer_size
  83. );
  84. void *
  85. UTIL_calloc(
  86. size_t n,
  87. size_t size
  88. );
  89. FILE *
  90. UTIL_OpenRequiredFile(
  91. LPCSTR lpszFileName
  92. );
  93. FILE *
  94. UTIL_OpenRequiredFileForMode(
  95. LPCSTR lpszFileName,
  96. LPCSTR szMode
  97. );
  98. FILE *
  99. UTIL_OpenFile(
  100. LPCSTR lpszFileName
  101. );
  102. FILE *
  103. UTIL_OpenFileForMode(
  104. LPCSTR lpszFileName,
  105. LPCSTR szMode
  106. );
  107. VOID
  108. UTIL_CloseFile(
  109. FILE *fp
  110. );
  111. /*++
  112. Purpose:
  113. Combine the 'dir' and 'file' part into a single path string.
  114. If 'dir' is non-NULL, then it ensures that the output string contains
  115. '/' between 'dir' and 'file' (no matter whether 'file' is NULL or not).
  116. Parameters:
  117. buffer - user-supplied buffer.
  118. buflen - size of the buffer, including null-terminator.
  119. dir - the directory path.
  120. file - the file path.
  121. Return value:
  122. The value of buffer if buffer is non-NULL and buflen > 0, otherwise NULL.
  123. --*/
  124. const char *
  125. UTIL_CombinePath(
  126. char *buffer,
  127. int buflen,
  128. int numentry,
  129. ...
  130. );
  131. #define PAL_CombinePath(i, d, f) UTIL_CombinePath(UTIL_GlobalBuffer(i), PAL_GLOBAL_BUFFER_SIZE, 2, (d), (f))
  132. /*
  133. * Platform-specific utilities
  134. */
  135. BOOL
  136. UTIL_GetScreenSize(
  137. DWORD *pdwScreenWidth,
  138. DWORD *pdwScreenHeight
  139. );
  140. BOOL
  141. UTIL_IsAbsolutePath(
  142. const char *lpszFileName
  143. );
  144. int
  145. UTIL_Platform_Init(
  146. int argc,
  147. char *argv[]
  148. );
  149. void
  150. UTIL_Platform_Quit(
  151. void
  152. );
  153. /*
  154. * Logging utilities
  155. */
  156. /*++
  157. Purpose:
  158. The pointer to callback function that produces actual log output.
  159. Parameters:
  160. [IN] level - The log level of this output call.
  161. [IN] full_log - The full log string produced by UTIL_LogOutput.
  162. [IN] user_log - The log string produced by user-provided format.
  163. Return value:
  164. None.
  165. --*/
  166. typedef void(*LOGCALLBACK)(LOGLEVEL level, const char *full_log, const char *user_log);
  167. /*++
  168. Purpose:
  169. Adds a log output callback.
  170. Parameters:
  171. [IN] callback - The callback function to be added. Once added,
  172. it will be called by UTIL_LogOutput.
  173. [IN] loglevel - The minimal log level that the callback should
  174. be called. Any log whose level below this will
  175. be ignored by the callback.
  176. Return value:
  177. The slot id (>= 0), -1 if all slots are used or callback is NULL.
  178. --*/
  179. int
  180. UTIL_LogAddOutputCallback(
  181. LOGCALLBACK callback,
  182. LOGLEVEL loglevel
  183. );
  184. /*++
  185. Purpose:
  186. Removes a log output callback.
  187. Parameters:
  188. [IN] id - The id of callback function to be removed.
  189. Return value:
  190. None
  191. --*/
  192. void
  193. UTIL_LogRemoveOutputCallback(
  194. int id
  195. );
  196. /*++
  197. Purpose:
  198. Set the minimal log level that could be output.
  199. Any level below this level will produce no output.
  200. Parameters:
  201. [IN] minlevel - The minimal log level, must be within the
  202. range [LOGLEVEL_MIN, LOGLEVEL_MAX].
  203. Return value:
  204. None.
  205. --*/
  206. void
  207. UTIL_LogOutput(
  208. LOGLEVEL level,
  209. const char *fmt,
  210. ...
  211. );
  212. /*++
  213. Purpose:
  214. Set the minimal log level that could be output.
  215. Any level below this level will produce no output.
  216. Parameters:
  217. [IN] minlevel - The minimal log level, must be within the
  218. range [LOGLEVEL_MIN, LOGLEVEL_MAX].
  219. Return value:
  220. None.
  221. --*/
  222. void
  223. UTIL_LogSetLevel(
  224. LOGLEVEL minlevel
  225. );
  226. void
  227. UTIL_LogToFile(
  228. LOGLEVEL _,
  229. const char *string,
  230. const char *__
  231. );
  232. PAL_C_LINKAGE_END
  233. #endif