raylib_parser.c 40 KB

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