Implement conditional compilation
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 28 Mar 2024 10:40:14 +0000 (11:40 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 28 Mar 2024 10:40:14 +0000 (11:40 +0100)
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.

philo/Makefile
philo/inner_pars_arg.c [new file with mode: 0644]
philo/pars_arg.c [new file with mode: 0644]
philo/parsing.c
philo/parsing.h [new file with mode: 0644]
philo/parsing_misc.c
philo/philo.h

index 5d5094c1a67366eb7e9292b7a4f07c5f5bca99bf..33a9055cb812e11db846f628e1a3a111cc118179 100644 (file)
@@ -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 (file)
index 0000000..ac1b753
--- /dev/null
@@ -0,0 +1,31 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   inner_pars_arg.c                                   :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/28 11:35:59 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/03/28 11:36:24 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "parsing.h"
+#include <stdlib.h>
+
+//     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 (file)
index 0000000..8c44809
--- /dev/null
@@ -0,0 +1,83 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   pars_arg.c                                         :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/28 11:29:47 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/03/28 11:36:24 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "parsing.h"
+#include <stddef.h>
+#include <stdlib.h>
+
+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
index 4e7575b9fec333bbb3ac71b732d39a31f34f13fa..1c95776ee441c7d7ebc511a93bb287ca8dfb6922 100644 (file)
@@ -6,67 +6,15 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 <stddef.h>
 #include <stdlib.h>
 
-//     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 (file)
index 0000000..727331e
--- /dev/null
@@ -0,0 +1,45 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   parsing.h                                          :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   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 <limits.h>
+# include <stddef.h>
+# include <unistd.h>
+
+//     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
index 64f136eb4c006fdf910ec1fe1f96e9b74ee35d79..569b146c76057f5b64c7e507718e8eb5a198f430 100644 (file)
@@ -6,11 +6,11 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 <stdlib.h>
 
 t_big_uint     ft_atobui(const char *str)
index f9f47d4ceaf0e13a9048198be83a6219d50a5572..8076801b1ea98f528d2896ecac9c847327e60f82 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
 # include <stddef.h>
 # include <pthread.h>
 # include <sys/time.h>
-# include <limits.h>
 # include <unistd.h>
 
-//     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);