You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2000 lines
81 KiB

3 months ago
3 months ago
1 year ago
1 year ago
  1. /**********************************************************************************************
  2. raylib API parser
  3. This parser scans raylib.h to get API information about defines, structs, aliases, enums, callbacks and functions.
  4. All data is divided into pieces, usually as strings. The following types are used for data:
  5. - struct DefineInfo
  6. - struct StructInfo
  7. - struct AliasInfo
  8. - struct EnumInfo
  9. - struct FunctionInfo
  10. CONSTRAINTS:
  11. This parser is specifically designed to work with raylib.h, so, it has some constraints:
  12. - Functions are expected as a single line with the following structure:
  13. <retType> <name>(<paramType[0]> <paramName[0]>, <paramType[1]> <paramName[1]>); <desc>
  14. Be careful with functions broken into several lines, it breaks the process!
  15. - Structures are expected as several lines with the following form:
  16. <desc>
  17. typedef struct <name> {
  18. <fieldType[0]> <fieldName[0]>; <fieldDesc[0]>
  19. <fieldType[1]> <fieldName[1]>; <fieldDesc[1]>
  20. <fieldType[2]> <fieldName[2]>; <fieldDesc[2]>
  21. } <name>;
  22. - Enums are expected as several lines with the following form:
  23. <desc>
  24. typedef enum {
  25. <valueName[0]> = <valueInteger[0]>, <valueDesc[0]>
  26. <valueName[1]>,
  27. <valueName[2]>, <valueDesc[2]>
  28. <valueName[3]> <valueDesc[3]>
  29. } <name>;
  30. NOTE: Multiple options are supported for enums:
  31. - If value is not provided, (<valueInteger[i -1]> + 1) is assigned
  32. - Value description can be provided or not
  33. OTHER NOTES:
  34. - This parser could work with other C header files if mentioned constraints are followed.
  35. - This parser does not require <string.h> library, all data is parsed directly from char buffers.
  36. LICENSE: zlib/libpng
  37. raylib-parser is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  38. BSD-like license that allows static linking with closed source software:
  39. Copyright (c) 2021-2024 Ramon Santamaria (@raysan5)
  40. **********************************************************************************************/
  41. #define _CRT_SECURE_NO_WARNINGS
  42. #include <stdlib.h> // Required for: malloc(), calloc(), realloc(), free(), atoi(), strtol()
  43. #include <stdio.h> // Required for: printf(), fopen(), fseek(), ftell(), fread(), fclose()
  44. #include <stdbool.h> // Required for: bool
  45. #include <ctype.h> // Required for: isdigit()
  46. #define MAX_DEFINES_TO_PARSE 2048 // Maximum number of defines to parse
  47. #define MAX_STRUCTS_TO_PARSE 64 // Maximum number of structures to parse
  48. #define MAX_ALIASES_TO_PARSE 64 // Maximum number of aliases to parse
  49. #define MAX_ENUMS_TO_PARSE 64 // Maximum number of enums to parse
  50. #define MAX_CALLBACKS_TO_PARSE 64 // Maximum number of callbacks to parse
  51. #define MAX_FUNCS_TO_PARSE 1024 // Maximum number of functions to parse
  52. #define MAX_LINE_LENGTH 1024 // Maximum length of one line (including comments)
  53. #define MAX_STRUCT_FIELDS 64 // Maximum number of struct fields
  54. #define MAX_ENUM_VALUES 512 // Maximum number of enum values
  55. #define MAX_FUNCTION_PARAMETERS 12 // Maximum number of function parameters
  56. //----------------------------------------------------------------------------------
  57. // Types and Structures Definition
  58. //----------------------------------------------------------------------------------
  59. // Type of parsed define
  60. typedef enum {
  61. UNKNOWN = 0,
  62. MACRO,
  63. GUARD,
  64. INT,
  65. INT_MATH,
  66. LONG,
  67. LONG_MATH,
  68. FLOAT,
  69. FLOAT_MATH,
  70. DOUBLE,
  71. DOUBLE_MATH,
  72. CHAR,
  73. STRING,
  74. COLOR
  75. } DefineType;
  76. // Define info data
  77. typedef struct DefineInfo {
  78. char name[64]; // Define name
  79. int type; // Define type
  80. char value[256]; // Define value
  81. char desc[128]; // Define description
  82. bool isHex; // Define is hex number (for types INT, LONG)
  83. } DefineInfo;
  84. // Struct info data
  85. typedef struct StructInfo {
  86. char name[64]; // Struct name
  87. char desc[128]; // Struct type description
  88. int fieldCount; // Number of fields in the struct
  89. char fieldType[MAX_STRUCT_FIELDS][64]; // Field type
  90. char fieldName[MAX_STRUCT_FIELDS][64]; // Field name
  91. char fieldDesc[MAX_STRUCT_FIELDS][128]; // Field description
  92. } StructInfo;
  93. // Alias info data
  94. typedef struct AliasInfo {
  95. char type[64]; // Alias type
  96. char name[64]; // Alias name
  97. char desc[128]; // Alias description
  98. } AliasInfo;
  99. // Enum info data
  100. typedef struct EnumInfo {
  101. char name[64]; // Enum name
  102. char desc[128]; // Enum description
  103. int valueCount; // Number of values in enumerator
  104. char valueName[MAX_ENUM_VALUES][64]; // Value name definition
  105. int valueInteger[MAX_ENUM_VALUES]; // Value integer
  106. char valueDesc[MAX_ENUM_VALUES][128]; // Value description
  107. } EnumInfo;
  108. // Function info data
  109. typedef struct FunctionInfo {
  110. char name[64]; // Function name
  111. char desc[512]; // Function description (comment at the end)
  112. char retType[32]; // Return value type
  113. int paramCount; // Number of function parameters
  114. char paramType[MAX_FUNCTION_PARAMETERS][32]; // Parameters type
  115. char paramName[MAX_FUNCTION_PARAMETERS][32]; // Parameters name
  116. char paramDesc[MAX_FUNCTION_PARAMETERS][128]; // Parameters description
  117. } FunctionInfo;
  118. // Output format for parsed data
  119. typedef enum { DEFAULT = 0, JSON, XML, LUA, CODE } OutputFormat;
  120. //----------------------------------------------------------------------------------
  121. // Global Variables Definition
  122. //----------------------------------------------------------------------------------
  123. static int defineCount = 0;
  124. static int structCount = 0;
  125. static int aliasCount = 0;
  126. static int enumCount = 0;
  127. static int callbackCount = 0;
  128. static int funcCount = 0;
  129. static DefineInfo *defines = NULL;
  130. static StructInfo *structs = NULL;
  131. static AliasInfo *aliases = NULL;
  132. static EnumInfo *enums = NULL;
  133. static FunctionInfo *callbacks = NULL;
  134. static FunctionInfo *funcs = NULL;
  135. // Command line variables
  136. static char apiDefine[32] = { 0 }; // Functions define (i.e. RLAPI for raylib.h, RMDEF for raymath.h, etc.)
  137. static char truncAfter[32] = { 0 }; // Truncate marker (i.e. "RLGL IMPLEMENTATION" for rlgl.h)
  138. static int outputFormat = DEFAULT;
  139. // NOTE: Max length depends on OS, in Windows MAX_PATH = 256
  140. static char inFileName[512] = { 0 }; // Input file name (required in case of drag & drop over executable)
  141. static char outFileName[512] = { 0 }; // Output file name (required for file save/export)
  142. //----------------------------------------------------------------------------------
  143. // Module Functions Declaration
  144. //----------------------------------------------------------------------------------
  145. static void ShowCommandLineInfo(void); // Show command line usage info
  146. static void ProcessCommandLine(int argc, char *argv[]); // Process command line input
  147. static char *LoadFileText(const char *fileName, int *length);
  148. static char **GetTextLines(const char *buffer, int length, int *linesCount);
  149. static void GetDataTypeAndName(const char *typeName, int typeNameLen, char *type, char *name);
  150. static void GetDescription(const char *source, char *description);
  151. static void MoveArraySize(char *name, char *type); // Move array size from name to type
  152. static unsigned int TextLength(const char *text); // Get text length in bytes, check for \0 character
  153. static bool IsTextEqual(const char *text1, const char *text2, unsigned int count);
  154. static int TextFindIndex(const char *text, const char *find); // Find first text occurrence within a string
  155. static void MemoryCopy(void *dest, const void *src, unsigned int count);
  156. static char *EscapeBackslashes(char *text); // Replace '\' by "\\" when exporting to JSON and XML
  157. static const char *StrDefineType(DefineType type); // Get string of define type
  158. static void ExportParsedData(const char *fileName, int format); // Export parsed data in desired format
  159. //----------------------------------------------------------------------------------
  160. // Program main entry point
  161. //----------------------------------------------------------------------------------
  162. int main(int argc, char* argv[])
  163. {
  164. if (argc > 1) ProcessCommandLine(argc, argv);
  165. const char *raylibhPath = "../src/raylib.h\0";
  166. const char *raylibapiPath = "raylib_api.txt\0";
  167. const char *rlapiPath = "RLAPI\0";
  168. if (inFileName[0] == '\0') MemoryCopy(inFileName, raylibhPath, TextLength(raylibhPath) + 1);
  169. if (outFileName[0] == '\0') MemoryCopy(outFileName, raylibapiPath, TextLength(raylibapiPath) + 1);
  170. if (apiDefine[0] == '\0') MemoryCopy(apiDefine, rlapiPath, TextLength(rlapiPath) + 1);
  171. int length = 0;
  172. char *buffer = LoadFileText(inFileName, &length);
  173. if (buffer == NULL)
  174. {
  175. printf("Could not read input file: %s\n", inFileName);
  176. return 1;
  177. }
  178. // Preprocess buffer to get separate lines
  179. // NOTE: GetTextLines() also removes leading spaces/tabs
  180. int linesCount = 0;
  181. char **lines = GetTextLines(buffer, length, &linesCount);
  182. // Truncate lines
  183. if (truncAfter[0] != '\0')
  184. {
  185. int newCount = -1;
  186. for (int i = 0; i < linesCount; i++)
  187. {
  188. if (newCount > -1) free(lines[i]);
  189. else if (TextFindIndex(lines[i], truncAfter) > -1) newCount = i;
  190. }
  191. if (newCount > -1) linesCount = newCount;
  192. printf("Number of truncated text lines: %i\n", linesCount);
  193. }
  194. // Defines line indices
  195. int *defineLines = (int *)malloc(MAX_DEFINES_TO_PARSE*sizeof(int));
  196. // Structs line indices
  197. int *structLines = (int *)malloc(MAX_STRUCTS_TO_PARSE*sizeof(int));
  198. // Aliases line indices
  199. int *aliasLines = (int *)malloc(MAX_ALIASES_TO_PARSE*sizeof(int));
  200. // Enums line indices
  201. int *enumLines = (int *)malloc(MAX_ENUMS_TO_PARSE*sizeof(int));
  202. // Callbacks line indices
  203. int *callbackLines = (int *)malloc(MAX_CALLBACKS_TO_PARSE*sizeof(int));
  204. // Function line indices
  205. int *funcLines = (int *)malloc(MAX_FUNCS_TO_PARSE*sizeof(int));
  206. // Prepare required lines for parsing
  207. //----------------------------------------------------------------------------------
  208. // Read define lines
  209. for (int i = 0; i < linesCount; i++)
  210. {
  211. int j = 0;
  212. while ((lines[i][j] == ' ') || (lines[i][j] == '\t')) j++; // skip spaces and tabs in the begining
  213. // Read define line
  214. if (IsTextEqual(lines[i]+j, "#define ", 8))
  215. {
  216. // Keep the line position in the array of lines,
  217. // so, we can scan that position and following lines
  218. defineLines[defineCount] = i;
  219. defineCount++;
  220. }
  221. }
  222. // Read struct lines
  223. for (int i = 0; i < linesCount; i++)
  224. {
  225. // Find structs
  226. // starting with "typedef struct ... {" or "typedef struct ... ; \n struct ... {"
  227. // ending with "} ... ;"
  228. // i.e. excluding "typedef struct rAudioBuffer rAudioBuffer;" -> Typedef and forward declaration only
  229. if (IsTextEqual(lines[i], "typedef struct", 14))
  230. {
  231. bool validStruct = IsTextEqual(lines[i + 1], "struct", 6);
  232. if (!validStruct)
  233. {
  234. for (int c = 0; c < MAX_LINE_LENGTH; c++)
  235. {
  236. char v = lines[i][c];
  237. if (v == '{') validStruct = true;
  238. if ((v == '{') || (v == ';') || (v == '\0')) break;
  239. }
  240. }
  241. if (!validStruct) continue;
  242. structLines[structCount] = i;
  243. while (lines[i][0] != '}') i++;
  244. while (lines[i][0] != '\0') i++;
  245. structCount++;
  246. }
  247. }
  248. // Read alias lines
  249. for (int i = 0; i < linesCount; i++)
  250. {
  251. // Find aliases (lines with "typedef ... ...;")
  252. if (IsTextEqual(lines[i], "typedef", 7))
  253. {
  254. int spaceCount = 0;
  255. bool validAlias = false;
  256. for (int c = 0; c < MAX_LINE_LENGTH; c++)
  257. {
  258. char v = lines[i][c];
  259. if (v == ' ') spaceCount++;
  260. if ((v == ';') && (spaceCount == 2)) validAlias = true;
  261. if ((v == ';') || (v == '(') || (v == '\0')) break;
  262. }
  263. if (!validAlias) continue;
  264. aliasLines[aliasCount] = i;
  265. aliasCount++;
  266. }
  267. }
  268. // Read enum lines
  269. for (int i = 0; i < linesCount; i++)
  270. {
  271. // Read enum line
  272. if (IsTextEqual(lines[i], "typedef enum {", 14) && (lines[i][TextLength(lines[i])-1] != ';')) // ignore inline enums
  273. {
  274. // Keep the line position in the array of lines,
  275. // so, we can scan that position and following lines
  276. enumLines[enumCount] = i;
  277. enumCount++;
  278. }
  279. }
  280. // Read callback lines
  281. for (int i = 0; i < linesCount; i++)
  282. {
  283. // Find callbacks (lines with "typedef ... (* ... )( ... );")
  284. if (IsTextEqual(lines[i], "typedef", 7))
  285. {
  286. bool hasBeginning = false;
  287. bool hasMiddle = false;
  288. bool hasEnd = false;
  289. for (int c = 0; c < MAX_LINE_LENGTH; c++)
  290. {
  291. if ((lines[i][c] == '(') && (lines[i][c + 1] == '*')) hasBeginning = true;
  292. if ((lines[i][c] == ')') && (lines[i][c + 1] == '(')) hasMiddle = true;
  293. if ((lines[i][c] == ')') && (lines[i][c + 1] == ';')) hasEnd = true;
  294. if (hasEnd) break;
  295. }
  296. if (hasBeginning && hasMiddle && hasEnd)
  297. {
  298. callbackLines[callbackCount] = i;
  299. callbackCount++;
  300. }
  301. }
  302. }
  303. // Read function lines
  304. for (int i = 0; i < linesCount; i++)
  305. {
  306. // Read function line (starting with `define`, i.e. for raylib.h "RLAPI")
  307. if (IsTextEqual(lines[i], apiDefine, TextLength(apiDefine)))
  308. {
  309. funcLines[funcCount] = i;
  310. funcCount++;
  311. }
  312. }
  313. // At this point we have all raylib defines, structs, aliases, enums, callbacks, functions lines data to start parsing
  314. free(buffer); // Unload text buffer
  315. // Parsing raylib data
  316. //----------------------------------------------------------------------------------
  317. // Define info data
  318. defines = (DefineInfo *)calloc(MAX_DEFINES_TO_PARSE, sizeof(DefineInfo));
  319. int defineIndex = 0;
  320. for (int i = 0; i < defineCount; i++)
  321. {
  322. char *linePtr = lines[defineLines[i]];
  323. int j = 0;
  324. while ((linePtr[j] == ' ') || (linePtr[j] == '\t')) j++; // Skip spaces and tabs in the begining
  325. j += 8; // Skip "#define "
  326. while ((linePtr[j] == ' ') || (linePtr[j] == '\t')) j++; // Skip spaces and tabs after "#define "
  327. // Extract name
  328. int defineNameStart = j;
  329. int openBraces = 0;
  330. while (linePtr[j] != '\0')
  331. {
  332. if (((linePtr[j] == ' ') || (linePtr[j] == '\t')) && (openBraces == 0)) break;
  333. if (linePtr[j] == '(') openBraces++;
  334. if (linePtr[j] == ')') openBraces--;
  335. j++;
  336. }
  337. int defineNameEnd = j-1;
  338. // Skip duplicates
  339. unsigned int nameLen = defineNameEnd - defineNameStart + 1;
  340. bool isDuplicate = false;
  341. for (int k = 0; k < defineIndex; k++)
  342. {
  343. if ((nameLen == TextLength(defines[k].name)) && IsTextEqual(defines[k].name, &linePtr[defineNameStart], nameLen))
  344. {
  345. isDuplicate = true;
  346. break;
  347. }
  348. }
  349. if (isDuplicate) continue;
  350. MemoryCopy(defines[defineIndex].name, &linePtr[defineNameStart], nameLen);
  351. // Determine type
  352. if (linePtr[defineNameEnd] == ')') defines[defineIndex].type = MACRO;
  353. while ((linePtr[j] == ' ') || (linePtr[j] == '\t')) j++; // Skip spaces and tabs after name
  354. int defineValueStart = j;
  355. if ((linePtr[j] == '\0') || (linePtr[j] == '/')) defines[defineIndex].type = GUARD;
  356. if (linePtr[j] == '"') defines[defineIndex].type = STRING;
  357. else if (linePtr[j] == '\'') defines[defineIndex].type = CHAR;
  358. else if (IsTextEqual(linePtr+j, "CLITERAL(Color)", 15)) defines[defineIndex].type = COLOR;
  359. else if (isdigit(linePtr[j])) // Parsing numbers
  360. {
  361. bool isFloat = false, isNumber = true, isHex = false;
  362. while ((linePtr[j] != ' ') && (linePtr[j] != '\t') && (linePtr[j] != '\0'))
  363. {
  364. char ch = linePtr[j];
  365. if (ch == '.') isFloat = true;
  366. if (ch == 'x') isHex = true;
  367. if (!(isdigit(ch) ||
  368. ((ch >= 'a') && (ch <= 'f')) ||
  369. ((ch >= 'A') && (ch <= 'F')) ||
  370. (ch == 'x') ||
  371. (ch == 'L') ||
  372. (ch == '.') ||
  373. (ch == '+') ||
  374. (ch == '-'))) isNumber = false;
  375. j++;
  376. }
  377. if (isNumber)
  378. {
  379. if (isFloat)
  380. {
  381. defines[defineIndex].type = (linePtr[j-1] == 'f')? FLOAT : DOUBLE;
  382. }
  383. else
  384. {
  385. defines[defineIndex].type = (linePtr[j-1] == 'L')? LONG : INT;
  386. defines[defineIndex].isHex = isHex;
  387. }
  388. }
  389. }
  390. // Extracting value
  391. while ((linePtr[j] != '\\') && (linePtr[j] != '\0') && !((linePtr[j] == '/') && (linePtr[j+1] == '/'))) j++;
  392. int defineValueEnd = j-1;
  393. while ((linePtr[defineValueEnd] == ' ') || (linePtr[defineValueEnd] == '\t')) defineValueEnd--; // Remove trailing spaces and tabs
  394. if ((defines[defineIndex].type == LONG) || (defines[defineIndex].type == FLOAT)) defineValueEnd--; // Remove number postfix
  395. int valueLen = defineValueEnd - defineValueStart + 1;
  396. if (valueLen > 255) valueLen = 255;
  397. if (valueLen > 0) MemoryCopy(defines[defineIndex].value, &linePtr[defineValueStart], valueLen);
  398. // Extracting description
  399. if ((linePtr[j] == '/') && linePtr[j + 1] == '/')
  400. {
  401. j += 2;
  402. while (linePtr[j] == ' ') j++;
  403. int commentStart = j;
  404. while ((linePtr[j] != '\\') && (linePtr[j] != '\0')) j++;
  405. int commentEnd = j-1;
  406. int commentLen = commentEnd - commentStart + 1;
  407. if (commentLen > 127) commentLen = 127;
  408. MemoryCopy(defines[defineIndex].desc, &linePtr[commentStart], commentLen);
  409. }
  410. // Parse defines of type UNKNOWN to find calculated numbers
  411. if (defines[defineIndex].type == UNKNOWN)
  412. {
  413. int largestType = UNKNOWN;
  414. bool isMath = true;
  415. char *valuePtr = defines[defineIndex].value;
  416. for (unsigned int c = 0; c < TextLength(valuePtr); c++)
  417. {
  418. char ch = valuePtr[c];
  419. // Skip operators and whitespace
  420. if ((ch == '(') ||
  421. (ch == ')') ||
  422. (ch == '+') ||
  423. (ch == '-') ||
  424. (ch == '*') ||
  425. (ch == '/') ||
  426. (ch == ' ') ||
  427. (ch == '\t')) continue;
  428. // Read number operand
  429. else if (isdigit(ch))
  430. {
  431. bool isNumber = true, isFloat = false;
  432. while (!((ch == '(') ||
  433. (ch == ')') ||
  434. (ch == '*') ||
  435. (ch == '/') ||
  436. (ch == ' ') ||
  437. (ch == '\t') ||
  438. (ch == '\0')))
  439. {
  440. if (ch == '.') isFloat = true;
  441. if (!(isdigit(ch) ||
  442. ((ch >= 'a') && (ch <= 'f')) ||
  443. ((ch >= 'A') && (ch <= 'F')) ||
  444. (ch == 'x') ||
  445. (ch == 'L') ||
  446. (ch == '.') ||
  447. (ch == '+') ||
  448. (ch == '-')))
  449. {
  450. isNumber = false;
  451. break;
  452. }
  453. c++;
  454. ch = valuePtr[c];
  455. }
  456. if (isNumber)
  457. {
  458. // Found a valid number -> update largestType
  459. int numberType;
  460. if (isFloat) numberType = (valuePtr[c - 1] == 'f')? FLOAT_MATH : DOUBLE_MATH;
  461. else numberType = (valuePtr[c - 1] == 'L')? LONG_MATH : INT_MATH;
  462. if (numberType > largestType) largestType = numberType;
  463. }
  464. else
  465. {
  466. isMath = false;
  467. break;
  468. }
  469. }
  470. else // Read string operand
  471. {
  472. int operandStart = c;
  473. while (!((ch == '\0') ||
  474. (ch == ' ') ||
  475. (ch == '(') ||
  476. (ch == ')') ||
  477. (ch == '+') ||
  478. (ch == '-') ||
  479. (ch == '*') ||
  480. (ch == '/')))
  481. {
  482. c++;
  483. ch = valuePtr[c];
  484. }
  485. int operandEnd = c;
  486. int operandLength = operandEnd - operandStart;
  487. // Search previous defines for operand
  488. bool foundOperand = false;
  489. for (int previousDefineIndex = 0; previousDefineIndex < defineIndex; previousDefineIndex++)
  490. {
  491. if (IsTextEqual(defines[previousDefineIndex].name, &valuePtr[operandStart], operandLength))
  492. {
  493. if ((defines[previousDefineIndex].type >= INT) && (defines[previousDefineIndex].type <= DOUBLE_MATH))
  494. {
  495. // Found operand and it's a number -> update largestType
  496. if (defines[previousDefineIndex].type > largestType) largestType = defines[previousDefineIndex].type;
  497. foundOperand = true;
  498. }
  499. break;
  500. }
  501. }
  502. if (!foundOperand)
  503. {
  504. isMath = false;
  505. break;
  506. }
  507. }
  508. }
  509. if (isMath)
  510. {
  511. // Define is a calculated number -> update type
  512. if (largestType == INT) largestType = INT_MATH;
  513. else if (largestType == LONG) largestType = LONG_MATH;
  514. else if (largestType == FLOAT) largestType = FLOAT_MATH;
  515. else if (largestType == DOUBLE) largestType = DOUBLE_MATH;
  516. defines[defineIndex].type = largestType;
  517. }
  518. }
  519. defineIndex++;
  520. }
  521. defineCount = defineIndex;
  522. free(defineLines);
  523. // Structs info data
  524. structs = (StructInfo *)calloc(MAX_STRUCTS_TO_PARSE, sizeof(StructInfo));
  525. for (int i = 0; i < structCount; i++)
  526. {
  527. char **linesPtr = &lines[structLines[i]];
  528. // Parse struct description
  529. GetDescription(linesPtr[-1], structs[i].desc);
  530. // Get struct name: typedef struct name {
  531. const int TDS_LEN = 15; // length of "typedef struct "
  532. for (int c = TDS_LEN; c < 64 + TDS_LEN; c++)
  533. {
  534. if ((linesPtr[0][c] == '{') || (linesPtr[0][c] == ' '))
  535. {
  536. int nameLen = c - TDS_LEN;
  537. while (linesPtr[0][TDS_LEN + nameLen - 1] == ' ') nameLen--;
  538. MemoryCopy(structs[i].name, &linesPtr[0][TDS_LEN], nameLen);
  539. break;
  540. }
  541. }
  542. // Get struct fields and count them -> fields finish with ;
  543. int l = 1;
  544. while (linesPtr[l][0] != '}')
  545. {
  546. // WARNING: Some structs have empty spaces and comments -> OK, processed
  547. if ((linesPtr[l][0] != ' ') && (linesPtr[l][0] != '\0'))
  548. {
  549. // Scan one field line
  550. char *fieldLine = linesPtr[l];
  551. int fieldEndPos = 0;
  552. while (fieldLine[fieldEndPos] != ';') fieldEndPos++;
  553. if ((fieldLine[0] != '/') && !IsTextEqual(fieldLine, "struct", 6)) // Field line is not a comment and not a struct declaration
  554. {
  555. //printf("Struct field: %s_\n", fieldLine); // OK!
  556. // Get struct field type and name
  557. GetDataTypeAndName(fieldLine, fieldEndPos, structs[i].fieldType[structs[i].fieldCount], structs[i].fieldName[structs[i].fieldCount]);
  558. // Get the field description
  559. GetDescription(&fieldLine[fieldEndPos], structs[i].fieldDesc[structs[i].fieldCount]);
  560. structs[i].fieldCount++;
  561. // Split field names containing multiple fields (like Matrix)
  562. int additionalFields = 0;
  563. int originalIndex = structs[i].fieldCount - 1;
  564. for (unsigned int c = 0; c < TextLength(structs[i].fieldName[originalIndex]); c++)
  565. {
  566. if (structs[i].fieldName[originalIndex][c] == ',') additionalFields++;
  567. }
  568. if (additionalFields > 0)
  569. {
  570. int originalLength = -1;
  571. int lastStart;
  572. for (unsigned int c = 0; c < TextLength(structs[i].fieldName[originalIndex]) + 1; c++)
  573. {
  574. char v = structs[i].fieldName[originalIndex][c];
  575. bool isEndOfString = (v == '\0');
  576. if ((v == ',') || isEndOfString)
  577. {
  578. if (originalLength == -1)
  579. {
  580. // Save length of original field name
  581. // Don't truncate yet, still needed for copying
  582. originalLength = c;
  583. }
  584. else
  585. {
  586. // Copy field data from original field
  587. int nameLength = c - lastStart;
  588. MemoryCopy(structs[i].fieldName[structs[i].fieldCount], &structs[i].fieldName[originalIndex][lastStart], nameLength);
  589. MemoryCopy(structs[i].fieldType[structs[i].fieldCount], &structs[i].fieldType[originalIndex][0], TextLength(structs[i].fieldType[originalIndex]));
  590. MemoryCopy(structs[i].fieldDesc[structs[i].fieldCount], &structs[i].fieldDesc[originalIndex][0], TextLength(structs[i].fieldDesc[originalIndex]));
  591. structs[i].fieldCount++;
  592. }
  593. if (!isEndOfString)
  594. {
  595. // Skip comma and spaces
  596. c++;
  597. while (structs[i].fieldName[originalIndex][c] == ' ') c++;
  598. // Save position for next field
  599. lastStart = c;
  600. }
  601. }
  602. }
  603. // Set length of original field to truncate the first field name
  604. structs[i].fieldName[originalIndex][originalLength] = '\0';
  605. }
  606. // Split field types containing multiple fields (like MemNode)
  607. additionalFields = 0;
  608. originalIndex = structs[i].fieldCount - 1;
  609. for (unsigned int c = 0; c < TextLength(structs[i].fieldType[originalIndex]); c++)
  610. {
  611. if (structs[i].fieldType[originalIndex][c] == ',') additionalFields++;
  612. }
  613. if (additionalFields > 0)
  614. {
  615. // Copy original name to last additional field
  616. structs[i].fieldCount += additionalFields;
  617. MemoryCopy(structs[i].fieldName[originalIndex + additionalFields], &structs[i].fieldName[originalIndex][0], TextLength(structs[i].fieldName[originalIndex]));
  618. // Copy names from type to additional fields
  619. int fieldsRemaining = additionalFields;
  620. int nameStart = -1;
  621. int nameEnd = -1;
  622. for (int k = TextLength(structs[i].fieldType[originalIndex]); k > 0; k--)
  623. {
  624. char v = structs[i].fieldType[originalIndex][k];
  625. if ((v == '*') || (v == ' ') || (v == ','))
  626. {
  627. if (nameEnd != -1) {
  628. // Don't copy to last additional field
  629. if (fieldsRemaining != additionalFields)
  630. {
  631. nameStart = k + 1;
  632. MemoryCopy(structs[i].fieldName[originalIndex + fieldsRemaining], &structs[i].fieldType[originalIndex][nameStart], nameEnd - nameStart + 1);
  633. }
  634. nameEnd = -1;
  635. fieldsRemaining--;
  636. }
  637. }
  638. else if (nameEnd == -1) nameEnd = k;
  639. }
  640. // Truncate original field type
  641. int fieldTypeLength = nameStart;
  642. structs[i].fieldType[originalIndex][fieldTypeLength] = '\0';
  643. // Set field type and description of additional fields
  644. for (int j = 1; j <= additionalFields; j++)
  645. {
  646. MemoryCopy(structs[i].fieldType[originalIndex + j], &structs[i].fieldType[originalIndex][0], fieldTypeLength);
  647. MemoryCopy(structs[i].fieldDesc[originalIndex + j], &structs[i].fieldDesc[originalIndex][0], TextLength(structs[i].fieldDesc[originalIndex]));
  648. }
  649. }
  650. }
  651. }
  652. l++;
  653. }
  654. // Move array sizes from name to type
  655. for (int j = 0; j < structs[i].fieldCount; j++)
  656. {
  657. MoveArraySize(structs[i].fieldName[j], structs[i].fieldType[j]);
  658. }
  659. }
  660. free(structLines);
  661. // Alias info data
  662. aliases = (AliasInfo *)calloc(MAX_ALIASES_TO_PARSE, sizeof(AliasInfo));
  663. for (int i = 0; i < aliasCount; i++)
  664. {
  665. // Description from previous line
  666. GetDescription(lines[aliasLines[i] - 1], aliases[i].desc);
  667. char *linePtr = lines[aliasLines[i]];
  668. // Skip "typedef "
  669. int c = 8;
  670. // Type
  671. int typeStart = c;
  672. while(linePtr[c] != ' ') c++;
  673. int typeLen = c - typeStart;
  674. MemoryCopy(aliases[i].type, &linePtr[typeStart], typeLen);
  675. // Skip space
  676. c++;
  677. // Name
  678. int nameStart = c;
  679. while(linePtr[c] != ';') c++;
  680. int nameLen = c - nameStart;
  681. MemoryCopy(aliases[i].name, &linePtr[nameStart], nameLen);
  682. // Description
  683. GetDescription(&linePtr[c], aliases[i].desc);
  684. }
  685. free(aliasLines);
  686. // Enum info data
  687. enums = (EnumInfo *)calloc(MAX_ENUMS_TO_PARSE, sizeof(EnumInfo));
  688. for (int i = 0; i < enumCount; i++)
  689. {
  690. // Parse enum description
  691. // NOTE: This is not necessarily from the line immediately before,
  692. // some of the enums have extra lines between the "description"
  693. // and the typedef enum
  694. for (int j = enumLines[i] - 1; j > 0; j--)
  695. {
  696. char *linePtr = lines[j];
  697. if ((linePtr[0] != '/') || (linePtr[2] != ' '))
  698. {
  699. GetDescription(&lines[j + 1][0], enums[i].desc);
  700. break;
  701. }
  702. }
  703. for (int j = 1; j < MAX_ENUM_VALUES*2; j++) // Maximum number of lines following enum first line
  704. {
  705. char *linePtr = lines[enumLines[i] + j];
  706. if ((linePtr[0] >= 'A') && (linePtr[0] <= 'Z'))
  707. {
  708. // Parse enum value line, possible options:
  709. //ENUM_VALUE_NAME,
  710. //ENUM_VALUE_NAME
  711. //ENUM_VALUE_NAME = 99
  712. //ENUM_VALUE_NAME = 99,
  713. //ENUM_VALUE_NAME = 0x00000040, // Value description
  714. // We start reading the value name
  715. int c = 0;
  716. while ((linePtr[c] != ',') &&
  717. (linePtr[c] != ' ') &&
  718. (linePtr[c] != '=') &&
  719. (linePtr[c] != '\0'))
  720. {
  721. enums[i].valueName[enums[i].valueCount][c] = linePtr[c];
  722. c++;
  723. }
  724. // After the name we can have:
  725. // '=' -> value is provided
  726. // ',' -> value is equal to previous + 1, there could be a description if not '\0'
  727. // ' ' -> value is equal to previous + 1, there could be a description if not '\0'
  728. // '\0' -> value is equal to previous + 1
  729. // Let's start checking if the line is not finished
  730. if ((linePtr[c] != ',') && (linePtr[c] != '\0'))
  731. {
  732. // Two options:
  733. // '=' -> value is provided
  734. // ' ' -> value is equal to previous + 1, there could be a description if not '\0'
  735. bool foundValue = false;
  736. while ((linePtr[c] != '\0') && (linePtr[c] != '/'))
  737. {
  738. if (linePtr[c] == '=')
  739. {
  740. foundValue = true;
  741. break;
  742. }
  743. c++;
  744. }
  745. if (foundValue)
  746. {
  747. if (linePtr[c + 1] == ' ') c += 2;
  748. else c++;
  749. // Parse integer value
  750. int n = 0;
  751. char integer[16] = { 0 };
  752. while ((linePtr[c] != ',') && (linePtr[c] != ' ') && (linePtr[c] != '\0'))
  753. {
  754. integer[n] = linePtr[c];
  755. c++; n++;
  756. }
  757. if (integer[1] == 'x') enums[i].valueInteger[enums[i].valueCount] = (int)strtol(integer, NULL, 16);
  758. else enums[i].valueInteger[enums[i].valueCount] = atoi(integer);
  759. }
  760. else enums[i].valueInteger[enums[i].valueCount] = (enums[i].valueInteger[enums[i].valueCount - 1] + 1);
  761. }
  762. else enums[i].valueInteger[enums[i].valueCount] = (enums[i].valueInteger[enums[i].valueCount - 1] + 1);
  763. // Parse value description
  764. GetDescription(&linePtr[c], enums[i].valueDesc[enums[i].valueCount]);
  765. enums[i].valueCount++;
  766. }
  767. else if (linePtr[0] == '}')
  768. {
  769. // Get enum name from typedef
  770. int c = 0;
  771. while (linePtr[2 + c] != ';')
  772. {
  773. enums[i].name[c] = linePtr[2 + c];
  774. c++;
  775. }
  776. break; // Enum ended, break for() loop
  777. }
  778. }
  779. }
  780. free(enumLines);
  781. // Callback info data
  782. callbacks = (FunctionInfo *)calloc(MAX_CALLBACKS_TO_PARSE, sizeof(FunctionInfo));
  783. for (int i = 0; i < callbackCount; i++)
  784. {
  785. char *linePtr = lines[callbackLines[i]];
  786. // Skip "typedef "
  787. unsigned int c = 8;
  788. // Return type
  789. int retTypeStart = c;
  790. while(linePtr[c] != '(') c++;
  791. int retTypeLen = c - retTypeStart;
  792. while(linePtr[retTypeStart + retTypeLen - 1] == ' ') retTypeLen--;
  793. MemoryCopy(callbacks[i].retType, &linePtr[retTypeStart], retTypeLen);
  794. // Skip "(*"
  795. c += 2;
  796. // Name
  797. int nameStart = c;
  798. while(linePtr[c] != ')') c++;
  799. int nameLen = c - nameStart;
  800. MemoryCopy(callbacks[i].name, &linePtr[nameStart], nameLen);
  801. // Skip ")("
  802. c += 2;
  803. // Params
  804. int paramStart = c;
  805. for (; c < MAX_LINE_LENGTH; c++)
  806. {
  807. if ((linePtr[c] == ',') || (linePtr[c] == ')'))
  808. {
  809. // Get parameter type + name, extract info
  810. int paramLen = c - paramStart;
  811. GetDataTypeAndName(&linePtr[paramStart], paramLen, callbacks[i].paramType[callbacks[i].paramCount], callbacks[i].paramName[callbacks[i].paramCount]);
  812. callbacks[i].paramCount++;
  813. paramStart = c + 1;
  814. while(linePtr[paramStart] == ' ') paramStart++;
  815. }
  816. if (linePtr[c] == ')') break;
  817. }
  818. // Description
  819. GetDescription(&linePtr[c], callbacks[i].desc);
  820. // Move array sizes from name to type
  821. for (int j = 0; j < callbacks[i].paramCount; j++)
  822. {
  823. MoveArraySize(callbacks[i].paramName[j], callbacks[i].paramType[j]);
  824. }
  825. }
  826. free(callbackLines);
  827. // Functions info data
  828. funcs = (FunctionInfo *)calloc(MAX_FUNCS_TO_PARSE, sizeof(FunctionInfo));
  829. for (int i = 0; i < funcCount; i++)
  830. {
  831. char *linePtr = lines[funcLines[i]];
  832. int funcParamsStart = 0;
  833. int funcEnd = 0;
  834. // Get return type and function name from func line
  835. for (int c = 0; (c < MAX_LINE_LENGTH) && (linePtr[c] != '\n'); c++)
  836. {
  837. if (linePtr[c] == '(') // Starts function parameters
  838. {
  839. funcParamsStart = c + 1;
  840. // At this point we have function return type and function name
  841. char funcRetTypeName[128] = { 0 };
  842. int dc = TextLength(apiDefine) + 1;
  843. int funcRetTypeNameLen = c - dc; // Substract `define` ("RLAPI " for raylib.h)
  844. MemoryCopy(funcRetTypeName, &linePtr[dc], funcRetTypeNameLen);
  845. GetDataTypeAndName(funcRetTypeName, funcRetTypeNameLen, funcs[i].retType, funcs[i].name);
  846. break;
  847. }
  848. }
  849. // Get parameters from func line
  850. for (int c = funcParamsStart; c < MAX_LINE_LENGTH; c++)
  851. {
  852. if (linePtr[c] == ',') // Starts function parameters
  853. {
  854. // Get parameter type + name, extract info
  855. char funcParamTypeName[128] = { 0 };
  856. int funcParamTypeNameLen = c - funcParamsStart;
  857. MemoryCopy(funcParamTypeName, &linePtr[funcParamsStart], funcParamTypeNameLen);
  858. GetDataTypeAndName(funcParamTypeName, funcParamTypeNameLen, funcs[i].paramType[funcs[i].paramCount], funcs[i].paramName[funcs[i].paramCount]);
  859. funcParamsStart = c + 1;
  860. if (linePtr[c + 1] == ' ') funcParamsStart += 1;
  861. funcs[i].paramCount++; // Move to next parameter
  862. }
  863. else if (linePtr[c] == ')')
  864. {
  865. funcEnd = c + 2;
  866. // Check if there are no parameters
  867. if ((funcEnd - funcParamsStart == 2) ||
  868. ((linePtr[c - 4] == 'v') &&
  869. (linePtr[c - 3] == 'o') &&
  870. (linePtr[c - 2] == 'i') &&
  871. (linePtr[c - 1] == 'd'))) {
  872. break;
  873. }
  874. // Get parameter type + name, extract info
  875. char funcParamTypeName[128] = { 0 };
  876. int funcParamTypeNameLen = c - funcParamsStart;
  877. MemoryCopy(funcParamTypeName, &linePtr[funcParamsStart], funcParamTypeNameLen);
  878. GetDataTypeAndName(funcParamTypeName, funcParamTypeNameLen, funcs[i].paramType[funcs[i].paramCount], funcs[i].paramName[funcs[i].paramCount]);
  879. funcs[i].paramCount++; // Move to next parameter
  880. break;
  881. }
  882. }
  883. // Get function description
  884. GetDescription(&linePtr[funcEnd], funcs[i].desc);
  885. // Move array sizes from name to type
  886. for (int j = 0; j < funcs[i].paramCount; j++)
  887. {
  888. MoveArraySize(funcs[i].paramName[j], funcs[i].paramType[j]);
  889. }
  890. }
  891. free(funcLines);
  892. for (int i = 0; i < linesCount; i++) free(lines[i]);
  893. free(lines);
  894. // At this point, all raylib data has been parsed!
  895. //----------------------------------------------------------------------------------
  896. // defines[] -> We have all the defines decomposed into pieces for further analysis
  897. // structs[] -> We have all the structs decomposed into pieces for further analysis
  898. // aliases[] -> We have all the aliases decomposed into pieces for further analysis
  899. // enums[] -> We have all the enums decomposed into pieces for further analysis
  900. // callbacks[] -> We have all the callbacks decomposed into pieces for further analysis
  901. // funcs[] -> We have all the functions decomposed into pieces for further analysis
  902. printf("\nInput file: %s", inFileName);
  903. printf("\nOutput file: %s", outFileName);
  904. if (outputFormat == DEFAULT) printf("\nOutput format: DEFAULT\n\n");
  905. else if (outputFormat == JSON) printf("\nOutput format: JSON\n\n");
  906. else if (outputFormat == XML) printf("\nOutput format: XML\n\n");
  907. else if (outputFormat == LUA) printf("\nOutput format: LUA\n\n");
  908. else if (outputFormat == CODE) printf("\nOutput format: CODE\n\n");
  909. ExportParsedData(outFileName, outputFormat);
  910. free(defines);
  911. free(structs);
  912. free(aliases);
  913. free(enums);
  914. free(callbacks);
  915. free(funcs);
  916. }
  917. //----------------------------------------------------------------------------------
  918. // Module Functions Definition
  919. //----------------------------------------------------------------------------------
  920. // Show command line usage info
  921. static void ShowCommandLineInfo(void)
  922. {
  923. printf("\n//////////////////////////////////////////////////////////////////////////////////\n");
  924. printf("// //\n");
  925. printf("// raylib API parser //\n");
  926. printf("// //\n");
  927. printf("// more info and bugs-report: github.com/raysan5/raylib/parser //\n");
  928. printf("// //\n");
  929. printf("// Copyright (c) 2021-2024 Ramon Santamaria (@raysan5) //\n");
  930. printf("// //\n");
  931. printf("//////////////////////////////////////////////////////////////////////////////////\n\n");
  932. printf("USAGE:\n\n");
  933. printf(" > raylib_parser [--help] [--input <filename.h>] [--output <filename.ext>] [--format <type>]\n");
  934. printf("\nOPTIONS:\n\n");
  935. printf(" -h, --help : Show tool version and command line usage help\n\n");
  936. printf(" -i, --input <filename.h> : Define input header file to parse.\n");
  937. printf(" NOTE: If not specified, defaults to: raylib.h\n\n");
  938. printf(" -o, --output <filename.ext> : Define output file and format.\n");
  939. printf(" Supported extensions: .txt, .json, .xml, .lua, .h\n");
  940. printf(" NOTE: If not specified, defaults to: raylib_api.txt\n\n");
  941. printf(" -f, --format <type> : Define output format for parser data.\n");
  942. printf(" Supported types: DEFAULT, JSON, XML, LUA, CODE\n\n");
  943. printf(" -d, --define <DEF> : Define functions specifiers (i.e. RLAPI for raylib.h, RMAPI for raymath.h, etc.)\n");
  944. printf(" NOTE: If no specifier defined, defaults to: RLAPI\n\n");
  945. printf(" -t, --truncate <after> : Define string to truncate input after (i.e. \"RLGL IMPLEMENTATION\" for rlgl.h)\n");
  946. printf(" NOTE: If not specified, the full input file is parsed.\n\n");
  947. printf("\nEXAMPLES:\n\n");
  948. printf(" > raylib_parser --input raylib.h --output api.json\n");
  949. printf(" Process <raylib.h> to generate <api.json>\n\n");
  950. printf(" > raylib_parser --output raylib_data.info --format XML\n");
  951. printf(" Process <raylib.h> to generate <raylib_data.info> as XML text data\n\n");
  952. printf(" > raylib_parser --input raymath.h --output raymath_data.info --format XML --define RMAPI\n");
  953. printf(" Process <raymath.h> to generate <raymath_data.info> as XML text data\n\n");
  954. }
  955. // Process command line arguments
  956. static void ProcessCommandLine(int argc, char *argv[])
  957. {
  958. for (int i = 1; i < argc; i++)
  959. {
  960. if (IsTextEqual(argv[i], "-h", 2) || IsTextEqual(argv[i], "--help", 6))
  961. {
  962. // Show info
  963. ShowCommandLineInfo();
  964. exit(0);
  965. }
  966. else if (IsTextEqual(argv[i], "-i", 2) || IsTextEqual(argv[i], "--input", 7))
  967. {
  968. // Check for valid argument and valid file extension
  969. if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
  970. {
  971. MemoryCopy(inFileName, argv[i + 1], TextLength(argv[i + 1])); // Read input filename
  972. i++;
  973. }
  974. else printf("WARNING: No input file provided\n");
  975. }
  976. else if (IsTextEqual(argv[i], "-o", 2) || IsTextEqual(argv[i], "--output", 8))
  977. {
  978. if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
  979. {
  980. MemoryCopy(outFileName, argv[i + 1], TextLength(argv[i + 1])); // Read output filename
  981. i++;
  982. }
  983. else printf("WARNING: No output file provided\n");
  984. }
  985. else if (IsTextEqual(argv[i], "-f", 2) || IsTextEqual(argv[i], "--format", 8))
  986. {
  987. if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
  988. {
  989. if (IsTextEqual(argv[i + 1], "DEFAULT\0", 8)) outputFormat = DEFAULT;
  990. else if (IsTextEqual(argv[i + 1], "JSON\0", 5)) outputFormat = JSON;
  991. else if (IsTextEqual(argv[i + 1], "XML\0", 4)) outputFormat = XML;
  992. else if (IsTextEqual(argv[i + 1], "LUA\0", 4)) outputFormat = LUA;
  993. else if (IsTextEqual(argv[i + 1], "CODE\0", 4)) outputFormat = CODE;
  994. }
  995. else printf("WARNING: No format parameters provided\n");
  996. }
  997. else if (IsTextEqual(argv[i], "-d", 2) || IsTextEqual(argv[i], "--define", 8))
  998. {
  999. if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
  1000. {
  1001. MemoryCopy(apiDefine, argv[i + 1], TextLength(argv[i + 1])); // Read functions define
  1002. apiDefine[TextLength(argv[i + 1])] = '\0';
  1003. i++;
  1004. }
  1005. else printf("WARNING: No define key provided\n");
  1006. }
  1007. else if (IsTextEqual(argv[i], "-t", 2) || IsTextEqual(argv[i], "--truncate", 10))
  1008. {
  1009. if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
  1010. {
  1011. MemoryCopy(truncAfter, argv[i + 1], TextLength(argv[i + 1])); // Read truncate marker
  1012. truncAfter[TextLength(argv[i + 1])] = '\0';
  1013. i++;
  1014. }
  1015. }
  1016. }
  1017. }
  1018. // Load text data from file, returns a '\0' terminated string
  1019. // NOTE: text chars array should be freed manually
  1020. static char *LoadFileText(const char *fileName, int *length)
  1021. {
  1022. char *text = NULL;
  1023. if (fileName != NULL)
  1024. {
  1025. FILE *file = fopen(fileName, "rt");
  1026. if (file != NULL)
  1027. {
  1028. // WARNING: When reading a file as 'text' file,
  1029. // text mode causes carriage return-linefeed translation...
  1030. // ...but using fseek() should return correct byte-offset
  1031. fseek(file, 0, SEEK_END);
  1032. int size = ftell(file);
  1033. fseek(file, 0, SEEK_SET);
  1034. if (size > 0)
  1035. {
  1036. text = (char *)calloc((size + 1), sizeof(char));
  1037. unsigned int count = (unsigned int)fread(text, sizeof(char), size, file);
  1038. // WARNING: \r\n is converted to \n on reading, so,
  1039. // read bytes count gets reduced by the number of lines
  1040. if (count < (unsigned int)size)
  1041. {
  1042. text = realloc(text, count + 1);
  1043. *length = count;
  1044. }
  1045. else *length = size;
  1046. // Zero-terminate the string
  1047. text[count] = '\0';
  1048. }
  1049. fclose(file);
  1050. }
  1051. }
  1052. return text;
  1053. }
  1054. // Get all lines from a text buffer (expecting lines ending with '\n')
  1055. static char **GetTextLines(const char *buffer, int length, int *linesCount)
  1056. {
  1057. // Get the number of lines in the text
  1058. int count = 0;
  1059. for (int i = 0; i < length; i++) if (buffer[i] == '\n') count++;
  1060. printf("Number of text lines in buffer: %i\n", count);
  1061. // Allocate as many pointers as lines
  1062. char **lines = (char **)malloc(count*sizeof(char **));
  1063. char *bufferPtr = (char *)buffer;
  1064. for (int i = 0; (i < count) || (bufferPtr[0] != '\0'); i++)
  1065. {
  1066. lines[i] = (char *)calloc(MAX_LINE_LENGTH, sizeof(char));
  1067. // Remove line leading spaces
  1068. // Find last index of space/tab character
  1069. int index = 0;
  1070. while ((bufferPtr[index] == ' ') || (bufferPtr[index] == '\t')) index++;
  1071. int j = 0;
  1072. while (bufferPtr[index + j] != '\n' && bufferPtr[index + j] != '\0')
  1073. {
  1074. lines[i][j] = bufferPtr[index + j];
  1075. j++;
  1076. }
  1077. bufferPtr += (index + j + 1);
  1078. }
  1079. *linesCount = count;
  1080. return lines;
  1081. }
  1082. // Get data type and name from a string containing both
  1083. // NOTE: Useful to parse function parameters and struct fields
  1084. static void GetDataTypeAndName(const char *typeName, int typeNameLen, char *type, char *name)
  1085. {
  1086. for (int k = typeNameLen; k > 0; k--)
  1087. {
  1088. if ((typeName[k] == ' ') && (typeName[k - 1] != ','))
  1089. {
  1090. // Function name starts at this point (and ret type finishes at this point)
  1091. MemoryCopy(type, typeName, k);
  1092. MemoryCopy(name, typeName + k + 1, typeNameLen - k - 1);
  1093. break;
  1094. }
  1095. else if (typeName[k] == '*')
  1096. {
  1097. MemoryCopy(type, typeName, k + 1);
  1098. MemoryCopy(name, typeName + k + 1, typeNameLen - k - 1);
  1099. break;
  1100. }
  1101. else if ((typeName[k] == '.') && (typeNameLen == 3)) // Handle varargs ...);
  1102. {
  1103. const char *varargsDots = "...";
  1104. const char *varargsArg = "args";
  1105. MemoryCopy(type, varargsDots, TextLength(varargsDots));
  1106. MemoryCopy(name, varargsArg, TextLength(varargsArg));
  1107. break;
  1108. }
  1109. }
  1110. }
  1111. // Get comment from a line, do nothing if no comment in line
  1112. static void GetDescription(const char *line, char *description)
  1113. {
  1114. int c = 0;
  1115. int descStart = -1;
  1116. int lastSlash = -2;
  1117. bool isValid = false;
  1118. while (line[c] != '\0')
  1119. {
  1120. if (isValid && (descStart == -1) && (line[c] != ' ')) descStart = c;
  1121. else if (line[c] == '/')
  1122. {
  1123. if (lastSlash == c - 1) isValid = true;
  1124. lastSlash = c;
  1125. }
  1126. c++;
  1127. }
  1128. if (descStart != -1) MemoryCopy(description, &line[descStart], c - descStart);
  1129. }
  1130. // Move array size from name to type
  1131. static void MoveArraySize(char *name, char *type)
  1132. {
  1133. int nameLength = TextLength(name);
  1134. if (name[nameLength - 1] == ']')
  1135. {
  1136. for (int k = nameLength; k > 0; k--)
  1137. {
  1138. if (name[k] == '[')
  1139. {
  1140. int sizeLength = nameLength - k;
  1141. MemoryCopy(&type[TextLength(type)], &name[k], sizeLength);
  1142. name[k] = '\0';
  1143. }
  1144. }
  1145. }
  1146. }
  1147. // Get text length in bytes, check for \0 character
  1148. static unsigned int TextLength(const char *text)
  1149. {
  1150. unsigned int length = 0;
  1151. if (text != NULL) while (*text++) length++;
  1152. return length;
  1153. }
  1154. // Compare two text strings, requires number of characters to compare
  1155. static bool IsTextEqual(const char *text1, const char *text2, unsigned int count)
  1156. {
  1157. bool result = true;
  1158. for (unsigned int i = 0; i < count; i++)
  1159. {
  1160. if (text1[i] != text2[i])
  1161. {
  1162. result = false;
  1163. break;
  1164. }
  1165. }
  1166. return result;
  1167. }
  1168. // Find first text occurrence within a string
  1169. int TextFindIndex(const char *text, const char *find)
  1170. {
  1171. int textLen = TextLength(text);
  1172. int findLen = TextLength(find);
  1173. for (int i = 0; i <= textLen - findLen; i++)
  1174. {
  1175. if (IsTextEqual(&text[i], find, findLen)) return i;
  1176. }
  1177. return -1;
  1178. }
  1179. // Custom memcpy() to avoid <string.h>
  1180. static void MemoryCopy(void *dest, const void *src, unsigned int count)
  1181. {
  1182. char *srcPtr = (char *)src;
  1183. char *destPtr = (char *)dest;
  1184. for (unsigned int i = 0; i < count; i++) destPtr[i] = srcPtr[i];
  1185. }
  1186. // Escape backslashes in a string, writing the escaped string into a static buffer
  1187. static char *EscapeBackslashes(char *text)
  1188. {
  1189. static char buffer[256] = { 0 };
  1190. int count = 0;
  1191. for (int i = 0; (text[i] != '\0') && (i < 255); i++, count++)
  1192. {
  1193. buffer[count] = text[i];
  1194. if (text[i] == '\\')
  1195. {
  1196. buffer[count + 1] = '\\';
  1197. count++;
  1198. }
  1199. }
  1200. buffer[count] = '\0';
  1201. return buffer;
  1202. }
  1203. // Get string of define type
  1204. static const char *StrDefineType(DefineType type)
  1205. {
  1206. switch (type)
  1207. {
  1208. case UNKNOWN: return "UNKNOWN";
  1209. case GUARD: return "GUARD";
  1210. case MACRO: return "MACRO";
  1211. case INT: return "INT";
  1212. case INT_MATH: return "INT_MATH";
  1213. case LONG: return "LONG";
  1214. case LONG_MATH: return "LONG_MATH";
  1215. case FLOAT: return "FLOAT";
  1216. case FLOAT_MATH: return "FLOAT_MATH";
  1217. case DOUBLE: return "DOUBLE";
  1218. case DOUBLE_MATH: return "DOUBLE_MATH";
  1219. case CHAR: return "CHAR";
  1220. case STRING: return "STRING";
  1221. case COLOR: return "COLOR";
  1222. }
  1223. return "";
  1224. }
  1225. /*
  1226. // Replace text string
  1227. // REQUIRES: strlen(), strstr(), strncpy(), strcpy() -> TODO: Replace by custom implementations!
  1228. // WARNING: Returned buffer must be freed by the user (if return != NULL)
  1229. static char *TextReplace(char *text, const char *replace, const char *by)
  1230. {
  1231. // Sanity checks and initialization
  1232. if (!text || !replace || !by) return NULL;
  1233. char *result;
  1234. char *insertPoint; // Next insert point
  1235. char *temp; // Temp pointer
  1236. int replaceLen; // Replace string length of (the string to remove)
  1237. int byLen; // Replacement length (the string to replace replace by)
  1238. int lastReplacePos; // Distance between replace and end of last replace
  1239. int count; // Number of replacements
  1240. replaceLen = strlen(replace);
  1241. if (replaceLen == 0) return NULL; // Empty replace causes infinite loop during count
  1242. byLen = strlen(by);
  1243. // Count the number of replacements needed
  1244. insertPoint = text;
  1245. for (count = 0; (temp = strstr(insertPoint, replace)); count++) insertPoint = temp + replaceLen;
  1246. // Allocate returning string and point temp to it
  1247. temp = result = (char *)malloc(strlen(text) + (byLen - replaceLen)*count + 1);
  1248. if (!result) return NULL; // Memory could not be allocated
  1249. // First time through the loop, all the variable are set correctly from here on,
  1250. // - 'temp' points to the end of the result string
  1251. // - 'insertPoint' points to the next occurrence of replace in text
  1252. // - 'text' points to the remainder of text after "end of replace"
  1253. while (count--)
  1254. {
  1255. insertPoint = strstr(text, replace);
  1256. lastReplacePos = (int)(insertPoint - text);
  1257. temp = strncpy(temp, text, lastReplacePos) + lastReplacePos;
  1258. temp = strcpy(temp, by) + byLen;
  1259. text += lastReplacePos + replaceLen; // Move to next "end of replace"
  1260. }
  1261. // Copy remaind text part after replacement to result (pointed by moving temp)
  1262. strcpy(temp, text);
  1263. return result;
  1264. }
  1265. */
  1266. // Export parsed data in desired format
  1267. static void ExportParsedData(const char *fileName, int format)
  1268. {
  1269. FILE *outFile = fopen(fileName, "wt");
  1270. switch (format)
  1271. {
  1272. case DEFAULT:
  1273. {
  1274. // Print defines info
  1275. fprintf(outFile, "\nDefines found: %i\n\n", defineCount);
  1276. for (int i = 0; i < defineCount; i++)
  1277. {
  1278. fprintf(outFile, "Define %03i: %s\n", i + 1, defines[i].name);
  1279. fprintf(outFile, " Name: %s\n", defines[i].name);
  1280. fprintf(outFile, " Type: %s\n", StrDefineType(defines[i].type));
  1281. fprintf(outFile, " Value: %s\n", defines[i].value);
  1282. fprintf(outFile, " Description: %s\n", defines[i].desc);
  1283. }
  1284. // Print structs info
  1285. fprintf(outFile, "\nStructures found: %i\n\n", structCount);
  1286. for (int i = 0; i < structCount; i++)
  1287. {
  1288. fprintf(outFile, "Struct %02i: %s (%i fields)\n", i + 1, structs[i].name, structs[i].fieldCount);
  1289. fprintf(outFile, " Name: %s\n", structs[i].name);
  1290. fprintf(outFile, " Description: %s\n", structs[i].desc);
  1291. for (int f = 0; f < structs[i].fieldCount; f++)
  1292. {
  1293. fprintf(outFile, " Field[%i]: %s %s ", f + 1, structs[i].fieldType[f], structs[i].fieldName[f]);
  1294. if (structs[i].fieldDesc[f][0]) fprintf(outFile, "// %s\n", structs[i].fieldDesc[f]);
  1295. else fprintf(outFile, "\n");
  1296. }
  1297. }
  1298. // Print aliases info
  1299. fprintf(outFile, "\nAliases found: %i\n\n", aliasCount);
  1300. for (int i = 0; i < aliasCount; i++)
  1301. {
  1302. fprintf(outFile, "Alias %03i: %s\n", i + 1, aliases[i].name);
  1303. fprintf(outFile, " Type: %s\n", aliases[i].type);
  1304. fprintf(outFile, " Name: %s\n", aliases[i].name);
  1305. fprintf(outFile, " Description: %s\n", aliases[i].desc);
  1306. }
  1307. // Print enums info
  1308. fprintf(outFile, "\nEnums found: %i\n\n", enumCount);
  1309. for (int i = 0; i < enumCount; i++)
  1310. {
  1311. fprintf(outFile, "Enum %02i: %s (%i values)\n", i + 1, enums[i].name, enums[i].valueCount);
  1312. fprintf(outFile, " Name: %s\n", enums[i].name);
  1313. fprintf(outFile, " Description: %s\n", enums[i].desc);
  1314. for (int e = 0; e < enums[i].valueCount; e++) fprintf(outFile, " Value[%s]: %i\n", enums[i].valueName[e], enums[i].valueInteger[e]);
  1315. }
  1316. // Print callbacks info
  1317. fprintf(outFile, "\nCallbacks found: %i\n\n", callbackCount);
  1318. for (int i = 0; i < callbackCount; i++)
  1319. {
  1320. fprintf(outFile, "Callback %03i: %s() (%i input parameters)\n", i + 1, callbacks[i].name, callbacks[i].paramCount);
  1321. fprintf(outFile, " Name: %s\n", callbacks[i].name);
  1322. fprintf(outFile, " Return type: %s\n", callbacks[i].retType);
  1323. fprintf(outFile, " Description: %s\n", callbacks[i].desc);
  1324. for (int p = 0; p < callbacks[i].paramCount; p++) fprintf(outFile, " Param[%i]: %s (type: %s)\n", p + 1, callbacks[i].paramName[p], callbacks[i].paramType[p]);
  1325. if (callbacks[i].paramCount == 0) fprintf(outFile, " No input parameters\n");
  1326. }
  1327. // Print functions info
  1328. fprintf(outFile, "\nFunctions found: %i\n\n", funcCount);
  1329. for (int i = 0; i < funcCount; i++)
  1330. {
  1331. fprintf(outFile, "Function %03i: %s() (%i input parameters)\n", i + 1, funcs[i].name, funcs[i].paramCount);
  1332. fprintf(outFile, " Name: %s\n", funcs[i].name);
  1333. fprintf(outFile, " Return type: %s\n", funcs[i].retType);
  1334. fprintf(outFile, " Description: %s\n", funcs[i].desc);
  1335. for (int p = 0; p < funcs[i].paramCount; p++) fprintf(outFile, " Param[%i]: %s (type: %s)\n", p + 1, funcs[i].paramName[p], funcs[i].paramType[p]);
  1336. if (funcs[i].paramCount == 0) fprintf(outFile, " No input parameters\n");
  1337. }
  1338. } break;
  1339. case JSON:
  1340. {
  1341. fprintf(outFile, "{\n");
  1342. // Print defines info
  1343. fprintf(outFile, " \"defines\": [\n");
  1344. for (int i = 0; i < defineCount; i++)
  1345. {
  1346. fprintf(outFile, " {\n");
  1347. fprintf(outFile, " \"name\": \"%s\",\n", defines[i].name);
  1348. fprintf(outFile, " \"type\": \"%s\",\n", StrDefineType(defines[i].type));
  1349. if (defines[i].isHex) // INT or LONG
  1350. {
  1351. fprintf(outFile, " \"value\": %ld,\n", strtol(defines[i].value, NULL, 16));
  1352. }
  1353. else if ((defines[i].type == INT) ||
  1354. (defines[i].type == LONG) ||
  1355. (defines[i].type == FLOAT) ||
  1356. (defines[i].type == DOUBLE) ||
  1357. (defines[i].type == STRING))
  1358. {
  1359. fprintf(outFile, " \"value\": %s,\n", defines[i].value);
  1360. }
  1361. else
  1362. {
  1363. fprintf(outFile, " \"value\": \"%s\",\n", defines[i].value);
  1364. }
  1365. fprintf(outFile, " \"description\": \"%s\"\n", defines[i].desc);
  1366. fprintf(outFile, " }");
  1367. if (i < defineCount - 1) fprintf(outFile, ",\n");
  1368. else fprintf(outFile, "\n");
  1369. }
  1370. fprintf(outFile, " ],\n");
  1371. // Print structs info
  1372. fprintf(outFile, " \"structs\": [\n");
  1373. for (int i = 0; i < structCount; i++)
  1374. {
  1375. fprintf(outFile, " {\n");
  1376. fprintf(outFile, " \"name\": \"%s\",\n", structs[i].name);
  1377. fprintf(outFile, " \"description\": \"%s\",\n", EscapeBackslashes(structs[i].desc));
  1378. fprintf(outFile, " \"fields\": [\n");
  1379. for (int f = 0; f < structs[i].fieldCount; f++)
  1380. {
  1381. fprintf(outFile, " {\n");
  1382. fprintf(outFile, " \"type\": \"%s\",\n", structs[i].fieldType[f]);
  1383. fprintf(outFile, " \"name\": \"%s\",\n", structs[i].fieldName[f]);
  1384. fprintf(outFile, " \"description\": \"%s\"\n", EscapeBackslashes(structs[i].fieldDesc[f]));
  1385. fprintf(outFile, " }");
  1386. if (f < structs[i].fieldCount - 1) fprintf(outFile, ",\n");
  1387. else fprintf(outFile, "\n");
  1388. }
  1389. fprintf(outFile, " ]\n");
  1390. fprintf(outFile, " }");
  1391. if (i < structCount - 1) fprintf(outFile, ",\n");
  1392. else fprintf(outFile, "\n");
  1393. }
  1394. fprintf(outFile, " ],\n");
  1395. // Print aliases info
  1396. fprintf(outFile, " \"aliases\": [\n");
  1397. for (int i = 0; i < aliasCount; i++)
  1398. {
  1399. fprintf(outFile, " {\n");
  1400. fprintf(outFile, " \"type\": \"%s\",\n", aliases[i].type);
  1401. fprintf(outFile, " \"name\": \"%s\",\n", aliases[i].name);
  1402. fprintf(outFile, " \"description\": \"%s\"\n", aliases[i].desc);
  1403. fprintf(outFile, " }");
  1404. if (i < aliasCount - 1) fprintf(outFile, ",\n");
  1405. else fprintf(outFile, "\n");
  1406. }
  1407. fprintf(outFile, " ],\n");
  1408. // Print enums info
  1409. fprintf(outFile, " \"enums\": [\n");
  1410. for (int i = 0; i < enumCount; i++)
  1411. {
  1412. fprintf(outFile, " {\n");
  1413. fprintf(outFile, " \"name\": \"%s\",\n", enums[i].name);
  1414. fprintf(outFile, " \"description\": \"%s\",\n", EscapeBackslashes(enums[i].desc));
  1415. fprintf(outFile, " \"values\": [\n");
  1416. for (int e = 0; e < enums[i].valueCount; e++)
  1417. {
  1418. fprintf(outFile, " {\n");
  1419. fprintf(outFile, " \"name\": \"%s\",\n", enums[i].valueName[e]);
  1420. fprintf(outFile, " \"value\": %i,\n", enums[i].valueInteger[e]);
  1421. fprintf(outFile, " \"description\": \"%s\"\n", EscapeBackslashes(enums[i].valueDesc[e]));
  1422. fprintf(outFile, " }");
  1423. if (e < enums[i].valueCount - 1) fprintf(outFile, ",\n");
  1424. else fprintf(outFile, "\n");
  1425. }
  1426. fprintf(outFile, " ]\n");
  1427. fprintf(outFile, " }");
  1428. if (i < enumCount - 1) fprintf(outFile, ",\n");
  1429. else fprintf(outFile, "\n");
  1430. }
  1431. fprintf(outFile, " ],\n");
  1432. // Print callbacks info
  1433. fprintf(outFile, " \"callbacks\": [\n");
  1434. for (int i = 0; i < callbackCount; i++)
  1435. {
  1436. fprintf(outFile, " {\n");
  1437. fprintf(outFile, " \"name\": \"%s\",\n", callbacks[i].name);
  1438. fprintf(outFile, " \"description\": \"%s\",\n", EscapeBackslashes(callbacks[i].desc));
  1439. fprintf(outFile, " \"returnType\": \"%s\"", callbacks[i].retType);
  1440. if (callbacks[i].paramCount == 0) fprintf(outFile, "\n");
  1441. else
  1442. {
  1443. fprintf(outFile, ",\n \"params\": [\n");
  1444. for (int p = 0; p < callbacks[i].paramCount; p++)
  1445. {
  1446. fprintf(outFile, " {\n");
  1447. fprintf(outFile, " \"type\": \"%s\",\n", callbacks[i].paramType[p]);
  1448. fprintf(outFile, " \"name\": \"%s\"\n", callbacks[i].paramName[p]);
  1449. fprintf(outFile, " }");
  1450. if (p < callbacks[i].paramCount - 1) fprintf(outFile, ",\n");
  1451. else fprintf(outFile, "\n");
  1452. }
  1453. fprintf(outFile, " ]\n");
  1454. }
  1455. fprintf(outFile, " }");
  1456. if (i < callbackCount - 1) fprintf(outFile, ",\n");
  1457. else fprintf(outFile, "\n");
  1458. }
  1459. fprintf(outFile, " ],\n");
  1460. // Print functions info
  1461. fprintf(outFile, " \"functions\": [\n");
  1462. for (int i = 0; i < funcCount; i++)
  1463. {
  1464. fprintf(outFile, " {\n");
  1465. fprintf(outFile, " \"name\": \"%s\",\n", funcs[i].name);
  1466. fprintf(outFile, " \"description\": \"%s\",\n", EscapeBackslashes(funcs[i].desc));
  1467. fprintf(outFile, " \"returnType\": \"%s\"", funcs[i].retType);
  1468. if (funcs[i].paramCount == 0) fprintf(outFile, "\n");
  1469. else
  1470. {
  1471. fprintf(outFile, ",\n \"params\": [\n");
  1472. for (int p = 0; p < funcs[i].paramCount; p++)
  1473. {
  1474. fprintf(outFile, " {\n");
  1475. fprintf(outFile, " \"type\": \"%s\",\n", funcs[i].paramType[p]);
  1476. fprintf(outFile, " \"name\": \"%s\"\n", funcs[i].paramName[p]);
  1477. fprintf(outFile, " }");
  1478. if (p < funcs[i].paramCount - 1) fprintf(outFile, ",\n");
  1479. else fprintf(outFile, "\n");
  1480. }
  1481. fprintf(outFile, " ]\n");
  1482. }
  1483. fprintf(outFile, " }");
  1484. if (i < funcCount - 1) fprintf(outFile, ",\n");
  1485. else fprintf(outFile, "\n");
  1486. }
  1487. fprintf(outFile, " ]\n");
  1488. fprintf(outFile, "}\n");
  1489. } break;
  1490. case XML:
  1491. {
  1492. // XML format to export data:
  1493. /*
  1494. <?xml version="1.0" encoding="Windows-1252" ?>
  1495. <raylibAPI>
  1496. <Defines count="">
  1497. <Define name="" type="" value="" desc="" />
  1498. </Defines>
  1499. <Structs count="">
  1500. <Struct name="" fieldCount="" desc="">
  1501. <Field type="" name="" desc="" />
  1502. <Field type="" name="" desc="" />
  1503. </Struct>
  1504. <Structs>
  1505. <Aliases count="">
  1506. <Alias type="" name="" desc="" />
  1507. </Aliases>
  1508. <Enums count="">
  1509. <Enum name="" valueCount="" desc="">
  1510. <Value name="" integer="" desc="" />
  1511. <Value name="" integer="" desc="" />
  1512. </Enum>
  1513. </Enums>
  1514. <Callbacks count="">
  1515. <Callback name="" retType="" paramCount="" desc="">
  1516. <Param type="" name="" desc="" />
  1517. <Param type="" name="" desc="" />
  1518. </Callback>
  1519. </Callbacks>
  1520. <Functions count="">
  1521. <Function name="" retType="" paramCount="" desc="">
  1522. <Param type="" name="" desc="" />
  1523. <Param type="" name="" desc="" />
  1524. </Function>
  1525. </Functions>
  1526. </raylibAPI>
  1527. */
  1528. fprintf(outFile, "<?xml version=\"1.0\" encoding=\"Windows-1252\" ?>\n");
  1529. fprintf(outFile, "<raylibAPI>\n");
  1530. // Print defines info
  1531. fprintf(outFile, " <Defines count=\"%i\">\n", defineCount);
  1532. for (int i = 0; i < defineCount; i++)
  1533. {
  1534. fprintf(outFile, " <Define name=\"%s\" type=\"%s\" ", defines[i].name, StrDefineType(defines[i].type));
  1535. if (defines[i].type == STRING)
  1536. {
  1537. fprintf(outFile, "value=%s", defines[i].value);
  1538. }
  1539. else
  1540. {
  1541. fprintf(outFile, "value=\"%s\"", defines[i].value);
  1542. }
  1543. fprintf(outFile, " desc=\"%s\" />\n", defines[i].desc);
  1544. }
  1545. fprintf(outFile, " </Defines>\n");
  1546. // Print structs info
  1547. fprintf(outFile, " <Structs count=\"%i\">\n", structCount);
  1548. for (int i = 0; i < structCount; i++)
  1549. {
  1550. fprintf(outFile, " <Struct name=\"%s\" fieldCount=\"%i\" desc=\"%s\">\n", structs[i].name, structs[i].fieldCount, structs[i].desc);
  1551. for (int f = 0; f < structs[i].fieldCount; f++)
  1552. {
  1553. fprintf(outFile, " <Field type=\"%s\" name=\"%s\" desc=\"%s\" />\n", structs[i].fieldType[f], structs[i].fieldName[f], structs[i].fieldDesc[f]);
  1554. }
  1555. fprintf(outFile, " </Struct>\n");
  1556. }
  1557. fprintf(outFile, " </Structs>\n");
  1558. // Print aliases info
  1559. fprintf(outFile, " <Aliases count=\"%i\">\n", aliasCount);
  1560. for (int i = 0; i < aliasCount; i++)
  1561. {
  1562. fprintf(outFile, " <Alias type=\"%s\" name=\"%s\" desc=\"%s\" />\n", aliases[i].name, aliases[i].type, aliases[i].desc);
  1563. }
  1564. fprintf(outFile, " </Aliases>\n");
  1565. // Print enums info
  1566. fprintf(outFile, " <Enums count=\"%i\">\n", enumCount);
  1567. for (int i = 0; i < enumCount; i++)
  1568. {
  1569. fprintf(outFile, " <Enum name=\"%s\" valueCount=\"%i\" desc=\"%s\">\n", enums[i].name, enums[i].valueCount, enums[i].desc);
  1570. for (int v = 0; v < enums[i].valueCount; v++)
  1571. {
  1572. fprintf(outFile, " <Value name=\"%s\" integer=\"%i\" desc=\"%s\" />\n", enums[i].valueName[v], enums[i].valueInteger[v], enums[i].valueDesc[v]);
  1573. }
  1574. fprintf(outFile, " </Enum>\n");
  1575. }
  1576. fprintf(outFile, " </Enums>\n");
  1577. // Print callbacks info
  1578. fprintf(outFile, " <Callbacks count=\"%i\">\n", callbackCount);
  1579. for (int i = 0; i < callbackCount; i++)
  1580. {
  1581. fprintf(outFile, " <Callback name=\"%s\" retType=\"%s\" paramCount=\"%i\" desc=\"%s\">\n", callbacks[i].name, callbacks[i].retType, callbacks[i].paramCount, callbacks[i].desc);
  1582. for (int p = 0; p < callbacks[i].paramCount; p++)
  1583. {
  1584. fprintf(outFile, " <Param type=\"%s\" name=\"%s\" desc=\"%s\" />\n", callbacks[i].paramType[p], callbacks[i].paramName[p], callbacks[i].paramDesc[p]);
  1585. }
  1586. fprintf(outFile, " </Callback>\n");
  1587. }
  1588. fprintf(outFile, " </Callbacks>\n");
  1589. // Print functions info
  1590. fprintf(outFile, " <Functions count=\"%i\">\n", funcCount);
  1591. for (int i = 0; i < funcCount; i++)
  1592. {
  1593. fprintf(outFile, " <Function name=\"%s\" retType=\"%s\" paramCount=\"%i\" desc=\"%s\">\n", funcs[i].name, funcs[i].retType, funcs[i].paramCount, funcs[i].desc);
  1594. for (int p = 0; p < funcs[i].paramCount; p++)
  1595. {
  1596. fprintf(outFile, " <Param type=\"%s\" name=\"%s\" desc=\"%s\" />\n", funcs[i].paramType[p], funcs[i].paramName[p], funcs[i].paramDesc[p]);
  1597. }
  1598. fprintf(outFile, " </Function>\n");
  1599. }
  1600. fprintf(outFile, " </Functions>\n");
  1601. fprintf(outFile, "</raylibAPI>\n");
  1602. } break;
  1603. case LUA:
  1604. {
  1605. fprintf(outFile, "return {\n");
  1606. // Print defines info
  1607. fprintf(outFile, " defines = {\n");
  1608. for (int i = 0; i < defineCount; i++)
  1609. {
  1610. fprintf(outFile, " {\n");
  1611. fprintf(outFile, " name = \"%s\",\n", defines[i].name);
  1612. fprintf(outFile, " type = \"%s\",\n", StrDefineType(defines[i].type));
  1613. if ((defines[i].type == INT) ||
  1614. (defines[i].type == LONG) ||
  1615. (defines[i].type == FLOAT) ||
  1616. (defines[i].type == DOUBLE) ||
  1617. (defines[i].type == STRING))
  1618. {
  1619. fprintf(outFile, " value = %s,\n", defines[i].value);
  1620. }
  1621. else
  1622. {
  1623. fprintf(outFile, " value = \"%s\",\n", defines[i].value);
  1624. }
  1625. fprintf(outFile, " description = \"%s\"\n", defines[i].desc);
  1626. fprintf(outFile, " }");
  1627. if (i < defineCount - 1) fprintf(outFile, ",\n");
  1628. else fprintf(outFile, "\n");
  1629. }
  1630. fprintf(outFile, " },\n");
  1631. // Print structs info
  1632. fprintf(outFile, " structs = {\n");
  1633. for (int i = 0; i < structCount; i++)
  1634. {
  1635. fprintf(outFile, " {\n");
  1636. fprintf(outFile, " name = \"%s\",\n", structs[i].name);
  1637. fprintf(outFile, " description = \"%s\",\n", EscapeBackslashes(structs[i].desc));
  1638. fprintf(outFile, " fields = {\n");
  1639. for (int f = 0; f < structs[i].fieldCount; f++)
  1640. {
  1641. fprintf(outFile, " {\n");
  1642. fprintf(outFile, " type = \"%s\",\n", structs[i].fieldType[f]);
  1643. fprintf(outFile, " name = \"%s\",\n", structs[i].fieldName[f]);
  1644. fprintf(outFile, " description = \"%s\"\n", EscapeBackslashes(structs[i].fieldDesc[f]));
  1645. fprintf(outFile, " }");
  1646. if (f < structs[i].fieldCount - 1) fprintf(outFile, ",\n");
  1647. else fprintf(outFile, "\n");
  1648. }
  1649. fprintf(outFile, " }\n");
  1650. fprintf(outFile, " }");
  1651. if (i < structCount - 1) fprintf(outFile, ",\n");
  1652. else fprintf(outFile, "\n");
  1653. }
  1654. fprintf(outFile, " },\n");
  1655. // Print aliases info
  1656. fprintf(outFile, " aliases = {\n");
  1657. for (int i = 0; i < aliasCount; i++)
  1658. {
  1659. fprintf(outFile, " {\n");
  1660. fprintf(outFile, " type = \"%s\",\n", aliases[i].type);
  1661. fprintf(outFile, " name = \"%s\",\n", aliases[i].name);
  1662. fprintf(outFile, " description = \"%s\"\n", aliases[i].desc);
  1663. fprintf(outFile, " }");
  1664. if (i < aliasCount - 1) fprintf(outFile, ",\n");
  1665. else fprintf(outFile, "\n");
  1666. }
  1667. fprintf(outFile, " },\n");
  1668. // Print enums info
  1669. fprintf(outFile, " enums = {\n");
  1670. for (int i = 0; i < enumCount; i++)
  1671. {
  1672. fprintf(outFile, " {\n");
  1673. fprintf(outFile, " name = \"%s\",\n", enums[i].name);
  1674. fprintf(outFile, " description = \"%s\",\n", EscapeBackslashes(enums[i].desc));
  1675. fprintf(outFile, " values = {\n");
  1676. for (int e = 0; e < enums[i].valueCount; e++)
  1677. {
  1678. fprintf(outFile, " {\n");
  1679. fprintf(outFile, " name = \"%s\",\n", enums[i].valueName[e]);
  1680. fprintf(outFile, " value = %i,\n", enums[i].valueInteger[e]);
  1681. fprintf(outFile, " description = \"%s\"\n", EscapeBackslashes(enums[i].valueDesc[e]));
  1682. fprintf(outFile, " }");
  1683. if (e < enums[i].valueCount - 1) fprintf(outFile, ",\n");
  1684. else fprintf(outFile, "\n");
  1685. }
  1686. fprintf(outFile, " }\n");
  1687. fprintf(outFile, " }");
  1688. if (i < enumCount - 1) fprintf(outFile, ",\n");
  1689. else fprintf(outFile, "\n");
  1690. }
  1691. fprintf(outFile, " },\n");
  1692. // Print callbacks info
  1693. fprintf(outFile, " callbacks = {\n");
  1694. for (int i = 0; i < callbackCount; i++)
  1695. {
  1696. fprintf(outFile, " {\n");
  1697. fprintf(outFile, " name = \"%s\",\n", callbacks[i].name);
  1698. fprintf(outFile, " description = \"%s\",\n", EscapeBackslashes(callbacks[i].desc));
  1699. fprintf(outFile, " returnType = \"%s\"", callbacks[i].retType);
  1700. if (callbacks[i].paramCount == 0) fprintf(outFile, "\n");
  1701. else
  1702. {
  1703. fprintf(outFile, ",\n params = {\n");
  1704. for (int p = 0; p < callbacks[i].paramCount; p++)
  1705. {
  1706. fprintf(outFile, " {type = \"%s\", name = \"%s\"}", callbacks[i].paramType[p], callbacks[i].paramName[p]);
  1707. if (p < callbacks[i].paramCount - 1) fprintf(outFile, ",\n");
  1708. else fprintf(outFile, "\n");
  1709. }
  1710. fprintf(outFile, " }\n");
  1711. }
  1712. fprintf(outFile, " }");
  1713. if (i < callbackCount - 1) fprintf(outFile, ",\n");
  1714. else fprintf(outFile, "\n");
  1715. }
  1716. fprintf(outFile, " },\n");
  1717. // Print functions info
  1718. fprintf(outFile, " functions = {\n");
  1719. for (int i = 0; i < funcCount; i++)
  1720. {
  1721. fprintf(outFile, " {\n");
  1722. fprintf(outFile, " name = \"%s\",\n", funcs[i].name);
  1723. fprintf(outFile, " description = \"%s\",\n", EscapeBackslashes(funcs[i].desc));
  1724. fprintf(outFile, " returnType = \"%s\"", funcs[i].retType);
  1725. if (funcs[i].paramCount == 0) fprintf(outFile, "\n");
  1726. else
  1727. {
  1728. fprintf(outFile, ",\n params = {\n");
  1729. for (int p = 0; p < funcs[i].paramCount; p++)
  1730. {
  1731. fprintf(outFile, " {type = \"%s\", name = \"%s\"}", funcs[i].paramType[p], funcs[i].paramName[p]);
  1732. if (p < funcs[i].paramCount - 1) fprintf(outFile, ",\n");
  1733. else fprintf(outFile, "\n");
  1734. }
  1735. fprintf(outFile, " }\n");
  1736. }
  1737. fprintf(outFile, " }");
  1738. if (i < funcCount - 1) fprintf(outFile, ",\n");
  1739. else fprintf(outFile, "\n");
  1740. }
  1741. fprintf(outFile, " }\n");
  1742. fprintf(outFile, "}\n");
  1743. } break;
  1744. case CODE:
  1745. default: break;
  1746. }
  1747. fclose(outFile);
  1748. }