Skip to content

Commit

Permalink
Add cmp_rli2
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanadrel committed Jun 11, 2021
1 parent a704851 commit 8be220c
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 6 deletions.
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ linux:
${CC_LINUX} ${CFLAGS_LINUX} ${LDFLAGS_LINUX} -o leftlist.bin src/leftlist.c
${CC_LINUX} ${CFLAGS_LINUX} ${LDFLAGS_LINUX} -o sed_cut_coffee.bin src/sed_cut_coffee.c
${CC_LINUX} ${CFLAGS_LINUX} ${LDFLAGS_LINUX} -o dgrep2.bin src/dgrep2.c
${CC_LINUX} ${CFLAGS_LINUX} ${LDFLAGS_LINUX} -o metagen.exe src/metagen.c
${CC_LINUX} ${CFLAGS_LINUX} ${LDFLAGS_LINUX} -o metabuild.exe src/metabuild.c
${CC_LINUX} ${CFLAGS_LINUX} ${LDFLAGS_LINUX} -o rli2_len.exe src/rli2_len.c
${CC_LINUX} ${CFLAGS_LINUX} ${LDFLAGS_LINUX} -o metagen.bin src/metagen.c
${CC_LINUX} ${CFLAGS_LINUX} ${LDFLAGS_LINUX} -o metabuild.bin src/metabuild.c
${CC_LINUX} ${CFLAGS_LINUX} ${LDFLAGS_LINUX} -o rli2_len.bin src/rli2_len.c
${CC_LINUX} ${CFLAGS_LINUX} ${LDFLAGS_LINUX} -o cmp_rli2.bin src/cmp_rli2.c

##
## WINDOWS
Expand All @@ -53,4 +54,5 @@ windows:
${CC_WINDOWS} ${CFLAGS_WINDOWS} -o metagen.exe src/metagen.c
${CC_WINDOWS} ${CFLAGS_WINDOWS} -o metabuild.exe src/metabuild.c
${CC_WINDOWS} ${CFLAGS_WINDOWS} -o rli2_len.exe src/rli2_len.c
${CC_WINDOWS} ${CFLAGS_WINDOWS} -o cmp_rli2.exe src/cmp_rli2.c

102 changes: 102 additions & 0 deletions src/cmp_rli2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#define __MSVCRT_VERSION__ 0x0700

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "utils.c"

/**
* Name....: cmp_rli2.c
* Author..: Xanadrel
* Desc....: Get percentage of similar lines between two files
* License.: MIT
*/

static int cmp_cache(const void *p1, const void *p2)
{
return strcmp(p1, p2);
}

int main(int argc, char *argv[])
{
FILE *fd1;
FILE *fd2;

if (argc != 3)
{
fprintf(stderr, "usage: %s infile removefile\n", argv[0]);

return (-1);
}

char *infile = argv[1];
char *removefile = argv[2];

if ((fd1 = fopen(infile, "rb")) == NULL)
{
fprintf(stderr, "%s: %s\n", infile, strerror(errno));

return (-1);
}

if ((fd2 = fopen(removefile, "rb")) == NULL)
{
fprintf(stderr, "%s: %s\n", removefile, strerror(errno));

return (-1);
}

char line_buf1[BUFSIZ];
char line_buf2[BUFSIZ];

if (fgetl(fd1, BUFSIZ, line_buf1) == -1) memset(line_buf1, 0, BUFSIZ);
if (fgetl(fd2, BUFSIZ, line_buf2) == -1) memset(line_buf2, 0, BUFSIZ);

int comp = 1;
uint similar = 0;
uint total = 0;

while (!feof(fd1) && !feof(fd2))
{
comp = cmp_cache(line_buf1, line_buf2);

if (comp == 0)
{
if (fgetl(fd1, BUFSIZ, line_buf1) == -1) memset(line_buf1, 0, BUFSIZ);
if (fgetl(fd2, BUFSIZ, line_buf2) == -1) memset(line_buf2, 0, BUFSIZ);

similar++;
total++;
}
else if (comp > 0)
{
if (fgetl(fd2, BUFSIZ, line_buf2) == -1) memset(line_buf2, 0, BUFSIZ);
}
else if (comp < 0)
{
if (fgetl(fd1, BUFSIZ, line_buf1) == -1) memset(line_buf1, 0, BUFSIZ);
total++;
}
}

if (!feof (fd1) && comp == 0) similar++;

while (!feof (fd1))
{
if (fgetl (fd1, BUFSIZ, line_buf1) == -1)
{
memset (line_buf1, 0, BUFSIZ);
continue;
}
total++;
}

fclose(fd1);
fclose(fd2);

printf("%s:%s %f%%\n", infile, removefile, (float)100 * (float)similar / (float)total);

return 0;
}
6 changes: 3 additions & 3 deletions src/rli2_len.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#include "utils.c"

/**
* Name...: rli2_len.c
* Author.: Xanadrel
* Desc...: Same as rli2 but using line length from infile for the comparison
* Name....: rli2_len.c
* Author..: Xanadrel
* Desc....: Same as rli2 but using line length from infile for the comparison
* License.: MIT
*/

Expand Down

0 comments on commit 8be220c

Please sign in to comment.