aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/Makefile6
-rw-r--r--common/args.c (renamed from common/common.c)21
-rw-r--r--common/common.h15
-rw-r--r--common/helper.c49
-rw-r--r--common/io.c82
5 files changed, 96 insertions, 77 deletions
diff --git a/common/Makefile b/common/Makefile
index 62d75c9..e9c209d 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -6,7 +6,7 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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/common.c b/common/args.c
index 62fd382..6392f22 100644
--- a/common/common.c
+++ b/common/args.c
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* common.c :+: :+: :+: */
+/* args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */
-/* Updated: 2020/09/30 07:56:12 by cacharle ### ########.fr */
+/* Updated: 2020/09/30 10:14:24 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,20 +30,3 @@ bool parse_args(t_philo_args *args, int argc, char **argv)
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 <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}