From 99f67bde096ad84dad5b41bc779ae2ad2d807e6f Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Wed, 30 Sep 2020 10:35:23 +0200 Subject: Renaming io_* -> event_*, Changed philo_put with only 1 write call --- common/Makefile | 6 ++--- common/args.c | 32 ++++++++++++++++++++++ common/common.c | 49 ---------------------------------- common/common.h | 15 ++++++----- common/helper.c | 49 +--------------------------------- common/io.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 126 insertions(+), 107 deletions(-) create mode 100644 common/args.c delete mode 100644 common/common.c create mode 100644 common/io.c (limited to 'common') diff --git a/common/Makefile b/common/Makefile index 62d75c9..e9c209d 100644 --- a/common/Makefile +++ b/common/Makefile @@ -6,7 +6,7 @@ # By: cacharle +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/02/09 22:39:08 by cacharle #+# #+# # -# Updated: 2020/09/30 08:22:35 by cacharle ### ########.fr # +# Updated: 2020/09/30 10:27:40 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -14,7 +14,7 @@ LIB = ar rcs RM = rm -rf CC = gcc -CCFLAGS = -O2 -Wall -Wextra -Werror +CCFLAGS = -O2 -Wall -Wextra #-Werror NAME = libphilocommon.a @@ -33,6 +33,6 @@ clean: $(RM) $(OBJ) $(RM) $(NAME) -re: fclean all +re: clean all .PHONY: all clean re diff --git a/common/args.c b/common/args.c new file mode 100644 index 0000000..6392f22 --- /dev/null +++ b/common/args.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ +/* Updated: 2020/09/30 10:14:24 by cacharle ### ########.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/common.c b/common/common.c deleted file mode 100644 index 62fd382..0000000 --- a/common/common.c +++ /dev/null @@ -1,49 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* common.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ -/* Updated: 2020/09/30 07:56:12 by cacharle ### ########.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); -} - -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_EAT) - h_putstr(" is eating\n"); - else if (event == EVENT_SLEEP) - h_putstr(" is sleeping\n"); - else if (event == EVENT_THINK) - h_putstr(" is thinking\n"); - else if (event == EVENT_DIE) - h_putstr(" died\n"); -} diff --git a/common/common.h b/common/common.h index 0b64e92..cb71172 100644 --- a/common/common.h +++ b/common/common.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */ -/* Updated: 2020/09/30 08:40:38 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 10:15:10 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,22 +43,23 @@ typedef struct typedef void *(*t_routine)(void *arg); /* -** common.c +** args.c */ 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_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); t_time h_time_now(void); + +/* +** io.c +*/ + +void philo_put(size_t id, t_philo_event event); int h_err(int ret, const char *format, char *str); #endif diff --git a/common/helper.c b/common/helper.c index 61b85a1..b2107ae 100644 --- a/common/helper.c +++ b/common/helper.c @@ -6,22 +6,12 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ -/* Updated: 2020/09/30 07:57:21 by cacharle ### ########.fr */ +/* Updated: 2020/09/30 10:15:14 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "common.h" -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; @@ -46,43 +36,6 @@ long int h_atou_strict(char *s) return (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) -{ - write(STDOUT_FILENO, &c, 1); -} - -void h_putstr(char *s) -{ - write(STDOUT_FILENO, s, h_strlen(s)); -} - -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, 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) { struct timeval tv; diff --git a/common/io.c b/common/io.c new file mode 100644 index 0000000..7989575 --- /dev/null +++ b/common/io.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* io.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */ +/* Updated: 2020/09/30 10:32:41 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +size_t st_strlen(char *s) +{ + int 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'; + dst[1] = '\0'; + return dst + 1; +} + +static void st_strcat(char *dst, char *str) +{ + while (*dst != '\0') + dst++; + while (*str != '\0') + *dst++ = *str++; + *dst = '\0'; +} + +void philo_put(size_t id, t_philo_event event) +{ + static char buf[2048]; + + buf[0] = '\0'; + st_nbrcpy(buf, h_time_now()); + st_strcat(buf, " "); + st_nbrcpy(buf + st_strlen(buf), id); + if (event == EVENT_FORK) + st_strcat(buf, " has taken fork\n"); + else if (event == EVENT_EAT) + st_strcat(buf, " is eating\n"); + else if (event == EVENT_SLEEP) + st_strcat(buf, " is sleeping\n"); + else if (event == EVENT_THINK) + st_strcat(buf, " is thinking\n"); + else if (event == EVENT_DIE) + st_strcat(buf, " died\n"); + write(STDOUT_FILENO, buf, st_strlen(buf)); +} + +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