Replace string literals with global string consts
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 19 Mar 2024 13:20:48 +0000 (14:20 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 22 Mar 2024 10:01:34 +0000 (11:01 +0100)
This is done to have a consistent output for special cases as
changes have to only be made in one spot.

philo/main.c

index f24c5caa642c26367235b4edcb2cc3029b165a17..e32081da6a4cc0e3d756f5464c275e0e60ea415b 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/05 12:49:40 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/03/19 14:14:15 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/03/19 14:20:33 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include <unistd.h>
 
 static const char      *g_fed_str = "All philosophers are well fed";
+static const char      *g_think_str = "is thinking";
+static const char      *g_fork_str = "has taken a fork";
+static const char      *g_eat_str = "is eating";
+static const char      *g_sleep_str = "is sleeping";
+static const char      *g_die_str = "died";
 //     unsigned long would be more apropriate for min_eats_num
 //     but for the ease of parsing I'm using size_t
 typedef struct s_settings
@@ -64,24 +69,24 @@ void        thread_print(t_settings *set, size_t id, const char *message)
 
 void   philo_sleep(t_settings *set, size_t id)
 {
-       thread_print(set, id, "is sleeping");
+       thread_print(set, id, g_sleep_str);
        usleep(set->time_to_sleep);
        return ;
 }
 
 void   philo_think(t_settings *set, size_t id)
 {
-       thread_print(set, id, "is thinking");
+       thread_print(set, id, g_think_str);
        return ;
 }
 
 void   philo_eat(t_philo *philo)
 {
        pthread_mutex_lock(philo->forks[0]);
-       thread_print(philo->settings, philo->id, "has taken a fork");
+       thread_print(philo->settings, philo->id, g_fork_str);
        pthread_mutex_lock(philo->forks[1]);
-       thread_print(philo->settings, philo->id, "has taken a fork");
-       thread_print(philo->settings, philo->id, "is eating");
+       thread_print(philo->settings, philo->id, g_fork_str);
+       thread_print(philo->settings, philo->id, g_eat_str);
        philo->last_eaten = cur_time_sus();
        usleep(philo->settings->time_to_eat);
        ++philo->times_eaten;
@@ -321,7 +326,7 @@ void        check_death(t_philo *philos, t_settings *set)
                        if((now - philos[i].last_eaten) > set->time_to_die)
                        {
                                pthread_mutex_lock(&set->terminal);
-                               printf("%li %lu %s\n", (cur_time_sus() - set->program_start) / 1000, i + 1, "died");
+                               printf("%li %lu %s\n", (cur_time_sus() - set->program_start) / 1000, i + 1, g_die_str);
                                set->end = 1;
                                pthread_mutex_unlock(&set->terminal);
                                return ;
@@ -347,10 +352,10 @@ void      check_death(t_philo *philos, t_settings *set)
 int    handle_single_philo(t_settings *set)
 {
        set->program_start = cur_time_sus();
-       thread_print(set, 1, "is thinking");
-       thread_print(set, 1, "has taken a fork");
+       thread_print(set, 1, g_think_str);
+       thread_print(set, 1, g_fork_str);
        usleep(set->time_to_die);
-       thread_print(set, 1, "died");
+       thread_print(set, 1, g_die_str);
        return (0);
 }