util.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* MegaZeux
  2. *
  3. * Copyright (C) 2008 Alistair John Strachan <alistair@devzero.co.uk>
  4. * Copyright (C) 2004 Gilead Kutnick <exophase@adelphia.net>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. // Useful, generic utility functions
  21. #ifndef __UTIL_H
  22. #define __UTIL_H
  23. #include "compat.h"
  24. __M_BEGIN_DECLS
  25. #include <stdint.h>
  26. #include <stdio.h>
  27. #if !defined(_MSC_VER) && !defined(__amigaos__)
  28. #include <unistd.h>
  29. #endif
  30. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  31. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  32. #define CLAMP(x, low, high) \
  33. (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
  34. #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
  35. #define SGN(x) ((x > 0) - (x < 0))
  36. #ifndef DIR_SEPARATOR
  37. #ifdef __WIN32__
  38. #define DIR_SEPARATOR "\\"
  39. #define DIR_SEPARATOR_CHAR '\\'
  40. #else //!__WIN32__
  41. #define DIR_SEPARATOR "/"
  42. #define DIR_SEPARATOR_CHAR '/'
  43. #endif
  44. #endif //DIR_SEPARATOR
  45. enum resource_id
  46. {
  47. CONFIG_TXT = 0,
  48. MZX_DEFAULT_CHR,
  49. MZX_EDIT_CHR,
  50. SMZX_PAL,
  51. #ifdef CONFIG_EDITOR
  52. MZX_ASCII_CHR,
  53. MZX_BLANK_CHR,
  54. MZX_SMZX_CHR,
  55. #endif
  56. #ifdef CONFIG_HELPSYS
  57. MZX_HELP_FIL,
  58. #endif
  59. #ifdef CONFIG_RENDER_GL_PROGRAM
  60. GLSL_SHADER_SCALER_DIRECTORY,
  61. GLSL_SHADER_SCALER_VERT,
  62. GLSL_SHADER_SCALER_FRAG,
  63. GLSL_SHADER_TILEMAP_VERT,
  64. GLSL_SHADER_TILEMAP_FRAG,
  65. GLSL_SHADER_TILEMAP_SMZX_FRAG,
  66. GLSL_SHADER_MOUSE_VERT,
  67. GLSL_SHADER_MOUSE_FRAG,
  68. GLSL_SHADER_CURSOR_VERT,
  69. GLSL_SHADER_CURSOR_FRAG,
  70. #endif
  71. #ifdef CONFIG_GAMECONTROLLERDB
  72. GAMECONTROLLERDB_TXT,
  73. #endif
  74. END_RESOURCE_ID_T // must be last
  75. };
  76. #ifdef CONFIG_RENDER_GL_PROGRAM
  77. #define GLSL_SHADER_RES_FIRST GLSL_SHADER_SCALER_VERT
  78. #define GLSL_SHADER_RES_LAST GLSL_SHADER_CURSOR_FRAG
  79. #define GLSL_SHADER_RES_COUNT (GLSL_SHADER_RES_LAST - GLSL_SHADER_RES_FIRST + 1)
  80. #endif
  81. CORE_LIBSPEC int mzx_res_init(const char *argv0, boolean editor);
  82. CORE_LIBSPEC void mzx_res_free(void);
  83. CORE_LIBSPEC char *mzx_res_get_by_id(enum resource_id id);
  84. CORE_LIBSPEC boolean redirect_stdio(const char *base_path, boolean require_conf);
  85. // Code to load multi-byte ints from little endian file
  86. int fgetw(FILE *fp);
  87. int fgetd(FILE *fp);
  88. void fputw(int src, FILE *fp);
  89. void fputd(int src, FILE *fp);
  90. CORE_LIBSPEC long ftell_and_rewind(FILE *f);
  91. CORE_LIBSPEC void rng_seed_init(void);
  92. uint64_t rng_get_seed(void);
  93. void rng_set_seed(uint64_t seed);
  94. unsigned int Random(uint64_t range);
  95. CORE_LIBSPEC void add_ext(char *src, const char *ext);
  96. CORE_LIBSPEC int get_ext_pos(const char *filename);
  97. CORE_LIBSPEC ssize_t get_path(const char *file_name, char *dest, unsigned int buf_len);
  98. #ifdef CONFIG_UTILS
  99. ssize_t __get_path(const char *file_name, char *dest, unsigned int buf_len);
  100. #endif
  101. CORE_LIBSPEC void split_path_filename(const char *source,
  102. char *destpath, unsigned int path_buffer_len,
  103. char *destfile, unsigned int file_buffer_len);
  104. CORE_LIBSPEC int create_path_if_not_exists(const char *filename);
  105. CORE_LIBSPEC int change_dir_name(char *path_name, const char *dest);
  106. CORE_LIBSPEC void join_path_names(char* target, int max_len, const char* path1, const char* path2);
  107. CORE_LIBSPEC void clean_path_slashes(const char *source, char *dest, size_t buf_size);
  108. typedef void (*fn_ptr)(void);
  109. struct dso_syms_map
  110. {
  111. const char *name;
  112. fn_ptr *sym_ptr;
  113. };
  114. #include <sys/types.h>
  115. #include <dirent.h>
  116. #define PATH_BUF_LEN MAX_PATH
  117. struct mzx_dir {
  118. #if defined(CONFIG_PSP) || defined(CONFIG_3DS) || defined(CONFIG_SWITCH)
  119. char path[PATH_BUF_LEN];
  120. #endif
  121. DIR *d;
  122. long entries;
  123. long pos;
  124. };
  125. boolean dir_open(struct mzx_dir *dir, const char *path);
  126. void dir_close(struct mzx_dir *dir);
  127. void dir_seek(struct mzx_dir *dir, long offset);
  128. long dir_tell(struct mzx_dir *dir);
  129. boolean dir_get_next_entry(struct mzx_dir *dir, char *entry);
  130. CORE_LIBSPEC void boyer_moore_index(const void *B, const size_t b_len,
  131. int index[256], boolean ignore_case);
  132. CORE_LIBSPEC void *boyer_moore_search(const void *A, const size_t a_len,
  133. const void *B, const size_t b_len, const int index[256], boolean ignore_case);
  134. // Code to load/save multi-byte ints to/from little endian memory
  135. int mem_getc(const unsigned char **ptr);
  136. int mem_getd(const unsigned char **ptr);
  137. int mem_getw(const unsigned char **ptr);
  138. void mem_putc(int src, unsigned char **ptr);
  139. void mem_putd(int src, unsigned char **ptr);
  140. void mem_putw(int src, unsigned char **ptr);
  141. CORE_LIBSPEC int memsafegets(char *dest, int size, char **src, char *end);
  142. #if defined(__WIN32__) && defined(__STRICT_ANSI__)
  143. CORE_LIBSPEC int strcasecmp(const char *s1, const char *s2);
  144. CORE_LIBSPEC int strncasecmp(const char *s1, const char *s2, size_t n);
  145. #endif // __WIN32__ && __STRICT_ANSI__
  146. #if defined(__WIN32__) || defined(__amigaos__)
  147. CORE_LIBSPEC char *strsep(char **stringp, const char *delim);
  148. #endif // __WIN32__ || __amigaos__
  149. #ifndef __WIN32__
  150. #if defined(CONFIG_PSP) || defined(CONFIG_GP2X) \
  151. || defined(CONFIG_NDS) || defined(CONFIG_WII) \
  152. || defined(CONFIG_3DS) || defined(CONFIG_SWITCH)
  153. #include <string.h>
  154. #else
  155. #include <strings.h>
  156. #endif
  157. #endif // !__WIN32__
  158. #if defined(CONFIG_NDS) || defined(CONFIG_WII)
  159. // FIXME: rmdir() needs implementing on NDS/Wii
  160. #define rmdir(x)
  161. #endif
  162. #if defined(__WIN32__) && !defined(_MSC_VER)
  163. #define mkdir(file,mode) mkdir(file)
  164. #endif
  165. #if defined(__amigaos__)
  166. CORE_LIBSPEC extern long __stack_chk_guard[8];
  167. CORE_LIBSPEC void __stack_chk_fail(void);
  168. #endif
  169. #if defined(ANDROID)
  170. #define info(...) LOGI(__VA_ARGS__)
  171. #define warn(...) LOGW(__VA_ARGS__)
  172. #ifdef DEBUG
  173. #define debug(...) LOGD(__VA_ARGS__)
  174. #else
  175. #define debug(...) do { } while(0)
  176. #endif
  177. #elif defined(CONFIG_NDS) /* ANDROID */
  178. // When the graphics have initialized, print to a debug buffer rather than the screen.
  179. void info(const char *format, ...) __attribute__((format(printf, 1, 2)));
  180. void warn(const char *format, ...) __attribute__((format(printf, 1, 2)));
  181. #ifdef DEBUG
  182. void debug(const char *format, ...) __attribute__((format(printf, 1, 2)));
  183. #else
  184. #define debug(...) do { } while(0)
  185. #endif
  186. #else /* ANDROID, CONFIG_NDS */
  187. #define info(...) \
  188. do { \
  189. fprintf(stdout, "INFO: " __VA_ARGS__); \
  190. fflush(stdout); \
  191. } while(0)
  192. #define warn(...) \
  193. do { \
  194. fprintf(stderr, "WARNING: " __VA_ARGS__); \
  195. fflush(stderr); \
  196. } while(0)
  197. #ifdef DEBUG
  198. #define debug(...) \
  199. do { \
  200. fprintf(stderr, "DEBUG: " __VA_ARGS__); \
  201. fflush(stderr); \
  202. } while(0)
  203. #else
  204. #define debug(...) do { } while(0)
  205. #endif
  206. #endif /* ANDROID, CONFIG_NDS */
  207. __M_END_DECLS
  208. #endif // __UTIL_H