aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-09-30 07:59:27 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-09-30 07:59:27 +0200
commitd7aea773431926cefb6430b948329da1662d1dee (patch)
tree984fd10acf619ee8da84e7111742399a55a02770 /common
parentb6f4db572d7d40c178ec286373422faa2172f135 (diff)
downloadphilosophers-d7aea773431926cefb6430b948329da1662d1dee.tar.gz
philosophers-d7aea773431926cefb6430b948329da1662d1dee.tar.bz2
philosophers-d7aea773431926cefb6430b948329da1662d1dee.zip
Refactoring common lib
Diffstat (limited to 'common')
-rw-r--r--common/common.c34
-rw-r--r--common/common.h55
-rw-r--r--common/helper.c81
-rw-r--r--common/philo.c37
4 files changed, 76 insertions, 131 deletions
diff --git a/common/common.c b/common/common.c
index face0b5..62fd382 100644
--- a/common/common.c
+++ b/common/common.c
@@ -6,44 +6,44 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */
-/* Updated: 2020/02/15 00:54:03 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 07:56:12 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "common.h"
-t_bool parse_args(t_philo_args *philo_args, int argc, char **argv)
+bool parse_args(t_philo_args *args, int argc, char **argv)
{
if (argc != 5 && argc != 6)
- return (FALSE);
- if ((philo_args->philo_num = h_strtoposint(argv[1])) == -1 ||
- (philo_args->timeout_death = h_strtoposint(argv[2])) == -1 ||
- (philo_args->timeout_eat = h_strtoposint(argv[3])) == -1 ||
- (philo_args->timeout_sleep = h_strtoposint(argv[4])) == -1)
- return (FALSE);
+ return h_err(false, "Usage: %s philosophers_num death_timeout eat_timeout sleep_timeout [meal_num]", argv[0]);
+ if ((args->philo_num = h_atou_strict(argv[1])) == -1
+ || (args->timeout_death = h_atou_strict(argv[2])) == -1
+ || (args->timeout_eat = h_atou_strict(argv[3])) == -1
+ || (args->timeout_sleep = h_atou_strict(argv[4])) == -1)
+ return (false);
if (argc == 6)
{
- if ((philo_args->meal_num = h_strtoposint(argv[5])) == -1)
- return (FALSE);
+ if ((args->meal_num = h_atou_strict(argv[5])) == -1)
+ return (false);
}
else
- philo_args->meal_num = -1;
- return (TRUE);
+ args->meal_num = 1;
+ return (true);
}
-void philo_put_state_change(int id, t_philo_event event) // not correct for philo3
+void philo_put(size_t id, t_philo_event event)
{
h_putnbr(h_time_now());
h_putchar(' ');
h_putnbr(id);
if (event == EVENT_FORK)
h_putstr(" has taken fork\n");
- else if (event == EVENT_EATING)
+ else if (event == EVENT_EAT)
h_putstr(" is eating\n");
- else if (event == EVENT_SLEEPING)
+ else if (event == EVENT_SLEEP)
h_putstr(" is sleeping\n");
- else if (event == EVENT_THINKING)
+ else if (event == EVENT_THINK)
h_putstr(" is thinking\n");
- else if (event == EVENT_DIED)
+ else if (event == EVENT_DIE)
h_putstr(" died\n");
}
diff --git a/common/common.h b/common/common.h
index a838750..b6f21c2 100644
--- a/common/common.h
+++ b/common/common.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */
-/* Updated: 2020/02/14 20:45:33 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 07:57:32 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,73 +15,50 @@
# include <unistd.h>
# include <stdlib.h>
+# include <stdbool.h>
# include <pthread.h>
# include <sys/time.h>
+# include <limits.h>
-# define FALSE 0
-# define TRUE 1
-
-typedef int t_bool;
typedef long int t_time;
typedef enum
{
- PSTATE_EATING = 0,
- PSTATE_SLEEPING,
- PSTATE_THINKING,
- PSTATE_NUM
-} t_philo_state;
-
-typedef enum
-{
EVENT_FORK,
- EVENT_EATING,
- EVENT_SLEEPING,
- EVENT_THINKING,
- EVENT_DIED
-} t_philo_event;
+ EVENT_EAT,
+ EVENT_SLEEP,
+ EVENT_THINK,
+ EVENT_DIE
+} t_philo_event;
typedef struct
{
- int philo_num;
+ long int philo_num;
t_time timeout_death;
t_time timeout_eat;
t_time timeout_sleep;
- int meal_num;
- t_bool all_alive;
- pthread_mutex_t mutex_stdout;
- pthread_mutex_t mutex_all_alive;
+ long int meal_num;
} t_philo_args;
-typedef void (*t_philo_routine)(void *arg);
+typedef void (*t_routine)(void *arg);
/*
** common.c
*/
-t_bool parse_args(t_philo_args *philo_args, int argc, char **argv);
-void philo_put_state_change(int id, t_philo_event event);
-
-/*
-** philo.c
-*/
-
-void philo_eat(int id, t_time timeout);
-void philo_sleep(int id, t_time timeout);
-void philo_think(int id);
-void philo_die(int id);
+bool parse_args(t_philo_args *args, int argc, char **argv);
+void philo_put(size_t id, t_philo_event event);
/*
** helper.c
*/
-long int h_strtoposint(char *s);
-int h_strlen(char *s);
+long int h_atou_strict(char *s);
+size_t h_strlen(char *s);
void h_putnbr(unsigned long num);
void h_putchar(char c);
void h_putstr(char *s);
-void *h_calloc(int count, int size);
-t_time h_timeval_to_time(struct timeval *tp);
t_time h_time_now(void);
+int h_err(int ret, const char *format, char *str);
#endif
diff --git a/common/helper.c b/common/helper.c
index 7799a0c..61b85a1 100644
--- a/common/helper.c
+++ b/common/helper.c
@@ -6,83 +6,88 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */
-/* Updated: 2020/02/15 00:53:47 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 07:57:21 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "common.h"
-long int h_strtoposint(char *s)
+size_t h_strlen(char *s)
+{
+ int counter;
+
+ counter = 0;
+ while (s[counter])
+ counter++;
+ return (counter);
+}
+
+long int h_atou_strict(char *s)
{
long int num;
+ char *origin;
+ origin = s;
if (*s < '0' || *s > '9')
- return (-1);
+ return (h_err(-1, "Error: %s: is not a number", origin));
num = 0;
while (*s >= '0' && *s <= '9')
{
num *= 10;
+ if (num > UINT_MAX)
+ return (h_err(-1, "Error: %s: is too big", origin));
num += *s - '0';
+ if (num > UINT_MAX)
+ return (h_err(-1, "Error: %s: is too big", origin));
s++;
}
if (*s != '\0')
- return (-1);
- return num;
+ return (h_err(-1, "Error: %s: is not a number", origin));
+ return (num);
}
-int h_strlen(char *s)
-{
- int counter;
-
- counter = 0;
- while (s[counter])
- counter++;
- return (counter);
-}
-
-void h_putnbr(unsigned long int num)
+void h_putnbr(unsigned long int num)
{
if (num > 9)
h_putnbr(num / 10);
h_putchar(num % 10 + '0');
}
-void h_putchar(char c)
+void h_putchar(char c)
{
write(STDOUT_FILENO, &c, 1);
}
-void h_putstr(char *s)
+void h_putstr(char *s)
{
write(STDOUT_FILENO, s, h_strlen(s));
}
-void *h_calloc(int count, int size)
+int h_err(int ret, const char *format, char *str)
{
- int i;
- void *ptr;
-
- if ((ptr = malloc(count * size)) == NULL)
- return (NULL);
- i = count * size;
- while (i-- > 0)
- ((unsigned char*)ptr)[i] = 0x0;
- return (ptr);
-}
-
-t_time h_timeval_to_time(struct timeval *tp)
-{
- t_time t;
-
- t = tp->tv_sec * 1000 + tp->tv_usec / 1000;
- return (t);
+ while (*format != '\0')
+ {
+ if (format[0] == '%' && format[1] == 's')
+ {
+ if (str != NULL)
+ write(STDERR_FILENO, str, h_strlen(str));
+ format += 2;
+ }
+ else
+ {
+ write(STDERR_FILENO, format, 1);
+ format++;
+ }
+ }
+ write(STDERR_FILENO, "\n", 1);
+ return (ret);
}
-t_time h_time_now(void)
+t_time h_time_now(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1)
return (-1);
- return (h_timeval_to_time(&tv));
+ return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
}
diff --git a/common/philo.c b/common/philo.c
deleted file mode 100644
index d342802..0000000
--- a/common/philo.c
+++ /dev/null
@@ -1,37 +0,0 @@
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* philo.c :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* Created: 2020/02/09 01:54:53 by cacharle #+# #+# */
-/* Updated: 2020/02/14 21:21:02 by cacharle ### ########.fr */
-/* */
-/* ************************************************************************** */
-
-#include "common.h"
-
-void philo_eat(int id, t_time timeout)
-{
- philo_put_state_change(id, EVENT_EATING);
- usleep(timeout * 1000);
-}
-
-void philo_sleep(int id, t_time timeout)
-{
- philo_put_state_change(id, EVENT_SLEEPING);
- usleep(timeout * 1000);
-}
-
-void philo_think(int id)
-{
- philo_put_state_change(id, EVENT_THINKING);
-}
-
-void philo_die(int id)
-{
- philo_put_state_change(id, EVENT_DIED);
-}
-
-