Skip to content

Commit 1937fed

Browse files
committed
rlparser: Minor tweaks
1 parent d7a83a1 commit 1937fed

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

tools/rlparser/rlparser.c

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
// Types and Structures Definition
8181
//----------------------------------------------------------------------------------
8282

83-
// Type of parsed define
83+
// Define value type
8484
typedef enum {
8585
UNKNOWN = 0,
8686
MACRO,
@@ -101,7 +101,7 @@ typedef enum {
101101
// Define info data
102102
typedef struct DefineInfo {
103103
char name[64]; // Define name
104-
int type; // Define type
104+
int type; // Define type: enum DefineType
105105
char value[256]; // Define value
106106
char desc[128]; // Define description
107107
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
169169
static char truncAfter[32] = { 0 }; // Truncate marker (i.e. "RLGL IMPLEMENTATION" for rlgl.h)
170170
static int outputFormat = DEFAULT;
171171

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
173173
static char inFileName[512] = { 0 }; // Input file name (required in case of drag & drop over executable)
174174
static char outFileName[512] = { 0 }; // Output file name (required for file save/export)
175175

@@ -179,15 +179,17 @@ static char outFileName[512] = { 0 }; // Output file name (required for fi
179179
static void ShowCommandLineInfo(void); // Show command line usage info
180180
static void ProcessCommandLine(int argc, char *argv[]); // Process command line input
181181

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
186188
static void MoveArraySize(char *name, char *type); // Move array size from name to type
187189
static unsigned int TextLength(const char *text); // Get text length in bytes, check for \0 character
188190
static bool IsTextEqual(const char *text1, const char *text2, unsigned int count);
189191
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>
191193
static char *EscapeBackslashes(char *text); // Replace '\' by "\\" when exporting to JSON and XML
192194
static const char *StrDefineType(DefineType type); // Get string of define type
193195

@@ -217,21 +219,21 @@ int main(int argc, char* argv[])
217219
}
218220

219221
// 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);
223225

224-
// Truncate lines
226+
// Truncate lines (if required)
225227
if (truncAfter[0] != '\0')
226228
{
227229
int newCount = -1;
228-
for (int i = 0; i < linesCount; i++)
230+
for (int i = 0; i < lineCount; i++)
229231
{
230232
if (newCount > -1) free(lines[i]);
231233
else if (TextFindIndex(lines[i], truncAfter) > -1) newCount = i;
232234
}
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);
235237
}
236238

237239
// Defines line indices
@@ -254,9 +256,8 @@ int main(int argc, char* argv[])
254256

255257
// Prepare required lines for parsing
256258
//----------------------------------------------------------------------------------
257-
258259
// Read define lines
259-
for (int i = 0; i < linesCount; i++)
260+
for (int i = 0; i < lineCount; i++)
260261
{
261262
int j = 0;
262263
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[])
271272
}
272273

273274
// Read struct lines
274-
for (int i = 0; i < linesCount; i++)
275+
for (int i = 0; i < lineCount; i++)
275276
{
276277
// Find structs
277278
// starting with "typedef struct ... {" or "typedef struct ... ; \n struct ... {"
@@ -298,7 +299,7 @@ int main(int argc, char* argv[])
298299
}
299300

300301
// Read alias lines
301-
for (int i = 0; i < linesCount; i++)
302+
for (int i = 0; i < lineCount; i++)
302303
{
303304
// Find aliases (lines with "typedef ... ...;")
304305
if (IsTextEqual(lines[i], "typedef", 7))
@@ -320,7 +321,7 @@ int main(int argc, char* argv[])
320321
}
321322

322323
// Read enum lines
323-
for (int i = 0; i < linesCount; i++)
324+
for (int i = 0; i < lineCount; i++)
324325
{
325326
// Read enum line
326327
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[])
333334
}
334335

335336
// Read callback lines
336-
for (int i = 0; i < linesCount; i++)
337+
for (int i = 0; i < lineCount; i++)
337338
{
338339
// Find callbacks (lines with "typedef ... (* ... )( ... );")
339340
if (IsTextEqual(lines[i], "typedef", 7))
@@ -359,7 +360,7 @@ int main(int argc, char* argv[])
359360
}
360361

361362
// Read function lines
362-
for (int i = 0; i < linesCount; i++)
363+
for (int i = 0; i < lineCount; i++)
363364
{
364365
// Read function line (starting with `define`, i.e. for raylib.h "RLAPI")
365366
if (IsTextEqual(lines[i], apiDefine, TextLength(apiDefine)))
@@ -371,14 +372,13 @@ int main(int argc, char* argv[])
371372

372373
// At this point we have all raylib defines, structs, aliases, enums, callbacks, functions lines data to start parsing
373374

374-
free(buffer); // Unload text buffer
375+
UnloadFileText(buffer); // Unload text buffer
375376

376377
// Parsing raylib data
377378
//----------------------------------------------------------------------------------
378-
379379
// Define info data
380-
defines = (DefineInfo *)calloc(MAX_DEFINES_TO_PARSE, sizeof(DefineInfo));
381380
int defineIndex = 0;
381+
defines = (DefineInfo *)calloc(MAX_DEFINES_TO_PARSE, sizeof(DefineInfo));
382382

383383
for (int i = 0; i < defineCount; i++)
384384
{
@@ -1038,8 +1038,7 @@ int main(int argc, char* argv[])
10381038
}
10391039
free(funcLines);
10401040

1041-
for (int i = 0; i < linesCount; i++) free(lines[i]);
1042-
free(lines);
1041+
UnloadTextLines(lines, lineCount);
10431042

10441043
// At this point, all raylib data has been parsed!
10451044
//----------------------------------------------------------------------------------
@@ -1219,8 +1218,14 @@ static char *LoadFileText(const char *fileName, int *length)
12191218
return text;
12201219
}
12211220

1221+
// Unload text data
1222+
static void UnloadFileText(char *text)
1223+
{
1224+
free(text);
1225+
}
1226+
12221227
// 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)
12241229
{
12251230
// Get the number of lines in the text
12261231
int count = 0;
@@ -1235,7 +1240,7 @@ static char **GetTextLines(const char *buffer, int length, int *linesCount)
12351240

12361241
for (int i = 0; (i < count) || (bufferPtr[0] != '\0'); i++)
12371242
{
1238-
lines[i] = (char *)calloc(MAX_LINE_LENGTH, sizeof(char));
1243+
lines[i] = (char *)calloc(MAX_LINE_LENGTH, sizeof(char)); // MAX_LINE_LENGTH=1024
12391244

12401245
// Remove line leading spaces
12411246
// Find last index of space/tab character
@@ -1252,10 +1257,17 @@ static char **GetTextLines(const char *buffer, int length, int *linesCount)
12521257
bufferPtr += (index + j + 1);
12531258
}
12541259

1255-
*linesCount = count;
1260+
*lineCount = count;
12561261
return lines;
12571262
}
12581263

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+
12591271
// Get data type and name from a string containing both
12601272
// NOTE: Useful to parse function parameters and struct fields
12611273
static void GetDataTypeAndName(const char *typeName, int typeNameLen, char *type, char *name)

0 commit comments

Comments
 (0)