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.

1987 lines
81 KiB

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