aboutsummaryrefslogtreecommitdiff
path: root/philo/src/main.c
blob: ae0e54be5f73ebaff495fba8c9402ce910ebdf6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   main.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/01/14 17:13:30 by dkaiser           #+#    #+#             */
/*   Updated: 2025/01/28 13:20:47 by dkaiser          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "philo.h"

int	load_data(t_phdata *data, int argc, char *argv[])
{
	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[5]);
		if (data->times_must_eat < 1)
			return (ft_err("times_must_eat can't be negative or zero"));
	}
	else
		data->times_must_eat = -1;
	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"));
	data->philos_must_eat = data->nbr_of_philos;
	return (EXIT_SUCCESS);
}

int	main(int argc, char *argv[])
{
	t_phdata	data;
	t_philo		*philos;
	int			result;

	if (argc != 5 && argc != 6)
		return (ft_err(ERR_USAGE));
	result = load_data(&data, argc, argv);
	if (result != EXIT_SUCCESS)
		return (result);
	result = init(&philos, &data);
	if (result != EXIT_SUCCESS)
		return (result);
	data.simulation_running = 1;
	result = run_simulation(data.nbr_of_philos, philos, &data);
	free(philos);
	free(data.forks);
	return (result);
}