80
80
// Types and Structures Definition
81
81
//----------------------------------------------------------------------------------
82
82
83
- // Type of parsed define
83
+ // Define value type
84
84
typedef enum {
85
85
UNKNOWN = 0 ,
86
86
MACRO ,
@@ -101,7 +101,7 @@ typedef enum {
101
101
// Define info data
102
102
typedef struct DefineInfo {
103
103
char name [64 ]; // Define name
104
- int type ; // Define type
104
+ int type ; // Define type: enum DefineType
105
105
char value [256 ]; // Define value
106
106
char desc [128 ]; // Define description
107
107
bool isHex ; // Define is hex number (for types INT, LONG)
@@ -169,7 +169,7 @@ static char apiDefine[32] = { 0 }; // Functions define (i.e. RLAPI for r
169
169
static char truncAfter [32 ] = { 0 }; // Truncate marker (i.e. "RLGL IMPLEMENTATION" for rlgl.h)
170
170
static int outputFormat = DEFAULT ;
171
171
172
- // NOTE: Max length depends on OS, in Windows MAX_PATH = 256
172
+ // NOTE: Filename max length depends on OS, in Windows MAX_PATH = 256
173
173
static char inFileName [512 ] = { 0 }; // Input file name (required in case of drag & drop over executable)
174
174
static char outFileName [512 ] = { 0 }; // Output file name (required for file save/export)
175
175
@@ -179,15 +179,17 @@ static char outFileName[512] = { 0 }; // Output file name (required for fi
179
179
static void ShowCommandLineInfo (void ); // Show command line usage info
180
180
static void ProcessCommandLine (int argc , char * argv []); // Process command line input
181
181
182
- static char * LoadFileText (const char * fileName , int * length );
183
- static char * * GetTextLines (const char * buffer , int length , int * linesCount );
184
- static void GetDataTypeAndName (const char * typeName , int typeNameLen , char * type , char * name );
185
- static void GetDescription (const char * source , char * description );
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
186
188
static void MoveArraySize (char * name , char * type ); // Move array size from name to type
187
189
static unsigned int TextLength (const char * text ); // Get text length in bytes, check for \0 character
188
190
static bool IsTextEqual (const char * text1 , const char * text2 , unsigned int count );
189
191
static int TextFindIndex (const char * text , const char * find ); // Find first text occurrence within a string
190
- static void MemoryCopy (void * dest , const void * src , unsigned int count );
192
+ static void MemoryCopy (void * dest , const void * src , unsigned int count ); // Memory copy, memcpy() replacement to avoid <string.h>
191
193
static char * EscapeBackslashes (char * text ); // Replace '\' by "\\" when exporting to JSON and XML
192
194
static const char * StrDefineType (DefineType type ); // Get string of define type
193
195
@@ -217,21 +219,21 @@ int main(int argc, char* argv[])
217
219
}
218
220
219
221
// Preprocess buffer to get separate lines
220
- // NOTE: GetTextLines () also removes leading spaces/tabs
221
- int linesCount = 0 ;
222
- char * * lines = GetTextLines (buffer , length , & linesCount );
222
+ // NOTE: LoadTextLines () also removes leading spaces/tabs
223
+ int lineCount = 0 ;
224
+ char * * lines = LoadTextLines (buffer , length , & lineCount );
223
225
224
- // Truncate lines
226
+ // Truncate lines (if required)
225
227
if (truncAfter [0 ] != '\0' )
226
228
{
227
229
int newCount = -1 ;
228
- for (int i = 0 ; i < linesCount ; i ++ )
230
+ for (int i = 0 ; i < lineCount ; i ++ )
229
231
{
230
232
if (newCount > -1 ) free (lines [i ]);
231
233
else if (TextFindIndex (lines [i ], truncAfter ) > -1 ) newCount = i ;
232
234
}
233
- if (newCount > -1 ) linesCount = newCount ;
234
- printf ("Number of truncated text lines: %i\n" , linesCount );
235
+ if (newCount > -1 ) lineCount = newCount ;
236
+ printf ("Number of truncated text lines: %i\n" , lineCount );
235
237
}
236
238
237
239
// Defines line indices
@@ -254,9 +256,8 @@ int main(int argc, char* argv[])
254
256
255
257
// Prepare required lines for parsing
256
258
//----------------------------------------------------------------------------------
257
-
258
259
// Read define lines
259
- for (int i = 0 ; i < linesCount ; i ++ )
260
+ for (int i = 0 ; i < lineCount ; i ++ )
260
261
{
261
262
int j = 0 ;
262
263
while ((lines [i ][j ] == ' ' ) || (lines [i ][j ] == '\t' )) j ++ ; // skip spaces and tabs in the begining
@@ -271,7 +272,7 @@ int main(int argc, char* argv[])
271
272
}
272
273
273
274
// Read struct lines
274
- for (int i = 0 ; i < linesCount ; i ++ )
275
+ for (int i = 0 ; i < lineCount ; i ++ )
275
276
{
276
277
// Find structs
277
278
// starting with "typedef struct ... {" or "typedef struct ... ; \n struct ... {"
@@ -298,7 +299,7 @@ int main(int argc, char* argv[])
298
299
}
299
300
300
301
// Read alias lines
301
- for (int i = 0 ; i < linesCount ; i ++ )
302
+ for (int i = 0 ; i < lineCount ; i ++ )
302
303
{
303
304
// Find aliases (lines with "typedef ... ...;")
304
305
if (IsTextEqual (lines [i ], "typedef" , 7 ))
@@ -320,7 +321,7 @@ int main(int argc, char* argv[])
320
321
}
321
322
322
323
// Read enum lines
323
- for (int i = 0 ; i < linesCount ; i ++ )
324
+ for (int i = 0 ; i < lineCount ; i ++ )
324
325
{
325
326
// Read enum line
326
327
if (IsTextEqual (lines [i ], "typedef enum {" , 14 ) && (lines [i ][TextLength (lines [i ])- 1 ] != ';' )) // ignore inline enums
@@ -333,7 +334,7 @@ int main(int argc, char* argv[])
333
334
}
334
335
335
336
// Read callback lines
336
- for (int i = 0 ; i < linesCount ; i ++ )
337
+ for (int i = 0 ; i < lineCount ; i ++ )
337
338
{
338
339
// Find callbacks (lines with "typedef ... (* ... )( ... );")
339
340
if (IsTextEqual (lines [i ], "typedef" , 7 ))
@@ -359,7 +360,7 @@ int main(int argc, char* argv[])
359
360
}
360
361
361
362
// Read function lines
362
- for (int i = 0 ; i < linesCount ; i ++ )
363
+ for (int i = 0 ; i < lineCount ; i ++ )
363
364
{
364
365
// Read function line (starting with `define`, i.e. for raylib.h "RLAPI")
365
366
if (IsTextEqual (lines [i ], apiDefine , TextLength (apiDefine )))
@@ -371,14 +372,13 @@ int main(int argc, char* argv[])
371
372
372
373
// At this point we have all raylib defines, structs, aliases, enums, callbacks, functions lines data to start parsing
373
374
374
- free (buffer ); // Unload text buffer
375
+ UnloadFileText (buffer ); // Unload text buffer
375
376
376
377
// Parsing raylib data
377
378
//----------------------------------------------------------------------------------
378
-
379
379
// Define info data
380
- defines = (DefineInfo * )calloc (MAX_DEFINES_TO_PARSE , sizeof (DefineInfo ));
381
380
int defineIndex = 0 ;
381
+ defines = (DefineInfo * )calloc (MAX_DEFINES_TO_PARSE , sizeof (DefineInfo ));
382
382
383
383
for (int i = 0 ; i < defineCount ; i ++ )
384
384
{
@@ -1038,8 +1038,7 @@ int main(int argc, char* argv[])
1038
1038
}
1039
1039
free (funcLines );
1040
1040
1041
- for (int i = 0 ; i < linesCount ; i ++ ) free (lines [i ]);
1042
- free (lines );
1041
+ UnloadTextLines (lines , lineCount );
1043
1042
1044
1043
// At this point, all raylib data has been parsed!
1045
1044
//----------------------------------------------------------------------------------
@@ -1219,8 +1218,14 @@ static char *LoadFileText(const char *fileName, int *length)
1219
1218
return text ;
1220
1219
}
1221
1220
1221
+ // Unload text data
1222
+ static void UnloadFileText (char * text )
1223
+ {
1224
+ free (text );
1225
+ }
1226
+
1222
1227
// Get all lines from a text buffer (expecting lines ending with '\n')
1223
- static char * * GetTextLines (const char * buffer , int length , int * linesCount )
1228
+ static char * * LoadTextLines (const char * buffer , int length , int * lineCount )
1224
1229
{
1225
1230
// Get the number of lines in the text
1226
1231
int count = 0 ;
@@ -1235,7 +1240,7 @@ static char **GetTextLines(const char *buffer, int length, int *linesCount)
1235
1240
1236
1241
for (int i = 0 ; (i < count ) || (bufferPtr [0 ] != '\0' ); i ++ )
1237
1242
{
1238
- lines [i ] = (char * )calloc (MAX_LINE_LENGTH , sizeof (char ));
1243
+ lines [i ] = (char * )calloc (MAX_LINE_LENGTH , sizeof (char )); // MAX_LINE_LENGTH=1024
1239
1244
1240
1245
// Remove line leading spaces
1241
1246
// Find last index of space/tab character
@@ -1252,10 +1257,17 @@ static char **GetTextLines(const char *buffer, int length, int *linesCount)
1252
1257
bufferPtr += (index + j + 1 );
1253
1258
}
1254
1259
1255
- * linesCount = count ;
1260
+ * lineCount = count ;
1256
1261
return lines ;
1257
1262
}
1258
1263
1264
+ // Unload text lines data
1265
+ static void UnloadTextLines (char * * lines , int lineCount )
1266
+ {
1267
+ for (int i = 0 ; i < lineCount ; i ++ ) free (lines [i ]);
1268
+ free (lines );
1269
+ }
1270
+
1259
1271
// Get data type and name from a string containing both
1260
1272
// NOTE: Useful to parse function parameters and struct fields
1261
1273
static void GetDataTypeAndName (const char * typeName , int typeNameLen , char * type , char * name )
0 commit comments