From ad7ed73c124c8fcda6629350307f0f4f41b87fe3 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Fri, 8 Jan 2021 14:35:53 +0100 Subject: Updated Makefile, restructuring common files --- common/src/args.c | 35 +++++++++++++++++++++ common/src/helper.c | 59 +++++++++++++++++++++++++++++++++++ common/src/io.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+) create mode 100644 common/src/args.c create mode 100644 common/src/helper.c create mode 100644 common/src/io.c (limited to 'common/src') diff --git a/common/src/args.c b/common/src/args.c new file mode 100644 index 0000000..2070e46 --- /dev/null +++ b/common/src/args.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ +/* Updated: 2020/12/30 13:44:54 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +bool parse_args(t_philo_args *args, int argc, char **argv) +{ + if (argc != 5 && argc != 6) + { + 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 ((args->meal_num = h_atou_strict(argv[5])) == -1) + return (false); + } + else + args->meal_num = -1; + return (true); +} diff --git a/common/src/helper.c b/common/src/helper.c new file mode 100644 index 0000000..a9d0652 --- /dev/null +++ b/common/src/helper.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ +/* Updated: 2021/01/03 16:55:42 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +long int h_atou_strict(char *s) +{ + long int num; + char *origin; + + origin = s; + if (*s < '0' || *s > '9') + 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 (h_err(-1, "Error: %s: is not a number", origin)); + return (num); +} + +/* +** No need to check error of gettimeofday +** (only for settimeofday and passed pointer validity) +*/ + +t_time h_time_now(void) +{ + struct timeval tv; + + gettimeofday(&tv, NULL); + return (tv.tv_sec * 1000 + tv.tv_usec / 1000); +} + +void h_sleep(t_time sleep_time) +{ + t_time start; + + start = h_time_now(); + while (h_time_now() - start < sleep_time) + usleep(500); +} diff --git a/common/src/io.c b/common/src/io.c new file mode 100644 index 0000000..c01accd --- /dev/null +++ b/common/src/io.c @@ -0,0 +1,90 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* io.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */ +/* Updated: 2021/01/04 12:10:14 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +static size_t st_strlen(char *s) +{ + size_t counter; + + counter = 0; + while (s[counter]) + counter++; + return (counter); +} + +static char *st_nbrcpy(char *dst, long long int num) +{ + if (num > 9) + dst = st_nbrcpy(dst, num / 10); + dst[0] = num % 10 + '0'; + return (dst + 1); +} + +static char *st_strcpy_end(char *dst, char *str) +{ + while (*str != '\0') + *dst++ = *str++; + return (dst); +} + +#define PHILO_PUT_BUF_SIZE 4048 + +static char g_buf[PHILO_PUT_BUF_SIZE + 256] = {'\0'}; +static char *g_curr = g_buf; + +void philo_put(size_t id, t_philo_event event, t_time initial_time) +{ + + g_curr = st_nbrcpy(g_curr, h_time_now() - initial_time); + g_curr = st_strcpy_end(g_curr, " "); + g_curr = st_nbrcpy(g_curr, id); + if (event == EVENT_FORK) + g_curr = st_strcpy_end(g_curr, " has taken fork\n"); + else if (event == EVENT_EAT) + g_curr = st_strcpy_end(g_curr, " is eating\n"); + else if (event == EVENT_SLEEP) + g_curr = st_strcpy_end(g_curr, " is sleeping\n"); + else if (event == EVENT_THINK) + g_curr = st_strcpy_end(g_curr, " is thinking\n"); + else if (event == EVENT_DIE) + g_curr = st_strcpy_end(g_curr, " died\n"); + if (g_curr - g_buf > PHILO_PUT_BUF_SIZE) + philo_put_flush(); +} + +void philo_put_flush(void) +{ + write(STDOUT_FILENO, g_buf, g_curr - g_buf); + g_curr = g_buf; + g_buf[0] = '\0'; +} + +int h_err(int ret, const char *format, char *str) +{ + while (*format != '\0') + { + if (format[0] == '%' && format[1] == 's') + { + if (str != NULL) + write(STDERR_FILENO, str, st_strlen(str)); + format += 2; + } + else + { + write(STDERR_FILENO, format, 1); + format++; + } + } + write(STDERR_FILENO, "\n", 1); + return (ret); +} -- cgit