您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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