Debian Codegraph @

tools/rlparser/rlparser.c

raw symbols SCIP
1 /**********************************************************************************************
2
3 rlparser - raylib header API parser, extracts API information as separate tokens
4
5 This parser scans raylib.h to get API information about defines, structs, aliases, enums, callbacks and functions.
6 All data is divided into pieces, usually as strings. The following types are used for data:
7 - struct DefineInfo
8 - struct StructInfo
9 - struct AliasInfo
10 - struct EnumInfo
11 - struct FunctionInfo
12
13 WARNING: This parser is specifically designed to work with raylib.h, and has some contraints
14 in that regards. Still, it can also work with other header files that follow same file structure
15 conventions as raylib.h: rlgl.h, raymath.h, raygui.h, reasings.h
16
17 CONSTRAINTS:
18 This parser is specifically designed to work with raylib.h, so, it has some constraints:
19
20 - Functions are expected as a single line with the following structure:
21 <retType> <name>(<paramType[0]> <paramName[0]>, <paramType[1]> <paramName[1]>); <desc>
22
23 WARNING: Be careful with functions broken into several lines, it breaks the process!
24
25 - Structures are expected as several lines with the following form:
26 <desc>
27 typedef struct <name> {
28 <fieldType[0]> <fieldName[0]>; <fieldDesc[0]>
29 <fieldType[1]> <fieldName[1]>; <fieldDesc[1]>
30 <fieldType[2]> <fieldName[2]>; <fieldDesc[2]>
31 } <name>;
32
33 - Enums are expected as several lines with the following form:
34 <desc>
35 typedef enum {
36 <valueName[0]> = <valueInteger[0]>, <valueDesc[0]>
37 <valueName[1]>,
38 <valueName[2]>, <valueDesc[2]>
39 <valueName[3]> <valueDesc[3]>
40 } <name>;
41
42 NOTE: Multiple options are supported for enums:
43 - If value is not provided, (<valueInteger[i -1]> + 1) is assigned
44 - Value description can be provided or not
45
46 OTHER NOTES:
47 - This parser could work with other C header files if mentioned constraints are followed.
48 - This parser does not require <string.h> library, all data is parsed directly from char buffers.
49
50 LICENSE: zlib/libpng
51
52 raylib-parser is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
53 BSD-like license that allows static linking with closed source software:
54
55 Copyright (c) 2021-2026 Ramon Santamaria (@raysan5)
56
57 **********************************************************************************************/
58
59 #define _CRT_SECURE_NO_WARNINGS
60
61 #include <stdlib.h> // Required for: malloc(), calloc(), realloc(), free(), atoi(), strtol()
62 #include <stdio.h> // Required for: printf(), fopen(), fseek(), ftell(), fread(), fclose()
63 #include <stdbool.h> // Required for: bool
64 #include <ctype.h> // Required for: isdigit()
65
66 #define MAX_DEFINES_TO_PARSE 2048 // Maximum number of defines to parse
67 #define MAX_STRUCTS_TO_PARSE 64 // Maximum number of structures to parse
68 #define MAX_ALIASES_TO_PARSE 64 // Maximum number of aliases to parse
69 #define MAX_ENUMS_TO_PARSE 64 // Maximum number of enums to parse
70 #define MAX_CALLBACKS_TO_PARSE 64 // Maximum number of callbacks to parse
71 #define MAX_FUNCS_TO_PARSE 1024 // Maximum number of functions to parse
72
73 #define MAX_LINE_LENGTH 1024 // Maximum length of one line (including comments)
74
75 #define MAX_STRUCT_FIELDS 64 // Maximum number of struct fields
76 #define MAX_ENUM_VALUES 512 // Maximum number of enum values
77 #define MAX_FUNCTION_PARAMETERS 12 // Maximum number of function parameters
78
79 //----------------------------------------------------------------------------------
80 // Types and Structures Definition
81 //----------------------------------------------------------------------------------
82
83 // Define value type
84 typedef enum {
85 UNKNOWN = 0,
86 MACRO,
87 GUARD,
88 INT,
89 INT_MATH,
90 LONG,
91 LONG_MATH,
92 FLOAT,
93 FLOAT_MATH,
94 DOUBLE,
95 DOUBLE_MATH,
96 CHAR,
97 STRING,
98 COLOR
99 } DefineType;
100
101 // Define info data
102 typedef struct DefineInfo {
103 char name[64]; // Define name
104 int type; // Define type: enum DefineType
105 char value[256]; // Define value
106 char desc[128]; // Define description
107 bool isHex; // Define is hex number (for types INT, LONG)
108 } DefineInfo;
109
110 // Struct info data
111 typedef struct StructInfo {
112 char name[64]; // Struct name
113 char desc[128]; // Struct type description
114 int fieldCount; // Number of fields in the struct
115 char fieldType[MAX_STRUCT_FIELDS][64]; // Field type
116 char fieldName[MAX_STRUCT_FIELDS][64]; // Field name
117 char fieldDesc[MAX_STRUCT_FIELDS][128]; // Field description
118 } StructInfo;
119
120 // Alias info data
121 typedef struct AliasInfo {
122 char type[64]; // Alias type
123 char name[64]; // Alias name
124 char desc[128]; // Alias description
125 } AliasInfo;
126
127 // Enum info data
128 typedef struct EnumInfo {
129 char name[64]; // Enum name
130 char desc[128]; // Enum description
131 int valueCount; // Number of values in enumerator
132 char valueName[MAX_ENUM_VALUES][64]; // Value name definition
133 int valueInteger[MAX_ENUM_VALUES]; // Value integer
134 char valueDesc[MAX_ENUM_VALUES][128]; // Value description
135 } EnumInfo;
136
137 // Function info data
138 typedef struct FunctionInfo {
139 char name[64]; // Function name
140 char desc[512]; // Function description (comment at the end)
141 char retType[32]; // Return value type
142 int paramCount; // Number of function parameters
143 char paramType[MAX_FUNCTION_PARAMETERS][32]; // Parameters type
144 char paramName[MAX_FUNCTION_PARAMETERS][32]; // Parameters name
145 char paramDesc[MAX_FUNCTION_PARAMETERS][128]; // Parameters description
146 } FunctionInfo;
147
148 // Output format for parsed data
149 typedef enum { DEFAULT = 0, JSON, XML, LUA, CODE } OutputFormat;
150
151 //----------------------------------------------------------------------------------
152 // Global Variables Definition
153 //----------------------------------------------------------------------------------
154 static int defineCount = 0;
155 static int structCount = 0;
156 static int aliasCount = 0;
157 static int enumCount = 0;
158 static int callbackCount = 0;
159 static int funcCount = 0;
160 static DefineInfo *defines = NULL;
161 static StructInfo *structs = NULL;
162 static AliasInfo *aliases = NULL;
163 static EnumInfo *enums = NULL;
164 static FunctionInfo *callbacks = NULL;
165 static FunctionInfo *funcs = NULL;
166
167 // Command line variables
168 static char apiDefine[32] = { 0 }; // Functions define (i.e. RLAPI for raylib.h, RMDEF for raymath.h, etc.)
169 static char truncAfter[32] = { 0 }; // Truncate marker (i.e. "RLGL IMPLEMENTATION" for rlgl.h)
170 static int outputFormat = DEFAULT;
171
172 // NOTE: Filename max length depends on OS, in Windows MAX_PATH = 256
173 static char inFileName[512] = { 0 }; // Input file name (required in case of drag & drop over executable)
174 static char outFileName[512] = { 0 }; // Output file name (required for file save/export)
175
176 //----------------------------------------------------------------------------------
177 // Module Functions Declaration
178 //----------------------------------------------------------------------------------
179 static void ShowCommandLineInfo(void); // Show command line usage info
180 static void ProcessCommandLine(int argc, char *argv[]); // Process command line input
181
182 static char *LoadFileText(const char *fileName, int *length); // Load text file - UnloadFileText() required!
183 static void UnloadFileText(char *text); // Unload text data
184 static char **LoadTextLines(const char *buffer, int length, int *lineCount); // Load all lines from a text buffer (expecting lines ending with '\n') - UnloadTextLines() required
185 static void UnloadTextLines(char **lines, int lineCount); // Unload text lines data
186 static void GetDataTypeAndName(const char *typeName, int typeNameLen, char *type, char *name); // Get data type and name from a string containing both (i.e function param and struct fields)
187 static void GetDescription(const char *source, char *description); // Get description comment from a line, do nothing if no comment in line
188 static void MoveArraySize(char *name, char *type); // Move array size from name to type
189 static unsigned int TextLength(const char *text); // Get text length in bytes, check for \0 character
190 static bool IsTextEqual(const char *text1, const char *text2, unsigned int count);
191 static int TextFindIndex(const char *text, const char *find); // Find first text occurrence within a string
192 static void MemoryCopy(void *dest, const void *src, unsigned int count); // Memory copy, memcpy() replacement to avoid <string.h>
193 static char *EscapeBackslashes(char *text); // Replace '\' by "\\" when exporting to JSON and XML
194 static const char *StrDefineType(DefineType type); // Get string of define type
195
196 static void ExportParsedData(const char *fileName, int format); // Export parsed data in desired format
197
198 //----------------------------------------------------------------------------------
199 // Program main entry point
200 //----------------------------------------------------------------------------------
201 int main(int argc, char *argv[])
202 {
203 if (argc > 1) ProcessCommandLine(argc, argv);
204
205 const char *raylibhPath = "../src/raylib.h\0";
206 const char *raylibapiPath = "raylib_api.txt\0";
207 const char *rlapiPath = "RLAPI\0";
208 if (inFileName[0] == '\0') MemoryCopy(inFileName, raylibhPath, TextLength(raylibhPath) + 1);
209 if (outFileName[0] == '\0') MemoryCopy(outFileName, raylibapiPath, TextLength(raylibapiPath) + 1);
210 if (apiDefine[0] == '\0') MemoryCopy(apiDefine, rlapiPath, TextLength(rlapiPath) + 1);
211
212 int length = 0;
213 char *buffer = LoadFileText(inFileName, &length);
214
215 if (buffer == NULL)
216 {
217 printf("Could not read input file: %s\n", inFileName);
218 return 1;
219 }
220
221 // Preprocess buffer to get separate lines
222 // NOTE: LoadTextLines() also removes leading spaces/tabs
223 int lineCount = 0;
224 char **lines = LoadTextLines(buffer, length, &lineCount);
225
226 // Truncate lines (if required)
227 if (truncAfter[0] != '\0')
228 {
229 int newCount = -1;
230 for (int i = 0; i < lineCount; i++)
231 {
232 if (newCount > -1) free(lines[i]);
233 else if (TextFindIndex(lines[i], truncAfter) > -1) newCount = i;
234 }
235 if (newCount > -1) lineCount = newCount;
236 printf("Number of truncated text lines: %i\n", lineCount);
237 }
238
239 // Defines line indices
240 int *defineLines = (int *)malloc(MAX_DEFINES_TO_PARSE*sizeof(int));
241
242 // Structs line indices
243 int *structLines = (int *)malloc(MAX_STRUCTS_TO_PARSE*sizeof(int));
244
245 // Aliases line indices
246 int *aliasLines = (int *)malloc(MAX_ALIASES_TO_PARSE*sizeof(int));
247
248 // Enums line indices
249 int *enumLines = (int *)malloc(MAX_ENUMS_TO_PARSE*sizeof(int));
250
251 // Callbacks line indices
252 int *callbackLines = (int *)malloc(MAX_CALLBACKS_TO_PARSE*sizeof(int));
253
254 // Function line indices
255 int *funcLines = (int *)malloc(MAX_FUNCS_TO_PARSE*sizeof(int));
256
257 // Prepare required lines for parsing
258 //----------------------------------------------------------------------------------
259 // Read define lines
260 for (int i = 0; i < lineCount; i++)
261 {
262 int j = 0;
263 while ((lines[i][j] == ' ') || (lines[i][j] == '\t')) j++; // skip spaces and tabs in the beginning
264 // Read define line
265 if (IsTextEqual(lines[i]+j, "#define ", 8))
266 {
267 // Keep the line position in the array of lines,
268 // so, we can scan that position and following lines
269 defineLines[defineCount] = i;
270 defineCount++;
271 }
272 }
273
274 // Read struct lines
275 for (int i = 0; i < lineCount; i++)
276 {
277 // Find structs
278 // starting with "typedef struct ... {" or "typedef struct ... ; \n struct ... {"
279 // ending with "} ... ;"
280 // i.e. excluding "typedef struct rAudioBuffer rAudioBuffer;" -> Typedef and forward declaration only
281 if (IsTextEqual(lines[i], "typedef struct", 14))
282 {
283 bool validStruct = IsTextEqual(lines[i + 1], "struct", 6);
284 if (!validStruct)
285 {
286 for (int c = 0; c < MAX_LINE_LENGTH; c++)
287 {
288 char v = lines[i][c];
289 if (v == '{') validStruct = true;
290 if ((v == '{') || (v == ';') || (v == '\0')) break;
291 }
292 }
293 if (!validStruct) continue;
294 structLines[structCount] = i;
295 while (lines[i][0] != '}') i++;
296 while (lines[i][0] != '\0') i++;
297 structCount++;
298 }
299 }
300
301 // Read alias lines
302 for (int i = 0; i < lineCount; i++)
303 {
304 // Find aliases (lines with "typedef ... ...;")
305 if (IsTextEqual(lines[i], "typedef", 7))
306 {
307 int spaceCount = 0;
308 bool validAlias = false;
309
310 for (int c = 0; c < MAX_LINE_LENGTH; c++)
311 {
312 char v = lines[i][c];
313 if (v == ' ') spaceCount++;
314 if ((v == ';') && (spaceCount == 2)) validAlias = true;
315 if ((v == ';') || (v == '(') || (v == '\0')) break;
316 }
317 if (!validAlias) continue;
318 aliasLines[aliasCount] = i;
319 aliasCount++;
320 }
321 }
322
323 // Read enum lines
324 for (int i = 0; i < lineCount; i++)
325 {
326 // Read enum line
327 if (IsTextEqual(lines[i], "typedef enum {", 14) && (lines[i][TextLength(lines[i])-1] != ';')) // ignore inline enums
328 {
329 // Keep the line position in the array of lines,
330 // so, we can scan that position and following lines
331 enumLines[enumCount] = i;
332 enumCount++;
333 }
334 }
335
336 // Read callback lines
337 for (int i = 0; i < lineCount; i++)
338 {
339 // Find callbacks (lines with "typedef ... (* ... )( ... );")
340 if (IsTextEqual(lines[i], "typedef", 7))
341 {
342 bool hasBeginning = false;
343 bool hasMiddle = false;
344 bool hasEnd = false;
345
346 for (int c = 0; c < MAX_LINE_LENGTH; c++)
347 {
348 if ((lines[i][c] == '(') && (lines[i][c + 1] == '*')) hasBeginning = true;
349 if ((lines[i][c] == ')') && (lines[i][c + 1] == '(')) hasMiddle = true;
350 if ((lines[i][c] == ')') && (lines[i][c + 1] == ';')) hasEnd = true;
351 if (hasEnd) break;
352 }
353
354 if (hasBeginning && hasMiddle && hasEnd)
355 {
356 callbackLines[callbackCount] = i;
357 callbackCount++;
358 }
359 }
360 }
361
362 // Read function lines
363 for (int i = 0; i < lineCount; i++)
364 {
365 // Read function line (starting with `define`, i.e. for raylib.h "RLAPI")
366 if (IsTextEqual(lines[i], apiDefine, TextLength(apiDefine)))
367 {
368 funcLines[funcCount] = i;
369 funcCount++;
370 }
371 }
372
373 // At this point we have all raylib defines, structs, aliases, enums, callbacks, functions lines data to start parsing
374
375 UnloadFileText(buffer); // Unload text buffer
376
377 // Parsing raylib data
378 //----------------------------------------------------------------------------------
379 // Define info data
380 int defineIndex = 0;
381 defines = (DefineInfo *)calloc(MAX_DEFINES_TO_PARSE, sizeof(DefineInfo));
382
383 for (int i = 0; i < defineCount; i++)
384 {
385 char *linePtr = lines[defineLines[i]];
386 int j = 0;
387
388 while ((linePtr[j] == ' ') || (linePtr[j] == '\t')) j++; // Skip spaces and tabs in the beginning
389 j += 8; // Skip "#define "
390 while ((linePtr[j] == ' ') || (linePtr[j] == '\t')) j++; // Skip spaces and tabs after "#define "
391
392 // Extract name
393 int defineNameStart = j;
394 int openBraces = 0;
395 while (linePtr[j] != '\0')
396 {
397 if (((linePtr[j] == ' ') || (linePtr[j] == '\t')) && (openBraces == 0)) break;
398 if (linePtr[j] == '(') openBraces++;
399 if (linePtr[j] == ')') openBraces--;
400 j++;
401 }
402 int defineNameEnd = j-1;
403
404 // Skip duplicates
405 unsigned int nameLen = defineNameEnd - defineNameStart + 1;
406 bool isDuplicate = false;
407 for (int k = 0; k < defineIndex; k++)
408 {
409 if ((nameLen == TextLength(defines[k].name)) && IsTextEqual(defines[k].name, &linePtr[defineNameStart], nameLen))
410 {
411 isDuplicate = true;
412 break;
413 }
414 }
415 if (isDuplicate) continue;
416
417 MemoryCopy(defines[defineIndex].name, &linePtr[defineNameStart], nameLen);
418
419 // Determine type
420 if (linePtr[defineNameEnd] == ')') defines[defineIndex].type = MACRO;
421
422 while ((linePtr[j] == ' ') || (linePtr[j] == '\t')) j++; // Skip spaces and tabs after name
423
424 int defineValueStart = j;
425 if ((linePtr[j] == '\0') || (linePtr[j] == '/')) defines[defineIndex].type = GUARD;
426 if (linePtr[j] == '"') defines[defineIndex].type = STRING;
427 else if (linePtr[j] == '\'') defines[defineIndex].type = CHAR;
428 else if (IsTextEqual(linePtr+j, "CLITERAL(Color)", 15)) defines[defineIndex].type = COLOR;
429 else if (isdigit(linePtr[j])) // Parsing numbers
430 {
431 bool isFloat = false, isNumber = true, isHex = false;
432 while ((linePtr[j] != ' ') && (linePtr[j] != '\t') && (linePtr[j] != '\0'))
433 {
434 char ch = linePtr[j];
435 if (ch == '.') isFloat = true;
436 if (ch == 'x') isHex = true;
437 if (!(isdigit(ch) ||
438 ((ch >= 'a') && (ch <= 'f')) ||
439 ((ch >= 'A') && (ch <= 'F')) ||
440 (ch == 'x') ||
441 (ch == 'L') ||
442 (ch == '.') ||
443 (ch == '+') ||
444 (ch == '-'))) isNumber = false;
445 j++;
446 }
447 if (isNumber)
448 {
449 if (isFloat)
450 {
451 defines[defineIndex].type = (linePtr[j-1] == 'f')? FLOAT : DOUBLE;
452 }
453 else
454 {
455 defines[defineIndex].type = (linePtr[j-1] == 'L')? LONG : INT;
456 defines[defineIndex].isHex = isHex;
457 }
458 }
459 }
460
461 // Extracting value
462 while ((linePtr[j] != '\\') && (linePtr[j] != '\0') && !((linePtr[j] == '/') && (linePtr[j+1] == '/'))) j++;
463 int defineValueEnd = j-1;
464 while ((linePtr[defineValueEnd] == ' ') || (linePtr[defineValueEnd] == '\t')) defineValueEnd--; // Remove trailing spaces and tabs
465 if ((defines[defineIndex].type == LONG) || (defines[defineIndex].type == FLOAT)) defineValueEnd--; // Remove number postfix
466 int valueLen = defineValueEnd - defineValueStart + 1;
467 if (valueLen > 255) valueLen = 255;
468
469 if (valueLen > 0) MemoryCopy(defines[defineIndex].value, &linePtr[defineValueStart], valueLen);
470
471 // Extracting description
472 if ((linePtr[j] == '/') && linePtr[j + 1] == '/')
473 {
474 j += 2;
475 while (linePtr[j] == ' ') j++;
476 int commentStart = j;
477 while ((linePtr[j] != '\\') && (linePtr[j] != '\0')) j++;
478 int commentEnd = j-1;
479 int commentLen = commentEnd - commentStart + 1;
480 if (commentLen > 127) commentLen = 127;
481
482 MemoryCopy(defines[defineIndex].desc, &linePtr[commentStart], commentLen);
483 }
484
485 // Parse defines of type UNKNOWN to find calculated numbers
486 if (defines[defineIndex].type == UNKNOWN)
487 {
488 int largestType = UNKNOWN;
489 bool isMath = true;
490 char *valuePtr = defines[defineIndex].value;
491
492 for (unsigned int c = 0; c < TextLength(valuePtr); c++)
493 {
494 char ch = valuePtr[c];
495
496 // Skip operators and whitespace
497 if ((ch == '(') ||
498 (ch == ')') ||
499 (ch == '+') ||
500 (ch == '-') ||
501 (ch == '*') ||
502 (ch == '/') ||
503 (ch == ' ') ||
504 (ch == '\t')) continue;
505
506 // Read number operand
507 else if (isdigit(ch))
508 {
509 bool isNumber = true, isFloat = false;
510 while (!((ch == '(') ||
511 (ch == ')') ||
512 (ch == '*') ||
513 (ch == '/') ||
514 (ch == ' ') ||
515 (ch == '\t') ||
516 (ch == '\0')))
517 {
518 if (ch == '.') isFloat = true;
519 if (!(isdigit(ch) ||
520 ((ch >= 'a') && (ch <= 'f')) ||
521 ((ch >= 'A') && (ch <= 'F')) ||
522 (ch == 'x') ||
523 (ch == 'L') ||
524 (ch == '.') ||
525 (ch == '+') ||
526 (ch == '-')))
527 {
528 isNumber = false;
529 break;
530 }
531 c++;
532 ch = valuePtr[c];
533 }
534 if (isNumber)
535 {
536 // Found a valid number -> update largestType
537 int numberType;
538 if (isFloat) numberType = (valuePtr[c - 1] == 'f')? FLOAT_MATH : DOUBLE_MATH;
539 else numberType = (valuePtr[c - 1] == 'L')? LONG_MATH : INT_MATH;
540
541 if (numberType > largestType) largestType = numberType;
542 }
543 else
544 {
545 isMath = false;
546 break;
547 }
548 }
549 else // Read string operand
550 {
551 int operandStart = c;
552 while (!((ch == '\0') ||
553 (ch == ' ') ||
554 (ch == '(') ||
555 (ch == ')') ||
556 (ch == '+') ||
557 (ch == '-') ||
558 (ch == '*') ||
559 (ch == '/')))
560 {
561 c++;
562 ch = valuePtr[c];
563 }
564 int operandEnd = c;
565 int operandLength = operandEnd - operandStart;
566
567 // Search previous defines for operand
568 bool foundOperand = false;
569 for (int previousDefineIndex = 0; previousDefineIndex < defineIndex; previousDefineIndex++)
570 {
571 if (IsTextEqual(defines[previousDefineIndex].name, &valuePtr[operandStart], operandLength))
572 {
573 if ((defines[previousDefineIndex].type >= INT) && (defines[previousDefineIndex].type <= DOUBLE_MATH))
574 {
575 // Found operand and it's a number -> update largestType
576 if (defines[previousDefineIndex].type > largestType) largestType = defines[previousDefineIndex].type;
577 foundOperand = true;
578 }
579 break;
580 }
581 }
582 if (!foundOperand)
583 {
584 isMath = false;
585 break;
586 }
587 }
588 }
589
590 if (isMath)
591 {
592 // Define is a calculated number -> update type
593 if (largestType == INT) largestType = INT_MATH;
594 else if (largestType == LONG) largestType = LONG_MATH;
595 else if (largestType == FLOAT) largestType = FLOAT_MATH;
596 else if (largestType == DOUBLE) largestType = DOUBLE_MATH;
597 defines[defineIndex].type = largestType;
598 }
599 }
600
601 defineIndex++;
602 }
603 defineCount = defineIndex;
604 free(defineLines);
605
606 // Structs info data
607 structs = (StructInfo *)calloc(MAX_STRUCTS_TO_PARSE, sizeof(StructInfo));
608
609 for (int i = 0; i < structCount; i++)
610 {
611 char **linesPtr = &lines[structLines[i]];
612
613 // Parse struct description
614 GetDescription(linesPtr[-1], structs[i].desc);
615
616 // Get struct name: typedef struct name {
617 const int TDS_LEN = 15; // length of "typedef struct "
618 for (int c = TDS_LEN; c < 64 + TDS_LEN; c++)
619 {
620 if ((linesPtr[0][c] == '{') || (linesPtr[0][c] == ' '))
621 {
622 int nameLen = c - TDS_LEN;
623 while (linesPtr[0][TDS_LEN + nameLen - 1] == ' ') nameLen--;
624 MemoryCopy(structs[i].name, &linesPtr[0][TDS_LEN], nameLen);
625 break;
626 }
627 }
628
629 // Get struct fields and count them -> fields finish with ;
630 int l = 1;
631 while (linesPtr[l][0] != '}')
632 {
633 // WARNING: Some structs have empty spaces and comments -> OK, processed
634 if ((linesPtr[l][0] != ' ') && (linesPtr[l][0] != '\0'))
635 {
636 // Scan one field line
637 char *fieldLine = linesPtr[l];
638 int fieldEndPos = 0;
639 while (fieldLine[fieldEndPos] != ';') fieldEndPos++;
640
641 if ((fieldLine[0] != '/') && !IsTextEqual(fieldLine, "struct", 6)) // Field line is not a comment and not a struct declaration
642 {
643 //printf("Struct field: %s_\n", fieldLine); // OK!
644
645 // Get struct field type and name
646 GetDataTypeAndName(fieldLine, fieldEndPos, structs[i].fieldType[structs[i].fieldCount], structs[i].fieldName[structs[i].fieldCount]);
647
648 // Get the field description
649 GetDescription(&fieldLine[fieldEndPos], structs[i].fieldDesc[structs[i].fieldCount]);
650
651 structs[i].fieldCount++;
652
653 // Split field names containing multiple fields (like Matrix)
654 int additionalFields = 0;
655 int originalIndex = structs[i].fieldCount - 1;
656 for (unsigned int c = 0; c < TextLength(structs[i].fieldName[originalIndex]); c++)
657 {
658 if (structs[i].fieldName[originalIndex][c] == ',') additionalFields++;
659 }
660
661 if (additionalFields > 0)
662 {
663 int originalLength = -1;
664 int lastStart;
665 for (unsigned int c = 0; c < TextLength(structs[i].fieldName[originalIndex]) + 1; c++)
666 {
667 char v = structs[i].fieldName[originalIndex][c];
668 bool isEndOfString = (v == '\0');
669 if ((v == ',') || isEndOfString)
670 {
671 if (originalLength == -1)
672 {
673 // Save length of original field name
674 // Don't truncate yet, still needed for copying
675 originalLength = c;
676 }
677 else
678 {
679 // Copy field data from original field
680 int nameLength = c - lastStart;
681 MemoryCopy(structs[i].fieldName[structs[i].fieldCount], &structs[i].fieldName[originalIndex][lastStart], nameLength);
682 MemoryCopy(structs[i].fieldType[structs[i].fieldCount], &structs[i].fieldType[originalIndex][0], TextLength(structs[i].fieldType[originalIndex]));
683 MemoryCopy(structs[i].fieldDesc[structs[i].fieldCount], &structs[i].fieldDesc[originalIndex][0], TextLength(structs[i].fieldDesc[originalIndex]));
684 structs[i].fieldCount++;
685 }
686 if (!isEndOfString)
687 {
688 // Skip comma and spaces
689 c++;
690 while (structs[i].fieldName[originalIndex][c] == ' ') c++;
691
692 // Save position for next field
693 lastStart = c;
694 }
695 }
696 }
697 // Set length of original field to truncate the first field name
698 structs[i].fieldName[originalIndex][originalLength] = '\0';
699 }
700
701 // Split field types containing multiple fields (like MemNode)
702 additionalFields = 0;
703 originalIndex = structs[i].fieldCount - 1;
704 for (unsigned int c = 0; c < TextLength(structs[i].fieldType[originalIndex]); c++)
705 {
706 if (structs[i].fieldType[originalIndex][c] == ',') additionalFields++;
707 }
708
709 if (additionalFields > 0)
710 {
711 // Copy original name to last additional field
712 structs[i].fieldCount += additionalFields;
713 MemoryCopy(structs[i].fieldName[originalIndex + additionalFields], &structs[i].fieldName[originalIndex][0], TextLength(structs[i].fieldName[originalIndex]));
714
715 // Copy names from type to additional fields
716 int fieldsRemaining = additionalFields;
717 int nameStart = -1;
718 int nameEnd = -1;
719 for (int k = TextLength(structs[i].fieldType[originalIndex]); k > 0; k--)
720 {
721 char v = structs[i].fieldType[originalIndex][k];
722 if ((v == '*') || (v == ' ') || (v == ','))
723 {
724 if (nameEnd != -1)
725 {
726 // Don't copy to last additional field
727 if (fieldsRemaining != additionalFields)
728 {
729 nameStart = k + 1;
730 MemoryCopy(structs[i].fieldName[originalIndex + fieldsRemaining], &structs[i].fieldType[originalIndex][nameStart], nameEnd - nameStart + 1);
731 }
732 nameEnd = -1;
733 fieldsRemaining--;
734 }
735 }
736 else if (nameEnd == -1) nameEnd = k;
737 }
738
739 // Truncate original field type
740 int fieldTypeLength = nameStart;
741 structs[i].fieldType[originalIndex][fieldTypeLength] = '\0';
742
743 // Set field type and description of additional fields
744 for (int j = 1; j <= additionalFields; j++)
745 {
746 MemoryCopy(structs[i].fieldType[originalIndex + j], &structs[i].fieldType[originalIndex][0], fieldTypeLength);
747 MemoryCopy(structs[i].fieldDesc[originalIndex + j], &structs[i].fieldDesc[originalIndex][0], TextLength(structs[i].fieldDesc[originalIndex]));
748 }
749 }
750 }
751 }
752
753 l++;
754 }
755
756 // Move array sizes from name to type
757 for (int j = 0; j < structs[i].fieldCount; j++)
758 {
759 MoveArraySize(structs[i].fieldName[j], structs[i].fieldType[j]);
760 }
761 }
762 free(structLines);
763
764 // Alias info data
765 aliases = (AliasInfo *)calloc(MAX_ALIASES_TO_PARSE, sizeof(AliasInfo));
766
767 for (int i = 0; i < aliasCount; i++)
768 {
769 // Description from previous line
770 GetDescription(lines[aliasLines[i] - 1], aliases[i].desc);
771
772 char *linePtr = lines[aliasLines[i]];
773
774 // Skip "typedef "
775 int c = 8;
776
777 // Type
778 int typeStart = c;
779 while(linePtr[c] != ' ') c++;
780 int typeLen = c - typeStart;
781 MemoryCopy(aliases[i].type, &linePtr[typeStart], typeLen);
782
783 // Skip space
784 c++;
785
786 // Name
787 int nameStart = c;
788 while(linePtr[c] != ';') c++;
789 int nameLen = c - nameStart;
790 MemoryCopy(aliases[i].name, &linePtr[nameStart], nameLen);
791
792 // Description
793 GetDescription(&linePtr[c], aliases[i].desc);
794 }
795 free(aliasLines);
796
797 // Enum info data
798 enums = (EnumInfo *)calloc(MAX_ENUMS_TO_PARSE, sizeof(EnumInfo));
799
800 for (int i = 0; i < enumCount; i++)
801 {
802 // Parse enum description
803 // NOTE: This is not necessarily from the line immediately before,
804 // some of the enums have extra lines between the "description"
805 // and the typedef enum
806 for (int j = enumLines[i] - 1; j > 0; j--)
807 {
808 char *linePtr = lines[j];
809 if ((linePtr[0] != '/') || (linePtr[2] != ' '))
810 {
811 GetDescription(&lines[j + 1][0], enums[i].desc);
812 break;
813 }
814 }
815
816 for (int j = 1; j < MAX_ENUM_VALUES*2; j++) // Maximum number of lines following enum first line
817 {
818 char *linePtr = lines[enumLines[i] + j];
819
820 if ((linePtr[0] >= 'A') && (linePtr[0] <= 'Z'))
821 {
822 // Parse enum value line, possible options:
823 //ENUM_VALUE_NAME,
824 //ENUM_VALUE_NAME
825 //ENUM_VALUE_NAME = 99
826 //ENUM_VALUE_NAME = 99,
827 //ENUM_VALUE_NAME = 0x00000040, // Value description
828
829 // We start reading the value name
830 int c = 0;
831 while ((linePtr[c] != ',') &&
832 (linePtr[c] != ' ') &&
833 (linePtr[c] != '=') &&
834 (linePtr[c] != '\0'))
835 {
836 enums[i].valueName[enums[i].valueCount][c] = linePtr[c];
837 c++;
838 }
839
840 // After the name we can have:
841 // '=' -> value is provided
842 // ',' -> value is equal to previous + 1, there could be a description if not '\0'
843 // ' ' -> value is equal to previous + 1, there could be a description if not '\0'
844 // '\0' -> value is equal to previous + 1
845
846 // Let's start checking if the line is not finished
847 if ((linePtr[c] != ',') && (linePtr[c] != '\0'))
848 {
849 // Two options:
850 // '=' -> value is provided
851 // ' ' -> value is equal to previous + 1, there could be a description if not '\0'
852 bool foundValue = false;
853 while ((linePtr[c] != '\0') && (linePtr[c] != '/'))
854 {
855 if (linePtr[c] == '=')
856 {
857 foundValue = true;
858 break;
859 }
860 c++;
861 }
862
863 if (foundValue)
864 {
865 if (linePtr[c + 1] == ' ') c += 2;
866 else c++;
867
868 // Parse integer value
869 int n = 0;
870 char integer[16] = { 0 };
871
872 while ((linePtr[c] != ',') && (linePtr[c] != ' ') && (linePtr[c] != '\0'))
873 {
874 integer[n] = linePtr[c];
875 c++; n++;
876 }
877
878 if (integer[1] == 'x') enums[i].valueInteger[enums[i].valueCount] = (int)strtol(integer, NULL, 16);
879 else enums[i].valueInteger[enums[i].valueCount] = atoi(integer);
880 }
881 else enums[i].valueInteger[enums[i].valueCount] = (enums[i].valueInteger[enums[i].valueCount - 1] + 1);
882 }
883 else enums[i].valueInteger[enums[i].valueCount] = (enums[i].valueInteger[enums[i].valueCount - 1] + 1);
884
885 // Parse value description
886 GetDescription(&linePtr[c], enums[i].valueDesc[enums[i].valueCount]);
887
888 enums[i].valueCount++;
889 }
890 else if (linePtr[0] == '}')
891 {
892 // Get enum name from typedef
893 int c = 0;
894 while (linePtr[2 + c] != ';')
895 {
896 enums[i].name[c] = linePtr[2 + c];
897 c++;
898 }
899
900 break; // Enum ended, break for() loop
901 }
902 }
903 }
904 free(enumLines);
905
906 // Callback info data
907 callbacks = (FunctionInfo *)calloc(MAX_CALLBACKS_TO_PARSE, sizeof(FunctionInfo));
908
909 for (int i = 0; i < callbackCount; i++)
910 {
911 char *linePtr = lines[callbackLines[i]];
912
913 // Skip "typedef "
914 unsigned int c = 8;
915
916 // Return type
917 int retTypeStart = c;
918 while(linePtr[c] != '(') c++;
919 int retTypeLen = c - retTypeStart;
920 while(linePtr[retTypeStart + retTypeLen - 1] == ' ') retTypeLen--;
921 MemoryCopy(callbacks[i].retType, &linePtr[retTypeStart], retTypeLen);
922
923 // Skip "(*"
924 c += 2;
925
926 // Name
927 int nameStart = c;
928 while(linePtr[c] != ')') c++;
929 int nameLen = c - nameStart;
930 MemoryCopy(callbacks[i].name, &linePtr[nameStart], nameLen);
931
932 // Skip ")("
933 c += 2;
934
935 // Params
936 int paramStart = c;
937 for (; c < MAX_LINE_LENGTH; c++)
938 {
939 if ((linePtr[c] == ',') || (linePtr[c] == ')'))
940 {
941 // Get parameter type + name, extract info
942 int paramLen = c - paramStart;
943 GetDataTypeAndName(&linePtr[paramStart], paramLen, callbacks[i].paramType[callbacks[i].paramCount], callbacks[i].paramName[callbacks[i].paramCount]);
944 callbacks[i].paramCount++;
945 paramStart = c + 1;
946 while(linePtr[paramStart] == ' ') paramStart++;
947 }
948 if (linePtr[c] == ')') break;
949 }
950
951 // Description
952 GetDescription(&linePtr[c], callbacks[i].desc);
953
954 // Move array sizes from name to type
955 for (int j = 0; j < callbacks[i].paramCount; j++)
956 {
957 MoveArraySize(callbacks[i].paramName[j], callbacks[i].paramType[j]);
958 }
959 }
960 free(callbackLines);
961
962 // Functions info data
963 funcs = (FunctionInfo *)calloc(MAX_FUNCS_TO_PARSE, sizeof(FunctionInfo));
964
965 for (int i = 0; i < funcCount; i++)
966 {
967 char *linePtr = lines[funcLines[i]];
968
969 int funcParamsStart = 0;
970 int funcEnd = 0;
971
972 // Get return type and function name from func line
973 for (int c = 0; (c < MAX_LINE_LENGTH) && (linePtr[c] != '\n'); c++)
974 {
975 if (linePtr[c] == '(') // Starts function parameters
976 {
977 funcParamsStart = c + 1;
978
979 // At this point we have function return type and function name
980 char funcRetTypeName[128] = { 0 };
981 int dc = TextLength(apiDefine) + 1;
982 int funcRetTypeNameLen = c - dc; // Substract `define` ("RLAPI " for raylib.h)
983 MemoryCopy(funcRetTypeName, &linePtr[dc], funcRetTypeNameLen);
984
985 GetDataTypeAndName(funcRetTypeName, funcRetTypeNameLen, funcs[i].retType, funcs[i].name);
986 break;
987 }
988 }
989
990 // Get parameters from func line
991 for (int c = funcParamsStart; c < MAX_LINE_LENGTH; c++)
992 {
993 if (linePtr[c] == ',') // Starts function parameters
994 {
995 // Get parameter type + name, extract info
996 char funcParamTypeName[128] = { 0 };
997 int funcParamTypeNameLen = c - funcParamsStart;
998 MemoryCopy(funcParamTypeName, &linePtr[funcParamsStart], funcParamTypeNameLen);
999
1000 GetDataTypeAndName(funcParamTypeName, funcParamTypeNameLen, funcs[i].paramType[funcs[i].paramCount], funcs[i].paramName[funcs[i].paramCount]);
1001
1002 funcParamsStart = c + 1;
1003 if (linePtr[c + 1] == ' ') funcParamsStart += 1;
1004 funcs[i].paramCount++; // Move to next parameter
1005 }
1006 else if (linePtr[c] == ')')
1007 {
1008 funcEnd = c + 2;
1009
1010 // Check if there are no parameters
1011 if ((funcEnd - funcParamsStart == 2) ||
1012 ((linePtr[c - 4] == 'v') &&
1013 (linePtr[c - 3] == 'o') &&
1014 (linePtr[c - 2] == 'i') &&
1015 (linePtr[c - 1] == 'd')))
1016 {
1017 break;
1018 }
1019
1020 // Get parameter type + name, extract info
1021 char funcParamTypeName[128] = { 0 };
1022 int funcParamTypeNameLen = c - funcParamsStart;
1023 MemoryCopy(funcParamTypeName, &linePtr[funcParamsStart], funcParamTypeNameLen);
1024
1025 GetDataTypeAndName(funcParamTypeName, funcParamTypeNameLen, funcs[i].paramType[funcs[i].paramCount], funcs[i].paramName[funcs[i].paramCount]);
1026
1027 funcs[i].paramCount++; // Move to next parameter
1028 break;
1029 }
1030 }
1031
1032 // Get function description
1033 GetDescription(&linePtr[funcEnd], funcs[i].desc);
1034
1035 // Move array sizes from name to type
1036 for (int j = 0; j < funcs[i].paramCount; j++)
1037 {
1038 MoveArraySize(funcs[i].paramName[j], funcs[i].paramType[j]);
1039 }
1040 }
1041 free(funcLines);
1042
1043 UnloadTextLines(lines, lineCount);
1044
1045 // At this point, all raylib data has been parsed!
1046 //----------------------------------------------------------------------------------
1047 // defines[] -> We have all the defines decomposed into pieces for further analysis
1048 // structs[] -> We have all the structs decomposed into pieces for further analysis
1049 // aliases[] -> We have all the aliases decomposed into pieces for further analysis
1050 // enums[] -> We have all the enums decomposed into pieces for further analysis
1051 // callbacks[] -> We have all the callbacks decomposed into pieces for further analysis
1052 // funcs[] -> We have all the functions decomposed into pieces for further analysis
1053
1054 // Export data as required
1055 // NOTE: We are exporting data in several common formats (JSON, XML, LUA...) for convenience
1056 // but this data can be directly used to create bindings for other languages modifying this
1057 // small parser for every binding need, there is no need to use (and re-parse) the
1058 // generated files... despite it seems that's the usual approach...
1059 printf("\nInput file: %s", inFileName);
1060 printf("\nOutput file: %s", outFileName);
1061 if (outputFormat == DEFAULT) printf("\nOutput format: DEFAULT\n\n");
1062 else if (outputFormat == JSON) printf("\nOutput format: JSON\n\n");
1063 else if (outputFormat == XML) printf("\nOutput format: XML\n\n");
1064 else if (outputFormat == LUA) printf("\nOutput format: LUA\n\n");
1065 else if (outputFormat == CODE) printf("\nOutput format: CODE\n\n");
1066
1067 ExportParsedData(outFileName, outputFormat);
1068
1069 free(defines);
1070 free(structs);
1071 free(aliases);
1072 free(enums);
1073 free(callbacks);
1074 free(funcs);
1075 }
1076
1077 //----------------------------------------------------------------------------------
1078 // Module Functions Definition
1079 //----------------------------------------------------------------------------------
1080 // Show command line usage info
1081 static void ShowCommandLineInfo(void)
1082 {
1083 printf("\n//////////////////////////////////////////////////////////////////////////////////\n");
1084 printf("// //\n");
1085 printf("// rlparser - raylib header API parser //\n");
1086 printf("// //\n");
1087 printf("// more info and bugs-report: github.com/raysan5/raylib/tools/rlparser //\n");
1088 printf("// //\n");
1089 printf("// Copyright (c) 2021-2026 Ramon Santamaria (@raysan5) //\n");
1090 printf("// //\n");
1091 printf("//////////////////////////////////////////////////////////////////////////////////\n\n");
1092
1093 printf("USAGE:\n\n");
1094 printf(" > rlparser [--help] [--input <filename.h>] [--output <filename.ext>] [--format <type>]\n");
1095
1096 printf("\nOPTIONS:\n\n");
1097 printf(" -h, --help : Show tool version and command line usage help\n\n");
1098 printf(" -i, --input <filename.h> : Define input header file to parse.\n");
1099 printf(" NOTE: If not specified, defaults to: raylib.h\n\n");
1100 printf(" -o, --output <filename.ext> : Define output file and format.\n");
1101 printf(" Supported extensions: .txt, .json, .xml, .lua, .h\n");
1102 printf(" NOTE: If not specified, defaults to: raylib_api.txt\n\n");
1103 printf(" -f, --format <type> : Define output format for parser data.\n");
1104 printf(" Supported types: DEFAULT, JSON, XML, LUA, CODE\n\n");
1105 printf(" -d, --define <DEF> : Define functions specifiers (i.e. RLAPI for raylib.h, RMAPI for raymath.h, etc.)\n");
1106 printf(" NOTE: If no specifier defined, defaults to: RLAPI\n\n");
1107 printf(" -t, --truncate <after> : Define string to truncate input after (i.e. \"RLGL IMPLEMENTATION\" for rlgl.h)\n");
1108 printf(" NOTE: If not specified, the full input file is parsed.\n\n");
1109
1110 printf("\nEXAMPLES:\n\n");
1111 printf(" > rlparser --input raylib.h --output api.json\n");
1112 printf(" Process <raylib.h> to generate <api.json>\n\n");
1113 printf(" > rlparser --output raylib_data.info --format XML\n");
1114 printf(" Process <raylib.h> to generate <raylib_data.info> as XML text data\n\n");
1115 printf(" > rlparser --input raymath.h --output raymath_data.info --format XML --define RMAPI\n");
1116 printf(" Process <raymath.h> to generate <raymath_data.info> as XML text data\n\n");
1117 }
1118
1119 // Process command line arguments
1120 static void ProcessCommandLine(int argc, char *argv[])
1121 {
1122 for (int i = 1; i < argc; i++)
1123 {
1124 if (IsTextEqual(argv[i], "-h", 2) || IsTextEqual(argv[i], "--help", 6))
1125 {
1126 // Show info
1127 ShowCommandLineInfo();
1128 exit(0);
1129 }
1130 else if (IsTextEqual(argv[i], "-i", 2) || IsTextEqual(argv[i], "--input", 7))
1131 {
1132 // Check for valid argument and valid file extension
1133 if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
1134 {
1135 MemoryCopy(inFileName, argv[i + 1], TextLength(argv[i + 1])); // Read input filename
1136 i++;
1137 }
1138 else printf("WARNING: No input file provided\n");
1139 }
1140 else if (IsTextEqual(argv[i], "-o", 2) || IsTextEqual(argv[i], "--output", 8))
1141 {
1142 if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
1143 {
1144 MemoryCopy(outFileName, argv[i + 1], TextLength(argv[i + 1])); // Read output filename
1145 i++;
1146 }
1147 else printf("WARNING: No output file provided\n");
1148 }
1149 else if (IsTextEqual(argv[i], "-f", 2) || IsTextEqual(argv[i], "--format", 8))
1150 {
1151 if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
1152 {
1153 if (IsTextEqual(argv[i + 1], "DEFAULT\0", 8)) outputFormat = DEFAULT;
1154 else if (IsTextEqual(argv[i + 1], "JSON\0", 5)) outputFormat = JSON;
1155 else if (IsTextEqual(argv[i + 1], "XML\0", 4)) outputFormat = XML;
1156 else if (IsTextEqual(argv[i + 1], "LUA\0", 4)) outputFormat = LUA;
1157 else if (IsTextEqual(argv[i + 1], "CODE\0", 4)) outputFormat = CODE;
1158 }
1159 else printf("WARNING: No format parameters provided\n");
1160 }
1161 else if (IsTextEqual(argv[i], "-d", 2) || IsTextEqual(argv[i], "--define", 8))
1162 {
1163 if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
1164 {
1165 MemoryCopy(apiDefine, argv[i + 1], TextLength(argv[i + 1])); // Read functions define
1166 apiDefine[TextLength(argv[i + 1])] = '\0';
1167 i++;
1168 }
1169 else printf("WARNING: No define key provided\n");
1170 }
1171 else if (IsTextEqual(argv[i], "-t", 2) || IsTextEqual(argv[i], "--truncate", 10))
1172 {
1173 if (((i + 1) < argc) && (argv[i + 1][0] != '-'))
1174 {
1175 MemoryCopy(truncAfter, argv[i + 1], TextLength(argv[i + 1])); // Read truncate marker
1176 truncAfter[TextLength(argv[i + 1])] = '\0';
1177 i++;
1178 }
1179 }
1180 }
1181 }
1182
1183 // Load text data from file, returns a '\0' terminated string
1184 // NOTE: text chars array should be freed manually
1185 static char *LoadFileText(const char *fileName, int *length)
1186 {
1187 char *text = NULL;
1188
1189 if (fileName != NULL)
1190 {
1191 FILE *file = fopen(fileName, "rt");
1192
1193 if (file != NULL)
1194 {
1195 // WARNING: When reading a file as 'text' file,
1196 // text mode causes carriage return-linefeed translation...
1197 // ...but using fseek() should return correct byte-offset
1198 fseek(file, 0, SEEK_END);
1199 int size = ftell(file);
1200 fseek(file, 0, SEEK_SET);
1201
1202 if (size > 0)
1203 {
1204 text = (char *)calloc((size + 1), sizeof(char));
1205 unsigned int count = (unsigned int)fread(text, sizeof(char), size, file);
1206
1207 // WARNING: \r\n is converted to \n on reading, so,
1208 // read bytes count gets reduced by the number of lines
1209 if (count < (unsigned int)size)
1210 {
1211 text = realloc(text, count + 1);
1212 *length = count;
1213 }
1214 else *length = size;
1215
1216 // Zero-terminate the string
1217 text[count] = '\0';
1218 }
1219
1220 fclose(file);
1221 }
1222 }
1223
1224 return text;
1225 }
1226
1227 // Unload text data
1228 static void UnloadFileText(char *text)
1229 {
1230 free(text);
1231 }
1232
1233 // Get all lines from a text buffer (expecting lines ending with '\n')
1234 static char **LoadTextLines(const char *buffer, int length, int *lineCount)
1235 {
1236 // Get the number of lines in the text
1237 int count = 0;
1238 for (int i = 0; i < length; i++) if (buffer[i] == '\n') count++;
1239
1240 printf("Number of text lines in buffer: %i\n", count);
1241
1242 // Allocate as many pointers as lines
1243 char **lines = (char **)malloc(count*sizeof(char **));
1244
1245 char *bufferPtr = (char *)buffer;
1246
1247 for (int i = 0; (i < count) || (bufferPtr[0] != '\0'); i++)
1248 {
1249 lines[i] = (char *)calloc(MAX_LINE_LENGTH, sizeof(char)); // MAX_LINE_LENGTH=1024
1250
1251 // Remove line leading spaces
1252 // Find last index of space/tab character
1253 int index = 0;
1254 while ((bufferPtr[index] == ' ') || (bufferPtr[index] == '\t')) index++;
1255
1256 int j = 0;
1257 while (bufferPtr[index + j] != '\n' && bufferPtr[index + j] != '\0')
1258 {
1259 lines[i][j] = bufferPtr[index + j];
1260 j++;
1261 }
1262
1263 bufferPtr += (index + j + 1);
1264 }
1265
1266 *lineCount = count;
1267 return lines;
1268 }
1269
1270 // Unload text lines data
1271 static void UnloadTextLines(char **lines, int lineCount)
1272 {
1273 for (int i = 0; i < lineCount; i++) free(lines[i]);
1274 free(lines);
1275 }
1276
1277 // Get data type and name from a string containing both
1278 // NOTE: Useful to parse function parameters and struct fields
1279 static void GetDataTypeAndName(const char *typeName, int typeNameLen, char *type, char *name)
1280 {
1281 for (int k = typeNameLen; k > 0; k--)
1282 {
1283 if ((typeName[k] == ' ') && (typeName[k - 1] != ','))
1284 {
1285 // Function name starts at this point (and ret type finishes at this point)
1286 MemoryCopy(type, typeName, k);
1287 MemoryCopy(name, typeName + k + 1, typeNameLen - k - 1);
1288 break;
1289 }
1290 else if (typeName[k] == '*')
1291 {
1292 MemoryCopy(type, typeName, k + 1);
1293 MemoryCopy(name, typeName + k + 1, typeNameLen - k - 1);
1294 break;
1295 }
1296 else if ((typeName[k] == '.') && (typeNameLen == 3)) // Handle varargs ...);
1297 {
1298 const char *varargsDots = "...";
1299 const char *varargsArg = "args";
1300 MemoryCopy(type, varargsDots, TextLength(varargsDots));
1301 MemoryCopy(name, varargsArg, TextLength(varargsArg));
1302 break;
1303 }
1304 }
1305 }
1306
1307 // Get comment from a line, do nothing if no comment in line
1308 static void GetDescription(const char *line, char *description)
1309 {
1310 int c = 0;
1311 int descStart = -1;
1312 int lastSlash = -2;
1313 bool isValid = false;
1314 while (line[c] != '\0')
1315 {
1316 if (isValid && (descStart == -1) && (line[c] != ' ')) descStart = c;
1317 else if (line[c] == '/')
1318 {
1319 if (lastSlash == c - 1) isValid = true;
1320 lastSlash = c;
1321 }
1322 c++;
1323 }
1324 if (descStart != -1) MemoryCopy(description, &line[descStart], c - descStart);
1325 }
1326
1327 // Move array size from name to type
1328 static void MoveArraySize(char *name, char *type)
1329 {
1330 int nameLength = TextLength(name);
1331 if (name[nameLength - 1] == ']')
1332 {
1333 for (int k = nameLength; k > 0; k--)
1334 {
1335 if (name[k] == '[')
1336 {
1337 int sizeLength = nameLength - k;
1338 MemoryCopy(&type[TextLength(type)], &name[k], sizeLength);
1339 name[k] = '\0';
1340 }
1341 }
1342 }
1343 }
1344
1345 // Get text length in bytes, check for \0 character
1346 static unsigned int TextLength(const char *text)
1347 {
1348 unsigned int length = 0;
1349
1350 if (text != NULL) while (*text++) length++;
1351
1352 return length;
1353 }
1354
1355 // Compare two text strings, requires number of characters to compare
1356 static bool IsTextEqual(const char *text1, const char *text2, unsigned int count)
1357 {
1358 bool result = true;
1359
1360 for (unsigned int i = 0; i < count; i++)
1361 {
1362 if (text1[i] != text2[i])
1363 {
1364 result = false;
1365 break;
1366 }
1367 }
1368
1369 return result;
1370 }
1371
1372 // Find first text occurrence within a string
1373 int TextFindIndex(const char *text, const char *find)
1374 {
1375 int textLen = TextLength(text);
1376 int findLen = TextLength(find);
1377
1378 for (int i = 0; i <= textLen - findLen; i++)
1379 {
1380 if (IsTextEqual(&text[i], find, findLen)) return i;
1381 }
1382
1383 return -1;
1384 }
1385
1386 // Custom memcpy() to avoid <string.h>
1387 static void MemoryCopy(void *dest, const void *src, unsigned int count)
1388 {
1389 char *srcPtr = (char *)src;
1390 char *destPtr = (char *)dest;
1391
1392 for (unsigned int i = 0; i < count; i++) destPtr[i] = srcPtr[i];
1393 }
1394
1395 // Escape backslashes in a string, writing the escaped string into a static buffer
1396 static char *EscapeBackslashes(char *text)
1397 {
1398 static char buffer[256] = { 0 };
1399
1400 int count = 0;
1401
1402 for (int i = 0; (text[i] != '\0') && (i < 255); i++, count++)
1403 {
1404 buffer[count] = text[i];
1405
1406 if (text[i] == '\\')
1407 {
1408 buffer[count + 1] = '\\';
1409 count++;
1410 }
1411 }
1412
1413 buffer[count] = '\0';
1414
1415 return buffer;
1416 }
1417
1418 // Get string of define type
1419 static const char *StrDefineType(DefineType type)
1420 {
1421 switch (type)
1422 {
1423 case UNKNOWN: return "UNKNOWN";
1424 case GUARD: return "GUARD";
1425 case MACRO: return "MACRO";
1426 case INT: return "INT";
1427 case INT_MATH: return "INT_MATH";
1428 case LONG: return "LONG";
1429 case LONG_MATH: return "LONG_MATH";
1430 case FLOAT: return "FLOAT";
1431 case FLOAT_MATH: return "FLOAT_MATH";
1432 case DOUBLE: return "DOUBLE";
1433 case DOUBLE_MATH: return "DOUBLE_MATH";
1434 case CHAR: return "CHAR";
1435 case STRING: return "STRING";
1436 case COLOR: return "COLOR";
1437 }
1438 return "";
1439 }
1440
1441 /*
1442 // Replace text string
1443 // REQUIRES: strlen(), strstr(), strncpy(), strcpy() -> TODO: Replace by custom implementations!
1444 // WARNING: Returned buffer must be freed by the user (if return != NULL)
1445 static char *TextReplace(char *text, const char *replace, const char *by)
1446 {
1447 // Sanity checks and initialization
1448 if (!text || !replace || !by) return NULL;
1449
1450 char *result;
1451
1452 char *insertPoint; // Next insert point
1453 char *temp; // Temp pointer
1454 int replaceLen; // Replace string length of (the string to remove)
1455 int byLen; // Replacement length (the string to replace replace by)
1456 int lastReplacePos; // Distance between replace and end of last replace
1457 int count; // Number of replacements
1458
1459 replaceLen = strlen(replace);
1460 if (replaceLen == 0) return NULL; // Empty replace causes infinite loop during count
1461
1462 byLen = strlen(by);
1463
1464 // Count the number of replacements needed
1465 insertPoint = text;
1466 for (count = 0; (temp = strstr(insertPoint, replace)); count++) insertPoint = temp + replaceLen;
1467
1468 // Allocate returning string and point temp to it
1469 temp = result = (char *)malloc(strlen(text) + (byLen - replaceLen)*count + 1);
1470
1471 if (!result) return NULL; // Memory could not be allocated
1472
1473 // First time through the loop, all the variable are set correctly from here on,
1474 // - 'temp' points to the end of the result string
1475 // - 'insertPoint' points to the next occurrence of replace in text
1476 // - 'text' points to the remainder of text after "end of replace"
1477 while (count--)
1478 {
1479 insertPoint = strstr(text, replace);
1480 lastReplacePos = (int)(insertPoint - text);
1481 temp = strncpy(temp, text, lastReplacePos) + lastReplacePos;
1482 temp = strcpy(temp, by) + byLen;
1483 text += lastReplacePos + replaceLen; // Move to next "end of replace"
1484 }
1485
1486 // Copy remaind text part after replacement to result (pointed by moving temp)
1487 strcpy(temp, text);
1488
1489 return result;
1490 }
1491 */
1492
1493 // Export parsed data in desired format
1494 static void ExportParsedData(const char *fileName, int format)
1495 {
1496 FILE *outFile = fopen(fileName, "wt");
1497
1498 switch (format)
1499 {
1500 case DEFAULT:
1501 {
1502 // Print defines info
1503 fprintf(outFile, "\nDefines found: %i\n\n", defineCount);
1504 for (int i = 0; i < defineCount; i++)
1505 {
1506 fprintf(outFile, "Define %03i: %s\n", i + 1, defines[i].name);
1507 fprintf(outFile, " Name: %s\n", defines[i].name);
1508 fprintf(outFile, " Type: %s\n", StrDefineType(defines[i].type));
1509 fprintf(outFile, " Value: %s\n", defines[i].value);
1510 fprintf(outFile, " Description: %s\n", defines[i].desc);
1511 }
1512
1513 // Print structs info
1514 fprintf(outFile, "\nStructures found: %i\n\n", structCount);
1515 for (int i = 0; i < structCount; i++)
1516 {
1517 fprintf(outFile, "Struct %02i: %s (%i fields)\n", i + 1, structs[i].name, structs[i].fieldCount);
1518 fprintf(outFile, " Name: %s\n", structs[i].name);
1519 fprintf(outFile, " Description: %s\n", structs[i].desc);
1520 for (int f = 0; f < structs[i].fieldCount; f++)
1521 {
1522 fprintf(outFile, " Field[%i]: %s %s ", f + 1, structs[i].fieldType[f], structs[i].fieldName[f]);
1523 if (structs[i].fieldDesc[f][0]) fprintf(outFile, "// %s\n", structs[i].fieldDesc[f]);
1524 else fprintf(outFile, "\n");
1525 }
1526 }
1527
1528 // Print aliases info
1529 fprintf(outFile, "\nAliases found: %i\n\n", aliasCount);
1530 for (int i = 0; i < aliasCount; i++)
1531 {
1532 fprintf(outFile, "Alias %03i: %s\n", i + 1, aliases[i].name);
1533 fprintf(outFile, " Type: %s\n", aliases[i].type);
1534 fprintf(outFile, " Name: %s\n", aliases[i].name);
1535 fprintf(outFile, " Description: %s\n", aliases[i].desc);
1536 }
1537
1538 // Print enums info
1539 fprintf(outFile, "\nEnums found: %i\n\n", enumCount);
1540 for (int i = 0; i < enumCount; i++)
1541 {
1542 fprintf(outFile, "Enum %02i: %s (%i values)\n", i + 1, enums[i].name, enums[i].valueCount);
1543 fprintf(outFile, " Name: %s\n", enums[i].name);
1544 fprintf(outFile, " Description: %s\n", enums[i].desc);
1545 for (int e = 0; e < enums[i].valueCount; e++) fprintf(outFile, " Value[%s]: %i\n", enums[i].valueName[e], enums[i].valueInteger[e]);
1546 }
1547
1548 // Print callbacks info
1549 fprintf(outFile, "\nCallbacks found: %i\n\n", callbackCount);
1550 for (int i = 0; i < callbackCount; i++)
1551 {
1552 fprintf(outFile, "Callback %03i: %s() (%i input parameters)\n", i + 1, callbacks[i].name, callbacks[i].paramCount);
1553 fprintf(outFile, " Name: %s\n", callbacks[i].name);
1554 fprintf(outFile, " Return type: %s\n", callbacks[i].retType);
1555 fprintf(outFile, " Description: %s\n", callbacks[i].desc);
1556 for (int p = 0; p < callbacks[i].paramCount; p++) fprintf(outFile, " Param[%i]: %s (type: %s)\n", p + 1, callbacks[i].paramName[p], callbacks[i].paramType[p]);
1557 if (callbacks[i].paramCount == 0) fprintf(outFile, " No input parameters\n");
1558 }
1559
1560 // Print functions info
1561 fprintf(outFile, "\nFunctions found: %i\n\n", funcCount);
1562 for (int i = 0; i < funcCount; i++)
1563 {
1564 fprintf(outFile, "Function %03i: %s() (%i input parameters)\n", i + 1, funcs[i].name, funcs[i].paramCount);
1565 fprintf(outFile, " Name: %s\n", funcs[i].name);
1566 fprintf(outFile, " Return type: %s\n", funcs[i].retType);
1567 fprintf(outFile, " Description: %s\n", funcs[i].desc);
1568 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]);
1569 if (funcs[i].paramCount == 0) fprintf(outFile, " No input parameters\n");
1570 }
1571 } break;
1572 case JSON:
1573 {
1574 fprintf(outFile, "{\n");
1575
1576 // Print defines info
1577 fprintf(outFile, " \"defines\": [\n");
1578 for (int i = 0; i < defineCount; i++)
1579 {
1580 fprintf(outFile, " {\n");
1581 fprintf(outFile, " \"name\": \"%s\",\n", defines[i].name);
1582 fprintf(outFile, " \"type\": \"%s\",\n", StrDefineType(defines[i].type));
1583 if (defines[i].isHex) // INT or LONG
1584 {
1585 fprintf(outFile, " \"value\": %ld,\n", strtol(defines[i].value, NULL, 16));
1586 }
1587 else if ((defines[i].type == INT) ||
1588 (defines[i].type == LONG) ||
1589 (defines[i].type == FLOAT) ||
1590 (defines[i].type == DOUBLE) ||
1591 (defines[i].type == STRING))
1592 {
1593 fprintf(outFile, " \"value\": %s,\n", defines[i].value);
1594 }
1595 else
1596 {
1597 fprintf(outFile, " \"value\": \"%s\",\n", defines[i].value);
1598 }
1599 fprintf(outFile, " \"description\": \"%s\"\n", defines[i].desc);
1600 fprintf(outFile, " }");
1601
1602 if (i < defineCount - 1) fprintf(outFile, ",\n");
1603 else fprintf(outFile, "\n");
1604 }
1605 fprintf(outFile, " ],\n");
1606
1607 // Print structs info
1608 fprintf(outFile, " \"structs\": [\n");
1609 for (int i = 0; i < structCount; i++)
1610 {
1611 fprintf(outFile, " {\n");
1612 fprintf(outFile, " \"name\": \"%s\",\n", structs[i].name);
1613 fprintf(outFile, " \"description\": \"%s\",\n", EscapeBackslashes(structs[i].desc));
1614 fprintf(outFile, " \"fields\": [\n");
1615 for (int f = 0; f < structs[i].fieldCount; f++)
1616 {
1617 fprintf(outFile, " {\n");
1618 fprintf(outFile, " \"type\": \"%s\",\n", structs[i].fieldType[f]);
1619 fprintf(outFile, " \"name\": \"%s\",\n", structs[i].fieldName[f]);
1620 fprintf(outFile, " \"description\": \"%s\"\n", EscapeBackslashes(structs[i].fieldDesc[f]));
1621 fprintf(outFile, " }");
1622 if (f < structs[i].fieldCount - 1) fprintf(outFile, ",\n");
1623 else fprintf(outFile, "\n");
1624 }
1625 fprintf(outFile, " ]\n");
1626 fprintf(outFile, " }");
1627 if (i < structCount - 1) fprintf(outFile, ",\n");
1628 else fprintf(outFile, "\n");
1629 }
1630 fprintf(outFile, " ],\n");
1631
1632 // Print aliases info
1633 fprintf(outFile, " \"aliases\": [\n");
1634 for (int i = 0; i < aliasCount; i++)
1635 {
1636 fprintf(outFile, " {\n");
1637 fprintf(outFile, " \"type\": \"%s\",\n", aliases[i].type);
1638 fprintf(outFile, " \"name\": \"%s\",\n", aliases[i].name);
1639 fprintf(outFile, " \"description\": \"%s\"\n", aliases[i].desc);
1640 fprintf(outFile, " }");
1641
1642 if (i < aliasCount - 1) fprintf(outFile, ",\n");
1643 else fprintf(outFile, "\n");
1644 }
1645 fprintf(outFile, " ],\n");
1646
1647 // Print enums info
1648 fprintf(outFile, " \"enums\": [\n");
1649 for (int i = 0; i < enumCount; i++)
1650 {
1651 fprintf(outFile, " {\n");
1652 fprintf(outFile, " \"name\": \"%s\",\n", enums[i].name);
1653 fprintf(outFile, " \"description\": \"%s\",\n", EscapeBackslashes(enums[i].desc));
1654 fprintf(outFile, " \"values\": [\n");
1655 for (int e = 0; e < enums[i].valueCount; e++)
1656 {
1657 fprintf(outFile, " {\n");
1658 fprintf(outFile, " \"name\": \"%s\",\n", enums[i].valueName[e]);
1659 fprintf(outFile, " \"value\": %i,\n", enums[i].valueInteger[e]);
1660 fprintf(outFile, " \"description\": \"%s\"\n", EscapeBackslashes(enums[i].valueDesc[e]));
1661 fprintf(outFile, " }");
1662 if (e < enums[i].valueCount - 1) fprintf(outFile, ",\n");
1663 else fprintf(outFile, "\n");
1664 }
1665 fprintf(outFile, " ]\n");
1666 fprintf(outFile, " }");
1667 if (i < enumCount - 1) fprintf(outFile, ",\n");
1668 else fprintf(outFile, "\n");
1669 }
1670 fprintf(outFile, " ],\n");
1671
1672 // Print callbacks info
1673 fprintf(outFile, " \"callbacks\": [\n");
1674 for (int i = 0; i < callbackCount; i++)
1675 {
1676 fprintf(outFile, " {\n");
1677 fprintf(outFile, " \"name\": \"%s\",\n", callbacks[i].name);
1678 fprintf(outFile, " \"description\": \"%s\",\n", EscapeBackslashes(callbacks[i].desc));
1679 fprintf(outFile, " \"returnType\": \"%s\"", callbacks[i].retType);
1680
1681 if (callbacks[i].paramCount == 0) fprintf(outFile, "\n");
1682 else
1683 {
1684 fprintf(outFile, ",\n \"params\": [\n");
1685 for (int p = 0; p < callbacks[i].paramCount; p++)
1686 {
1687 fprintf(outFile, " {\n");
1688 fprintf(outFile, " \"type\": \"%s\",\n", callbacks[i].paramType[p]);
1689 fprintf(outFile, " \"name\": \"%s\"\n", callbacks[i].paramName[p]);
1690 fprintf(outFile, " }");
1691 if (p < callbacks[i].paramCount - 1) fprintf(outFile, ",\n");
1692 else fprintf(outFile, "\n");
1693 }
1694 fprintf(outFile, " ]\n");
1695 }
1696 fprintf(outFile, " }");
1697
1698 if (i < callbackCount - 1) fprintf(outFile, ",\n");
1699 else fprintf(outFile, "\n");
1700 }
1701 fprintf(outFile, " ],\n");
1702
1703 // Print functions info
1704 fprintf(outFile, " \"functions\": [\n");
1705 for (int i = 0; i < funcCount; i++)
1706 {
1707 fprintf(outFile, " {\n");
1708 fprintf(outFile, " \"name\": \"%s\",\n", funcs[i].name);
1709 fprintf(outFile, " \"description\": \"%s\",\n", EscapeBackslashes(funcs[i].desc));
1710 fprintf(outFile, " \"returnType\": \"%s\"", funcs[i].retType);
1711
1712 if (funcs[i].paramCount == 0) fprintf(outFile, "\n");
1713 else
1714 {
1715 fprintf(outFile, ",\n \"params\": [\n");
1716 for (int p = 0; p < funcs[i].paramCount; p++)
1717 {
1718 fprintf(outFile, " {\n");
1719 fprintf(outFile, " \"type\": \"%s\",\n", funcs[i].paramType[p]);
1720 fprintf(outFile, " \"name\": \"%s\"\n", funcs[i].paramName[p]);
1721 fprintf(outFile, " }");
1722 if (p < funcs[i].paramCount - 1) fprintf(outFile, ",\n");
1723 else fprintf(outFile, "\n");
1724 }
1725 fprintf(outFile, " ]\n");
1726 }
1727 fprintf(outFile, " }");
1728
1729 if (i < funcCount - 1) fprintf(outFile, ",\n");
1730 else fprintf(outFile, "\n");
1731 }
1732 fprintf(outFile, " ]\n");
1733 fprintf(outFile, "}\n");
1734 } break;
1735 case XML:
1736 {
1737 // XML format to export data:
1738 /*
1739 <?xml version="1.0" encoding="Windows-1252" ?>
1740 <raylibAPI>
1741 <Defines count="">
1742 <Define name="" type="" value="" desc="" />
1743 </Defines>
1744 <Structs count="">
1745 <Struct name="" fieldCount="" desc="">
1746 <Field type="" name="" desc="" />
1747 <Field type="" name="" desc="" />
1748 </Struct>
1749 <Structs>
1750 <Aliases count="">
1751 <Alias type="" name="" desc="" />
1752 </Aliases>
1753 <Enums count="">
1754 <Enum name="" valueCount="" desc="">
1755 <Value name="" integer="" desc="" />
1756 <Value name="" integer="" desc="" />
1757 </Enum>
1758 </Enums>
1759 <Callbacks count="">
1760 <Callback name="" retType="" paramCount="" desc="">
1761 <Param type="" name="" desc="" />
1762 <Param type="" name="" desc="" />
1763 </Callback>
1764 </Callbacks>
1765 <Functions count="">
1766 <Function name="" retType="" paramCount="" desc="">
1767 <Param type="" name="" desc="" />
1768 <Param type="" name="" desc="" />
1769 </Function>
1770 </Functions>
1771 </raylibAPI>
1772 */
1773
1774 fprintf(outFile, "<?xml version=\"1.0\" encoding=\"Windows-1252\" ?>\n");
1775 fprintf(outFile, "<raylibAPI>\n");
1776
1777 // Print defines info
1778 fprintf(outFile, " <Defines count=\"%i\">\n", defineCount);
1779 for (int i = 0; i < defineCount; i++)
1780 {
1781 fprintf(outFile, " <Define name=\"%s\" type=\"%s\" ", defines[i].name, StrDefineType(defines[i].type));
1782 if (defines[i].type == STRING)
1783 {
1784 fprintf(outFile, "value=%s", defines[i].value);
1785 }
1786 else
1787 {
1788 fprintf(outFile, "value=\"%s\"", defines[i].value);
1789 }
1790 fprintf(outFile, " desc=\"%s\" />\n", defines[i].desc);
1791 }
1792 fprintf(outFile, " </Defines>\n");
1793
1794 // Print structs info
1795 fprintf(outFile, " <Structs count=\"%i\">\n", structCount);
1796 for (int i = 0; i < structCount; i++)
1797 {
1798 fprintf(outFile, " <Struct name=\"%s\" fieldCount=\"%i\" desc=\"%s\">\n", structs[i].name, structs[i].fieldCount, structs[i].desc);
1799 for (int f = 0; f < structs[i].fieldCount; f++)
1800 {
1801 fprintf(outFile, " <Field type=\"%s\" name=\"%s\" desc=\"%s\" />\n", structs[i].fieldType[f], structs[i].fieldName[f], structs[i].fieldDesc[f]);
1802 }
1803 fprintf(outFile, " </Struct>\n");
1804 }
1805 fprintf(outFile, " </Structs>\n");
1806
1807 // Print aliases info
1808 fprintf(outFile, " <Aliases count=\"%i\">\n", aliasCount);
1809 for (int i = 0; i < aliasCount; i++)
1810 {
1811 fprintf(outFile, " <Alias type=\"%s\" name=\"%s\" desc=\"%s\" />\n", aliases[i].name, aliases[i].type, aliases[i].desc);
1812 }
1813 fprintf(outFile, " </Aliases>\n");
1814
1815 // Print enums info
1816 fprintf(outFile, " <Enums count=\"%i\">\n", enumCount);
1817 for (int i = 0; i < enumCount; i++)
1818 {
1819 fprintf(outFile, " <Enum name=\"%s\" valueCount=\"%i\" desc=\"%s\">\n", enums[i].name, enums[i].valueCount, enums[i].desc);
1820 for (int v = 0; v < enums[i].valueCount; v++)
1821 {
1822 fprintf(outFile, " <Value name=\"%s\" integer=\"%i\" desc=\"%s\" />\n", enums[i].valueName[v], enums[i].valueInteger[v], enums[i].valueDesc[v]);
1823 }
1824 fprintf(outFile, " </Enum>\n");
1825 }
1826 fprintf(outFile, " </Enums>\n");
1827
1828 // Print callbacks info
1829 fprintf(outFile, " <Callbacks count=\"%i\">\n", callbackCount);
1830 for (int i = 0; i < callbackCount; i++)
1831 {
1832 fprintf(outFile, " <Callback name=\"%s\" retType=\"%s\" paramCount=\"%i\" desc=\"%s\">\n", callbacks[i].name, callbacks[i].retType, callbacks[i].paramCount, callbacks[i].desc);
1833 for (int p = 0; p < callbacks[i].paramCount; p++)
1834 {
1835 fprintf(outFile, " <Param type=\"%s\" name=\"%s\" desc=\"%s\" />\n", callbacks[i].paramType[p], callbacks[i].paramName[p], callbacks[i].paramDesc[p]);
1836 }
1837 fprintf(outFile, " </Callback>\n");
1838 }
1839 fprintf(outFile, " </Callbacks>\n");
1840
1841 // Print functions info
1842 fprintf(outFile, " <Functions count=\"%i\">\n", funcCount);
1843 for (int i = 0; i < funcCount; i++)
1844 {
1845 fprintf(outFile, " <Function name=\"%s\" retType=\"%s\" paramCount=\"%i\" desc=\"%s\">\n", funcs[i].name, funcs[i].retType, funcs[i].paramCount, funcs[i].desc);
1846 for (int p = 0; p < funcs[i].paramCount; p++)
1847 {
1848 fprintf(outFile, " <Param type=\"%s\" name=\"%s\" desc=\"%s\" />\n", funcs[i].paramType[p], funcs[i].paramName[p], funcs[i].paramDesc[p]);
1849 }
1850 fprintf(outFile, " </Function>\n");
1851 }
1852 fprintf(outFile, " </Functions>\n");
1853
1854 fprintf(outFile, "</raylibAPI>\n");
1855
1856 } break;
1857 case LUA:
1858 {
1859 fprintf(outFile, "return {\n");
1860
1861 // Print defines info
1862 fprintf(outFile, " defines = {\n");
1863 for (int i = 0; i < defineCount; i++)
1864 {
1865 fprintf(outFile, " {\n");
1866 fprintf(outFile, " name = \"%s\",\n", defines[i].name);
1867 fprintf(outFile, " type = \"%s\",\n", StrDefineType(defines[i].type));
1868 if ((defines[i].type == INT) ||
1869 (defines[i].type == LONG) ||
1870 (defines[i].type == FLOAT) ||
1871 (defines[i].type == DOUBLE) ||
1872 (defines[i].type == STRING))
1873 {
1874 fprintf(outFile, " value = %s,\n", defines[i].value);
1875 }
1876 else
1877 {
1878 fprintf(outFile, " value = \"%s\",\n", defines[i].value);
1879 }
1880 fprintf(outFile, " description = \"%s\"\n", defines[i].desc);
1881 fprintf(outFile, " }");
1882
1883 if (i < defineCount - 1) fprintf(outFile, ",\n");
1884 else fprintf(outFile, "\n");
1885 }
1886 fprintf(outFile, " },\n");
1887
1888 // Print structs info
1889 fprintf(outFile, " structs = {\n");
1890 for (int i = 0; i < structCount; i++)
1891 {
1892 fprintf(outFile, " {\n");
1893 fprintf(outFile, " name = \"%s\",\n", structs[i].name);
1894 fprintf(outFile, " description = \"%s\",\n", EscapeBackslashes(structs[i].desc));
1895 fprintf(outFile, " fields = {\n");
1896 for (int f = 0; f < structs[i].fieldCount; f++)
1897 {
1898 fprintf(outFile, " {\n");
1899 fprintf(outFile, " type = \"%s\",\n", structs[i].fieldType[f]);
1900 fprintf(outFile, " name = \"%s\",\n", structs[i].fieldName[f]);
1901 fprintf(outFile, " description = \"%s\"\n", EscapeBackslashes(structs[i].fieldDesc[f]));
1902 fprintf(outFile, " }");
1903 if (f < structs[i].fieldCount - 1) fprintf(outFile, ",\n");
1904 else fprintf(outFile, "\n");
1905 }
1906 fprintf(outFile, " }\n");
1907 fprintf(outFile, " }");
1908 if (i < structCount - 1) fprintf(outFile, ",\n");
1909 else fprintf(outFile, "\n");
1910 }
1911 fprintf(outFile, " },\n");
1912
1913 // Print aliases info
1914 fprintf(outFile, " aliases = {\n");
1915 for (int i = 0; i < aliasCount; i++)
1916 {
1917 fprintf(outFile, " {\n");
1918 fprintf(outFile, " type = \"%s\",\n", aliases[i].type);
1919 fprintf(outFile, " name = \"%s\",\n", aliases[i].name);
1920 fprintf(outFile, " description = \"%s\"\n", aliases[i].desc);
1921 fprintf(outFile, " }");
1922
1923 if (i < aliasCount - 1) fprintf(outFile, ",\n");
1924 else fprintf(outFile, "\n");
1925 }
1926 fprintf(outFile, " },\n");
1927
1928 // Print enums info
1929 fprintf(outFile, " enums = {\n");
1930 for (int i = 0; i < enumCount; i++)
1931 {
1932 fprintf(outFile, " {\n");
1933 fprintf(outFile, " name = \"%s\",\n", enums[i].name);
1934 fprintf(outFile, " description = \"%s\",\n", EscapeBackslashes(enums[i].desc));
1935 fprintf(outFile, " values = {\n");
1936 for (int e = 0; e < enums[i].valueCount; e++)
1937 {
1938 fprintf(outFile, " {\n");
1939 fprintf(outFile, " name = \"%s\",\n", enums[i].valueName[e]);
1940 fprintf(outFile, " value = %i,\n", enums[i].valueInteger[e]);
1941 fprintf(outFile, " description = \"%s\"\n", EscapeBackslashes(enums[i].valueDesc[e]));
1942 fprintf(outFile, " }");
1943 if (e < enums[i].valueCount - 1) fprintf(outFile, ",\n");
1944 else fprintf(outFile, "\n");
1945 }
1946 fprintf(outFile, " }\n");
1947 fprintf(outFile, " }");
1948 if (i < enumCount - 1) fprintf(outFile, ",\n");
1949 else fprintf(outFile, "\n");
1950 }
1951 fprintf(outFile, " },\n");
1952
1953 // Print callbacks info
1954 fprintf(outFile, " callbacks = {\n");
1955 for (int i = 0; i < callbackCount; i++)
1956 {
1957 fprintf(outFile, " {\n");
1958 fprintf(outFile, " name = \"%s\",\n", callbacks[i].name);
1959 fprintf(outFile, " description = \"%s\",\n", EscapeBackslashes(callbacks[i].desc));
1960 fprintf(outFile, " returnType = \"%s\"", callbacks[i].retType);
1961
1962 if (callbacks[i].paramCount == 0) fprintf(outFile, "\n");
1963 else
1964 {
1965 fprintf(outFile, ",\n params = {\n");
1966 for (int p = 0; p < callbacks[i].paramCount; p++)
1967 {
1968 fprintf(outFile, " {type = \"%s\", name = \"%s\"}", callbacks[i].paramType[p], callbacks[i].paramName[p]);
1969 if (p < callbacks[i].paramCount - 1) fprintf(outFile, ",\n");
1970 else fprintf(outFile, "\n");
1971 }
1972 fprintf(outFile, " }\n");
1973 }
1974 fprintf(outFile, " }");
1975
1976 if (i < callbackCount - 1) fprintf(outFile, ",\n");
1977 else fprintf(outFile, "\n");
1978 }
1979 fprintf(outFile, " },\n");
1980
1981 // Print functions info
1982 fprintf(outFile, " functions = {\n");
1983 for (int i = 0; i < funcCount; i++)
1984 {
1985 fprintf(outFile, " {\n");
1986 fprintf(outFile, " name = \"%s\",\n", funcs[i].name);
1987 fprintf(outFile, " description = \"%s\",\n", EscapeBackslashes(funcs[i].desc));
1988 fprintf(outFile, " returnType = \"%s\"", funcs[i].retType);
1989
1990 if (funcs[i].paramCount == 0) fprintf(outFile, "\n");
1991 else
1992 {
1993 fprintf(outFile, ",\n params = {\n");
1994 for (int p = 0; p < funcs[i].paramCount; p++)
1995 {
1996 fprintf(outFile, " {type = \"%s\", name = \"%s\"}", funcs[i].paramType[p], funcs[i].paramName[p]);
1997 if (p < funcs[i].paramCount - 1) fprintf(outFile, ",\n");
1998 else fprintf(outFile, "\n");
1999 }
2000 fprintf(outFile, " }\n");
2001 }
2002 fprintf(outFile, " }");
2003
2004 if (i < funcCount - 1) fprintf(outFile, ",\n");
2005 else fprintf(outFile, "\n");
2006 }
2007 fprintf(outFile, " }\n");
2008 fprintf(outFile, "}\n");
2009 } break;
2010 case CODE:
2011 default: break;
2012 }
2013
2014 fclose(outFile);
2015 }