From: Lukas Jiriste Date: Thu, 28 Mar 2024 10:40:14 +0000 (+0100) Subject: Implement conditional compilation X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=d9f489d226da5cf5a5985ef841fa6ef11a51c468;p=42%2Fphilosophers.git Implement conditional compilation Before this commit, compilation would stop at file parsing.c with error (warning + -Werror) caused by -Wtype-limits. Conditional compilation is added so that the offending if statement is not present, when not needed. The #if directive could have only been set up around the problematic if statement. The 42 Norm however forbids using macros anywhere but the global scope. This made me write the the affected functions twice, which then broke the 5 function limit per .c file. Hence this commit is quite large for such a small change. --- diff --git a/philo/Makefile b/philo/Makefile index 5d5094c..33a9055 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -9,6 +9,8 @@ SRCDIR := . SRCS := main.c \ parsing.c \ parsing_misc.c \ + pars_arg.c \ + inner_pars_arg.c \ mem_management.c \ mutex.c \ diff --git a/philo/inner_pars_arg.c b/philo/inner_pars_arg.c new file mode 100644 index 0000000..ac1b753 --- /dev/null +++ b/philo/inner_pars_arg.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* inner_pars_arg.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/28 11:35:59 by ljiriste #+# #+# */ +/* Updated: 2024/03/28 11:36:24 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" +#include + +// Maybe this should be changed so that it only changes +// res when the parse is successful? +int inner_parse_arg(t_big_uint *res, const char *arg) +{ + char *tmp; + + *res = ft_atobui(arg); + tmp = ft_buitoa(*res); + if (!same_number(tmp, arg)) + { + free(tmp); + return (1); + } + free(tmp); + return (0); +} diff --git a/philo/pars_arg.c b/philo/pars_arg.c new file mode 100644 index 0000000..8c44809 --- /dev/null +++ b/philo/pars_arg.c @@ -0,0 +1,83 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pars_arg.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/28 11:29:47 by ljiriste #+# #+# */ +/* Updated: 2024/03/28 11:36:24 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" +#include +#include + +int parse_arg_usec(useconds_t *res, const char *arg) +{ + t_big_uint tmp; + + if (inner_parse_arg(&tmp, arg)) + return (1); + if (tmp > (useconds_t)-1) + return (1); + *res = (useconds_t)tmp; + return (0); +} + +#if SIZE_MAX > UINT_MAX + +int parse_arg_size(size_t *res, const char *arg) +{ + t_big_uint tmp; + + if (inner_parse_arg(&tmp, arg)) + return (1); + if (tmp > (size_t)-1) + return (1); + *res = (size_t)tmp; + return (0); +} + +#else + +int parse_arg_size(size_t *res, const char *arg) +{ + t_big_uint tmp; + + if (inner_parse_arg(&tmp, arg)) + return (1); + *res = (size_t)tmp; + return (0); +} + +#endif //SIZE_MAX > UINT_MAX + +#if UINT_MAX > SIZE_MAX + +int parse_arg_uint(unsigned int *res, const char *arg) +{ + t_big_uint tmp; + + if (inner_parse_arg(&tmp, arg)) + return (1); + if (tmp > (unsigned int)-1) + return (1); + *res = (unsigned int)tmp; + return (0); +} + +#else + +int parse_arg_uint(unsigned int *res, const char *arg) +{ + t_big_uint tmp; + + if (inner_parse_arg(&tmp, arg)) + return (1); + *res = (unsigned int)tmp; + return (0); +} + +#endif //UINT_MAX > SIZE_MAX diff --git a/philo/parsing.c b/philo/parsing.c index 4e7575b..1c95776 100644 --- a/philo/parsing.c +++ b/philo/parsing.c @@ -6,67 +6,15 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/26 09:31:35 by ljiriste #+# #+# */ -/* Updated: 2024/03/28 09:32:31 by ljiriste ### ########.fr */ +/* Updated: 2024/03/28 11:32:40 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ +#include "parsing.h" #include "philo.h" #include #include -// Maybe this should be changed so that it only changes -// res when the parse is successful? -static int inner_parse_arg(t_big_uint *res, const char *arg) -{ - char *tmp; - - *res = ft_atobui(arg); - tmp = ft_buitoa(*res); - if (!same_number(tmp, arg)) - { - free(tmp); - return (1); - } - free(tmp); - return (0); -} - -static int parse_arg_size(size_t *res, const char *arg) -{ - t_big_uint tmp; - - if (inner_parse_arg(&tmp, arg)) - return (1); - if (tmp > (size_t)-1) - return (1); - *res = (size_t)tmp; - return (0); -} - -static int parse_arg_usec(useconds_t *res, const char *arg) -{ - t_big_uint tmp; - - if (inner_parse_arg(&tmp, arg)) - return (1); - if (tmp > (useconds_t)-1) - return (1); - *res = (useconds_t)tmp; - return (0); -} - -static int parse_arg_uint(unsigned int *res, const char *arg) -{ - t_big_uint tmp; - - if (inner_parse_arg(&tmp, arg)) - return (1); - if (tmp > (unsigned int)-1) - return (1); - *res = (unsigned int)tmp; - return (0); -} - int parse_input(t_settings *settings, int argc, char **argv) { if (argc != 5 && argc != 6) diff --git a/philo/parsing.h b/philo/parsing.h new file mode 100644 index 0000000..727331e --- /dev/null +++ b/philo/parsing.h @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/28 11:24:41 by ljiriste #+# #+# */ +/* Updated: 2024/03/28 11:40:04 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PARSING_H +# define PARSING_H + +# include +# include +# include + +// This done to make parsing easier/non-repetitive +// but it cannot be a part of parsing.c as more files depend on it +// +// This assumes useconds_t is not the biggest type +// The assumption is made because it is not easy to check +# if SIZE_MAX > UINT_MAX + +typedef size_t t_big_uint; + +# else + +typedef unsigned int t_big_uint; + +# endif //SIZE_MAX > UINT_MAX + +t_big_uint ft_atobui(const char *str); +char *ft_buitoa(t_big_uint num); +int same_number(const char *num1, const char *num2); + +int inner_parse_arg(t_big_uint *res, const char *arg); + +int parse_arg_usec(useconds_t *res, const char *arg); +int parse_arg_size(size_t *res, const char *arg); +int parse_arg_uint(unsigned int *res, const char *arg); + +#endif //PARSING_H diff --git a/philo/parsing_misc.c b/philo/parsing_misc.c index 64f136e..569b146 100644 --- a/philo/parsing_misc.c +++ b/philo/parsing_misc.c @@ -6,11 +6,11 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/26 10:42:02 by ljiriste #+# #+# */ -/* Updated: 2024/03/28 09:31:14 by ljiriste ### ########.fr */ +/* Updated: 2024/03/28 11:28:12 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ -#include "philo.h" +#include "parsing.h" #include t_big_uint ft_atobui(const char *str) diff --git a/philo/philo.h b/philo/philo.h index f9f47d4..8076801 100644 --- a/philo/philo.h +++ b/philo/philo.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/22 11:10:17 by ljiriste #+# #+# */ -/* Updated: 2024/03/28 10:50:26 by ljiriste ### ########.fr */ +/* Updated: 2024/03/28 11:27:15 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,23 +16,8 @@ # include # include # include -# include # include -// This done to make parsing easier/non-repetitive -// but it cannot be a part of parsing.c as more files depend on it -// May it should have its own header file? -// -// This assumes useconds_t is not the biggest type -// The assumption is made because it is not easy to check -# if SIZE_MAX > UINT_MAX - -typedef size_t t_big_uint; -# else - -typedef unsigned int t_big_uint; -# endif //SIZE_MAX > UINT_MAX - typedef struct s_mutex { int is_init; @@ -74,9 +59,6 @@ typedef struct s_diner } t_diner; int parse_input(t_settings *settings, int argc, char **argv); -t_big_uint ft_atobui(const char *str); -char *ft_buitoa(t_big_uint num); -int same_number(const char *num1, const char *num2); int init(t_diner *diner, pthread_t **threads); void cleanup(t_diner *diner, pthread_t *threads);