aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Kaiser2025-01-17 12:40:20 +0100
committerDominik Kaiser2025-01-17 12:40:20 +0100
commit0d910cd1b5e3b40ef91c4ee5334305530015a181 (patch)
tree413c49ffb1572b593b3b921779e8c965f29b4764
parent987211580c3f92cf33416edf8d71e22daaf9e65e (diff)
downloadPhilosophers-0d910cd1b5e3b40ef91c4ee5334305530015a181.tar.gz
Philosophers-0d910cd1b5e3b40ef91c4ee5334305530015a181.zip
Add loading data from args
-rw-r--r--philo/Makefile2
-rw-r--r--philo/include/ft_utils.h7
-rw-r--r--philo/src/main.c26
3 files changed, 28 insertions, 7 deletions
diff --git a/philo/Makefile b/philo/Makefile
index b3af4fb..1841707 100644
--- a/philo/Makefile
+++ b/philo/Makefile
@@ -9,7 +9,7 @@ CFLAGS = -Wall -Wextra -Werror
HEADERS = -Iinclude
VPATH := src
-SRC := main.c
+SRC := main.c ft_utils.c
OBJ_DIR := _obj
OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o))
diff --git a/philo/include/ft_utils.h b/philo/include/ft_utils.h
index 64804c2..3c9bd69 100644
--- a/philo/include/ft_utils.h
+++ b/philo/include/ft_utils.h
@@ -6,12 +6,15 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/17 11:57:44 by dkaiser #+# #+# */
-/* Updated: 2025/01/17 12:00:11 by dkaiser ### ########.fr */
+/* Updated: 2025/01/17 12:39:17 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_UTILS_H
-#define FT_UTILS_H
+# define FT_UTILS_H
+
+# include <stdlib.h>
+# include <stdio.h>
/*
** Prints error message and returns EXIT_FAILURE
diff --git a/philo/src/main.c b/philo/src/main.c
index efcc9a5..e3b90ab 100644
--- a/philo/src/main.c
+++ b/philo/src/main.c
@@ -6,16 +6,34 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/14 17:13:30 by dkaiser #+# #+# */
-/* Updated: 2025/01/17 11:53:53 by dkaiser ### ########.fr */
+/* Updated: 2025/01/17 12:40:05 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
-int ft_err(char *str)
+
+int load_data(t_phdata *data, int argc, char *argv[])
{
- printf("\e[31m[ERROR] %s\e[0m\n", str);
- return (EXIT_FAILURE);
+ data->nbr_of_philos = ft_atoi(argv[1]);
+ data->time_to_die = ft_atoi(argv[2]);
+ data->time_to_eat = ft_atoi(argv[3]);
+ data->time_to_sleep = ft_atoi(argv[4]);
+ if (argc == 6)
+ data->times_must_eat = ft_atoi(argv[2]);
+ else
+ data->times_must_eat = 0;
+ if (data->nbr_of_philos <= 0)
+ return (ft_err("Must have at least one philosopher"));
+ if (data->time_to_die < 0)
+ return (ft_err("ttd can't be negative"));
+ if (data->time_to_eat < 0)
+ return (ft_err("tte can't be negative"));
+ if (data->time_to_sleep < 0)
+ return (ft_err("tts can't be negative"));
+ if (data->times_must_eat < 0)
+ data->times_must_eat = 0;
+ return (EXIT_SUCCESS);
}
int main(int argc, char *argv[])