util.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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_Init(
  182. int argc,
  183. char *argv[]
  184. );
  185. void
  186. UTIL_Platform_Quit(
  187. void
  188. );
  189. /*
  190. * Logging utilities
  191. */
  192. /*++
  193. Purpose:
  194. The pointer to callback function that produces actual log output.
  195. Parameters:
  196. [IN] level - The log level of this output call.
  197. [IN] full_log - The full log string produced by UTIL_LogOutput.
  198. [IN] user_log - The log string produced by user-provided format.
  199. Return value:
  200. None.
  201. --*/
  202. typedef void(*LOGCALLBACK)(LOGLEVEL level, const char *full_log, const char *user_log);
  203. /*++
  204. Purpose:
  205. Adds a log output callback.
  206. Parameters:
  207. [IN] callback - The callback function to be added. Once added,
  208. it will be called by UTIL_LogOutput.
  209. [IN] loglevel - The minimal log level that the callback should
  210. be called. Any log whose level below this will
  211. be ignored by the callback.
  212. Return value:
  213. The slot id (>= 0), -1 if all slots are used or callback is NULL.
  214. --*/
  215. int
  216. UTIL_LogAddOutputCallback(
  217. LOGCALLBACK callback,
  218. LOGLEVEL loglevel
  219. );
  220. /*++
  221. Purpose:
  222. Removes a log output callback.
  223. Parameters:
  224. [IN] id - The id of callback function to be removed.
  225. Return value:
  226. None
  227. --*/
  228. void
  229. UTIL_LogRemoveOutputCallback(
  230. int id
  231. );
  232. /*++
  233. Purpose:
  234. Set the minimal log level that could be output.
  235. Any level below this level will produce no output.
  236. Parameters:
  237. [IN] minlevel - The minimal log level, must be within the
  238. range [LOGLEVEL_MIN, LOGLEVEL_MAX].
  239. Return value:
  240. None.
  241. --*/
  242. void
  243. UTIL_LogOutput(
  244. LOGLEVEL level,
  245. const char *fmt,
  246. ...
  247. );
  248. /*++
  249. Purpose:
  250. Set the minimal log level that could be output.
  251. Any level below this level will produce no output.
  252. Parameters:
  253. [IN] minlevel - The minimal log level, must be within the
  254. range [LOGLEVEL_MIN, LOGLEVEL_MAX].
  255. Return value:
  256. None.
  257. --*/
  258. void
  259. UTIL_LogSetLevel(
  260. LOGLEVEL minlevel
  261. );
  262. void
  263. UTIL_LogToFile(
  264. LOGLEVEL _,
  265. const char *string,
  266. const char *__
  267. );
  268. PAL_C_LINKAGE_END
  269. #endif