util.h 6.0 KB

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