]> git.dkaiser.de - 42/Philosophers.git/commitdiff
Add ft_utils
authorDominik Kaiser <dkaiser@student.42heilbronn.de>
Fri, 17 Jan 2025 11:06:21 +0000 (12:06 +0100)
committerDominik Kaiser <dkaiser@student.42heilbronn.de>
Fri, 17 Jan 2025 11:06:21 +0000 (12:06 +0100)
philo/include/ft_utils.h [new file with mode: 0644]
philo/include/philo.h [new file with mode: 0644]
philo/src/ft_utils.c [new file with mode: 0644]
philo/src/main.c

diff --git a/philo/include/ft_utils.h b/philo/include/ft_utils.h
new file mode 100644 (file)
index 0000000..64804c2
--- /dev/null
@@ -0,0 +1,26 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_utils.h                                         :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   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       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef FT_UTILS_H
+#define FT_UTILS_H
+
+/*
+** Prints error message and returns EXIT_FAILURE
+*/
+int ft_err(const char *str);
+
+/*
+** Returns integer value from str
+*/
+int ft_atoi(const char *str);
+
+#endif
diff --git a/philo/include/philo.h b/philo/include/philo.h
new file mode 100644 (file)
index 0000000..1ec9c3d
--- /dev/null
@@ -0,0 +1,34 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   philo.h                                            :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/01/17 10:23:19 by dkaiser           #+#    #+#             */
+/*   Updated: 2025/01/17 12:01:01 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef PHILO_H
+# define PHILO_H
+
+# include <unistd.h>
+# include <stdlib.h>
+# include <stdio.h>
+# include <pthread.h>
+# include <sys/time.h>
+# include "ft_utils.h"
+
+# define ERR_USAGE "Usage: <nbr_of_philos> <ttd> <tte> <tts> [times_must_eat]"
+
+typedef struct s_phdata
+{
+    int nbr_of_philos;
+    int time_to_die;
+    int time_to_eat;
+    int time_to_sleep;
+    int times_must_eat;
+} t_phdata;
+
+#endif
diff --git a/philo/src/ft_utils.c b/philo/src/ft_utils.c
new file mode 100644 (file)
index 0000000..9eb660a
--- /dev/null
@@ -0,0 +1,47 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_utils.c                                         :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/01/17 11:57:11 by dkaiser           #+#    #+#             */
+/*   Updated: 2025/01/17 11:59:28 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "ft_utils.h"
+
+int ft_err(const char *str)
+{
+    printf("\e[31m[ERROR] %s\e[0m\n", str);
+    return (EXIT_FAILURE);
+}
+
+int    ft_atoi(const char *str)
+{
+       int     result;
+       int     i;
+       int     posneg;
+
+       posneg = 1;
+       result = 0;
+       i = 0;
+       while ((str[i] >= '\t' && str[i] <= '\r') || str[i] == ' ')
+       {
+               i++;
+       }
+       if (str[i] == '-')
+       {
+               posneg = -1;
+               i++;
+       }
+       else if (str[i] == '+')
+               i++;
+       while (str[i] >= '0' && str[i] <= '9')
+       {
+               result = 10 * result + str[i] - '0';
+               i++;
+       }
+       return (result * posneg);
+}
index bca80b5c35e5b3b7232a2ff180e2085de49b055b..efcc9a583fca3e41028b3a8eb537527c5f1d65f1 100644 (file)
@@ -6,13 +6,27 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/01/14 17:13:30 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/14 17:14:12 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/01/17 11:53:53 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "philo.h"
 
+int ft_err(char *str)
+{
+    printf("\e[31m[ERROR] %s\e[0m\n", str);
+    return (EXIT_FAILURE);
+}
+
 int main(int argc, char *argv[])
 {
-    return 0;
+    t_phdata data;
+    int result;
+
+    if (argc != 5 && argc != 6)
+        return(ft_err(ERR_USAGE));
+    result = load_data(&data, argc, argv);
+    if (result != EXIT_SUCCESS)
+        return (result);
+    return (EXIT_SUCCESS);
 }