diff --git a/arghandle.c b/arghandle.c new file mode 100644 index 0000000..869a577 --- /dev/null +++ b/arghandle.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* arghandle.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/18 23:37:32 by tischmid #+# #+# */ +/* Updated: 2023/03/18 23:37:33 by tischmid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int get_dim(char *type, int default_max) +{ + char output[10]; + char cmd[11]; + int max; + FILE *fp; + + ft_strcpy(cmd, "tput "); + ft_strcat(cmd, type); + fp = popen(cmd, "r"); + if (fp == NULL) + max = default_max; + else + while (fgets(output, sizeof(output), fp) != NULL) + max = ft_atoi(output); + pclose(fp); + return (max); +} + +int wrong_dimension(char *dim, int max, int actual) +{ + ft_putstr("\033[31mOutput will not fit into terminal (term"); + ft_putstr(dim); + ft_putstr(" "); + ft_putnbr(max); + ft_putstr(" provided "); + ft_putstr(dim); + ft_putstr(" "); + ft_putnbr(actual); + ft_putstr(")\033[m\n"); + return (2); +} + +void usage(char *argv0) +{ + ft_putstr("\033[32mUsage: "); + ft_putstr(argv0); + ft_putstr(" [width] [height]\033[m\n"); +} + +void wrong_argc(int expected, int argc) +{ + ft_putstr("\033[31mWrong number of arguments (expected "); + ft_putnbr(expected); + ft_putstr(" got "); + ft_putnbr(argc); + ft_putstr(")\033[m\n"); +} diff --git a/ft_lib.c b/ft_lib.c new file mode 100644 index 0000000..ad96827 --- /dev/null +++ b/ft_lib.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lib.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/18 22:33:38 by tischmid #+# #+# */ +/* Updated: 2023/03/18 23:35:30 by tischmid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_lib.h" + +void ft_putchar(char c) +{ + write(1, &c, 1); +} + +void ft_putstr(char *str) +{ + int i; + + i = 0; + while (str[i] != '\0') + ft_putchar(str[i++]); +} + +void ft_putnbr(int nb) +{ + char last_digit; + + if (nb < 0) + { + ft_putchar('-'); + if (nb == 1 << 31) + { + nb += 2e9; + ft_putchar('2'); + } + nb *= -1; + } + last_digit = nb % 10 + '0'; + if (nb > 9) + { + nb /= 10; + ft_putnbr(nb); + } + ft_putchar(last_digit); +} + +int ft_atoi(char *str) +{ + int i; + int number; + + i = 0; + number = 0; + while (str[i] >= '0' && str[i] <= '9') + number = number * 10 + (str[i++] - '0'); + return (number); +} + +char *ft_strcpy(char *s1, char *s2) +{ + int i; + + i = -1; + while (s2[++i] != '\0') + s1[i] = s2[i]; + s1[i] = '\0'; + return (s1); +} + +char *ft_strcat(char *s1, char *s2) +{ + int i; + int j; + + i = 0; + while (s1[i] != '\0') + ++i; + j = -1; + while (s2[++j] != '\0') + s1[i++] = s2[j]; + s1[i] = '\0'; + return (s1); +} diff --git a/include/arghandle.h b/include/arghandle.h new file mode 100644 index 0000000..f478d16 --- /dev/null +++ b/include/arghandle.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* arghandle.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/18 23:38:17 by tischmid #+# #+# */ +/* Updated: 2023/03/18 23:45:10 by tischmid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ARGHANDLE_H +# define ARGHANDLE_H + +void wrong_argc(int expected, int argc); +void usage(char *argv0); +int wrong_dimension(char *dim, int max, int actual); +int get_dim(char *type, int default_max); + +#endif diff --git a/include/ft_lib.h b/include/ft_lib.h new file mode 100644 index 0000000..1fbc386 --- /dev/null +++ b/include/ft_lib.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lib.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tischmid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/18 22:35:00 by tischmid #+# #+# */ +/* Updated: 2023/03/18 23:44:07 by tischmid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIB_H +# define FT_LIB_H + +char *ft_strcpy(char *s1, char *s2); +char *ft_strcat(char *s1, char *s2); +void ft_putchar(char c); +void ft_putstr(char *str); +void ft_putnbr(int nb); +int ft_atoi(char *str); + +#endif diff --git a/ft_putchar.c b/include/rush00.h similarity index 62% rename from ft_putchar.c rename to include/rush00.h index 16f7bf8..821c58b 100644 --- a/ft_putchar.c +++ b/include/rush00.h @@ -1,18 +1,19 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_putchar.c :+: :+: :+: */ +/* rush00.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jtorrez- +#+ +:+ +#+ */ +/* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2023/03/18 20:27:12 by jtorrez- #+# #+# */ -/* Updated: 2023/03/18 20:27:13 by jtorrez- ### ########.fr */ +/* Created: 2023/03/18 23:45:47 by tischmid #+# #+# */ +/* Updated: 2023/03/18 23:45:48 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ -#include +#ifndef RUSH00_H +# define RUSH00_H -void ft_putchar(char c) -{ - write(1, &c, 1); -} +void horiz_line(char left, char middle, char right, int width); +void rush(int x, int y) + +#endif diff --git a/main.c b/main.c index fa55b48..427fd90 100644 --- a/main.c +++ b/main.c @@ -6,34 +6,36 @@ /* By: jtorrez- +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/18 20:15:26 by jtorrez- #+# #+# */ -/* Updated: 2023/03/18 20:21:42 by jtorrez- ### ########.fr */ +/* Updated: 2023/03/18 23:45:18 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ -void rush(int x, int y); -int ft_atoi(char *str); +#include +#include "ft_lib.h" +#include "arghandle.h" +#include "rush00.h" int main(int argc, char *argv[]) { - int width; - int height; + int width; + int max_width; + int height; + int max_height; if (argc != 3) + { + wrong_argc(2, argc - 1); + usage(argv[0]); return (1); + } + max_width = get_dim("cols", 200); + max_height = get_dim("lines", 200); width = ft_atoi(argv[1]); height = ft_atoi(argv[2]); + if (width > max_width) + return (wrong_dimension("width", max_width, width)); + else if (height > max_height) + return (wrong_dimension("height", max_height - 3, height)); rush(width, height); return (0); } - -int ft_atoi(char *str) -{ - int i; - int number; - - i = 0; - number = 0; - while (str[i] >= '0' && str[i] <= '9') - number = number * 10 + (str[i++] - '0'); - return (number); -} diff --git a/rush00.c b/rush00.c index 41b6d2e..7ad5476 100644 --- a/rush00.c +++ b/rush00.c @@ -6,12 +6,12 @@ /* By: jtorrez- +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/18 20:15:12 by jtorrez- #+# #+# */ -/* Updated: 2023/03/18 20:26:32 by jtorrez- ### ########.fr */ +/* Updated: 2023/03/18 23:43:58 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ -void ft_putchar(char c); -void horiz_line(char left, char middle, char right, int width); +#include "ft_lib.h" +#include "rush00.h" const char g_inside = ' ';