From 3f2b4236e90534b3c2efb89121c64dbadc20794a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Mon, 22 Apr 2024 16:07:11 +0200 Subject: [PATCH] feat: init parsing --- src/panoramix.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++- src/panoramix.h | 18 ++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/panoramix.c b/src/panoramix.c index 5ef4643..5f3103d 100644 --- a/src/panoramix.c +++ b/src/panoramix.c @@ -7,9 +7,57 @@ #include "panoramix.h" +#include +#include +#include +#include +#include + +#include "debug.h" #include "utils.h" -int panoramix(UNUSED int argc, UNUSED char **argv) +char HELP_MSG[] = + "USAGE:\n" + "\t./panoramix \n"; + +static bool parse_args(int argc, char **argv, gaule_t *gaule) +{ + unsigned long *arr = (size_t *)gaule; + + if (argc != 5) { + fprintf(stderr, "Invalid number of arguments\n"); + fprintf(stderr, "%s", HELP_MSG); + return false; + } + for (int i = 1; i < argc; i++) { + char *ptr = argv[i]; + long tmp = strtol(argv[i], &ptr, 10); + if (tmp <= 0 || *ptr != '\0') { + fprintf(stderr, "Invalid argument: %s\n", argv[i]); + fprintf(stderr, "%s", HELP_MSG); + return false; + } + arr[i - 1] = tmp; + } + DEBUG("nb_villagers: %lu", gaule->nb_villagers); + DEBUG("pot_size: %lu", gaule->pot_size); + DEBUG("nb_fights: %lu", gaule->nb_fights); + DEBUG("nb_refills: %lu", gaule->nb_refills); + return true; +} + +int panoramix(int argc, char **argv) { + gaule_t pano = {0}; + + if (!parse_args(argc, argv, &pano)) + return Error; + pano.villagers = malloc(pano.nb_villagers * sizeof *pano.villagers); + if (pano.villagers == NULL) { + perror("malloc"); + return Error; + } + + free(pano.villagers); return Valid; } diff --git a/src/panoramix.h b/src/panoramix.h index 9a87386..9ef4d3b 100644 --- a/src/panoramix.h +++ b/src/panoramix.h @@ -7,4 +7,22 @@ #pragma once +#include + +typedef struct { + pthread_t thread; +} villager_t; + +typedef struct { + // Those variables MUST NOT be moved in the structure + unsigned long nb_villagers; + unsigned long pot_size; + unsigned long nb_fights; + unsigned long nb_refills; + + pthread_mutex_t mutex; + villager_t *villagers; + pthread_t tdruid; +} gaule_t; + int panoramix(int argc, char **argv);