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.

1989 lines
81 KiB

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