From: Viktor Sinagl Date: Tue, 14 Jan 2025 17:20:39 +0000 (+0100) Subject: added wrong input err message (file utils.c) X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=7428e1c;p=42%2FminiRT.git added wrong input err message (file utils.c) --- diff --git a/Libft b/Libft index 5aafaba..62f583c 160000 --- a/Libft +++ b/Libft @@ -1 +1 @@ -Subproject commit 5aafaba628a2eb5bc98021a9b686713966446db1 +Subproject commit 62f583c8ab43754b5265c708243f3f5353ebfa1e diff --git a/Makefile b/Makefile index ea2178a..a6836ac 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,7 @@ SOURCES := main.c \ manipulation.c \ parsing.c \ vec3.c \ + utils.c \ SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES)) diff --git a/inc/miniRT.h b/inc/miniRT.h index 34bccb0..8714a6a 100644 --- a/inc/miniRT.h +++ b/inc/miniRT.h @@ -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 diff --git a/src/main.c b/src/main.c index 1f39390..852cfb3 100644 --- a/src/main.c +++ b/src/main.c @@ -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); diff --git a/src/parsing.c b/src/parsing.c index 6b957c0..7f05d3a 100644 --- a/src/parsing.c +++ b/src/parsing.c @@ -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 index 0000000..4370288 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/28 12:34:20 by ljiriste #+# #+# */ +/* Updated: 2025/01/13 19:54:25 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "miniRT.h" +#include +#include +#include + +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 ; +}