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

989 行
39 KiB

  1. /**********************************************************************************************
  2. raylib API parser
  3. This parser scans raylib.h to get API information about structs, enums and functions.
  4. All data is divided into pieces, usually as strings. The following types are used for data:
  5. - struct FunctionInfo
  6. - struct StructInfo
  7. - struct EnumInfo
  8. CONSTRAINTS:
  9. This parser is specifically designed to work with raylib.h, so, it has some constraints:
  10. - Functions are expected as a single line with the following structure:
  11. <retType> <name>(<paramType[0]> <paramName[0]>, <paramType[1]> <paramName[1]>); <desc>
  12. Be careful with functions broken into several lines, it breaks the process!
  13. - Structures are expected as several lines with the following form:
  14. <desc>
  15. typedef struct <name> {
  16. <fieldType[0]> <fieldName[0]>; <fieldDesc[0]>
  17. <fieldType[1]> <fieldName[1]>; <fieldDesc[1]>
  18. <fieldType[2]> <fieldName[2]>; <fieldDesc[2]>
  19. } <name>;
  20. - Enums are expected as several lines with the following form:
  21. <desc>
  22. typedef enum {
  23. <valueName[0]> = <valueInteger[0]>, <valueDesc[0]>
  24. <valueName[1]>,
  25. <valueName[2]>, <valueDesc[2]>
  26. <valueName[3]> <valueDesc[3]>
  27. } <name>;
  28. NOTE: Multiple options are supported for enums:
  29. - If value is not provided, (<valueInteger[i -1]> + 1) is assigned
  30. - Value description can be provided or not
  31. OTHER NOTES:
  32. - This parser could work with other C header files if mentioned constraints are followed.
  33. - This parser does not require <string.h> library, all data is parsed directly from char buffers.
  34. LICENSE: zlib/libpng
  35. raylib-parser is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  36. BSD-like license that allows static linking with closed source software:
  37. Copyright (c) 2021 Ramon Santamaria (@raysan5)
  38. **********************************************************************************************/
  39. #define _CRT_SECURE_NO_WARNINGS
  40. #include <stdlib.h> // Required for: malloc(), calloc(), realloc(), free(), atoi(), strtol()
  41. #include <stdio.h> // Required for: printf(), fopen(), fseek(), ftell(), fread(), fclose()
  42. #include <stdbool.h> // Required for: bool
  43. #define MAX_FUNCS_TO_PARSE 512 // Maximum number of functions to parse
  44. #define MAX_STRUCTS_TO_PARSE 64 // Maximum number of structures to parse
  45. #define MAX_ENUMS_TO_PARSE 64 // Maximum number of enums to parse
  46. #define MAX_LINE_LENGTH 512 // Maximum length of one line (including comments)
  47. #define MAX_STRUCT_LINE_LENGTH 2048 // Maximum length of one struct (multiple lines)
  48. //----------------------------------------------------------------------------------
  49. // Types and Structures Definition
  50. //----------------------------------------------------------------------------------
  51. // Function info data
  52. typedef struct FunctionInfo {
  53. char name[64]; // Function name
  54. char desc[128]; // Function description (comment at the end)
  55. char retType[32]; // Return value type
  56. int paramCount; // Number of function parameters
  57. char paramType[12][32]; // Parameters type (max: 12 parameters)
  58. char paramName[12][32]; // Parameters name (max: 12 parameters)
  59. char paramDesc[12][8]; // Parameters description (max: 12 parameters)
  60. } FunctionInfo;
  61. // Struct info data
  62. typedef struct StructInfo {
  63. char name[64]; // Struct name
  64. char desc[64]; // Struct type description
  65. int fieldCount; // Number of fields in the struct
  66. char fieldType[16][32]; // Field type (max: 16 fields)
  67. char fieldName[16][32]; // Field name (max: 16 fields)
  68. char fieldDesc[16][128]; // Field description (max: 16 fields)
  69. } StructInfo;
  70. // Enum info data
  71. typedef struct EnumInfo {
  72. char name[64]; // Enum name
  73. char desc[64]; // Enum description
  74. int valueCount; // Number of values in enumerator
  75. char valueName[128][64]; // Value name definition (max: 128 values)
  76. int valueInteger[128]; // Value integer (max: 128 values)
  77. char valueDesc[128][64]; // Value description (max: 128 values)
  78. } EnumInfo;
  79. // Output format for parsed data
  80. typedef enum { DEFAULT = 0, JSON, XML } OutputFormat;
  81. //----------------------------------------------------------------------------------
  82. // Global Variables Definition
  83. //----------------------------------------------------------------------------------
  84. static int funcCount = 0;
  85. static int structCount = 0;
  86. static int enumCount = 0;
  87. static FunctionInfo *funcs = NULL;
  88. static StructInfo *structs = NULL;
  89. static EnumInfo *enums = NULL;
  90. // Command line variables
  91. static char inFileName[512] = { 0 }; // Input file name (required in case of provided through CLI)
  92. static char outFileName[512] = { 0 }; // Output file name (required for file save/export)
  93. static int outputFormat = DEFAULT;
  94. //----------------------------------------------------------------------------------
  95. // Module Functions Declaration
  96. //----------------------------------------------------------------------------------
  97. static void ShowCommandLineInfo(void); // Show command line usage info
  98. static void ProcessCommandLine(int argc, char *argv[]); // Process command line input
  99. static char *LoadFileText(const char *fileName, int *length);
  100. static char **GetTextLines(const char *buffer, int length, int *linesCount);
  101. static void GetDataTypeAndName(const char *typeName, int typeNameLen, char *type, char *name);
  102. static unsigned int TextLength(const char *text); // Get text length in bytes, check for \0 character
  103. static bool IsTextEqual(const char *text1, const char *text2, unsigned int count);
  104. static void MemoryCopy(void *dest, const void *src, unsigned int count);
  105. static char* CharReplace(char* text, char search, char replace);
  106. static void ExportParsedData(const char *fileName, int format); // Export parsed data in desired format
  107. //------------------------------------------------------------------------------------
  108. // Program main entry point
  109. //------------------------------------------------------------------------------------
  110. int main(int argc, char* argv[])
  111. {
  112. if (argc > 1) ProcessCommandLine(argc, argv);
  113. if (inFileName[0] == '\0') MemoryCopy(inFileName, "../src/raylib.h\0", 16);
  114. int length = 0;
  115. char *buffer = LoadFileText(inFileName, &length);
  116. // Preprocess buffer to get separate lines
  117. // NOTE: GetTextLines() also removes leading spaces/tabs
  118. int linesCount = 0;
  119. char **lines = GetTextLines(buffer, length, &linesCount);
  120. // Function lines pointers, selected from buffer "lines"
  121. char **funcLines = (char **)malloc(MAX_FUNCS_TO_PARSE*sizeof(char *));
  122. // Structs data (multiple lines), selected from "buffer"
  123. char **structLines = (char **)malloc(MAX_STRUCTS_TO_PARSE*sizeof(char *));
  124. for (int i = 0; i < MAX_STRUCTS_TO_PARSE; i++) structLines[i] = (char *)calloc(MAX_STRUCT_LINE_LENGTH, sizeof(char));
  125. // Enums lines pointers, selected from buffer "lines"
  126. int *enumLines = (int *)malloc(MAX_ENUMS_TO_PARSE*sizeof(int));
  127. // Prepare required lines for parsing
  128. //--------------------------------------------------------------------------------------------------
  129. // Read function lines
  130. for (int i = 0; i < linesCount; i++)
  131. {
  132. // Read function line (starting with "RLAPI")
  133. if (IsTextEqual(lines[i], "RLAPI", 5))
  134. {
  135. // Keep a pointer to the function line
  136. funcLines[funcCount] = lines[i];
  137. funcCount++;
  138. }
  139. }
  140. // Read structs data (multiple lines, read directly from buffer)
  141. // TODO: Parse structs data from "lines" instead of "buffer" -> Easier to get struct definition
  142. for (int i = 0; i < length; i++)
  143. {
  144. // Read struct data (starting with "typedef struct", ending with '} ... ;')
  145. // NOTE: We read it directly from buffer
  146. if (IsTextEqual(buffer + i, "typedef struct", 14))
  147. {
  148. int j = 0;
  149. bool validStruct = false;
  150. // WARNING: Typedefs between types: typedef Vector4 Quaternion;
  151. for (int c = 0; c < 128; c++)
  152. {
  153. if (buffer[i + c] == '{')
  154. {
  155. validStruct = true;
  156. break;
  157. }
  158. else if (buffer[i + c] == ';')
  159. {
  160. // Not valid struct:
  161. // i.e typedef struct rAudioBuffer rAudioBuffer; -> Typedef and forward declaration
  162. i += c;
  163. break;
  164. }
  165. }
  166. if (validStruct)
  167. {
  168. while (buffer[i + j] != '}')
  169. {
  170. structLines[structCount][j] = buffer[i + j];
  171. j++;
  172. }
  173. while (buffer[i + j] != '}')
  174. {
  175. structLines[structCount][j] = buffer[i + j];
  176. j++;
  177. }
  178. while (buffer[i + j] != '\n')
  179. {
  180. structLines[structCount][j] = buffer[i + j];
  181. j++;
  182. }
  183. i += j;
  184. structCount++;
  185. }
  186. }
  187. }
  188. // Read enum lines
  189. for (int i = 0; i < linesCount; i++)
  190. {
  191. // Read function line (starting with "RLAPI")
  192. if (IsTextEqual(lines[i], "typedef enum {", 14))
  193. {
  194. // Keep the line position in the array of lines,
  195. // so, we can scan that position and following lines
  196. enumLines[enumCount] = i;
  197. enumCount++;
  198. }
  199. }
  200. // At this point we have all raylib structs, enums, functions lines data to start parsing
  201. free(buffer); // Unload text buffer
  202. // Parsing raylib data
  203. //--------------------------------------------------------------------------------------------------
  204. // Structs info data
  205. structs = (StructInfo *)calloc(MAX_STRUCTS_TO_PARSE, sizeof(StructInfo));
  206. for (int i = 0; i < structCount; i++)
  207. {
  208. int structLineOffset = 0;
  209. // Get struct name: typedef struct name {
  210. for (int c = 15; c < 64 + 15; c++)
  211. {
  212. if (structLines[i][c] == '{')
  213. {
  214. structLineOffset = c + 2;
  215. MemoryCopy(structs[i].name, &structLines[i][15], c - 15 - 1);
  216. break;
  217. }
  218. }
  219. // Get struct fields and count them -> fields finish with ;
  220. int j = 0;
  221. while (structLines[i][structLineOffset + j] != '}')
  222. {
  223. // WARNING: Some structs have empty spaces and comments -> OK, processed
  224. int fieldStart = 0;
  225. if ((structLines[i][structLineOffset + j] != ' ') && (structLines[i][structLineOffset + j] != '\n')) fieldStart = structLineOffset + j;
  226. if (fieldStart != 0)
  227. {
  228. // Scan one field line
  229. int c = 0;
  230. int fieldEndPos = 0;
  231. char fieldLine[256] = { 0 };
  232. while (structLines[i][structLineOffset + j] != '\n')
  233. {
  234. if (structLines[i][structLineOffset + j] == ';') fieldEndPos = c;
  235. fieldLine[c] = structLines[i][structLineOffset + j];
  236. c++; j++;
  237. }
  238. if (fieldLine[0] != '/') // Field line is not a comment
  239. {
  240. //printf("Struct field: %s_\n", fieldLine); // OK!
  241. // Get struct field type and name
  242. GetDataTypeAndName(fieldLine, fieldEndPos, structs[i].fieldType[structs[i].fieldCount], structs[i].fieldName[structs[i].fieldCount]);
  243. // Get the field description
  244. // We start skipping spaces in front of description comment
  245. int descStart = fieldEndPos;
  246. while ((fieldLine[descStart] != '/') && (fieldLine[descStart] != '\0')) descStart++;
  247. int k = 0;
  248. while ((fieldLine[descStart + k] != '\0') && (fieldLine[descStart + k] != '\n'))
  249. {
  250. structs[i].fieldDesc[structs[i].fieldCount][k] = fieldLine[descStart + k];
  251. k++;
  252. }
  253. structs[i].fieldCount++;
  254. }
  255. }
  256. j++;
  257. }
  258. }
  259. for (int i = 0; i < MAX_STRUCTS_TO_PARSE; i++) free(structLines[i]);
  260. free(structLines);
  261. // Enum info data
  262. enums = (EnumInfo *)calloc(MAX_ENUMS_TO_PARSE, sizeof(EnumInfo));
  263. for (int i = 0; i < enumCount; i++)
  264. {
  265. // TODO: Get enum description from lines[enumLines[i] - 1]
  266. for (int j = 1; j < 256; j++) // Maximum number of lines following enum first line
  267. {
  268. char *linePtr = lines[enumLines[i] + j];
  269. if ((linePtr[0] >= 'A') && (linePtr[0] <= 'Z'))
  270. {
  271. // Parse enum value line, possible options:
  272. //ENUM_VALUE_NAME,
  273. //ENUM_VALUE_NAME
  274. //ENUM_VALUE_NAME = 99
  275. //ENUM_VALUE_NAME = 99,
  276. //ENUM_VALUE_NAME = 0x00000040, // Value description
  277. // We start reading the value name
  278. int c = 0;
  279. while ((linePtr[c] != ',') &&
  280. (linePtr[c] != ' ') &&
  281. (linePtr[c] != '=') &&
  282. (linePtr[c] != '\0')) { enums[i].valueName[enums[i].valueCount][c] = linePtr[c]; c++; }
  283. // After the name we can have:
  284. // '=' -> value is provided
  285. // ',' -> value is equal to previous + 1, there could be a description if not '\0'
  286. // ' ' -> value is equal to previous + 1, there could be a description if not '\0'
  287. // '\0' -> value is equal to previous + 1
  288. // Let's start checking if the line is not finished
  289. if ((linePtr[c] != ',') && (linePtr[c] != '\0'))
  290. {
  291. // Two options:
  292. // '=' -> value is provided
  293. // ' ' -> value is equal to previous + 1, there could be a description if not '\0'
  294. bool foundValue = false;
  295. while (linePtr[c] != '\0')
  296. {
  297. if (linePtr[c] == '=') { foundValue = true; break; }
  298. c++;
  299. }
  300. if (foundValue)
  301. {
  302. if (linePtr[c + 1] == ' ') c += 2;
  303. else c++;
  304. // Parse integer value
  305. int n = 0;
  306. char integer[16] = { 0 };
  307. while ((linePtr[c] != ',') && (linePtr[c] != ' ') && (linePtr[c] != '\0'))
  308. {
  309. integer[n] = linePtr[c];
  310. c++; n++;
  311. }
  312. if (integer[1] == 'x') enums[i].valueInteger[enums[i].valueCount] = (int)strtol(integer, NULL, 16);
  313. else enums[i].valueInteger[enums[i].valueCount] = atoi(integer);
  314. }
  315. else enums[i].valueInteger[enums[i].valueCount] = (enums[i].valueInteger[enums[i].valueCount - 1] + 1);
  316. // TODO: Parse value description if any
  317. }
  318. else enums[i].valueInteger[enums[i].valueCount] = (enums[i].valueInteger[enums[i].valueCount - 1] + 1);
  319. enums[i].valueCount++;
  320. }
  321. else if (linePtr[0] == '}')
  322. {
  323. // Get enum name from typedef
  324. int c = 0;
  325. while (linePtr[2 + c] != ';') { enums[i].name[c] = linePtr[2 + c]; c++; }
  326. break; // Enum ended, break for() loop
  327. }
  328. }
  329. }
  330. // Functions info data
  331. funcs = (FunctionInfo *)calloc(MAX_FUNCS_TO_PARSE, sizeof(FunctionInfo));
  332. for (int i = 0; i < funcCount; i++)
  333. {
  334. int funcParamsStart = 0;
  335. int funcEnd = 0;
  336. // Get return type and function name from func line
  337. for (int c = 0; (c < MAX_LINE_LENGTH) && (funcLines[i][c] != '\n'); c++)
  338. {
  339. if (funcLines[i][c] == '(') // Starts function parameters
  340. {
  341. funcParamsStart = c + 1;
  342. // At this point we have function return type and function name
  343. char funcRetTypeName[128] = { 0 };
  344. int funcRetTypeNameLen = c - 6; // Substract "RLAPI "
  345. MemoryCopy(funcRetTypeName, &funcLines[i][6], funcRetTypeNameLen);
  346. GetDataTypeAndName(funcRetTypeName, funcRetTypeNameLen, funcs[i].retType, funcs[i].name);
  347. break;
  348. }
  349. }
  350. // Get parameters from func line
  351. for (int c = funcParamsStart; c < MAX_LINE_LENGTH; c++)
  352. {
  353. if (funcLines[i][c] == ',') // Starts function parameters
  354. {
  355. // Get parameter type + name, extract info
  356. char funcParamTypeName[128] = { 0 };
  357. int funcParamTypeNameLen = c - funcParamsStart;
  358. MemoryCopy(funcParamTypeName, &funcLines[i][funcParamsStart], funcParamTypeNameLen);
  359. GetDataTypeAndName(funcParamTypeName, funcParamTypeNameLen, funcs[i].paramType[funcs[i].paramCount], funcs[i].paramName[funcs[i].paramCount]);
  360. funcParamsStart = c + 1;
  361. if (funcLines[i][c + 1] == ' ') funcParamsStart += 1;
  362. funcs[i].paramCount++; // Move to next parameter
  363. }
  364. else if (funcLines[i][c] == ')')
  365. {
  366. funcEnd = c + 2;
  367. // Check if previous word is void
  368. if ((funcLines[i][c - 4] == 'v') && (funcLines[i][c - 3] == 'o') && (funcLines[i][c - 2] == 'i') && (funcLines[i][c - 1] == 'd')) break;
  369. // Get parameter type + name, extract info
  370. char funcParamTypeName[128] = { 0 };
  371. int funcParamTypeNameLen = c - funcParamsStart;
  372. MemoryCopy(funcParamTypeName, &funcLines[i][funcParamsStart], funcParamTypeNameLen);
  373. GetDataTypeAndName(funcParamTypeName, funcParamTypeNameLen, funcs[i].paramType[funcs[i].paramCount], funcs[i].paramName[funcs[i].paramCount]);
  374. funcs[i].paramCount++; // Move to next parameter
  375. break;
  376. }
  377. }
  378. // Get function description
  379. for (int c = funcEnd; c < MAX_LINE_LENGTH; c++)
  380. {
  381. if (funcLines[i][c] == '/')
  382. {
  383. MemoryCopy(funcs[i].desc, &funcLines[i][c], 127); // WARNING: Size could be too long for funcLines[i][c]?
  384. break;
  385. }
  386. }
  387. }
  388. //for (int i = 0; i < linesCount; i++) free(lines[i]);
  389. free(lines);
  390. free(funcLines);
  391. // At this point, all raylib data has been parsed!
  392. //-----------------------------------------------------------------------------------------
  393. // structs[] -> We have all the structs decomposed into pieces for further analysis
  394. // enums[] -> We have all the enums decomposed into pieces for further analysis
  395. // funcs[] -> We have all the functions decomposed into pieces for further analysis
  396. // Process input file to output
  397. if (outFileName[0] == '\0') MemoryCopy(outFileName, "raylib_api.txt\0", 15);
  398. printf("\nInput file: %s", inFileName);
  399. printf("\nOutput file: %s", outFileName);
  400. if (outputFormat == DEFAULT) printf("\nOutput format: DEFAULT\n\n");
  401. else if (outputFormat == JSON) printf("\nOutput format: JSON\n\n");
  402. else if (outputFormat == XML) printf("\nOutput format: XML\n\n");
  403. ExportParsedData(outFileName, outputFormat);
  404. free(funcs);
  405. free(structs);
  406. free(enums);
  407. }
  408. //----------------------------------------------------------------------------------
  409. // Module Functions Definition
  410. //----------------------------------------------------------------------------------
  411. // Show command line usage info
  412. static void ShowCommandLineInfo(void)
  413. {
  414. printf("\n//////////////////////////////////////////////////////////////////////////////////\n");
  415. printf("// //\n");
  416. printf("// raylib API parser //\n");
  417. printf("// //\n");
  418. printf("// more info and bugs-report: github.com/raysan5/raylib/parser //\n");
  419. printf("// //\n");
  420. printf("// Copyright (c) 2021 Ramon Santamaria (@raysan5) //\n");
  421. printf("// //\n");
  422. printf("//////////////////////////////////////////////////////////////////////////////////\n\n");
  423. printf("USAGE:\n\n");
  424. printf(" > raylib_parser [--help] [--input <filename.h>] [--output <filename.ext>] [--format <type>]\n");
  425. printf("\nOPTIONS:\n\n");
  426. printf(" -h, --help : Show tool version and command line usage help\n\n");
  427. printf(" -i, --input <filename.h> : Define input header file to parse.\n");
  428. printf(" NOTE: If not specified, defaults to: raylib.h\n\n");
  429. printf(" -o, --output <filename.ext> : Define output file and format.\n");
  430. printf(" Supported extensions: .txt, .json, .xml, .h\n");
  431. printf(" NOTE: If not specified, defaults to: raylib_api.txt\n\n");
  432. printf(" -f, --format <type> : Define output format for parser data.\n");
  433. printf(" Supported types: DEFAULT, JSON, XML\n\n");
  434. printf("\nEXAMPLES:\n\n");
  435. printf(" > raylib_parser --input raylib.h --output api.json\n");
  436. printf(" Process <raylib.h> to generate <api.json>\n\n");
  437. printf(" > raylib_parser --output raylib_data.info --format XML\n");
  438. printf(" Process <raylib.h> to generate <raylib_data.info> as XML text data\n\n");
  439. }
  440. // Process command line arguments
  441. static void ProcessCommandLine(int argc, char *argv[])
  442. {
  443. for (int i = 1; i < argc; i++)
  444. {
  445. if (IsTextEqual(argv[i], "-h", 2) || IsTextEqual(argv[i], "--help", 6))
  446. {
  447. // Show info
  448. ShowCommandLineInfo();
  449. }
  450. else if (IsTextEqual(argv[i], "-i", 2) || IsTextEqual(argv[i], "--input", 7))
  451. {
  452. // Check for valid argument and valid file extension
  453. if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
  454. {
  455. MemoryCopy(inFileName, argv[i + 1], TextLength(argv[i + 1])); // Read input filename
  456. i++;
  457. }
  458. else printf("WARNING: No input file provided\n");
  459. }
  460. else if (IsTextEqual(argv[i], "-o", 2) || IsTextEqual(argv[i], "--output", 8))
  461. {
  462. if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
  463. {
  464. MemoryCopy(outFileName, argv[i + 1], TextLength(argv[i + 1])); // Read output filename
  465. i++;
  466. }
  467. else printf("WARNING: No output file provided\n");
  468. }
  469. else if (IsTextEqual(argv[i], "-f", 2) || IsTextEqual(argv[i], "--format", 8))
  470. {
  471. if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
  472. {
  473. if (IsTextEqual(argv[i + 1], "DEFAULT\0", 8)) outputFormat = DEFAULT;
  474. else if (IsTextEqual(argv[i + 1], "JSON\0", 5)) outputFormat = JSON;
  475. else if (IsTextEqual(argv[i + 1], "XML\0", 4)) outputFormat = XML;
  476. }
  477. else printf("WARNING: No format parameters provided\n");
  478. }
  479. }
  480. }
  481. // Load text data from file, returns a '\0' terminated string
  482. // NOTE: text chars array should be freed manually
  483. static char *LoadFileText(const char *fileName, int *length)
  484. {
  485. char *text = NULL;
  486. if (fileName != NULL)
  487. {
  488. FILE *file = fopen(fileName, "rt");
  489. if (file != NULL)
  490. {
  491. // WARNING: When reading a file as 'text' file,
  492. // text mode causes carriage return-linefeed translation...
  493. // ...but using fseek() should return correct byte-offset
  494. fseek(file, 0, SEEK_END);
  495. int size = ftell(file);
  496. fseek(file, 0, SEEK_SET);
  497. if (size > 0)
  498. {
  499. text = (char *)calloc((size + 1), sizeof(char));
  500. unsigned int count = (unsigned int)fread(text, sizeof(char), size, file);
  501. // WARNING: \r\n is converted to \n on reading, so,
  502. // read bytes count gets reduced by the number of lines
  503. if (count < (unsigned int)size)
  504. {
  505. text = realloc(text, count + 1);
  506. *length = count;
  507. }
  508. else *length = size;
  509. // Zero-terminate the string
  510. text[count] = '\0';
  511. }
  512. fclose(file);
  513. }
  514. }
  515. return text;
  516. }
  517. // Get all lines from a text buffer (expecting lines ending with '\n')
  518. static char **GetTextLines(const char *buffer, int length, int *linesCount)
  519. {
  520. // Get the number of lines in the text
  521. int count = 0;
  522. for (int i = 0; i < length; i++) if (buffer[i] == '\n') count++;
  523. printf("Number of text lines in buffer: %i\n", count);
  524. // Allocate as many pointers as lines
  525. char **lines = (char **)malloc(count*sizeof(char **));
  526. char *bufferPtr = (char *)buffer;
  527. for (int i = 0; (i < count) || (bufferPtr[0] != '\0'); i++)
  528. {
  529. lines[i] = (char *)calloc(MAX_LINE_LENGTH, sizeof(char));
  530. // Remove line leading spaces
  531. // Find last index of space/tab character
  532. int index = 0;
  533. while ((bufferPtr[index] == ' ') || (bufferPtr[index] == '\t')) index++;
  534. int j = 0;
  535. while (bufferPtr[index + j] != '\n')
  536. {
  537. lines[i][j] = bufferPtr[index + j];
  538. j++;
  539. }
  540. bufferPtr += (index + j + 1);
  541. }
  542. *linesCount = count;
  543. return lines;
  544. }
  545. // Get data type and name from a string containing both
  546. // NOTE: Useful to parse function parameters and struct fields
  547. static void GetDataTypeAndName(const char *typeName, int typeNameLen, char *type, char *name)
  548. {
  549. for (int k = typeNameLen; k > 0; k--)
  550. {
  551. if (typeName[k] == ' ' && typeName[k - 1] != ',')
  552. {
  553. // Function name starts at this point (and ret type finishes at this point)
  554. MemoryCopy(type, typeName, k);
  555. MemoryCopy(name, typeName + k + 1, typeNameLen - k - 1);
  556. break;
  557. }
  558. else if (typeName[k] == '*')
  559. {
  560. MemoryCopy(type, typeName, k + 1);
  561. MemoryCopy(name, typeName + k + 1, typeNameLen - k - 1);
  562. break;
  563. }
  564. }
  565. }
  566. // Get text length in bytes, check for \0 character
  567. static unsigned int TextLength(const char *text)
  568. {
  569. unsigned int length = 0;
  570. if (text != NULL) while (*text++) length++;
  571. return length;
  572. }
  573. // Custom memcpy() to avoid <string.h>
  574. static void MemoryCopy(void *dest, const void *src, unsigned int count)
  575. {
  576. char *srcPtr = (char *)src;
  577. char *destPtr = (char *)dest;
  578. for (unsigned int i = 0; i < count; i++) destPtr[i] = srcPtr[i];
  579. }
  580. // Compare two text strings, requires number of characters to compare
  581. static bool IsTextEqual(const char *text1, const char *text2, unsigned int count)
  582. {
  583. bool result = true;
  584. for (unsigned int i = 0; i < count; i++)
  585. {
  586. if (text1[i] != text2[i])
  587. {
  588. result = false;
  589. break;
  590. }
  591. }
  592. return result;
  593. }
  594. // Search and replace a character within a string.
  595. static char* CharReplace(char* text, char search, char replace)
  596. {
  597. for (int i = 0; text[i] != '\0'; i++)
  598. if (text[i] == search)
  599. text[i] = replace;
  600. return text;
  601. }
  602. /*
  603. // Replace text string
  604. // REQUIRES: strlen(), strstr(), strncpy(), strcpy() -> TODO: Replace by custom implementations!
  605. // WARNING: Returned buffer must be freed by the user (if return != NULL)
  606. static char *TextReplace(char *text, const char *replace, const char *by)
  607. {
  608. // Sanity checks and initialization
  609. if (!text || !replace || !by) return NULL;
  610. char *result;
  611. char *insertPoint; // Next insert point
  612. char *temp; // Temp pointer
  613. int replaceLen; // Replace string length of (the string to remove)
  614. int byLen; // Replacement length (the string to replace replace by)
  615. int lastReplacePos; // Distance between replace and end of last replace
  616. int count; // Number of replacements
  617. replaceLen = strlen(replace);
  618. if (replaceLen == 0) return NULL; // Empty replace causes infinite loop during count
  619. byLen = strlen(by);
  620. // Count the number of replacements needed
  621. insertPoint = text;
  622. for (count = 0; (temp = strstr(insertPoint, replace)); count++) insertPoint = temp + replaceLen;
  623. // Allocate returning string and point temp to it
  624. temp = result = (char *)malloc(strlen(text) + (byLen - replaceLen)*count + 1);
  625. if (!result) return NULL; // Memory could not be allocated
  626. // First time through the loop, all the variable are set correctly from here on,
  627. // - 'temp' points to the end of the result string
  628. // - 'insertPoint' points to the next occurrence of replace in text
  629. // - 'text' points to the remainder of text after "end of replace"
  630. while (count--)
  631. {
  632. insertPoint = strstr(text, replace);
  633. lastReplacePos = (int)(insertPoint - text);
  634. temp = strncpy(temp, text, lastReplacePos) + lastReplacePos;
  635. temp = strcpy(temp, by) + byLen;
  636. text += lastReplacePos + replaceLen; // Move to next "end of replace"
  637. }
  638. // Copy remaind text part after replacement to result (pointed by moving temp)
  639. strcpy(temp, text);
  640. return result;
  641. }
  642. */
  643. // Export parsed data in desired format
  644. static void ExportParsedData(const char *fileName, int format)
  645. {
  646. FILE *outFile = fopen(fileName, "wt");
  647. switch (format)
  648. {
  649. case DEFAULT:
  650. {
  651. // Print structs info
  652. fprintf(outFile, "\nStructures found: %i\n\n", structCount);
  653. for (int i = 0; i < structCount; i++)
  654. {
  655. fprintf(outFile, "Struct %02i: %s (%i fields)\n", i + 1, structs[i].name, structs[i].fieldCount);
  656. fprintf(outFile, " Name: %s\n", structs[i].name);
  657. fprintf(outFile, " Description: %s\n", structs[i].desc + 3);
  658. for (int f = 0; f < structs[i].fieldCount; f++) fprintf(outFile, " Field[%i]: %s %s %s\n", f + 1, structs[i].fieldType[f], structs[i].fieldName[f], structs[i].fieldDesc[f]);
  659. }
  660. // Print enums info
  661. fprintf(outFile, "\nEnums found: %i\n\n", enumCount);
  662. for (int i = 0; i < enumCount; i++)
  663. {
  664. fprintf(outFile, "Enum %02i: %s (%i values)\n", i + 1, enums[i].name, enums[i].valueCount);
  665. fprintf(outFile, " Name: %s\n", enums[i].name);
  666. fprintf(outFile, " Description: %s\n", enums[i].desc + 3);
  667. for (int e = 0; e < enums[i].valueCount; e++) fprintf(outFile, " Value[%s]: %i\n", enums[i].valueName[e], enums[i].valueInteger[e]);
  668. }
  669. // Print functions info
  670. fprintf(outFile, "\nFunctions found: %i\n\n", funcCount);
  671. for (int i = 0; i < funcCount; i++)
  672. {
  673. fprintf(outFile, "Function %03i: %s() (%i input parameters)\n", i + 1, funcs[i].name, funcs[i].paramCount);
  674. fprintf(outFile, " Name: %s\n", funcs[i].name);
  675. fprintf(outFile, " Return type: %s\n", funcs[i].retType);
  676. fprintf(outFile, " Description: %s\n", funcs[i].desc + 3);
  677. 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]);
  678. if (funcs[i].paramCount == 0) fprintf(outFile, " No input parameters\n");
  679. }
  680. } break;
  681. case JSON:
  682. {
  683. fprintf(outFile, "{\n");
  684. // Print structs info
  685. fprintf(outFile, " \"structs\": [\n");
  686. for (int i = 0; i < structCount; i++)
  687. {
  688. fprintf(outFile, " {\n");
  689. fprintf(outFile, " \"name\": \"%s\",\n", structs[i].name);
  690. fprintf(outFile, " \"description\": \"%s\",\n", structs[i].desc);
  691. fprintf(outFile, " \"fields\": [\n");
  692. for (int f = 0; f < structs[i].fieldCount; f++)
  693. {
  694. fprintf(outFile, " {\n");
  695. fprintf(outFile, " \"name\": \"%s\",\n", structs[i].fieldName[f]),
  696. fprintf(outFile, " \"type\": \"%s\",\n", structs[i].fieldType[f]),
  697. fprintf(outFile, " \"description\": \"%s\"\n", structs[i].fieldDesc[f] + 3),
  698. fprintf(outFile, " }");
  699. if (f < structs[i].fieldCount - 1) fprintf(outFile, ",\n");
  700. else fprintf(outFile, "\n");
  701. }
  702. fprintf(outFile, " ]\n");
  703. fprintf(outFile, " }");
  704. if (i < structCount - 1) fprintf(outFile, ",\n");
  705. else fprintf(outFile, "\n");
  706. }
  707. fprintf(outFile, " ],\n");
  708. // Print enums info
  709. fprintf(outFile, " \"enums\": [\n");
  710. for (int i = 0; i < enumCount; i++)
  711. {
  712. fprintf(outFile, " {\n");
  713. fprintf(outFile, " \"name\": \"%s\",\n", enums[i].name);
  714. fprintf(outFile, " \"description\": \"%s\",\n", enums[i].desc + 3);
  715. fprintf(outFile, " \"values\": [\n");
  716. for (int e = 0; e < enums[i].valueCount; e++)
  717. {
  718. fprintf(outFile, " {\n");
  719. fprintf(outFile, " \"name\": \"%s\",\n", enums[i].valueName[e]),
  720. fprintf(outFile, " \"value\": %i,\n", enums[i].valueInteger[e]),
  721. fprintf(outFile, " \"description\": \"%s\"\n", enums[i].valueDesc[e] + 3),
  722. fprintf(outFile, " }");
  723. if (e < enums[i].valueCount - 1) fprintf(outFile, ",\n");
  724. else fprintf(outFile, "\n");
  725. }
  726. fprintf(outFile, " ]\n");
  727. fprintf(outFile, " }");
  728. if (i < enumCount - 1) fprintf(outFile, ",\n");
  729. else fprintf(outFile, "\n");
  730. }
  731. fprintf(outFile, " ],\n");
  732. // Print functions info
  733. fprintf(outFile, " \"functions\": [\n");
  734. for (int i = 0; i < funcCount; i++)
  735. {
  736. fprintf(outFile, " {\n");
  737. fprintf(outFile, " \"name\": \"%s\",\n", funcs[i].name);
  738. fprintf(outFile, " \"description\": \"%s\",\n", CharReplace(funcs[i].desc, '\\', ' ') + 3);
  739. fprintf(outFile, " \"returnType\": \"%s\"", funcs[i].retType);
  740. if (funcs[i].paramCount == 0) fprintf(outFile, "\n");
  741. else
  742. {
  743. fprintf(outFile, ",\n \"params\": {\n");
  744. for (int p = 0; p < funcs[i].paramCount; p++)
  745. {
  746. fprintf(outFile, " \"%s\": \"%s\"", funcs[i].paramName[p], funcs[i].paramType[p]);
  747. if (p < funcs[i].paramCount - 1) fprintf(outFile, ",\n");
  748. else fprintf(outFile, "\n");
  749. }
  750. fprintf(outFile, " }\n");
  751. }
  752. fprintf(outFile, " }");
  753. if (i < funcCount - 1) fprintf(outFile, ",\n");
  754. else fprintf(outFile, "\n");
  755. }
  756. fprintf(outFile, " ]\n");
  757. fprintf(outFile, "}\n");
  758. } break;
  759. case XML:
  760. {
  761. // XML format to export data:
  762. /*
  763. <?xml version="1.0" encoding="Windows-1252" ?>
  764. <raylibAPI>
  765. <Structs count="">
  766. <Struct name="" fieldCount="" desc="">
  767. <Field type="" name="" desc="">
  768. <Field type="" name="" desc="">
  769. </Struct>
  770. <Structs>
  771. <Enums count="">
  772. <Enum name="" valueCount="" desc="">
  773. <Value name="" integer="" desc="">
  774. <Value name="" integer="" desc="">
  775. </Enum>
  776. </Enums>
  777. <Functions count="">
  778. <Function name="" retType="" paramCount="" desc="">
  779. <Param type="" name="" desc="" />
  780. <Param type="" name="" desc="" />
  781. </Function>
  782. </Functions>
  783. </raylibAPI>
  784. */
  785. fprintf(outFile, "<?xml version=\"1.0\" encoding=\"Windows-1252\" ?>\n");
  786. fprintf(outFile, "<raylibAPI>\n");
  787. // Print structs info
  788. fprintf(outFile, " <Structs count=\"%i\">\n", structCount);
  789. for (int i = 0; i < structCount; i++)
  790. {
  791. fprintf(outFile, " <Struct name=\"%s\" fieldCount=\"%i\" desc=\"%s\">\n", structs[i].name, structs[i].fieldCount, structs[i].desc + 3);
  792. for (int f = 0; f < structs[i].fieldCount; f++)
  793. {
  794. fprintf(outFile, " <Field type=\"%s\" name=\"%s\" desc=\"%s\" />\n", structs[i].fieldType[f], structs[i].fieldName[f], structs[i].fieldDesc[f] + 3);
  795. }
  796. fprintf(outFile, " </Struct>\n");
  797. }
  798. fprintf(outFile, " </Structs>\n");
  799. // Print enums info
  800. fprintf(outFile, " <Enums count=\"%i\">\n", enumCount);
  801. for (int i = 0; i < enumCount; i++)
  802. {
  803. fprintf(outFile, " <Enum name=\"%s\" valueCount=\"%i\" desc=\"%s\">\n", enums[i].name, enums[i].valueCount, enums[i].desc + 3);
  804. for (int v = 0; v < enums[i].valueCount; v++)
  805. {
  806. fprintf(outFile, " <Value name=\"%s\" integer=\"%i\" desc=\"%s\" />\n", enums[i].valueName[v], enums[i].valueInteger[v], enums[i].valueDesc[v] + 3);
  807. }
  808. fprintf(outFile, " </Enum>\n");
  809. }
  810. fprintf(outFile, " </Enums>\n");
  811. // Print functions info
  812. fprintf(outFile, " <Functions count=\"%i\">\n", funcCount);
  813. for (int i = 0; i < funcCount; i++)
  814. {
  815. fprintf(outFile, " <Function name=\"%s\" retType=\"%s\" paramCount=\"%i\" desc=\"%s\">\n", funcs[i].name, funcs[i].retType, funcs[i].paramCount, funcs[i].desc + 3);
  816. for (int p = 0; p < funcs[i].paramCount; p++)
  817. {
  818. fprintf(outFile, " <Param type=\"%s\" name=\"%s\" desc=\"%s\" />\n", funcs[i].paramType[p], funcs[i].paramName[p], funcs[i].paramDesc[p] + 3);
  819. }
  820. fprintf(outFile, " </Function>\n");
  821. }
  822. fprintf(outFile, " </Functions>\n");
  823. fprintf(outFile, "</raylibAPI>\n");
  824. } break;
  825. default: break;
  826. }
  827. fclose(outFile);
  828. }