util.h 6.2 KB

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