raylib_parser.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. /**********************************************************************************************
  2. raylib API parser
  3. This parser scans raylib.h to get API information about structs, enums, functions and defines.
  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. - struct DefInfo
  9. CONSTRAINTS:
  10. This parser is specifically designed to work with raylib.h, so, it has some constraints:
  11. - Functions are expected as a single line with the following structure:
  12. <retType> <name>(<paramType[0]> <paramName[0]>, <paramType[1]> <paramName[1]>); <desc>
  13. Be careful with functions broken into several lines, it breaks the process!
  14. - Structures are expected as several lines with the following form:
  15. <desc>
  16. typedef struct <name> {
  17. <fieldType[0]> <fieldName[0]>; <fieldDesc[0]>
  18. <fieldType[1]> <fieldName[1]>; <fieldDesc[1]>
  19. <fieldType[2]> <fieldName[2]>; <fieldDesc[2]>
  20. } <name>;
  21. - Enums are expected as several lines with the following form:
  22. <desc>
  23. typedef enum {
  24. <valueName[0]> = <valueInteger[0]>, <valueDesc[0]>
  25. <valueName[1]>,
  26. <valueName[2]>, <valueDesc[2]>
  27. <valueName[3]> <valueDesc[3]>
  28. } <name>;
  29. NOTE: Multiple options are supported for enums:
  30. - If value is not provided, (<valueInteger[i -1]> + 1) is assigned
  31. - Value description can be provided or not
  32. OTHER NOTES:
  33. - This parser could work with other C header files if mentioned constraints are followed.
  34. - This parser does not require <string.h> library, all data is parsed directly from char buffers.
  35. LICENSE: zlib/libpng
  36. raylib-parser is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  37. BSD-like license that allows static linking with closed source software:
  38. Copyright (c) 2021 Ramon Santamaria (@raysan5)
  39. **********************************************************************************************/
  40. #define _CRT_SECURE_NO_WARNINGS
  41. #include <stdlib.h> // Required for: malloc(), calloc(), realloc(), free(), atoi(), strtol()
  42. #include <stdio.h> // Required for: printf(), fopen(), fseek(), ftell(), fread(), fclose()
  43. #include <stdbool.h> // Required for: bool
  44. #include <ctype.h> // Required for: isdigit()
  45. #define MAX_FUNCS_TO_PARSE 512 // Maximum number of functions to parse
  46. #define MAX_STRUCTS_TO_PARSE 64 // Maximum number of structures to parse
  47. #define MAX_ENUMS_TO_PARSE 64 // Maximum number of enums to parse
  48. #define MAX_DEFINES_TO_PARSE 2048 // Maximum number of defines to parse
  49. #define MAX_LINE_LENGTH 512 // Maximum length of one line (including comments)
  50. #define MAX_STRUCT_LINE_LENGTH 2048 // Maximum length of one struct (multiple lines)
  51. #define MAX_FUNCTION_PARAMETERS 12 // Maximum number of function parameters
  52. #define MAX_STRUCT_FIELDS 32 // Maximum number of struct fields
  53. #define MAX_ENUM_VALUES 512 // Maximum number of enum values
  54. //----------------------------------------------------------------------------------
  55. // Types and Structures Definition
  56. //----------------------------------------------------------------------------------
  57. // Function info data
  58. typedef struct FunctionInfo {
  59. char name[64]; // Function name
  60. char desc[128]; // Function description (comment at the end)
  61. char retType[32]; // Return value type
  62. int paramCount; // Number of function parameters
  63. char paramType[MAX_FUNCTION_PARAMETERS][32]; // Parameters type
  64. char paramName[MAX_FUNCTION_PARAMETERS][32]; // Parameters name
  65. char paramDesc[MAX_FUNCTION_PARAMETERS][128]; // Parameters description
  66. } FunctionInfo;
  67. // Struct info data
  68. typedef struct StructInfo {
  69. char name[64]; // Struct name
  70. char desc[128]; // Struct type description
  71. int fieldCount; // Number of fields in the struct
  72. char fieldType[MAX_STRUCT_FIELDS][64]; // Field type
  73. char fieldName[MAX_STRUCT_FIELDS][64]; // Field name
  74. char fieldDesc[MAX_STRUCT_FIELDS][128]; // Field description
  75. } StructInfo;
  76. // Enum info data
  77. typedef struct EnumInfo {
  78. char name[64]; // Enum name
  79. char desc[128]; // Enum description
  80. int valueCount; // Number of values in enumerator
  81. char valueName[MAX_ENUM_VALUES][64]; // Value name definition
  82. int valueInteger[MAX_ENUM_VALUES]; // Value integer
  83. char valueDesc[MAX_ENUM_VALUES][128]; // Value description
  84. } EnumInfo;
  85. // Type of parsed define
  86. typedef enum { UNKNOWN = 0, MACRO, GUARD, INT, LONG, FLOAT, DOUBLE, CHAR, STRING, COLOR } DefineType;
  87. // Define info data
  88. typedef struct DefineInfo {
  89. char name[64]; // Define name
  90. DefineType type; // Define type
  91. char value[256]; // Define value
  92. char desc[128]; // Define description
  93. bool isHex; // Define is hex number (for types INT, LONG)
  94. } DefineInfo;
  95. // Output format for parsed data
  96. typedef enum { DEFAULT = 0, JSON, XML, LUA } OutputFormat;
  97. //----------------------------------------------------------------------------------
  98. // Global Variables Definition
  99. //----------------------------------------------------------------------------------
  100. static int funcCount = 0;
  101. static int structCount = 0;
  102. static int enumCount = 0;
  103. static int defineCount = 0;
  104. static FunctionInfo *funcs = NULL;
  105. static StructInfo *structs = NULL;
  106. static EnumInfo *enums = NULL;
  107. static DefineInfo *defines = NULL;
  108. static char apiDefine[32] = "RLAPI\0";
  109. // Command line variables
  110. static char inFileName[512] = { 0 }; // Input file name (required in case of provided through CLI)
  111. static char outFileName[512] = { 0 }; // Output file name (required for file save/export)
  112. static int outputFormat = DEFAULT;
  113. //----------------------------------------------------------------------------------
  114. // Module Functions Declaration
  115. //----------------------------------------------------------------------------------
  116. static void ShowCommandLineInfo(void); // Show command line usage info
  117. static void ProcessCommandLine(int argc, char *argv[]); // Process command line input
  118. static char *LoadFileText(const char *fileName, int *length);
  119. static char **GetTextLines(const char *buffer, int length, int *linesCount);
  120. static void GetDataTypeAndName(const char *typeName, int typeNameLen, char *type, char *name);
  121. static unsigned int TextLength(const char *text); // Get text length in bytes, check for \0 character
  122. static bool IsTextEqual(const char *text1, const char *text2, unsigned int count);
  123. static void MemoryCopy(void *dest, const void *src, unsigned int count);
  124. static char *EscapeBackslashes(char *text); // Replace '\' by "\\" when exporting to JSON and XML
  125. static void ExportParsedData(const char *fileName, int format); // Export parsed data in desired format
  126. static const char *StrDefineType(DefineType type); // Get string of define type
  127. //------------------------------------------------------------------------------------
  128. // Program main entry point
  129. //------------------------------------------------------------------------------------
  130. int main(int argc, char* argv[])
  131. {
  132. if (argc > 1) ProcessCommandLine(argc, argv);
  133. if (inFileName[0] == '\0') MemoryCopy(inFileName, "../src/raylib.h\0", 16);
  134. int length = 0;
  135. char *buffer = LoadFileText(inFileName, &length);
  136. // Preprocess buffer to get separate lines
  137. // NOTE: GetTextLines() also removes leading spaces/tabs
  138. int linesCount = 0;
  139. char **lines = GetTextLines(buffer, length, &linesCount);
  140. // Function lines pointers, selected from buffer "lines"
  141. char **funcLines = (char **)malloc(MAX_FUNCS_TO_PARSE*sizeof(char *));
  142. // Structs data (multiple lines), selected from "buffer"
  143. int *structLines = (int *)malloc(MAX_STRUCTS_TO_PARSE*sizeof(int));
  144. // Enums lines pointers, selected from buffer "lines"
  145. int *enumLines = (int *)malloc(MAX_ENUMS_TO_PARSE*sizeof(int));
  146. // Defines lines pointers, selected from buffer "lines"
  147. int *defineLines = (int *)malloc(MAX_DEFINES_TO_PARSE*sizeof(int));
  148. // Prepare required lines for parsing
  149. //--------------------------------------------------------------------------------------------------
  150. // Read function lines
  151. for (int i = 0; i < linesCount; i++)
  152. {
  153. // Read function line (starting with `define`, i.e. for raylib.h "RLAPI")
  154. if (IsTextEqual(lines[i], apiDefine, TextLength(apiDefine)))
  155. {
  156. // Keep a pointer to the function line
  157. funcLines[funcCount] = lines[i];
  158. funcCount++;
  159. }
  160. }
  161. // Read struct lines
  162. for (int i = 0; i < linesCount; i++)
  163. {
  164. // Find structs (starting with "typedef struct ... {", ending with '} ... ;')
  165. if (IsTextEqual(lines[i], "typedef struct", 14))
  166. {
  167. int j = 0;
  168. bool validStruct = false;
  169. // WARNING: Typedefs between types: typedef Vector4 Quaternion;
  170. // (maybe we could export these too?)
  171. for (int c = 0; c < MAX_LINE_LENGTH; c++)
  172. {
  173. char v = lines[i][c];
  174. if (v == '{') validStruct = true;
  175. if (v == '{' || v == ';' || v == '\0')
  176. {
  177. // Not valid struct if it ends without '{':
  178. // i.e typedef struct rAudioBuffer rAudioBuffer; -> Typedef and forward declaration
  179. break;
  180. }
  181. }
  182. if (!validStruct) continue;
  183. structLines[structCount] = i;
  184. while (lines[i][0] != '}') i++;
  185. while (lines[i][0] != '\0') i++;
  186. structCount++;
  187. }
  188. }
  189. // Read enum lines
  190. for (int i = 0; i < linesCount; i++)
  191. {
  192. // Read enum line
  193. if (IsTextEqual(lines[i], "typedef enum {", 14) && lines[i][TextLength(lines[i])-1] != ';') // ignore inline enums
  194. {
  195. // Keep the line position in the array of lines,
  196. // so, we can scan that position and following lines
  197. enumLines[enumCount] = i;
  198. enumCount++;
  199. }
  200. }
  201. // Read const lines
  202. for (int i = 0; i < linesCount; i++)
  203. {
  204. int j = 0;
  205. while (lines[i][j] == ' ' || lines[i][j] == '\t') j++; // skip spaces and tabs in the begining
  206. // Read define line
  207. if (IsTextEqual(lines[i]+j, "#define ", 8))
  208. {
  209. // Keep the line position in the array of lines,
  210. // so, we can scan that position and following lines
  211. defineLines[defineCount] = i;
  212. defineCount++;
  213. }
  214. }
  215. // At this point we have all raylib structs, enums, functions, defines lines data to start parsing
  216. free(buffer); // Unload text buffer
  217. // Parsing raylib data
  218. //--------------------------------------------------------------------------------------------------
  219. // Structs info data
  220. structs = (StructInfo *)calloc(MAX_STRUCTS_TO_PARSE, sizeof(StructInfo));
  221. for (int i = 0; i < structCount; i++)
  222. {
  223. char **linesPtr = &lines[structLines[i]];
  224. // Parse struct description
  225. if (linesPtr[-1][0] == '/')
  226. {
  227. MemoryCopy(structs[i].desc, linesPtr[-1], TextLength(linesPtr[-1]));
  228. }
  229. // Get struct name: typedef struct name {
  230. const int TDS_LEN = 15; // length of "typedef struct "
  231. for (int c = TDS_LEN; c < 64 + TDS_LEN; c++)
  232. {
  233. if (linesPtr[0][c] == '{')
  234. {
  235. MemoryCopy(structs[i].name, &linesPtr[0][TDS_LEN], c - TDS_LEN - 1);
  236. break;
  237. }
  238. }
  239. // Get struct fields and count them -> fields finish with ;
  240. int l = 1;
  241. while (linesPtr[l][0] != '}')
  242. {
  243. // WARNING: Some structs have empty spaces and comments -> OK, processed
  244. if ((linesPtr[l][0] != ' ') && (linesPtr[l][0] != '\0'))
  245. {
  246. // Scan one field line
  247. char *fieldLine = linesPtr[l];
  248. int fieldEndPos = 0;
  249. while (fieldLine[fieldEndPos] != ';') fieldEndPos++;
  250. if (fieldLine[0] != '/') // Field line is not a comment
  251. {
  252. //printf("Struct field: %s_\n", fieldLine); // OK!
  253. // Get struct field type and name
  254. GetDataTypeAndName(fieldLine, fieldEndPos, structs[i].fieldType[structs[i].fieldCount], structs[i].fieldName[structs[i].fieldCount]);
  255. // Get the field description
  256. // We start skipping spaces in front of description comment
  257. int descStart = fieldEndPos;
  258. while ((fieldLine[descStart] != '/') && (fieldLine[descStart] != '\0')) descStart++;
  259. int k = 0;
  260. while ((fieldLine[descStart + k] != '\0') && (fieldLine[descStart + k] != '\n'))
  261. {
  262. structs[i].fieldDesc[structs[i].fieldCount][k] = fieldLine[descStart + k];
  263. k++;
  264. }
  265. structs[i].fieldCount++;
  266. }
  267. }
  268. l++;
  269. }
  270. }
  271. free(structLines);
  272. // Enum info data
  273. enums = (EnumInfo *)calloc(MAX_ENUMS_TO_PARSE, sizeof(EnumInfo));
  274. for (int i = 0; i < enumCount; i++)
  275. {
  276. // Parse enum description
  277. // NOTE: This is not necessarily from the line immediately before,
  278. // some of the enums have extra lines between the "description"
  279. // and the typedef enum
  280. for (int j = enumLines[i] - 1; j > 0; j--)
  281. {
  282. char *linePtr = lines[j];
  283. if ((linePtr[0] != '/') || (linePtr[2] != ' '))
  284. {
  285. MemoryCopy(enums[i].desc, &lines[j + 1][0], sizeof(enums[i].desc) - 1);
  286. break;
  287. }
  288. }
  289. for (int j = 1; j < MAX_ENUM_VALUES*2; j++) // Maximum number of lines following enum first line
  290. {
  291. char *linePtr = lines[enumLines[i] + j];
  292. if ((linePtr[0] >= 'A') && (linePtr[0] <= 'Z'))
  293. {
  294. // Parse enum value line, possible options:
  295. //ENUM_VALUE_NAME,
  296. //ENUM_VALUE_NAME
  297. //ENUM_VALUE_NAME = 99
  298. //ENUM_VALUE_NAME = 99,
  299. //ENUM_VALUE_NAME = 0x00000040, // Value description
  300. // We start reading the value name
  301. int c = 0;
  302. while ((linePtr[c] != ',') &&
  303. (linePtr[c] != ' ') &&
  304. (linePtr[c] != '=') &&
  305. (linePtr[c] != '\0')) { enums[i].valueName[enums[i].valueCount][c] = linePtr[c]; c++; }
  306. // After the name we can have:
  307. // '=' -> value is provided
  308. // ',' -> value is equal to previous + 1, there could be a description if not '\0'
  309. // ' ' -> value is equal to previous + 1, there could be a description if not '\0'
  310. // '\0' -> value is equal to previous + 1
  311. // Let's start checking if the line is not finished
  312. if ((linePtr[c] != ',') && (linePtr[c] != '\0'))
  313. {
  314. // Two options:
  315. // '=' -> value is provided
  316. // ' ' -> value is equal to previous + 1, there could be a description if not '\0'
  317. bool foundValue = false;
  318. while ((linePtr[c] != '\0') && (linePtr[c] != '/'))
  319. {
  320. if (linePtr[c] == '=') { foundValue = true; break; }
  321. c++;
  322. }
  323. if (foundValue)
  324. {
  325. if (linePtr[c + 1] == ' ') c += 2;
  326. else c++;
  327. // Parse integer value
  328. int n = 0;
  329. char integer[16] = { 0 };
  330. while ((linePtr[c] != ',') && (linePtr[c] != ' ') && (linePtr[c] != '\0'))
  331. {
  332. integer[n] = linePtr[c];
  333. c++; n++;
  334. }
  335. if (integer[1] == 'x') enums[i].valueInteger[enums[i].valueCount] = (int)strtol(integer, NULL, 16);
  336. else enums[i].valueInteger[enums[i].valueCount] = atoi(integer);
  337. }
  338. else enums[i].valueInteger[enums[i].valueCount] = (enums[i].valueInteger[enums[i].valueCount - 1] + 1);
  339. }
  340. else enums[i].valueInteger[enums[i].valueCount] = (enums[i].valueInteger[enums[i].valueCount - 1] + 1);
  341. // Look for description or end of line
  342. while ((linePtr[c] != '/') && (linePtr[c] != '\0')) { c++; }
  343. if (linePtr[c] == '/')
  344. {
  345. // Parse value description
  346. MemoryCopy(enums[i].valueDesc[enums[i].valueCount], &linePtr[c], sizeof(enums[0].valueDesc[0]) - c - 1);
  347. }
  348. enums[i].valueCount++;
  349. }
  350. else if (linePtr[0] == '}')
  351. {
  352. // Get enum name from typedef
  353. int c = 0;
  354. while (linePtr[2 + c] != ';') { enums[i].name[c] = linePtr[2 + c]; c++; }
  355. break; // Enum ended, break for() loop
  356. }
  357. }
  358. }
  359. free(enumLines);
  360. // Define info data
  361. defines = (DefineInfo *)calloc(MAX_DEFINES_TO_PARSE, sizeof(DefineInfo));
  362. int defineIndex = 0;
  363. for (int i = 0; i < defineCount; i++)
  364. {
  365. char *linePtr = lines[defineLines[i]];
  366. int j = 0;
  367. while (linePtr[j] == ' ' || linePtr[j] == '\t') j++; // Skip spaces and tabs in the begining
  368. j += 8; // Skip "#define "
  369. while (linePtr[j] == ' ' || linePtr[j] == '\t') j++; // Skip spaces and tabs after "#define "
  370. // Extract name
  371. int defineNameStart = j;
  372. while (linePtr[j] != ' ' && linePtr[j] != '\t' && linePtr[j] != '\0') j++;
  373. int defineNameEnd = j-1;
  374. // Skip duplicates
  375. int nameLen = defineNameEnd - defineNameStart + 1;
  376. bool isDuplicate = false;
  377. for (int k = 0; k < defineIndex; k++) {
  378. if (nameLen == TextLength(defines[k].name) && IsTextEqual(defines[k].name, linePtr + defineNameStart, nameLen)) {
  379. isDuplicate = true;
  380. break;
  381. }
  382. }
  383. if (isDuplicate) continue;
  384. MemoryCopy(defines[defineIndex].name, linePtr + defineNameStart, nameLen);
  385. // Determine type
  386. if (linePtr[defineNameEnd] == ')') defines[defineIndex].type = MACRO;
  387. while (linePtr[j] == ' ' || linePtr[j] == '\t') j++; // Skip spaces and tabs after name
  388. int defineValueStart = j;
  389. if (linePtr[j] == '\0' || linePtr == "/") defines[defineIndex].type = GUARD;
  390. if (linePtr[j] == '"') defines[defineIndex].type = STRING;
  391. else if (linePtr[j] == '\'') defines[defineIndex].type = CHAR;
  392. else if (IsTextEqual(linePtr+j, "CLITERAL(Color)", 15)) defines[defineIndex].type = COLOR;
  393. else if (isdigit(linePtr[j])) { // Parsing numbers
  394. bool isFloat = false, isNumber = true, isHex = false;
  395. while (linePtr[j] != ' ' && linePtr[j] != '\t' && linePtr[j] != '\0') {
  396. char ch = linePtr[j];
  397. if (ch == '.') isFloat = true;
  398. if (ch == 'x') isHex = true;
  399. if (!(isdigit(ch)||(ch >= 'a' && ch <= 'f')||(ch >= 'A' && ch <= 'F')||ch=='x'||ch=='L'||ch=='.'||ch=='+'||ch=='-')) isNumber = false;
  400. j++;
  401. }
  402. if (isNumber) {
  403. if (isFloat) {
  404. defines[defineIndex].type = linePtr[j-1] == 'f' ? FLOAT : DOUBLE;
  405. } else {
  406. defines[defineIndex].type = linePtr[j-1] == 'L' ? LONG : INT;
  407. defines[defineIndex].isHex = isHex;
  408. }
  409. }
  410. }
  411. // Extracting value
  412. while (linePtr[j] != '\\' && linePtr[j] != '\0' && !(linePtr[j] == '/' && linePtr[j+1] == '/')) j++;
  413. int defineValueEnd = j-1;
  414. while (linePtr[defineValueEnd] == ' ' || linePtr[defineValueEnd] == '\t') defineValueEnd--; // Remove trailing spaces and tabs
  415. if (defines[defineIndex].type == LONG || defines[defineIndex].type == FLOAT) defineValueEnd--; // Remove number postfix
  416. int valueLen = defineValueEnd - defineValueStart + 1;
  417. if (valueLen > 255) valueLen = 255;
  418. if (valueLen > 0) MemoryCopy(defines[defineIndex].value, linePtr + defineValueStart, valueLen);
  419. // Extracting description
  420. if (linePtr[j] == '/') {
  421. int commentStart = j;
  422. while (linePtr[j] != '\\' && linePtr[j] != '\0') j++;
  423. int commentEnd = j-1;
  424. int commentLen = commentEnd - commentStart + 1;
  425. if (commentLen > 127) commentLen = 127;
  426. MemoryCopy(defines[defineIndex].desc, linePtr + commentStart, commentLen);
  427. }
  428. defineIndex++;
  429. }
  430. defineCount = defineIndex;
  431. free(defineLines);
  432. // Functions info data
  433. funcs = (FunctionInfo *)calloc(MAX_FUNCS_TO_PARSE, sizeof(FunctionInfo));
  434. for (int i = 0; i < funcCount; i++)
  435. {
  436. int funcParamsStart = 0;
  437. int funcEnd = 0;
  438. // Get return type and function name from func line
  439. for (int c = 0; (c < MAX_LINE_LENGTH) && (funcLines[i][c] != '\n'); c++)
  440. {
  441. if (funcLines[i][c] == '(') // Starts function parameters
  442. {
  443. funcParamsStart = c + 1;
  444. // At this point we have function return type and function name
  445. char funcRetTypeName[128] = { 0 };
  446. int dc = TextLength(apiDefine) + 1;
  447. int funcRetTypeNameLen = c - dc; // Substract `define` ("RLAPI " for raylib.h)
  448. MemoryCopy(funcRetTypeName, &funcLines[i][dc], funcRetTypeNameLen);
  449. GetDataTypeAndName(funcRetTypeName, funcRetTypeNameLen, funcs[i].retType, funcs[i].name);
  450. break;
  451. }
  452. }
  453. // Get parameters from func line
  454. for (int c = funcParamsStart; c < MAX_LINE_LENGTH; c++)
  455. {
  456. if (funcLines[i][c] == ',') // Starts function parameters
  457. {
  458. // Get parameter type + name, extract info
  459. char funcParamTypeName[128] = { 0 };
  460. int funcParamTypeNameLen = c - funcParamsStart;
  461. MemoryCopy(funcParamTypeName, &funcLines[i][funcParamsStart], funcParamTypeNameLen);
  462. GetDataTypeAndName(funcParamTypeName, funcParamTypeNameLen, funcs[i].paramType[funcs[i].paramCount], funcs[i].paramName[funcs[i].paramCount]);
  463. funcParamsStart = c + 1;
  464. if (funcLines[i][c + 1] == ' ') funcParamsStart += 1;
  465. funcs[i].paramCount++; // Move to next parameter
  466. }
  467. else if (funcLines[i][c] == ')')
  468. {
  469. funcEnd = c + 2;
  470. // Check if previous word is void
  471. if ((funcLines[i][c - 4] == 'v') && (funcLines[i][c - 3] == 'o') && (funcLines[i][c - 2] == 'i') && (funcLines[i][c - 1] == 'd')) break;
  472. // Get parameter type + name, extract info
  473. char funcParamTypeName[128] = { 0 };
  474. int funcParamTypeNameLen = c - funcParamsStart;
  475. MemoryCopy(funcParamTypeName, &funcLines[i][funcParamsStart], funcParamTypeNameLen);
  476. GetDataTypeAndName(funcParamTypeName, funcParamTypeNameLen, funcs[i].paramType[funcs[i].paramCount], funcs[i].paramName[funcs[i].paramCount]);
  477. funcs[i].paramCount++; // Move to next parameter
  478. break;
  479. }
  480. }
  481. // Get function description
  482. for (int c = funcEnd; c < MAX_LINE_LENGTH; c++)
  483. {
  484. if (funcLines[i][c] == '/')
  485. {
  486. MemoryCopy(funcs[i].desc, &funcLines[i][c], 127); // WARNING: Size could be too long for funcLines[i][c]?
  487. break;
  488. }
  489. }
  490. }
  491. for (int i = 0; i < linesCount; i++) free(lines[i]);
  492. free(lines);
  493. free(funcLines);
  494. // At this point, all raylib data has been parsed!
  495. //-----------------------------------------------------------------------------------------
  496. // structs[] -> We have all the structs decomposed into pieces for further analysis
  497. // enums[] -> We have all the enums decomposed into pieces for further analysis
  498. // funcs[] -> We have all the functions decomposed into pieces for further analysis
  499. // defines[] -> We have all the defines decomposed into pieces for further analysis
  500. // Process input file to output
  501. if (outFileName[0] == '\0') MemoryCopy(outFileName, "raylib_api.txt\0", 15);
  502. printf("\nInput file: %s", inFileName);
  503. printf("\nOutput file: %s", outFileName);
  504. if (outputFormat == DEFAULT) printf("\nOutput format: DEFAULT\n\n");
  505. else if (outputFormat == JSON) printf("\nOutput format: JSON\n\n");
  506. else if (outputFormat == XML) printf("\nOutput format: XML\n\n");
  507. else if (outputFormat == LUA) printf("\nOutput format: LUA\n\n");
  508. ExportParsedData(outFileName, outputFormat);
  509. free(funcs);
  510. free(structs);
  511. free(enums);
  512. }
  513. //----------------------------------------------------------------------------------
  514. // Module Functions Definition
  515. //----------------------------------------------------------------------------------
  516. // Show command line usage info
  517. static void ShowCommandLineInfo(void)
  518. {
  519. printf("\n//////////////////////////////////////////////////////////////////////////////////\n");
  520. printf("// //\n");
  521. printf("// raylib API parser //\n");
  522. printf("// //\n");
  523. printf("// more info and bugs-report: github.com/raysan5/raylib/parser //\n");
  524. printf("// //\n");
  525. printf("// Copyright (c) 2021 Ramon Santamaria (@raysan5) //\n");
  526. printf("// //\n");
  527. printf("//////////////////////////////////////////////////////////////////////////////////\n\n");
  528. printf("USAGE:\n\n");
  529. printf(" > raylib_parser [--help] [--input <filename.h>] [--output <filename.ext>] [--format <type>] [--define <DEF>]\n");
  530. printf("\nOPTIONS:\n\n");
  531. printf(" -h, --help : Show tool version and command line usage help\n\n");
  532. printf(" -i, --input <filename.h> : Define input header file to parse.\n");
  533. printf(" NOTE: If not specified, defaults to: raylib.h\n\n");
  534. printf(" -o, --output <filename.ext> : Define output file and format.\n");
  535. printf(" Supported extensions: .txt, .json, .xml, .h\n");
  536. printf(" NOTE: If not specified, defaults to: raylib_api.txt\n\n");
  537. printf(" -f, --format <type> : Define output format for parser data.\n");
  538. printf(" Supported types: DEFAULT, JSON, XML, LUA\n\n");
  539. printf(" -d, --define <DEF> : Define functions define (i.e. RLAPI for raylib.h, RMDEF for raymath.h, etc\n");
  540. printf(" NOTE: If not specified, defaults to: RLAPI\n\n");
  541. printf("\nEXAMPLES:\n\n");
  542. printf(" > raylib_parser --input raylib.h --output api.json\n");
  543. printf(" Process <raylib.h> to generate <api.json>\n\n");
  544. printf(" > raylib_parser --output raylib_data.info --format XML\n");
  545. printf(" Process <raylib.h> to generate <raylib_data.info> as XML text data\n\n");
  546. printf(" > raylib_parser --input raymath.h --output raymath_data.info --format XML\n");
  547. printf(" Process <raymath.h> to generate <raymath_data.info> as XML text data\n\n");
  548. }
  549. // Process command line arguments
  550. static void ProcessCommandLine(int argc, char *argv[])
  551. {
  552. for (int i = 1; i < argc; i++)
  553. {
  554. if (IsTextEqual(argv[i], "-h", 2) || IsTextEqual(argv[i], "--help", 6))
  555. {
  556. // Show info
  557. ShowCommandLineInfo();
  558. }
  559. else if (IsTextEqual(argv[i], "-i", 2) || IsTextEqual(argv[i], "--input", 7))
  560. {
  561. // Check for valid argument and valid file extension
  562. if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
  563. {
  564. MemoryCopy(inFileName, argv[i + 1], TextLength(argv[i + 1])); // Read input filename
  565. i++;
  566. }
  567. else printf("WARNING: No input file provided\n");
  568. }
  569. else if (IsTextEqual(argv[i], "-o", 2) || IsTextEqual(argv[i], "--output", 8))
  570. {
  571. if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
  572. {
  573. MemoryCopy(outFileName, argv[i + 1], TextLength(argv[i + 1])); // Read output filename
  574. i++;
  575. }
  576. else printf("WARNING: No output file provided\n");
  577. }
  578. else if (IsTextEqual(argv[i], "-f", 2) || IsTextEqual(argv[i], "--format", 8))
  579. {
  580. if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
  581. {
  582. if (IsTextEqual(argv[i + 1], "DEFAULT\0", 8)) outputFormat = DEFAULT;
  583. else if (IsTextEqual(argv[i + 1], "JSON\0", 5)) outputFormat = JSON;
  584. else if (IsTextEqual(argv[i + 1], "XML\0", 4)) outputFormat = XML;
  585. else if (IsTextEqual(argv[i + 1], "LUA\0", 4)) outputFormat = LUA;
  586. }
  587. else printf("WARNING: No format parameters provided\n");
  588. }
  589. else if (IsTextEqual(argv[i], "-d", 2) || IsTextEqual(argv[i], "--define", 8))
  590. {
  591. if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
  592. {
  593. MemoryCopy(apiDefine, argv[i + 1], TextLength(argv[i + 1])); // Read functions define
  594. apiDefine[TextLength(argv[i + 1])] = '\0';
  595. i++;
  596. }
  597. else printf("WARNING: No define key provided\n");
  598. }
  599. }
  600. }
  601. // Load text data from file, returns a '\0' terminated string
  602. // NOTE: text chars array should be freed manually
  603. static char *LoadFileText(const char *fileName, int *length)
  604. {
  605. char *text = NULL;
  606. if (fileName != NULL)
  607. {
  608. FILE *file = fopen(fileName, "rt");
  609. if (file != NULL)
  610. {
  611. // WARNING: When reading a file as 'text' file,
  612. // text mode causes carriage return-linefeed translation...
  613. // ...but using fseek() should return correct byte-offset
  614. fseek(file, 0, SEEK_END);
  615. int size = ftell(file);
  616. fseek(file, 0, SEEK_SET);
  617. if (size > 0)
  618. {
  619. text = (char *)calloc((size + 1), sizeof(char));
  620. unsigned int count = (unsigned int)fread(text, sizeof(char), size, file);
  621. // WARNING: \r\n is converted to \n on reading, so,
  622. // read bytes count gets reduced by the number of lines
  623. if (count < (unsigned int)size)
  624. {
  625. text = realloc(text, count + 1);
  626. *length = count;
  627. }
  628. else *length = size;
  629. // Zero-terminate the string
  630. text[count] = '\0';
  631. }
  632. fclose(file);
  633. }
  634. }
  635. return text;
  636. }
  637. // Get all lines from a text buffer (expecting lines ending with '\n')
  638. static char **GetTextLines(const char *buffer, int length, int *linesCount)
  639. {
  640. // Get the number of lines in the text
  641. int count = 0;
  642. for (int i = 0; i < length; i++) if (buffer[i] == '\n') count++;
  643. printf("Number of text lines in buffer: %i\n", count);
  644. // Allocate as many pointers as lines
  645. char **lines = (char **)malloc(count*sizeof(char **));
  646. char *bufferPtr = (char *)buffer;
  647. for (int i = 0; (i < count) || (bufferPtr[0] != '\0'); i++)
  648. {
  649. lines[i] = (char *)calloc(MAX_LINE_LENGTH, sizeof(char));
  650. // Remove line leading spaces
  651. // Find last index of space/tab character
  652. int index = 0;
  653. while ((bufferPtr[index] == ' ') || (bufferPtr[index] == '\t')) index++;
  654. int j = 0;
  655. while (bufferPtr[index + j] != '\n')
  656. {
  657. lines[i][j] = bufferPtr[index + j];
  658. j++;
  659. }
  660. bufferPtr += (index + j + 1);
  661. }
  662. *linesCount = count;
  663. return lines;
  664. }
  665. // Get data type and name from a string containing both
  666. // NOTE: Useful to parse function parameters and struct fields
  667. static void GetDataTypeAndName(const char *typeName, int typeNameLen, char *type, char *name)
  668. {
  669. for (int k = typeNameLen; k > 0; k--)
  670. {
  671. if (typeName[k] == ' ' && typeName[k - 1] != ',')
  672. {
  673. // Function name starts at this point (and ret type finishes at this point)
  674. MemoryCopy(type, typeName, k);
  675. MemoryCopy(name, typeName + k + 1, typeNameLen - k - 1);
  676. break;
  677. }
  678. else if (typeName[k] == '*')
  679. {
  680. MemoryCopy(type, typeName, k + 1);
  681. MemoryCopy(name, typeName + k + 1, typeNameLen - k - 1);
  682. break;
  683. }
  684. else if (typeName[k] == '.' && typeNameLen == 3) // Handle varargs ...);
  685. {
  686. MemoryCopy(type, "...", 3);
  687. MemoryCopy(name, "args", 4);
  688. break;
  689. }
  690. }
  691. }
  692. // Get text length in bytes, check for \0 character
  693. static unsigned int TextLength(const char *text)
  694. {
  695. unsigned int length = 0;
  696. if (text != NULL) while (*text++) length++;
  697. return length;
  698. }
  699. // Custom memcpy() to avoid <string.h>
  700. static void MemoryCopy(void *dest, const void *src, unsigned int count)
  701. {
  702. char *srcPtr = (char *)src;
  703. char *destPtr = (char *)dest;
  704. for (unsigned int i = 0; i < count; i++) destPtr[i] = srcPtr[i];
  705. }
  706. // Compare two text strings, requires number of characters to compare
  707. static bool IsTextEqual(const char *text1, const char *text2, unsigned int count)
  708. {
  709. bool result = true;
  710. for (unsigned int i = 0; i < count; i++)
  711. {
  712. if (text1[i] != text2[i])
  713. {
  714. result = false;
  715. break;
  716. }
  717. }
  718. return result;
  719. }
  720. // Escape backslashes in a string, writing the escaped string into a static buffer
  721. static char *EscapeBackslashes(char *text)
  722. {
  723. static char buffer[256] = { 0 };
  724. int count = 0;
  725. for (int i = 0; (text[i] != '\0') && (i < 255); i++, count++)
  726. {
  727. buffer[count] = text[i];
  728. if (text[i] == '\\')
  729. {
  730. buffer[count + 1] = '\\';
  731. count++;
  732. }
  733. }
  734. buffer[count] = '\0';
  735. return buffer;
  736. }
  737. // Get string of define type
  738. static const char *StrDefineType(DefineType type)
  739. {
  740. switch (type)
  741. {
  742. case UNKNOWN: return "UNKNOWN";
  743. case GUARD: return "GUARD";
  744. case MACRO: return "MACRO";
  745. case INT: return "INT";
  746. case LONG: return "LONG";
  747. case FLOAT: return "FLOAT";
  748. case DOUBLE: return "DOUBLE";
  749. case CHAR: return "CHAR";
  750. case STRING: return "STRING";
  751. case COLOR: return "COLOR";
  752. }
  753. return "";
  754. }
  755. /*
  756. // Replace text string
  757. // REQUIRES: strlen(), strstr(), strncpy(), strcpy() -> TODO: Replace by custom implementations!
  758. // WARNING: Returned buffer must be freed by the user (if return != NULL)
  759. static char *TextReplace(char *text, const char *replace, const char *by)
  760. {
  761. // Sanity checks and initialization
  762. if (!text || !replace || !by) return NULL;
  763. char *result;
  764. char *insertPoint; // Next insert point
  765. char *temp; // Temp pointer
  766. int replaceLen; // Replace string length of (the string to remove)
  767. int byLen; // Replacement length (the string to replace replace by)
  768. int lastReplacePos; // Distance between replace and end of last replace
  769. int count; // Number of replacements
  770. replaceLen = strlen(replace);
  771. if (replaceLen == 0) return NULL; // Empty replace causes infinite loop during count
  772. byLen = strlen(by);
  773. // Count the number of replacements needed
  774. insertPoint = text;
  775. for (count = 0; (temp = strstr(insertPoint, replace)); count++) insertPoint = temp + replaceLen;
  776. // Allocate returning string and point temp to it
  777. temp = result = (char *)malloc(strlen(text) + (byLen - replaceLen)*count + 1);
  778. if (!result) return NULL; // Memory could not be allocated
  779. // First time through the loop, all the variable are set correctly from here on,
  780. // - 'temp' points to the end of the result string
  781. // - 'insertPoint' points to the next occurrence of replace in text
  782. // - 'text' points to the remainder of text after "end of replace"
  783. while (count--)
  784. {
  785. insertPoint = strstr(text, replace);
  786. lastReplacePos = (int)(insertPoint - text);
  787. temp = strncpy(temp, text, lastReplacePos) + lastReplacePos;
  788. temp = strcpy(temp, by) + byLen;
  789. text += lastReplacePos + replaceLen; // Move to next "end of replace"
  790. }
  791. // Copy remaind text part after replacement to result (pointed by moving temp)
  792. strcpy(temp, text);
  793. return result;
  794. }
  795. */
  796. // Export parsed data in desired format
  797. static void ExportParsedData(const char *fileName, int format)
  798. {
  799. FILE *outFile = fopen(fileName, "wt");
  800. switch (format)
  801. {
  802. case DEFAULT:
  803. {
  804. // Print structs info
  805. fprintf(outFile, "\nStructures found: %i\n\n", structCount);
  806. for (int i = 0; i < structCount; i++)
  807. {
  808. fprintf(outFile, "Struct %02i: %s (%i fields)\n", i + 1, structs[i].name, structs[i].fieldCount);
  809. fprintf(outFile, " Name: %s\n", structs[i].name);
  810. fprintf(outFile, " Description: %s\n", structs[i].desc + 3);
  811. 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]);
  812. }
  813. // Print enums info
  814. fprintf(outFile, "\nEnums found: %i\n\n", enumCount);
  815. for (int i = 0; i < enumCount; i++)
  816. {
  817. fprintf(outFile, "Enum %02i: %s (%i values)\n", i + 1, enums[i].name, enums[i].valueCount);
  818. fprintf(outFile, " Name: %s\n", enums[i].name);
  819. fprintf(outFile, " Description: %s\n", enums[i].desc + 3);
  820. for (int e = 0; e < enums[i].valueCount; e++) fprintf(outFile, " Value[%s]: %i\n", enums[i].valueName[e], enums[i].valueInteger[e]);
  821. }
  822. // Print functions info
  823. fprintf(outFile, "\nFunctions found: %i\n\n", funcCount);
  824. for (int i = 0; i < funcCount; i++)
  825. {
  826. fprintf(outFile, "Function %03i: %s() (%i input parameters)\n", i + 1, funcs[i].name, funcs[i].paramCount);
  827. fprintf(outFile, " Name: %s\n", funcs[i].name);
  828. fprintf(outFile, " Return type: %s\n", funcs[i].retType);
  829. fprintf(outFile, " Description: %s\n", funcs[i].desc + 3);
  830. 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]);
  831. if (funcs[i].paramCount == 0) fprintf(outFile, " No input parameters\n");
  832. }
  833. fprintf(outFile, "\nDefines found: %i\n\n", defineCount);
  834. for (int i = 0; i < defineCount; i++)
  835. {
  836. fprintf(outFile, "Define %03i: %s\n", i + 1, defines[i].name);
  837. fprintf(outFile, " Name: %s\n", defines[i].name);
  838. fprintf(outFile, " Type: %s\n", StrDefineType(defines[i].type));
  839. fprintf(outFile, " Value: %s\n", defines[i].value);
  840. fprintf(outFile, " Description: %s\n", defines[i].desc + 3);
  841. }
  842. } break;
  843. case LUA:
  844. {
  845. fprintf(outFile, "return {\n");
  846. // Print structs info
  847. fprintf(outFile, " structs = {\n");
  848. for (int i = 0; i < structCount; i++)
  849. {
  850. fprintf(outFile, " {\n");
  851. fprintf(outFile, " name = \"%s\",\n", structs[i].name);
  852. fprintf(outFile, " description = \"%s\",\n", EscapeBackslashes(structs[i].desc + 3));
  853. fprintf(outFile, " fields = {\n");
  854. for (int f = 0; f < structs[i].fieldCount; f++)
  855. {
  856. fprintf(outFile, " {\n");
  857. fprintf(outFile, " type = \"%s\",\n", structs[i].fieldType[f]);
  858. fprintf(outFile, " name = \"%s\",\n", structs[i].fieldName[f]);
  859. fprintf(outFile, " description = \"%s\"\n", EscapeBackslashes(structs[i].fieldDesc[f] + 3));
  860. fprintf(outFile, " }");
  861. if (f < structs[i].fieldCount - 1) fprintf(outFile, ",\n");
  862. else fprintf(outFile, "\n");
  863. }
  864. fprintf(outFile, " }\n");
  865. fprintf(outFile, " }");
  866. if (i < structCount - 1) fprintf(outFile, ",\n");
  867. else fprintf(outFile, "\n");
  868. }
  869. fprintf(outFile, " },\n");
  870. // Print enums info
  871. fprintf(outFile, " enums = {\n");
  872. for (int i = 0; i < enumCount; i++)
  873. {
  874. fprintf(outFile, " {\n");
  875. fprintf(outFile, " name = \"%s\",\n", enums[i].name);
  876. fprintf(outFile, " description = \"%s\",\n", EscapeBackslashes(enums[i].desc + 3));
  877. fprintf(outFile, " values = {\n");
  878. for (int e = 0; e < enums[i].valueCount; e++)
  879. {
  880. fprintf(outFile, " {\n");
  881. fprintf(outFile, " name = \"%s\",\n", enums[i].valueName[e]);
  882. fprintf(outFile, " value = %i,\n", enums[i].valueInteger[e]);
  883. fprintf(outFile, " description = \"%s\"\n", EscapeBackslashes(enums[i].valueDesc[e] + 3));
  884. fprintf(outFile, " }");
  885. if (e < enums[i].valueCount - 1) fprintf(outFile, ",\n");
  886. else fprintf(outFile, "\n");
  887. }
  888. fprintf(outFile, " }\n");
  889. fprintf(outFile, " }");
  890. if (i < enumCount - 1) fprintf(outFile, ",\n");
  891. else fprintf(outFile, "\n");
  892. }
  893. fprintf(outFile, " },\n");
  894. // Print defines info
  895. fprintf(outFile, " defines = {\n");
  896. for (int i = 0; i < defineCount; i++)
  897. {
  898. fprintf(outFile, " {\n");
  899. fprintf(outFile, " name = \"%s\",\n", defines[i].name);
  900. fprintf(outFile, " type = \"%s\",\n", StrDefineType(defines[i].type));
  901. if (defines[i].type == INT || defines[i].type == LONG || defines[i].type == FLOAT || defines[i].type == DOUBLE || defines[i].type == STRING) {
  902. fprintf(outFile, " value = %s,\n", defines[i].value);
  903. } else {
  904. fprintf(outFile, " value = \"%s\",\n", defines[i].value);
  905. }
  906. fprintf(outFile, " description = \"%s\"\n", defines[i].desc + 3);
  907. fprintf(outFile, " }");
  908. if (i < defineCount - 1) fprintf(outFile, ",\n");
  909. else fprintf(outFile, "\n");
  910. }
  911. fprintf(outFile, " },\n");
  912. // Print functions info
  913. fprintf(outFile, " functions = {\n");
  914. for (int i = 0; i < funcCount; i++)
  915. {
  916. fprintf(outFile, " {\n");
  917. fprintf(outFile, " name = \"%s\",\n", funcs[i].name);
  918. fprintf(outFile, " description = \"%s\",\n", EscapeBackslashes(funcs[i].desc + 3));
  919. fprintf(outFile, " returnType = \"%s\"", funcs[i].retType);
  920. if (funcs[i].paramCount == 0) fprintf(outFile, "\n");
  921. else
  922. {
  923. fprintf(outFile, ",\n params = {\n");
  924. for (int p = 0; p < funcs[i].paramCount; p++)
  925. {
  926. fprintf(outFile, " {type = \"%s\", name = \"%s\"}", funcs[i].paramType[p], funcs[i].paramName[p]);
  927. if (p < funcs[i].paramCount - 1) fprintf(outFile, ",\n");
  928. else fprintf(outFile, "\n");
  929. }
  930. fprintf(outFile, " }\n");
  931. }
  932. fprintf(outFile, " }");
  933. if (i < funcCount - 1) fprintf(outFile, ",\n");
  934. else fprintf(outFile, "\n");
  935. }
  936. fprintf(outFile, " }\n");
  937. fprintf(outFile, "}\n");
  938. } break;
  939. case JSON:
  940. {
  941. fprintf(outFile, "{\n");
  942. // Print structs info
  943. fprintf(outFile, " \"structs\": [\n");
  944. for (int i = 0; i < structCount; i++)
  945. {
  946. fprintf(outFile, " {\n");
  947. fprintf(outFile, " \"name\": \"%s\",\n", structs[i].name);
  948. fprintf(outFile, " \"description\": \"%s\",\n", EscapeBackslashes(structs[i].desc + 3));
  949. fprintf(outFile, " \"fields\": [\n");
  950. for (int f = 0; f < structs[i].fieldCount; f++)
  951. {
  952. fprintf(outFile, " {\n");
  953. fprintf(outFile, " \"type\": \"%s\",\n", structs[i].fieldType[f]);
  954. fprintf(outFile, " \"name\": \"%s\",\n", structs[i].fieldName[f]);
  955. fprintf(outFile, " \"description\": \"%s\"\n", EscapeBackslashes(structs[i].fieldDesc[f] + 3));
  956. fprintf(outFile, " }");
  957. if (f < structs[i].fieldCount - 1) fprintf(outFile, ",\n");
  958. else fprintf(outFile, "\n");
  959. }
  960. fprintf(outFile, " ]\n");
  961. fprintf(outFile, " }");
  962. if (i < structCount - 1) fprintf(outFile, ",\n");
  963. else fprintf(outFile, "\n");
  964. }
  965. fprintf(outFile, " ],\n");
  966. // Print enums info
  967. fprintf(outFile, " \"enums\": [\n");
  968. for (int i = 0; i < enumCount; i++)
  969. {
  970. fprintf(outFile, " {\n");
  971. fprintf(outFile, " \"name\": \"%s\",\n", enums[i].name);
  972. fprintf(outFile, " \"description\": \"%s\",\n", EscapeBackslashes(enums[i].desc + 3));
  973. fprintf(outFile, " \"values\": [\n");
  974. for (int e = 0; e < enums[i].valueCount; e++)
  975. {
  976. fprintf(outFile, " {\n");
  977. fprintf(outFile, " \"name\": \"%s\",\n", enums[i].valueName[e]);
  978. fprintf(outFile, " \"value\": %i,\n", enums[i].valueInteger[e]);
  979. fprintf(outFile, " \"description\": \"%s\"\n", EscapeBackslashes(enums[i].valueDesc[e] + 3));
  980. fprintf(outFile, " }");
  981. if (e < enums[i].valueCount - 1) fprintf(outFile, ",\n");
  982. else fprintf(outFile, "\n");
  983. }
  984. fprintf(outFile, " ]\n");
  985. fprintf(outFile, " }");
  986. if (i < enumCount - 1) fprintf(outFile, ",\n");
  987. else fprintf(outFile, "\n");
  988. }
  989. fprintf(outFile, " ],\n");
  990. // Print defines info
  991. fprintf(outFile, " \"defines\": [\n");
  992. for (int i = 0; i < defineCount; i++)
  993. {
  994. fprintf(outFile, " {\n");
  995. fprintf(outFile, " \"name\": \"%s\",\n", defines[i].name);
  996. fprintf(outFile, " \"type\": \"%s\",\n", StrDefineType(defines[i].type));
  997. if (defines[i].isHex) { // INT or LONG
  998. fprintf(outFile, " \"value\": %ld,\n", strtol(defines[i].value, NULL, 16));
  999. } else if (defines[i].type == INT || defines[i].type == LONG || defines[i].type == FLOAT || defines[i].type == DOUBLE || defines[i].type == STRING) {
  1000. fprintf(outFile, " \"value\": %s,\n", defines[i].value);
  1001. } else {
  1002. fprintf(outFile, " \"value\": \"%s\",\n", defines[i].value);
  1003. }
  1004. fprintf(outFile, " \"description\": \"%s\"\n", defines[i].desc + 3);
  1005. fprintf(outFile, " }");
  1006. if (i < defineCount - 1) fprintf(outFile, ",\n");
  1007. else fprintf(outFile, "\n");
  1008. }
  1009. fprintf(outFile, " ],\n");
  1010. // Print functions info
  1011. fprintf(outFile, " \"functions\": [\n");
  1012. for (int i = 0; i < funcCount; i++)
  1013. {
  1014. fprintf(outFile, " {\n");
  1015. fprintf(outFile, " \"name\": \"%s\",\n", funcs[i].name);
  1016. fprintf(outFile, " \"description\": \"%s\",\n", EscapeBackslashes(funcs[i].desc + 3));
  1017. fprintf(outFile, " \"returnType\": \"%s\"", funcs[i].retType);
  1018. if (funcs[i].paramCount == 0) fprintf(outFile, "\n");
  1019. else
  1020. {
  1021. fprintf(outFile, ",\n \"params\": [\n");
  1022. for (int p = 0; p < funcs[i].paramCount; p++)
  1023. {
  1024. fprintf(outFile, " {\n");
  1025. fprintf(outFile, " \"type\": \"%s\",\n", funcs[i].paramType[p]);
  1026. fprintf(outFile, " \"name\": \"%s\"\n", funcs[i].paramName[p]);
  1027. fprintf(outFile, " }");
  1028. if (p < funcs[i].paramCount - 1) fprintf(outFile, ",\n");
  1029. else fprintf(outFile, "\n");
  1030. }
  1031. fprintf(outFile, " ]\n");
  1032. }
  1033. fprintf(outFile, " }");
  1034. if (i < funcCount - 1) fprintf(outFile, ",\n");
  1035. else fprintf(outFile, "\n");
  1036. }
  1037. fprintf(outFile, " ]\n");
  1038. fprintf(outFile, "}\n");
  1039. } break;
  1040. case XML:
  1041. {
  1042. // XML format to export data:
  1043. /*
  1044. <?xml version="1.0" encoding="Windows-1252" ?>
  1045. <raylibAPI>
  1046. <Structs count="">
  1047. <Struct name="" fieldCount="" desc="">
  1048. <Field type="" name="" desc="">
  1049. <Field type="" name="" desc="">
  1050. </Struct>
  1051. <Structs>
  1052. <Enums count="">
  1053. <Enum name="" valueCount="" desc="">
  1054. <Value name="" integer="" desc="">
  1055. <Value name="" integer="" desc="">
  1056. </Enum>
  1057. </Enums>
  1058. <Functions count="">
  1059. <Function name="" retType="" paramCount="" desc="">
  1060. <Param type="" name="" desc="" />
  1061. <Param type="" name="" desc="" />
  1062. </Function>
  1063. </Functions>
  1064. </raylibAPI>
  1065. */
  1066. fprintf(outFile, "<?xml version=\"1.0\" encoding=\"Windows-1252\" ?>\n");
  1067. fprintf(outFile, "<raylibAPI>\n");
  1068. // Print structs info
  1069. fprintf(outFile, " <Structs count=\"%i\">\n", structCount);
  1070. for (int i = 0; i < structCount; i++)
  1071. {
  1072. fprintf(outFile, " <Struct name=\"%s\" fieldCount=\"%i\" desc=\"%s\">\n", structs[i].name, structs[i].fieldCount, structs[i].desc + 3);
  1073. for (int f = 0; f < structs[i].fieldCount; f++)
  1074. {
  1075. fprintf(outFile, " <Field type=\"%s\" name=\"%s\" desc=\"%s\" />\n", structs[i].fieldType[f], structs[i].fieldName[f], structs[i].fieldDesc[f] + 3);
  1076. }
  1077. fprintf(outFile, " </Struct>\n");
  1078. }
  1079. fprintf(outFile, " </Structs>\n");
  1080. // Print enums info
  1081. fprintf(outFile, " <Enums count=\"%i\">\n", enumCount);
  1082. for (int i = 0; i < enumCount; i++)
  1083. {
  1084. fprintf(outFile, " <Enum name=\"%s\" valueCount=\"%i\" desc=\"%s\">\n", enums[i].name, enums[i].valueCount, enums[i].desc + 3);
  1085. for (int v = 0; v < enums[i].valueCount; v++)
  1086. {
  1087. fprintf(outFile, " <Value name=\"%s\" integer=\"%i\" desc=\"%s\" />\n", enums[i].valueName[v], enums[i].valueInteger[v], enums[i].valueDesc[v] + 3);
  1088. }
  1089. fprintf(outFile, " </Enum>\n");
  1090. }
  1091. fprintf(outFile, " </Enums>\n");
  1092. // Print defines info
  1093. fprintf(outFile, " <Defines count=\"%i\">\n", defineCount);
  1094. for (int i = 0; i < defineCount; i++)
  1095. {
  1096. fprintf(outFile, " <Define name=\"%s\" type=\"%s\" ", defines[i].name, StrDefineType(defines[i].type));
  1097. if (defines[i].type == STRING)
  1098. {
  1099. fprintf(outFile, "value=%s", defines[i].value);
  1100. }
  1101. else
  1102. {
  1103. fprintf(outFile, "value=\"%s\"", defines[i].value);
  1104. }
  1105. fprintf(outFile, " desc=\"%s\" />\n", defines[i].desc + 3);
  1106. }
  1107. fprintf(outFile, " </Defines>\n");
  1108. // Print functions info
  1109. fprintf(outFile, " <Functions count=\"%i\">\n", funcCount);
  1110. for (int i = 0; i < funcCount; i++)
  1111. {
  1112. 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);
  1113. for (int p = 0; p < funcs[i].paramCount; p++)
  1114. {
  1115. fprintf(outFile, " <Param type=\"%s\" name=\"%s\" desc=\"%s\" />\n", funcs[i].paramType[p], funcs[i].paramName[p], funcs[i].paramDesc[p] + 3);
  1116. }
  1117. fprintf(outFile, " </Function>\n");
  1118. }
  1119. fprintf(outFile, " </Functions>\n");
  1120. fprintf(outFile, "</raylibAPI>\n");
  1121. } break;
  1122. default: break;
  1123. }
  1124. fclose(outFile);
  1125. }