Skip to content

Commit bb4e79c

Browse files
spinaevvictorjulien
authored andcommitted
reputation: deduplicate skipping empty lines/comments
1 parent 2e22ba6 commit bb4e79c

1 file changed

Lines changed: 13 additions & 18 deletions

File tree

src/reputation.c

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,17 @@ static int SRepLoadCatFile(const char *filename)
354354
return r;
355355
}
356356

357+
static inline size_t GetEffectiveLineLen(const char *line)
358+
{
359+
size_t len = strlen(line);
360+
/* ignore comments and empty lines */
361+
if (len == 0 || line[0] == '\n' || line[0] == '\r' || line[0] == ' ' || line[0] == '#' ||
362+
line[0] == '\t')
363+
return 0;
364+
365+
return len;
366+
}
367+
357368
int SRepLoadCatFileFromFD(FILE *fp)
358369
{
359370
char line[8192] = "";
@@ -365,19 +376,11 @@ int SRepLoadCatFileFromFD(FILE *fp)
365376
BUG_ON(SRepGetVersion() > 0);
366377

367378
while(fgets(line, (int)sizeof(line), fp) != NULL) {
368-
size_t len = strlen(line);
379+
size_t len = GetEffectiveLineLen(line);
369380
if (len == 0)
370381
continue;
371382

372-
/* ignore comments and empty lines */
373-
if (line[0] == '\n' || line [0] == '\r' || line[0] == ' ' || line[0] == '#' || line[0] == '\t')
374-
continue;
375-
376383
/* Check if we have a trailing newline, and remove it */
377-
len = strlen(line);
378-
if (len == 0)
379-
continue;
380-
381384
if (line[len - 1] == '\n' || line[len - 1] == '\r') {
382385
line[len - 1] = '\0';
383386
}
@@ -423,19 +426,11 @@ int SRepLoadFileFromFD(SRepCIDRTree *cidr_ctx, FILE *fp)
423426
char line[8192] = "";
424427

425428
while(fgets(line, (int)sizeof(line), fp) != NULL) {
426-
size_t len = strlen(line);
429+
size_t len = GetEffectiveLineLen(line);
427430
if (len == 0)
428431
continue;
429432

430-
/* ignore comments and empty lines */
431-
if (line[0] == '\n' || line [0] == '\r' || line[0] == ' ' || line[0] == '#' || line[0] == '\t')
432-
continue;
433-
434433
/* Check if we have a trailing newline, and remove it */
435-
len = strlen(line);
436-
if (len == 0)
437-
continue;
438-
439434
if (line[len - 1] == '\n' || line[len - 1] == '\r') {
440435
line[len - 1] = '\0';
441436
}

0 commit comments

Comments
 (0)