added wrong input err message (file utils.c)
authorViktor Sinagl <vsinagl@c2r7s6.42prague.com>
Tue, 14 Jan 2025 17:20:39 +0000 (18:20 +0100)
committerViktor Sinagl <vsinagl@c2r7s6.42prague.com>
Tue, 14 Jan 2025 17:20:39 +0000 (18:20 +0100)
Libft
Makefile
inc/miniRT.h
src/main.c
src/parsing.c
src/utils.c [new file with mode: 0644]

diff --git a/Libft b/Libft
index 5aafaba628a2eb5bc98021a9b686713966446db1..62f583c8ab43754b5265c708243f3f5353ebfa1e 160000 (submodule)
--- a/Libft
+++ b/Libft
@@ -1 +1 @@
-Subproject commit 5aafaba628a2eb5bc98021a9b686713966446db1
+Subproject commit 62f583c8ab43754b5265c708243f3f5353ebfa1e
index ea2178a98841fb6471c1a325f0959ee0d873e73d..a6836acd27c8209ec3be813fcd81beb136c5eded 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -25,6 +25,7 @@ SOURCES :=    main.c                          \
                        manipulation.c          \
                        parsing.c                       \
                        vec3.c                          \
+                       utils.c                         \
 
 SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES))
 
index 34bccb091c5a101103c6a391a81eeb2f9809ce4b..8714a6aaf809d80069ca1f92cba936f0e04ac101 100644 (file)
@@ -160,5 +160,6 @@ void                        translate(
                                        t_element *element, t_vec3 direction, double distance);
 void                   change_radius(t_element *element, double change);
 void                   change_height(t_element *element, double change);
+void                   print_help(int argc, char **argv);
 
 #endif // MINIRT_H
index 1f3939012beb8484bb24e08df3e179bed201349e..852cfb3d297cc42202be72d77e1d1b7f1fde2da7 100644 (file)
@@ -317,7 +317,7 @@ int main(int argc, char **argv)
        set_defaults(&s);
        if (parse_args(argc, argv, &s))
        {
-               //print_help();
+               print_help(argc, argv);
                return (1);
        }
        init_session(&s);
index 6b957c06e785ecbde7b4e549e948ad8b3b08ab24..7f05d3a3f8f6089e552ec687efa227caa3bc6b4e 100644 (file)
@@ -397,7 +397,9 @@ int parse_args(int argc, char **argv, t_session *s)
                if (!ft_strcmp(argv[i], "-f"))
                        got_file = 1;
                if (parse_arg(argv, s, &i))
+               {
                        return (1);
+               }
                ++i;
        }
        return (!got_file);
diff --git a/src/utils.c b/src/utils.c
new file mode 100644 (file)
index 0000000..4370288
--- /dev/null
@@ -0,0 +1,79 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   parsing.c                                          :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/11/28 12:34:20 by ljiriste          #+#    #+#             */
+/*   Updated: 2025/01/13 19:54:25 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "miniRT.h"
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+void   print_msg(char *msg)
+{
+       printf("%s\n", msg);
+       printf("Usage: miniRT [options] [file.rt]\n");
+       printf("Options:\n");
+       printf("\t-w [int] (width) - set the width of the window\n");
+       printf("\t-h [int] (height) - set the height of the window\n");
+}
+
+/*
+Function that check propriete file ending (.rt) and also
+checks if this file exists.
+- use fopen to open file and check if error occurs during
+opening --> file can be open
+*/
+int    check_file(char *filename)
+{
+       int             i;
+       int             fd;
+
+       fd = -1;
+       i = 0;
+       while (filename[i])
+               i++;
+       if ((ft_strlen(filename) < 4) || (filename[i - 1] != 't'
+                       || filename[i - 2] != 'r' || filename[i - 3] != '.'))
+       {
+               printf("Wrong file ending! Please, provide .rt file\n");
+               return (1);
+       }
+       else
+       {
+               fd = open(filename, 0);
+       }
+       if (fd == -1)
+       {
+               printf("Error in opening file.\n");
+               return (1);
+       }
+       close(fd);
+       return (0);
+}
+
+/*
+checks for errors in arguments and provide error message
+*/
+void   print_help(int argc, char **argv)
+{
+       if (argc < 2 || argc % 2 == 0)
+       {
+               print_msg("Wrong number of arguments");
+               return ;
+       }
+       else if (ft_strcmp(argv[argc - 2], "-f"))
+       {
+               print_msg("Provide -f flag for filename\n");
+               return ;
+       }
+       else
+               check_file(argv[argc - 1]);
+       return ;
+}