Repaired minor problems in main.
authorLukas Jiriste <ljiriste@student.42prague.com>
Wed, 11 Oct 2023 16:19:23 +0000 (18:19 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Wed, 11 Oct 2023 16:19:23 +0000 (18:19 +0200)
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 [new file with mode: 0644]
src/main.c

diff --git a/main.h b/main.h
new file mode 100644 (file)
index 0000000..270bebe
--- /dev/null
+++ b/main.h
@@ -0,0 +1,21 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   main.h                                             :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   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
index 235710ecb6c4120148afe57d1ad68212de49b946..6d7aac8211f45d2749f18ee669a98a26ba79f81d 100644 (file)
@@ -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 <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#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);