Create header files and good Makefile

This commit is contained in:
Timo Schmidt 2023-03-18 23:54:50 +01:00
parent 22d33c9359
commit 3aa6f93b42
7 changed files with 225 additions and 29 deletions

60
arghandle.c Normal file
View File

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* arghandle.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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");
}

89
ft_lib.c Normal file
View File

@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lib.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/18 22:33:38 by tischmid #+# #+# */
/* Updated: 2023/03/18 23:35:30 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#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);
}

21
include/arghandle.h Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* arghandle.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

23
include/ft_lib.h Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lib.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View File

@ -1,18 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar.c :+: :+: :+: */
/* rush00.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jtorrez- <marvin@42.fr> +#+ +:+ +#+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <unistd.h>
#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

36
main.c
View File

@ -6,34 +6,36 @@
/* By: jtorrez- <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdio.h>
#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);
}

View File

@ -6,12 +6,12 @@
/* By: jtorrez- <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 = ' ';