utils.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /**********************************************************************************************
  2. *
  3. * raylib.utils - Some common utility functions
  4. *
  5. * CONFIGURATION:
  6. *
  7. * #define SUPPORT_TRACELOG
  8. * Show TraceLog() output messages
  9. * NOTE: By default LOG_DEBUG traces not shown
  10. *
  11. *
  12. * LICENSE: zlib/libpng
  13. *
  14. * Copyright (c) 2014-2023 Ramon Santamaria (@raysan5)
  15. *
  16. * This software is provided "as-is", without any express or implied warranty. In no event
  17. * will the authors be held liable for any damages arising from the use of this software.
  18. *
  19. * Permission is granted to anyone to use this software for any purpose, including commercial
  20. * applications, and to alter it and redistribute it freely, subject to the following restrictions:
  21. *
  22. * 1. The origin of this software must not be misrepresented; you must not claim that you
  23. * wrote the original software. If you use this software in a product, an acknowledgment
  24. * in the product documentation would be appreciated but is not required.
  25. *
  26. * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
  27. * as being the original software.
  28. *
  29. * 3. This notice may not be removed or altered from any source distribution.
  30. *
  31. **********************************************************************************************/
  32. #include "raylib.h" // WARNING: Required for: LogType enum
  33. // Check if config flags have been externally provided on compilation line
  34. #if !defined(EXTERNAL_CONFIG_FLAGS)
  35. #include "config.h" // Defines module configuration flags
  36. #endif
  37. #include "utils.h"
  38. #if defined(PLATFORM_ANDROID)
  39. #include <errno.h> // Required for: Android error types
  40. #include <android/log.h> // Required for: Android log system: __android_log_vprint()
  41. #include <android/asset_manager.h> // Required for: Android assets manager: AAsset, AAssetManager_open(), ...
  42. #endif
  43. #include <stdlib.h> // Required for: exit()
  44. #include <stdio.h> // Required for: FILE, fopen(), fseek(), ftell(), fread(), fwrite(), fprintf(), vprintf(), fclose()
  45. #include <stdarg.h> // Required for: va_list, va_start(), va_end()
  46. #include <string.h> // Required for: strcpy(), strcat()
  47. //----------------------------------------------------------------------------------
  48. // Defines and Macros
  49. //----------------------------------------------------------------------------------
  50. #ifndef MAX_TRACELOG_MSG_LENGTH
  51. #define MAX_TRACELOG_MSG_LENGTH 256 // Max length of one trace-log message
  52. #endif
  53. //----------------------------------------------------------------------------------
  54. // Global Variables Definition
  55. //----------------------------------------------------------------------------------
  56. static int logTypeLevel = LOG_INFO; // Minimum log type level
  57. static TraceLogCallback traceLog = NULL; // TraceLog callback function pointer
  58. static LoadFileDataCallback loadFileData = NULL; // LoadFileData callback function pointer
  59. static SaveFileDataCallback saveFileData = NULL; // SaveFileText callback function pointer
  60. static LoadFileTextCallback loadFileText = NULL; // LoadFileText callback function pointer
  61. static SaveFileTextCallback saveFileText = NULL; // SaveFileText callback function pointer
  62. //----------------------------------------------------------------------------------
  63. // Functions to set internal callbacks
  64. //----------------------------------------------------------------------------------
  65. void SetTraceLogCallback(TraceLogCallback callback) { traceLog = callback; } // Set custom trace log
  66. void SetLoadFileDataCallback(LoadFileDataCallback callback) { loadFileData = callback; } // Set custom file data loader
  67. void SetSaveFileDataCallback(SaveFileDataCallback callback) { saveFileData = callback; } // Set custom file data saver
  68. void SetLoadFileTextCallback(LoadFileTextCallback callback) { loadFileText = callback; } // Set custom file text loader
  69. void SetSaveFileTextCallback(SaveFileTextCallback callback) { saveFileText = callback; } // Set custom file text saver
  70. #if defined(PLATFORM_ANDROID)
  71. static AAssetManager *assetManager = NULL; // Android assets manager pointer
  72. static const char *internalDataPath = NULL; // Android internal data path
  73. #endif
  74. //----------------------------------------------------------------------------------
  75. // Module specific Functions Declaration
  76. //----------------------------------------------------------------------------------
  77. #if defined(PLATFORM_ANDROID)
  78. FILE *funopen(const void *cookie, int (*readfn)(void *, char *, int), int (*writefn)(void *, const char *, int),
  79. fpos_t (*seekfn)(void *, fpos_t, int), int (*closefn)(void *));
  80. static int android_read(void *cookie, char *buf, int size);
  81. static int android_write(void *cookie, const char *buf, int size);
  82. static fpos_t android_seek(void *cookie, fpos_t offset, int whence);
  83. static int android_close(void *cookie);
  84. #endif
  85. //----------------------------------------------------------------------------------
  86. // Module Functions Definition - Utilities
  87. //----------------------------------------------------------------------------------
  88. // Set the current threshold (minimum) log level
  89. void SetTraceLogLevel(int logType) { logTypeLevel = logType; }
  90. // Show trace log messages (LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_DEBUG)
  91. void TraceLog(int logType, const char *text, ...)
  92. {
  93. #if defined(SUPPORT_TRACELOG)
  94. // Message has level below current threshold, don't emit
  95. if (logType < logTypeLevel) return;
  96. va_list args;
  97. va_start(args, text);
  98. if (traceLog)
  99. {
  100. traceLog(logType, text, args);
  101. va_end(args);
  102. return;
  103. }
  104. #if defined(PLATFORM_ANDROID)
  105. switch (logType)
  106. {
  107. case LOG_TRACE: __android_log_vprint(ANDROID_LOG_VERBOSE, "raylib", text, args); break;
  108. case LOG_DEBUG: __android_log_vprint(ANDROID_LOG_DEBUG, "raylib", text, args); break;
  109. case LOG_INFO: __android_log_vprint(ANDROID_LOG_INFO, "raylib", text, args); break;
  110. case LOG_WARNING: __android_log_vprint(ANDROID_LOG_WARN, "raylib", text, args); break;
  111. case LOG_ERROR: __android_log_vprint(ANDROID_LOG_ERROR, "raylib", text, args); break;
  112. case LOG_FATAL: __android_log_vprint(ANDROID_LOG_FATAL, "raylib", text, args); break;
  113. default: break;
  114. }
  115. #else
  116. char buffer[MAX_TRACELOG_MSG_LENGTH] = { 0 };
  117. switch (logType)
  118. {
  119. case LOG_TRACE: strcpy(buffer, "TRACE: "); break;
  120. case LOG_DEBUG: strcpy(buffer, "DEBUG: "); break;
  121. case LOG_INFO: strcpy(buffer, "INFO: "); break;
  122. case LOG_WARNING: strcpy(buffer, "WARNING: "); break;
  123. case LOG_ERROR: strcpy(buffer, "ERROR: "); break;
  124. case LOG_FATAL: strcpy(buffer, "FATAL: "); break;
  125. default: break;
  126. }
  127. unsigned int textSize = strlen(text);
  128. memcpy(buffer + strlen(buffer), text, (textSize < (MAX_TRACELOG_MSG_LENGTH - 12))? textSize : (MAX_TRACELOG_MSG_LENGTH - 12));
  129. strcat(buffer, "\n");
  130. vprintf(buffer, args);
  131. fflush(stdout);
  132. #endif
  133. va_end(args);
  134. if (logType == LOG_FATAL) exit(EXIT_FAILURE); // If fatal logging, exit program
  135. #endif // SUPPORT_TRACELOG
  136. }
  137. // Internal memory allocator
  138. // NOTE: Initializes to zero by default
  139. void *MemAlloc(unsigned int size)
  140. {
  141. void *ptr = RL_CALLOC(size, 1);
  142. return ptr;
  143. }
  144. // Internal memory reallocator
  145. void *MemRealloc(void *ptr, unsigned int size)
  146. {
  147. void *ret = RL_REALLOC(ptr, size);
  148. return ret;
  149. }
  150. // Internal memory free
  151. void MemFree(void *ptr)
  152. {
  153. RL_FREE(ptr);
  154. }
  155. // Load data from file into a buffer
  156. unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead)
  157. {
  158. unsigned char *data = NULL;
  159. *bytesRead = 0;
  160. if (fileName != NULL)
  161. {
  162. if (loadFileData)
  163. {
  164. data = loadFileData(fileName, bytesRead);
  165. return data;
  166. }
  167. #if defined(SUPPORT_STANDARD_FILEIO)
  168. FILE *file = fopen(fileName, "rb");
  169. if (file != NULL)
  170. {
  171. // WARNING: On binary streams SEEK_END could not be found,
  172. // using fseek() and ftell() could not work in some (rare) cases
  173. fseek(file, 0, SEEK_END);
  174. int size = ftell(file);
  175. fseek(file, 0, SEEK_SET);
  176. if (size > 0)
  177. {
  178. data = (unsigned char *)RL_MALLOC(size*sizeof(unsigned char));
  179. // NOTE: fread() returns number of read elements instead of bytes, so we read [1 byte, size elements]
  180. unsigned int count = (unsigned int)fread(data, sizeof(unsigned char), size, file);
  181. *bytesRead = count;
  182. if (count != size) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially loaded", fileName);
  183. else TRACELOG(LOG_INFO, "FILEIO: [%s] File loaded successfully", fileName);
  184. }
  185. else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName);
  186. fclose(file);
  187. }
  188. else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName);
  189. #else
  190. TRACELOG(LOG_WARNING, "FILEIO: Standard file io not supported, use custom file callback");
  191. #endif
  192. }
  193. else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
  194. return data;
  195. }
  196. // Unload file data allocated by LoadFileData()
  197. void UnloadFileData(unsigned char *data)
  198. {
  199. RL_FREE(data);
  200. }
  201. // Save data to file from buffer
  202. bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite)
  203. {
  204. bool success = false;
  205. if (fileName != NULL)
  206. {
  207. if (saveFileData)
  208. {
  209. return saveFileData(fileName, data, bytesToWrite);
  210. }
  211. #if defined(SUPPORT_STANDARD_FILEIO)
  212. FILE *file = fopen(fileName, "wb");
  213. if (file != NULL)
  214. {
  215. unsigned int count = (unsigned int)fwrite(data, sizeof(unsigned char), bytesToWrite, file);
  216. if (count == 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to write file", fileName);
  217. else if (count != bytesToWrite) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially written", fileName);
  218. else TRACELOG(LOG_INFO, "FILEIO: [%s] File saved successfully", fileName);
  219. int result = fclose(file);
  220. if (result == 0) success = true;
  221. }
  222. else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName);
  223. #else
  224. TRACELOG(LOG_WARNING, "FILEIO: Standard file io not supported, use custom file callback");
  225. #endif
  226. }
  227. else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
  228. return success;
  229. }
  230. // Export data to code (.h), returns true on success
  231. bool ExportDataAsCode(const unsigned char *data, unsigned int size, const char *fileName)
  232. {
  233. bool success = false;
  234. #ifndef TEXT_BYTES_PER_LINE
  235. #define TEXT_BYTES_PER_LINE 20
  236. #endif
  237. // NOTE: Text data buffer size is estimated considering raw data size in bytes
  238. // and requiring 6 char bytes for every byte: "0x00, "
  239. char *txtData = (char *)RL_CALLOC(size*6 + 2000, sizeof(char));
  240. int byteCount = 0;
  241. byteCount += sprintf(txtData + byteCount, "////////////////////////////////////////////////////////////////////////////////////////\n");
  242. byteCount += sprintf(txtData + byteCount, "// //\n");
  243. byteCount += sprintf(txtData + byteCount, "// DataAsCode exporter v1.0 - Raw data exported as an array of bytes //\n");
  244. byteCount += sprintf(txtData + byteCount, "// //\n");
  245. byteCount += sprintf(txtData + byteCount, "// more info and bugs-report: github.com/raysan5/raylib //\n");
  246. byteCount += sprintf(txtData + byteCount, "// feedback and support: ray[at]raylib.com //\n");
  247. byteCount += sprintf(txtData + byteCount, "// //\n");
  248. byteCount += sprintf(txtData + byteCount, "// Copyright (c) 2022-2023 Ramon Santamaria (@raysan5) //\n");
  249. byteCount += sprintf(txtData + byteCount, "// //\n");
  250. byteCount += sprintf(txtData + byteCount, "////////////////////////////////////////////////////////////////////////////////////////\n\n");
  251. // Get file name from path and convert variable name to uppercase
  252. char varFileName[256] = { 0 };
  253. strcpy(varFileName, GetFileNameWithoutExt(fileName));
  254. for (int i = 0; varFileName[i] != '\0'; i++) if ((varFileName[i] >= 'a') && (varFileName[i] <= 'z')) { varFileName[i] = varFileName[i] - 32; }
  255. byteCount += sprintf(txtData + byteCount, "static unsigned char %s_DATA[%i] = { ", varFileName, size);
  256. for (unsigned int i = 0; i < size - 1; i++) byteCount += sprintf(txtData + byteCount, ((i%TEXT_BYTES_PER_LINE == 0)? "0x%x,\n" : "0x%x, "), data[i]);
  257. byteCount += sprintf(txtData + byteCount, "0x%x };\n", data[size - 1]);
  258. // NOTE: Text data size exported is determined by '\0' (NULL) character
  259. success = SaveFileText(fileName, txtData);
  260. RL_FREE(txtData);
  261. if (success != 0) TRACELOG(LOG_INFO, "FILEIO: [%s] Data as code exported successfully", fileName);
  262. else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to export data as code", fileName);
  263. return success;
  264. }
  265. // Load text data from file, returns a '\0' terminated string
  266. // NOTE: text chars array should be freed manually
  267. char *LoadFileText(const char *fileName)
  268. {
  269. char *text = NULL;
  270. if (fileName != NULL)
  271. {
  272. if (loadFileText)
  273. {
  274. text = loadFileText(fileName);
  275. return text;
  276. }
  277. #if defined(SUPPORT_STANDARD_FILEIO)
  278. FILE *file = fopen(fileName, "rt");
  279. if (file != NULL)
  280. {
  281. // WARNING: When reading a file as 'text' file,
  282. // text mode causes carriage return-linefeed translation...
  283. // ...but using fseek() should return correct byte-offset
  284. fseek(file, 0, SEEK_END);
  285. unsigned int size = (unsigned int)ftell(file);
  286. fseek(file, 0, SEEK_SET);
  287. if (size > 0)
  288. {
  289. text = (char *)RL_MALLOC((size + 1)*sizeof(char));
  290. unsigned int count = (unsigned int)fread(text, sizeof(char), size, file);
  291. // WARNING: \r\n is converted to \n on reading, so,
  292. // read bytes count gets reduced by the number of lines
  293. if (count < size) text = RL_REALLOC(text, count + 1);
  294. // Zero-terminate the string
  295. text[count] = '\0';
  296. TRACELOG(LOG_INFO, "FILEIO: [%s] Text file loaded successfully", fileName);
  297. }
  298. else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read text file", fileName);
  299. fclose(file);
  300. }
  301. else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName);
  302. #else
  303. TRACELOG(LOG_WARNING, "FILEIO: Standard file io not supported, use custom file callback");
  304. #endif
  305. }
  306. else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
  307. return text;
  308. }
  309. // Unload file text data allocated by LoadFileText()
  310. void UnloadFileText(char *text)
  311. {
  312. RL_FREE(text);
  313. }
  314. // Save text data to file (write), string must be '\0' terminated
  315. bool SaveFileText(const char *fileName, char *text)
  316. {
  317. bool success = false;
  318. if (fileName != NULL)
  319. {
  320. if (saveFileText)
  321. {
  322. return saveFileText(fileName, text);
  323. }
  324. #if defined(SUPPORT_STANDARD_FILEIO)
  325. FILE *file = fopen(fileName, "wt");
  326. if (file != NULL)
  327. {
  328. int count = fprintf(file, "%s", text);
  329. if (count < 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to write text file", fileName);
  330. else TRACELOG(LOG_INFO, "FILEIO: [%s] Text file saved successfully", fileName);
  331. int result = fclose(file);
  332. if (result == 0) success = true;
  333. }
  334. else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName);
  335. #else
  336. TRACELOG(LOG_WARNING, "FILEIO: Standard file io not supported, use custom file callback");
  337. #endif
  338. }
  339. else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
  340. return success;
  341. }
  342. #if defined(PLATFORM_ANDROID)
  343. // Initialize asset manager from android app
  344. void InitAssetManager(AAssetManager *manager, const char *dataPath)
  345. {
  346. assetManager = manager;
  347. internalDataPath = dataPath;
  348. }
  349. // Replacement for fopen()
  350. // Ref: https://developer.android.com/ndk/reference/group/asset
  351. FILE *android_fopen(const char *fileName, const char *mode)
  352. {
  353. if (mode[0] == 'w')
  354. {
  355. // fopen() is mapped to android_fopen() that only grants read access to
  356. // assets directory through AAssetManager but we want to also be able to
  357. // write data when required using the standard stdio FILE access functions
  358. // Ref: https://stackoverflow.com/questions/11294487/android-writing-saving-files-from-native-code-only
  359. #undef fopen
  360. return fopen(TextFormat("%s/%s", internalDataPath, fileName), mode);
  361. #define fopen(name, mode) android_fopen(name, mode)
  362. }
  363. else
  364. {
  365. // NOTE: AAsset provides access to read-only asset
  366. AAsset *asset = AAssetManager_open(assetManager, fileName, AASSET_MODE_UNKNOWN);
  367. if (asset != NULL)
  368. {
  369. // Get pointer to file in the assets
  370. return funopen(asset, android_read, android_write, android_seek, android_close);
  371. }
  372. else
  373. {
  374. #undef fopen
  375. // Just do a regular open if file is not found in the assets
  376. return fopen(TextFormat("%s/%s", internalDataPath, fileName), mode);
  377. #define fopen(name, mode) android_fopen(name, mode)
  378. }
  379. }
  380. }
  381. #endif // PLATFORM_ANDROID
  382. //----------------------------------------------------------------------------------
  383. // Module specific Functions Definition
  384. //----------------------------------------------------------------------------------
  385. #if defined(PLATFORM_ANDROID)
  386. static int android_read(void *cookie, char *buf, int size)
  387. {
  388. return AAsset_read((AAsset *)cookie, buf, size);
  389. }
  390. static int android_write(void *cookie, const char *buf, int size)
  391. {
  392. TRACELOG(LOG_WARNING, "ANDROID: Failed to provide write access to APK");
  393. return EACCES;
  394. }
  395. static fpos_t android_seek(void *cookie, fpos_t offset, int whence)
  396. {
  397. return AAsset_seek((AAsset *)cookie, offset, whence);
  398. }
  399. static int android_close(void *cookie)
  400. {
  401. AAsset_close((AAsset *)cookie);
  402. return 0;
  403. }
  404. #endif // PLATFORM_ANDROID