raylib_parser.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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][128]; // Value description
  81. } EnumInfo;
  82. // Output format for parsed data
  83. typedef enum { DEFAULT = 0, JSON, XML, LUA } 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* EscapeBackslashes(char *text);
  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 and description
  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. // TODO: Get struct description
  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. // Parse enum description
  266. // NOTE: This is not necessarily from the line immediately before,
  267. // some of the enums have extra lines between the "description"
  268. // and the typedef enum
  269. for (int j = enumLines[i] - 1; j > 0; j--)
  270. {
  271. char *linePtr = lines[j];
  272. if ((linePtr[0] != '/') || (linePtr[2] != ' '))
  273. {
  274. MemoryCopy(enums[i].desc, &lines[j + 1][0], sizeof(enums[i].desc) - 1);
  275. break;
  276. }
  277. }
  278. for (int j = 1; j < MAX_ENUM_VALUES*2; j++) // Maximum number of lines following enum first line
  279. {
  280. char *linePtr = lines[enumLines[i] + j];
  281. if ((linePtr[0] >= 'A') && (linePtr[0] <= 'Z'))
  282. {
  283. // Parse enum value line, possible options:
  284. //ENUM_VALUE_NAME,
  285. //ENUM_VALUE_NAME
  286. //ENUM_VALUE_NAME = 99
  287. //ENUM_VALUE_NAME = 99,
  288. //ENUM_VALUE_NAME = 0x00000040, // Value description
  289. // We start reading the value name
  290. int c = 0;
  291. while ((linePtr[c] != ',') &&
  292. (linePtr[c] != ' ') &&
  293. (linePtr[c] != '=') &&
  294. (linePtr[c] != '\0')) { enums[i].valueName[enums[i].valueCount][c] = linePtr[c]; c++; }
  295. // After the name we can have:
  296. // '=' -> value is provided
  297. // ',' -> value is equal to previous + 1, there could be a description if not '\0'
  298. // ' ' -> value is equal to previous + 1, there could be a description if not '\0'
  299. // '\0' -> value is equal to previous + 1
  300. // Let's start checking if the line is not finished
  301. if ((linePtr[c] != ',') && (linePtr[c] != '\0'))
  302. {
  303. // Two options:
  304. // '=' -> value is provided
  305. // ' ' -> value is equal to previous + 1, there could be a description if not '\0'
  306. bool foundValue = false;
  307. while ((linePtr[c] != '\0') && (linePtr[c] != '/'))
  308. {
  309. if (linePtr[c] == '=') { foundValue = true; break; }
  310. c++;
  311. }
  312. if (foundValue)
  313. {
  314. if (linePtr[c + 1] == ' ') c += 2;
  315. else c++;
  316. // Parse integer value
  317. int n = 0;
  318. char integer[16] = { 0 };
  319. while ((linePtr[c] != ',') && (linePtr[c] != ' ') && (linePtr[c] != '\0'))
  320. {
  321. integer[n] = linePtr[c];
  322. c++; n++;
  323. }
  324. if (integer[1] == 'x') enums[i].valueInteger[enums[i].valueCount] = (int)strtol(integer, NULL, 16);
  325. else enums[i].valueInteger[enums[i].valueCount] = atoi(integer);
  326. }
  327. else enums[i].valueInteger[enums[i].valueCount] = (enums[i].valueInteger[enums[i].valueCount - 1] + 1);
  328. }
  329. else enums[i].valueInteger[enums[i].valueCount] = (enums[i].valueInteger[enums[i].valueCount - 1] + 1);
  330. // Look for description or end of line
  331. while ((linePtr[c] != '/') && (linePtr[c] != '\0')) { c++; }
  332. if (linePtr[c] == '/')
  333. {
  334. // Parse value description
  335. MemoryCopy(enums[i].valueDesc[enums[i].valueCount], &linePtr[c], sizeof(enums[0].valueDesc[0]) - c - 1);
  336. }
  337. enums[i].valueCount++;
  338. }
  339. else if (linePtr[0] == '}')
  340. {
  341. // Get enum name from typedef
  342. int c = 0;
  343. while (linePtr[2 + c] != ';') { enums[i].name[c] = linePtr[2 + c]; c++; }
  344. break; // Enum ended, break for() loop
  345. }
  346. }
  347. }
  348. free(enumLines);
  349. // Functions info data
  350. funcs = (FunctionInfo *)calloc(MAX_FUNCS_TO_PARSE, sizeof(FunctionInfo));
  351. for (int i = 0; i < funcCount; i++)
  352. {
  353. int funcParamsStart = 0;
  354. int funcEnd = 0;
  355. // Get return type and function name from func line
  356. for (int c = 0; (c < MAX_LINE_LENGTH) && (funcLines[i][c] != '\n'); c++)
  357. {
  358. if (funcLines[i][c] == '(') // Starts function parameters
  359. {
  360. funcParamsStart = c + 1;
  361. // At this point we have function return type and function name
  362. char funcRetTypeName[128] = { 0 };
  363. int dc = TextLength(apiDefine) + 1;
  364. int funcRetTypeNameLen = c - dc; // Substract `define` ("RLAPI " for raylib.h)
  365. MemoryCopy(funcRetTypeName, &funcLines[i][dc], funcRetTypeNameLen);
  366. GetDataTypeAndName(funcRetTypeName, funcRetTypeNameLen, funcs[i].retType, funcs[i].name);
  367. break;
  368. }
  369. }
  370. // Get parameters from func line
  371. for (int c = funcParamsStart; c < MAX_LINE_LENGTH; c++)
  372. {
  373. if (funcLines[i][c] == ',') // Starts function parameters
  374. {
  375. // Get parameter type + name, extract info
  376. char funcParamTypeName[128] = { 0 };
  377. int funcParamTypeNameLen = c - funcParamsStart;
  378. MemoryCopy(funcParamTypeName, &funcLines[i][funcParamsStart], funcParamTypeNameLen);
  379. GetDataTypeAndName(funcParamTypeName, funcParamTypeNameLen, funcs[i].paramType[funcs[i].paramCount], funcs[i].paramName[funcs[i].paramCount]);
  380. funcParamsStart = c + 1;
  381. if (funcLines[i][c + 1] == ' ') funcParamsStart += 1;
  382. funcs[i].paramCount++; // Move to next parameter
  383. }
  384. else if (funcLines[i][c] == ')')
  385. {
  386. funcEnd = c + 2;
  387. // Check if previous word is void
  388. if ((funcLines[i][c - 4] == 'v') && (funcLines[i][c - 3] == 'o') && (funcLines[i][c - 2] == 'i') && (funcLines[i][c - 1] == 'd')) break;
  389. // Get parameter type + name, extract info
  390. char funcParamTypeName[128] = { 0 };
  391. int funcParamTypeNameLen = c - funcParamsStart;
  392. MemoryCopy(funcParamTypeName, &funcLines[i][funcParamsStart], funcParamTypeNameLen);
  393. GetDataTypeAndName(funcParamTypeName, funcParamTypeNameLen, funcs[i].paramType[funcs[i].paramCount], funcs[i].paramName[funcs[i].paramCount]);
  394. funcs[i].paramCount++; // Move to next parameter
  395. break;
  396. }
  397. }
  398. // Get function description
  399. for (int c = funcEnd; c < MAX_LINE_LENGTH; c++)
  400. {
  401. if (funcLines[i][c] == '/')
  402. {
  403. MemoryCopy(funcs[i].desc, &funcLines[i][c], 127); // WARNING: Size could be too long for funcLines[i][c]?
  404. break;
  405. }
  406. }
  407. }
  408. for (int i = 0; i < linesCount; i++) free(lines[i]);
  409. free(lines);
  410. free(funcLines);
  411. // At this point, all raylib data has been parsed!
  412. //-----------------------------------------------------------------------------------------
  413. // structs[] -> We have all the structs decomposed into pieces for further analysis
  414. // enums[] -> We have all the enums decomposed into pieces for further analysis
  415. // funcs[] -> We have all the functions decomposed into pieces for further analysis
  416. // Process input file to output
  417. if (outFileName[0] == '\0') MemoryCopy(outFileName, "raylib_api.txt\0", 15);
  418. printf("\nInput file: %s", inFileName);
  419. printf("\nOutput file: %s", outFileName);
  420. if (outputFormat == DEFAULT) printf("\nOutput format: DEFAULT\n\n");
  421. else if (outputFormat == JSON) printf("\nOutput format: JSON\n\n");
  422. else if (outputFormat == XML) printf("\nOutput format: XML\n\n");
  423. else if (outputFormat == LUA) printf("\nOutput format: LUA\n\n");
  424. ExportParsedData(outFileName, outputFormat);
  425. free(funcs);
  426. free(structs);
  427. free(enums);
  428. }
  429. //----------------------------------------------------------------------------------
  430. // Module Functions Definition
  431. //----------------------------------------------------------------------------------
  432. // Show command line usage info
  433. static void ShowCommandLineInfo(void)
  434. {
  435. printf("\n//////////////////////////////////////////////////////////////////////////////////\n");
  436. printf("// //\n");
  437. printf("// raylib API parser //\n");
  438. printf("// //\n");
  439. printf("// more info and bugs-report: github.com/raysan5/raylib/parser //\n");
  440. printf("// //\n");
  441. printf("// Copyright (c) 2021 Ramon Santamaria (@raysan5) //\n");
  442. printf("// //\n");
  443. printf("//////////////////////////////////////////////////////////////////////////////////\n\n");
  444. printf("USAGE:\n\n");
  445. printf(" > raylib_parser [--help] [--input <filename.h>] [--output <filename.ext>] [--format <type>] [--define <DEF>]\n");
  446. printf("\nOPTIONS:\n\n");
  447. printf(" -h, --help : Show tool version and command line usage help\n\n");
  448. printf(" -i, --input <filename.h> : Define input header file to parse.\n");
  449. printf(" NOTE: If not specified, defaults to: raylib.h\n\n");
  450. printf(" -o, --output <filename.ext> : Define output file and format.\n");
  451. printf(" Supported extensions: .txt, .json, .xml, .h\n");
  452. printf(" NOTE: If not specified, defaults to: raylib_api.txt\n\n");
  453. printf(" -f, --format <type> : Define output format for parser data.\n");
  454. printf(" Supported types: DEFAULT, JSON, XML, LUA\n\n");
  455. printf(" -d, --define <DEF> : Define functions define (i.e. RLAPI for raylib.h, RMDEF for raymath.h, etc\n");
  456. printf(" NOTE: If not specified, defaults to: RLAPI\n\n");
  457. printf("\nEXAMPLES:\n\n");
  458. printf(" > raylib_parser --input raylib.h --output api.json\n");
  459. printf(" Process <raylib.h> to generate <api.json>\n\n");
  460. printf(" > raylib_parser --output raylib_data.info --format XML\n");
  461. printf(" Process <raylib.h> to generate <raylib_data.info> as XML text data\n\n");
  462. printf(" > raylib_parser --input raymath.h --output raymath_data.info --format XML\n");
  463. printf(" Process <raymath.h> to generate <raymath_data.info> as XML text data\n\n");
  464. }
  465. // Process command line arguments
  466. static void ProcessCommandLine(int argc, char *argv[])
  467. {
  468. for (int i = 1; i < argc; i++)
  469. {
  470. if (IsTextEqual(argv[i], "-h", 2) || IsTextEqual(argv[i], "--help", 6))
  471. {
  472. // Show info
  473. ShowCommandLineInfo();
  474. }
  475. else if (IsTextEqual(argv[i], "-i", 2) || IsTextEqual(argv[i], "--input", 7))
  476. {
  477. // Check for valid argument and valid file extension
  478. if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
  479. {
  480. MemoryCopy(inFileName, argv[i + 1], TextLength(argv[i + 1])); // Read input filename
  481. i++;
  482. }
  483. else printf("WARNING: No input file provided\n");
  484. }
  485. else if (IsTextEqual(argv[i], "-o", 2) || IsTextEqual(argv[i], "--output", 8))
  486. {
  487. if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
  488. {
  489. MemoryCopy(outFileName, argv[i + 1], TextLength(argv[i + 1])); // Read output filename
  490. i++;
  491. }
  492. else printf("WARNING: No output file provided\n");
  493. }
  494. else if (IsTextEqual(argv[i], "-f", 2) || IsTextEqual(argv[i], "--format", 8))
  495. {
  496. if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
  497. {
  498. if (IsTextEqual(argv[i + 1], "DEFAULT\0", 8)) outputFormat = DEFAULT;
  499. else if (IsTextEqual(argv[i + 1], "JSON\0", 5)) outputFormat = JSON;
  500. else if (IsTextEqual(argv[i + 1], "XML\0", 4)) outputFormat = XML;
  501. else if (IsTextEqual(argv[i + 1], "LUA\0", 4)) outputFormat = LUA;
  502. }
  503. else printf("WARNING: No format parameters provided\n");
  504. }
  505. else if (IsTextEqual(argv[i], "-d", 2) || IsTextEqual(argv[i], "--define", 8))
  506. {
  507. if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
  508. {
  509. MemoryCopy(apiDefine, argv[i + 1], TextLength(argv[i + 1])); // Read functions define
  510. apiDefine[TextLength(argv[i + 1])] = '\0';
  511. i++;
  512. }
  513. else printf("WARNING: No define key provided\n");
  514. }
  515. }
  516. }
  517. // Load text data from file, returns a '\0' terminated string
  518. // NOTE: text chars array should be freed manually
  519. static char *LoadFileText(const char *fileName, int *length)
  520. {
  521. char *text = NULL;
  522. if (fileName != NULL)
  523. {
  524. FILE *file = fopen(fileName, "rt");
  525. if (file != NULL)
  526. {
  527. // WARNING: When reading a file as 'text' file,
  528. // text mode causes carriage return-linefeed translation...
  529. // ...but using fseek() should return correct byte-offset
  530. fseek(file, 0, SEEK_END);
  531. int size = ftell(file);
  532. fseek(file, 0, SEEK_SET);
  533. if (size > 0)
  534. {
  535. text = (char *)calloc((size + 1), sizeof(char));
  536. unsigned int count = (unsigned int)fread(text, sizeof(char), size, file);
  537. // WARNING: \r\n is converted to \n on reading, so,
  538. // read bytes count gets reduced by the number of lines
  539. if (count < (unsigned int)size)
  540. {
  541. text = realloc(text, count + 1);
  542. *length = count;
  543. }
  544. else *length = size;
  545. // Zero-terminate the string
  546. text[count] = '\0';
  547. }
  548. fclose(file);
  549. }
  550. }
  551. return text;
  552. }
  553. // Get all lines from a text buffer (expecting lines ending with '\n')
  554. static char **GetTextLines(const char *buffer, int length, int *linesCount)
  555. {
  556. // Get the number of lines in the text
  557. int count = 0;
  558. for (int i = 0; i < length; i++) if (buffer[i] == '\n') count++;
  559. printf("Number of text lines in buffer: %i\n", count);
  560. // Allocate as many pointers as lines
  561. char **lines = (char **)malloc(count*sizeof(char **));
  562. char *bufferPtr = (char *)buffer;
  563. for (int i = 0; (i < count) || (bufferPtr[0] != '\0'); i++)
  564. {
  565. lines[i] = (char *)calloc(MAX_LINE_LENGTH, sizeof(char));
  566. // Remove line leading spaces
  567. // Find last index of space/tab character
  568. int index = 0;
  569. while ((bufferPtr[index] == ' ') || (bufferPtr[index] == '\t')) index++;
  570. int j = 0;
  571. while (bufferPtr[index + j] != '\n')
  572. {
  573. lines[i][j] = bufferPtr[index + j];
  574. j++;
  575. }
  576. bufferPtr += (index + j + 1);
  577. }
  578. *linesCount = count;
  579. return lines;
  580. }
  581. // Get data type and name from a string containing both
  582. // NOTE: Useful to parse function parameters and struct fields
  583. static void GetDataTypeAndName(const char *typeName, int typeNameLen, char *type, char *name)
  584. {
  585. for (int k = typeNameLen; k > 0; k--)
  586. {
  587. if (typeName[k] == ' ' && typeName[k - 1] != ',')
  588. {
  589. // Function name starts at this point (and ret type finishes at this point)
  590. MemoryCopy(type, typeName, k);
  591. MemoryCopy(name, typeName + k + 1, typeNameLen - k - 1);
  592. break;
  593. }
  594. else if (typeName[k] == '*')
  595. {
  596. MemoryCopy(type, typeName, k + 1);
  597. MemoryCopy(name, typeName + k + 1, typeNameLen - k - 1);
  598. break;
  599. }
  600. }
  601. }
  602. // Get text length in bytes, check for \0 character
  603. static unsigned int TextLength(const char *text)
  604. {
  605. unsigned int length = 0;
  606. if (text != NULL) while (*text++) length++;
  607. return length;
  608. }
  609. // Custom memcpy() to avoid <string.h>
  610. static void MemoryCopy(void *dest, const void *src, unsigned int count)
  611. {
  612. char *srcPtr = (char *)src;
  613. char *destPtr = (char *)dest;
  614. for (unsigned int i = 0; i < count; i++) destPtr[i] = srcPtr[i];
  615. }
  616. // Compare two text strings, requires number of characters to compare
  617. static bool IsTextEqual(const char *text1, const char *text2, unsigned int count)
  618. {
  619. bool result = true;
  620. for (unsigned int i = 0; i < count; i++)
  621. {
  622. if (text1[i] != text2[i])
  623. {
  624. result = false;
  625. break;
  626. }
  627. }
  628. return result;
  629. }
  630. // Escape backslashes in a string, writing the escaped string into a static buffer
  631. static char* EscapeBackslashes(char *text)
  632. {
  633. static char buf[256];
  634. char *a = text;
  635. char *b = buf;
  636. do
  637. {
  638. if (*a == '\\') *b++ = '\\';
  639. *b++ = *a;
  640. }
  641. while (*a++);
  642. return buf;
  643. }
  644. /*
  645. // Replace text string
  646. // REQUIRES: strlen(), strstr(), strncpy(), strcpy() -> TODO: Replace by custom implementations!
  647. // WARNING: Returned buffer must be freed by the user (if return != NULL)
  648. static char *TextReplace(char *text, const char *replace, const char *by)
  649. {
  650. // Sanity checks and initialization
  651. if (!text || !replace || !by) return NULL;
  652. char *result;
  653. char *insertPoint; // Next insert point
  654. char *temp; // Temp pointer
  655. int replaceLen; // Replace string length of (the string to remove)
  656. int byLen; // Replacement length (the string to replace replace by)
  657. int lastReplacePos; // Distance between replace and end of last replace
  658. int count; // Number of replacements
  659. replaceLen = strlen(replace);
  660. if (replaceLen == 0) return NULL; // Empty replace causes infinite loop during count
  661. byLen = strlen(by);
  662. // Count the number of replacements needed
  663. insertPoint = text;
  664. for (count = 0; (temp = strstr(insertPoint, replace)); count++) insertPoint = temp + replaceLen;
  665. // Allocate returning string and point temp to it
  666. temp = result = (char *)malloc(strlen(text) + (byLen - replaceLen)*count + 1);
  667. if (!result) return NULL; // Memory could not be allocated
  668. // First time through the loop, all the variable are set correctly from here on,
  669. // - 'temp' points to the end of the result string
  670. // - 'insertPoint' points to the next occurrence of replace in text
  671. // - 'text' points to the remainder of text after "end of replace"
  672. while (count--)
  673. {
  674. insertPoint = strstr(text, replace);
  675. lastReplacePos = (int)(insertPoint - text);
  676. temp = strncpy(temp, text, lastReplacePos) + lastReplacePos;
  677. temp = strcpy(temp, by) + byLen;
  678. text += lastReplacePos + replaceLen; // Move to next "end of replace"
  679. }
  680. // Copy remaind text part after replacement to result (pointed by moving temp)
  681. strcpy(temp, text);
  682. return result;
  683. }
  684. */
  685. // Export parsed data in desired format
  686. static void ExportParsedData(const char *fileName, int format)
  687. {
  688. FILE *outFile = fopen(fileName, "wt");
  689. switch (format)
  690. {
  691. case DEFAULT:
  692. {
  693. // Print structs info
  694. fprintf(outFile, "\nStructures found: %i\n\n", structCount);
  695. for (int i = 0; i < structCount; i++)
  696. {
  697. fprintf(outFile, "Struct %02i: %s (%i fields)\n", i + 1, structs[i].name, structs[i].fieldCount);
  698. fprintf(outFile, " Name: %s\n", structs[i].name);
  699. fprintf(outFile, " Description: %s\n", structs[i].desc + 3);
  700. 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]);
  701. }
  702. // Print enums info
  703. fprintf(outFile, "\nEnums found: %i\n\n", enumCount);
  704. for (int i = 0; i < enumCount; i++)
  705. {
  706. fprintf(outFile, "Enum %02i: %s (%i values)\n", i + 1, enums[i].name, enums[i].valueCount);
  707. fprintf(outFile, " Name: %s\n", enums[i].name);
  708. fprintf(outFile, " Description: %s\n", enums[i].desc + 3);
  709. for (int e = 0; e < enums[i].valueCount; e++) fprintf(outFile, " Value[%s]: %i\n", enums[i].valueName[e], enums[i].valueInteger[e]);
  710. }
  711. // Print functions info
  712. fprintf(outFile, "\nFunctions found: %i\n\n", funcCount);
  713. for (int i = 0; i < funcCount; i++)
  714. {
  715. fprintf(outFile, "Function %03i: %s() (%i input parameters)\n", i + 1, funcs[i].name, funcs[i].paramCount);
  716. fprintf(outFile, " Name: %s\n", funcs[i].name);
  717. fprintf(outFile, " Return type: %s\n", funcs[i].retType);
  718. fprintf(outFile, " Description: %s\n", funcs[i].desc + 3);
  719. 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]);
  720. if (funcs[i].paramCount == 0) fprintf(outFile, " No input parameters\n");
  721. }
  722. } break;
  723. case LUA:
  724. {
  725. fprintf(outFile, "return {\n");
  726. // Print structs info
  727. fprintf(outFile, " structs = {\n");
  728. for (int i = 0; i < structCount; i++)
  729. {
  730. fprintf(outFile, " {\n");
  731. fprintf(outFile, " name = \"%s\",\n", structs[i].name);
  732. fprintf(outFile, " description = \"%s\",\n", EscapeBackslashes(structs[i].desc + 3));
  733. fprintf(outFile, " fields = {\n");
  734. for (int f = 0; f < structs[i].fieldCount; f++)
  735. {
  736. fprintf(outFile, " {\n");
  737. fprintf(outFile, " name = \"%s\",\n", structs[i].fieldName[f]);
  738. fprintf(outFile, " type = \"%s\",\n", structs[i].fieldType[f]);
  739. fprintf(outFile, " description = \"%s\"\n", EscapeBackslashes(structs[i].fieldDesc[f] + 3));
  740. fprintf(outFile, " }");
  741. if (f < structs[i].fieldCount - 1) fprintf(outFile, ",\n");
  742. else fprintf(outFile, "\n");
  743. }
  744. fprintf(outFile, " }\n");
  745. fprintf(outFile, " }");
  746. if (i < structCount - 1) fprintf(outFile, ",\n");
  747. else fprintf(outFile, "\n");
  748. }
  749. fprintf(outFile, " },\n");
  750. // Print enums info
  751. fprintf(outFile, " enums = {\n");
  752. for (int i = 0; i < enumCount; i++)
  753. {
  754. fprintf(outFile, " {\n");
  755. fprintf(outFile, " name = \"%s\",\n", enums[i].name);
  756. fprintf(outFile, " description = \"%s\",\n", EscapeBackslashes(enums[i].desc + 3));
  757. fprintf(outFile, " values = {\n");
  758. for (int e = 0; e < enums[i].valueCount; e++)
  759. {
  760. fprintf(outFile, " {\n");
  761. fprintf(outFile, " name = \"%s\",\n", enums[i].valueName[e]);
  762. fprintf(outFile, " value = %i,\n", enums[i].valueInteger[e]);
  763. fprintf(outFile, " description = \"%s\"\n", EscapeBackslashes(enums[i].valueDesc[e] + 3));
  764. fprintf(outFile, " }");
  765. if (e < enums[i].valueCount - 1) fprintf(outFile, ",\n");
  766. else fprintf(outFile, "\n");
  767. }
  768. fprintf(outFile, " }\n");
  769. fprintf(outFile, " }");
  770. if (i < enumCount - 1) fprintf(outFile, ",\n");
  771. else fprintf(outFile, "\n");
  772. }
  773. fprintf(outFile, " },\n");
  774. // Print functions info
  775. fprintf(outFile, " functions = {\n");
  776. for (int i = 0; i < funcCount; i++)
  777. {
  778. fprintf(outFile, " {\n");
  779. fprintf(outFile, " name = \"%s\",\n", funcs[i].name);
  780. fprintf(outFile, " description = \"%s\",\n", EscapeBackslashes(funcs[i].desc + 3));
  781. fprintf(outFile, " returnType = \"%s\"", funcs[i].retType);
  782. if (funcs[i].paramCount == 0) fprintf(outFile, "\n");
  783. else
  784. {
  785. fprintf(outFile, ",\n params = {\n");
  786. for (int p = 0; p < funcs[i].paramCount; p++)
  787. {
  788. fprintf(outFile, " {name = \"%s\", type = \"%s\"}", funcs[i].paramName[p], funcs[i].paramType[p]);
  789. if (p < funcs[i].paramCount - 1) fprintf(outFile, ",\n");
  790. else fprintf(outFile, "\n");
  791. }
  792. fprintf(outFile, " }\n");
  793. }
  794. fprintf(outFile, " }");
  795. if (i < funcCount - 1) fprintf(outFile, ",\n");
  796. else fprintf(outFile, "\n");
  797. }
  798. fprintf(outFile, " }\n");
  799. fprintf(outFile, "}\n");
  800. } break;
  801. case JSON:
  802. {
  803. fprintf(outFile, "{\n");
  804. // Print structs info
  805. fprintf(outFile, " \"structs\": [\n");
  806. for (int i = 0; i < structCount; i++)
  807. {
  808. fprintf(outFile, " {\n");
  809. fprintf(outFile, " \"name\": \"%s\",\n", structs[i].name);
  810. fprintf(outFile, " \"description\": \"%s\",\n", EscapeBackslashes(structs[i].desc + 3));
  811. fprintf(outFile, " \"fields\": [\n");
  812. for (int f = 0; f < structs[i].fieldCount; f++)
  813. {
  814. fprintf(outFile, " {\n");
  815. fprintf(outFile, " \"name\": \"%s\",\n", structs[i].fieldName[f]);
  816. fprintf(outFile, " \"type\": \"%s\",\n", structs[i].fieldType[f]);
  817. fprintf(outFile, " \"description\": \"%s\"\n", EscapeBackslashes(structs[i].fieldDesc[f] + 3));
  818. fprintf(outFile, " }");
  819. if (f < structs[i].fieldCount - 1) fprintf(outFile, ",\n");
  820. else fprintf(outFile, "\n");
  821. }
  822. fprintf(outFile, " ]\n");
  823. fprintf(outFile, " }");
  824. if (i < structCount - 1) fprintf(outFile, ",\n");
  825. else fprintf(outFile, "\n");
  826. }
  827. fprintf(outFile, " ],\n");
  828. // Print enums info
  829. fprintf(outFile, " \"enums\": [\n");
  830. for (int i = 0; i < enumCount; i++)
  831. {
  832. fprintf(outFile, " {\n");
  833. fprintf(outFile, " \"name\": \"%s\",\n", enums[i].name);
  834. fprintf(outFile, " \"description\": \"%s\",\n", EscapeBackslashes(enums[i].desc + 3));
  835. fprintf(outFile, " \"values\": [\n");
  836. for (int e = 0; e < enums[i].valueCount; e++)
  837. {
  838. fprintf(outFile, " {\n");
  839. fprintf(outFile, " \"name\": \"%s\",\n", enums[i].valueName[e]);
  840. fprintf(outFile, " \"value\": %i,\n", enums[i].valueInteger[e]);
  841. fprintf(outFile, " \"description\": \"%s\"\n", EscapeBackslashes(enums[i].valueDesc[e] + 3));
  842. fprintf(outFile, " }");
  843. if (e < enums[i].valueCount - 1) fprintf(outFile, ",\n");
  844. else fprintf(outFile, "\n");
  845. }
  846. fprintf(outFile, " ]\n");
  847. fprintf(outFile, " }");
  848. if (i < enumCount - 1) fprintf(outFile, ",\n");
  849. else fprintf(outFile, "\n");
  850. }
  851. fprintf(outFile, " ],\n");
  852. // Print functions info
  853. fprintf(outFile, " \"functions\": [\n");
  854. for (int i = 0; i < funcCount; i++)
  855. {
  856. fprintf(outFile, " {\n");
  857. fprintf(outFile, " \"name\": \"%s\",\n", funcs[i].name);
  858. fprintf(outFile, " \"description\": \"%s\",\n", EscapeBackslashes(funcs[i].desc + 3));
  859. fprintf(outFile, " \"returnType\": \"%s\"", funcs[i].retType);
  860. if (funcs[i].paramCount == 0) fprintf(outFile, "\n");
  861. else
  862. {
  863. fprintf(outFile, ",\n \"params\": {\n");
  864. for (int p = 0; p < funcs[i].paramCount; p++)
  865. {
  866. fprintf(outFile, " \"%s\": \"%s\"", funcs[i].paramName[p], funcs[i].paramType[p]);
  867. if (p < funcs[i].paramCount - 1) fprintf(outFile, ",\n");
  868. else fprintf(outFile, "\n");
  869. }
  870. fprintf(outFile, " }\n");
  871. }
  872. fprintf(outFile, " }");
  873. if (i < funcCount - 1) fprintf(outFile, ",\n");
  874. else fprintf(outFile, "\n");
  875. }
  876. fprintf(outFile, " ]\n");
  877. fprintf(outFile, "}\n");
  878. } break;
  879. case XML:
  880. {
  881. // XML format to export data:
  882. /*
  883. <?xml version="1.0" encoding="Windows-1252" ?>
  884. <raylibAPI>
  885. <Structs count="">
  886. <Struct name="" fieldCount="" desc="">
  887. <Field type="" name="" desc="">
  888. <Field type="" name="" desc="">
  889. </Struct>
  890. <Structs>
  891. <Enums count="">
  892. <Enum name="" valueCount="" desc="">
  893. <Value name="" integer="" desc="">
  894. <Value name="" integer="" desc="">
  895. </Enum>
  896. </Enums>
  897. <Functions count="">
  898. <Function name="" retType="" paramCount="" desc="">
  899. <Param type="" name="" desc="" />
  900. <Param type="" name="" desc="" />
  901. </Function>
  902. </Functions>
  903. </raylibAPI>
  904. */
  905. fprintf(outFile, "<?xml version=\"1.0\" encoding=\"Windows-1252\" ?>\n");
  906. fprintf(outFile, "<raylibAPI>\n");
  907. // Print structs info
  908. fprintf(outFile, " <Structs count=\"%i\">\n", structCount);
  909. for (int i = 0; i < structCount; i++)
  910. {
  911. fprintf(outFile, " <Struct name=\"%s\" fieldCount=\"%i\" desc=\"%s\">\n", structs[i].name, structs[i].fieldCount, structs[i].desc + 3);
  912. for (int f = 0; f < structs[i].fieldCount; f++)
  913. {
  914. fprintf(outFile, " <Field type=\"%s\" name=\"%s\" desc=\"%s\" />\n", structs[i].fieldType[f], structs[i].fieldName[f], structs[i].fieldDesc[f] + 3);
  915. }
  916. fprintf(outFile, " </Struct>\n");
  917. }
  918. fprintf(outFile, " </Structs>\n");
  919. // Print enums info
  920. fprintf(outFile, " <Enums count=\"%i\">\n", enumCount);
  921. for (int i = 0; i < enumCount; i++)
  922. {
  923. fprintf(outFile, " <Enum name=\"%s\" valueCount=\"%i\" desc=\"%s\">\n", enums[i].name, enums[i].valueCount, enums[i].desc + 3);
  924. for (int v = 0; v < enums[i].valueCount; v++)
  925. {
  926. fprintf(outFile, " <Value name=\"%s\" integer=\"%i\" desc=\"%s\" />\n", enums[i].valueName[v], enums[i].valueInteger[v], enums[i].valueDesc[v] + 3);
  927. }
  928. fprintf(outFile, " </Enum>\n");
  929. }
  930. fprintf(outFile, " </Enums>\n");
  931. // Print functions info
  932. fprintf(outFile, " <Functions count=\"%i\">\n", funcCount);
  933. for (int i = 0; i < funcCount; i++)
  934. {
  935. 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);
  936. for (int p = 0; p < funcs[i].paramCount; p++)
  937. {
  938. fprintf(outFile, " <Param type=\"%s\" name=\"%s\" desc=\"%s\" />\n", funcs[i].paramType[p], funcs[i].paramName[p], funcs[i].paramDesc[p] + 3);
  939. }
  940. fprintf(outFile, " </Function>\n");
  941. }
  942. fprintf(outFile, " </Functions>\n");
  943. fprintf(outFile, "</raylibAPI>\n");
  944. } break;
  945. default: break;
  946. }
  947. fclose(outFile);
  948. }