From d158f3883fb919dcf724db467a2e416411907230 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Wed, 11 Oct 2023 18:19:23 +0200 Subject: [PATCH] Repaired minor problems in main. Created main.h as Libft has a collision with get_next_line in linked list definition. In future commits, Libft has to be integrated directly in this project. (Or some equivalent action has to be done) --- main.h | 21 +++++++++++++++++++++ src/main.c | 42 +++++++++++++++++++++++++++++++++--------- 2 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 main.h diff --git a/main.h b/main.h new file mode 100644 index 0000000..270bebe --- /dev/null +++ b/main.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/11 18:10:37 by ljiriste #+# #+# */ +/* Updated: 2023/10/11 18:15:50 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MAIN_H +# define MAIN_H + +char *ft_strdup(const char *str); +int ft_printf(const char *format, ...); +void ft_putstr_fd(const char *str, int fd); +int ft_strlen(const char *str); + +#endif diff --git a/src/main.c b/src/main.c index 235710e..6d7aac8 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,20 @@ -#define TEST_DIR "./test_files/" -#define RESULT_DIR "./results/" -#define SEPARATOR "---------------------\n" +#ifndef TEST_DIR +# define TEST_DIR "./test_files/" +#endif + +#ifndef RESULT_DIR +# define RESULT_DIR "./results/" +#endif + +#ifndef SEPARATOR +# define SEPARATOR "---------------------\n" +#endif + +#include +#include +#include +#include "main.h" +#include "get_next_line.h" int my_open(const char *fname) { @@ -8,18 +22,27 @@ int my_open(const char *fname) char *path; path = ft_strdup(TEST_DIR); - ft_strncat_alloc(&path, fname); + ft_strncat_alloc(&path, fname, ft_strlen(fname)); fd = open(path, O_RDONLY); return (fd); } +char last_char(const char *str) +{ + if (*str == '\0') + return ('\0'); + while (*str != '\0') + ++str; + return (*(str - 1)); +} + int create_res_file(const char *fname) { int fd; char *path; path = ft_strdup(RESULT_DIR); - ft_strncat_alloc(&path, fname); + ft_strncat_alloc(&path, fname, ft_strlen(fname)); fd = open(path, O_WRONLY | O_CREAT); return (fd); } @@ -31,10 +54,11 @@ void test_gnl(int testfd, int resfd) line = get_next_line(testfd); while (line) { - ft_pustr_fd(resfd, line); - ft_putstr_fd(resfd, SEPARATOR); + ft_putstr_fd(line, resfd); free(line); line = get_next_line(testfd); + if (last_char(line) == '\n') + ft_putstr_fd(SEPARATOR, resfd); } return ; } @@ -53,8 +77,8 @@ int main(int argc, char **argv) i = 1; while (i < argc) { - test = my_open(argv[i]); - result = create_res_file(argv[i]); + testfd = my_open(argv[i]); + resfd = create_res_file(argv[i]); test_gnl(testfd, resfd); close(testfd); close(resfd); -- 2.30.2