diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2021-01-10 11:13:54 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2021-01-10 11:13:54 +0100 |
| commit | ef97ae4e1659da4ef1a02505730d2ecf2389e608 (patch) | |
| tree | 23dcb59d3cd90c8169f52f2086ad626aa49514fa /common | |
| parent | eb2cfb574efafcf2c3c6200d1cd2de700ec8ddfb (diff) | |
| download | philosophers-ef97ae4e1659da4ef1a02505730d2ecf2389e608.tar.gz philosophers-ef97ae4e1659da4ef1a02505730d2ecf2389e608.tar.bz2 philosophers-ef97ae4e1659da4ef1a02505730d2ecf2389e608.zip | |
Refactoring common lib functions to expose only those we need
Diffstat (limited to 'common')
| -rw-r--r-- | common/inc/common.h | 8 | ||||
| -rw-r--r-- | common/src/args.c | 63 | ||||
| -rw-r--r-- | common/src/helper.c | 58 | ||||
| -rw-r--r-- | common/src/io.c | 62 |
4 files changed, 92 insertions, 99 deletions
diff --git a/common/inc/common.h b/common/inc/common.h index 56fb284..c6beb0a 100644 --- a/common/inc/common.h +++ b/common/inc/common.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */ -/* Updated: 2021/01/10 10:32:17 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 11:11:23 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,12 +53,8 @@ bool parse_args(t_philo_args *args, int argc, char **argv); ** helper.c */ -long int h_atou_strict(char *s); t_time h_time_now(void); void h_sleep(t_time sleep_time); -char *h_nbrcpy(char *dst, long long int num); -char *h_strcpy_end(char *dst, const char *str); -const char *philo_sem_eat_name(const char *prefix, long int id); /* ** io.c @@ -67,6 +63,6 @@ const char *philo_sem_eat_name(const char *prefix, long int id); void philo_put( size_t id, t_philo_event event, t_time initial_time); void philo_put_flush(void); -int h_err(int ret, const char *format, char *str); +const char *philo_sem_eat_name(const char *prefix, long int id); #endif diff --git a/common/src/args.c b/common/src/args.c index 2070e46..333852f 100644 --- a/common/src/args.c +++ b/common/src/args.c @@ -6,27 +6,74 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ -/* Updated: 2020/12/30 13:44:54 by charles ### ########.fr */ +/* Updated: 2021/01/10 11:12:26 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "common.h" -bool parse_args(t_philo_args *args, int argc, char **argv) +static int st_err(int ret, const char *format, char *str) +{ + while (*format != '\0') + { + if (format[0] == '%' && format[1] == 's') + { + if (str != NULL) + { + while (*str != '\0') + write(STDERR_FILENO, str++, 1); + } + format += 2; + } + else + { + write(STDERR_FILENO, format, 1); + format++; + } + } + write(STDERR_FILENO, "\n", 1); + return (ret); +} + +static long int st_atou_strict(char *s) +{ + long int num; + char *origin; + + origin = s; + if (*s < '0' || *s > '9') + return (st_err(-1, "Error: %s: is not a number", origin)); + num = 0; + while (*s >= '0' && *s <= '9') + { + num *= 10; + if (num > UINT_MAX) + return (st_err(-1, "Error: %s: is too big", origin)); + num += *s - '0'; + if (num > UINT_MAX) + return (st_err(-1, "Error: %s: is too big", origin)); + s++; + } + if (*s != '\0') + return (st_err(-1, "Error: %s: is not a number", origin)); + return (num); +} + +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" + return (st_err(false, "Usage: %s philosophers_num deatst_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) + if ((args->philo_num = st_atou_strict(argv[1])) == -1 + || (args->timeout_death = st_atou_strict(argv[2])) == -1 + || (args->timeout_eat = st_atou_strict(argv[3])) == -1 + || (args->timeout_sleep = st_atou_strict(argv[4])) == -1) return (false); if (argc == 6) { - if ((args->meal_num = h_atou_strict(argv[5])) == -1) + if ((args->meal_num = st_atou_strict(argv[5])) == -1) return (false); } else diff --git a/common/src/helper.c b/common/src/helper.c index 53c24c4..3515cfa 100644 --- a/common/src/helper.c +++ b/common/src/helper.c @@ -6,36 +6,12 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ -/* Updated: 2021/01/10 10:32:06 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 11:10:45 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) @@ -49,6 +25,10 @@ t_time h_time_now(void) return (tv.tv_sec * 1000 + tv.tv_usec / 1000); } +/* +** usleep is not precise and adds delay if we do one big usleep +*/ + void h_sleep(t_time sleep_time) { t_time start; @@ -57,31 +37,3 @@ void h_sleep(t_time sleep_time) while (h_time_now() - start < sleep_time) usleep(500); } - -char *h_nbrcpy(char *dst, long long int num) -{ - if (num > 9) - dst = h_nbrcpy(dst, num / 10); - dst[0] = num % 10 + '0'; - return (dst + 1); -} - -char *h_strcpy_end(char *dst, const char *str) -{ - while (*str != '\0') - *dst++ = *str++; - return (dst); -} - -#define PHILO_SEM_EAT_BUF_SIZE 2048 - -const char *philo_sem_eat_name(const char *prefix, long int id) -{ - static char buf[PHILO_SEM_EAT_BUF_SIZE]; - char *end; - - buf[0] = '\0'; - end = h_strcpy_end(buf, prefix); - h_nbrcpy(end, id); - return (buf); -} diff --git a/common/src/io.c b/common/src/io.c index abb26b7..9b8e5af 100644 --- a/common/src/io.c +++ b/common/src/io.c @@ -6,20 +6,25 @@ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/30 10:03:53 by cacharle #+# #+# */ -/* Updated: 2021/01/10 10:12:23 by cacharle ### ########.fr */ +/* Updated: 2021/01/10 11:06:51 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "common.h" -static size_t st_strlen(char *s) +static char *st_nbrcpy(char *dst, long long int num) { - size_t counter; + if (num > 9) + dst = st_nbrcpy(dst, num / 10); + dst[0] = num % 10 + '0'; + return (dst + 1); +} - counter = 0; - while (s[counter]) - counter++; - return (counter); +static char *st_strcpy_end(char *dst, const char *str) +{ + while (*str != '\0') + *dst++ = *str++; + return (dst); } #define PHILO_PUT_BUF_SIZE 20000 @@ -29,19 +34,19 @@ static char *g_curr = g_buf; void philo_put(size_t id, t_philo_event event, t_time initial_time) { - g_curr = h_nbrcpy(g_curr, h_time_now() - initial_time); - g_curr = h_strcpy_end(g_curr, " "); - g_curr = h_nbrcpy(g_curr, id); + 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 = h_strcpy_end(g_curr, " has taken fork\n"); + g_curr = st_strcpy_end(g_curr, " has taken fork\n"); else if (event == EVENT_EAT) - g_curr = h_strcpy_end(g_curr, " is eating\n"); + g_curr = st_strcpy_end(g_curr, " is eating\n"); else if (event == EVENT_SLEEP) - g_curr = h_strcpy_end(g_curr, " is sleeping\n"); + g_curr = st_strcpy_end(g_curr, " is sleeping\n"); else if (event == EVENT_THINK) - g_curr = h_strcpy_end(g_curr, " is thinking\n"); + g_curr = st_strcpy_end(g_curr, " is thinking\n"); else if (event == EVENT_DIE) - g_curr = h_strcpy_end(g_curr, " died\n"); + g_curr = st_strcpy_end(g_curr, " died\n"); if (g_curr - g_buf > PHILO_PUT_BUF_SIZE) philo_put_flush(); } @@ -53,22 +58,15 @@ void philo_put_flush(void) g_buf[0] = '\0'; } -int h_err(int ret, const char *format, char *str) +#define PHILO_SEM_EAT_BUF_SIZE 2048 + +const char *philo_sem_eat_name(const char *prefix, long int id) { - 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); + static char buf[PHILO_SEM_EAT_BUF_SIZE]; + char *end; + + buf[0] = '\0'; + end = st_strcpy_end(buf, prefix); + st_nbrcpy(end, id); + return (buf); } |
