Skip to content

Commit

Permalink
feat: init parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
drawbu committed Apr 22, 2024
1 parent 56fdd8b commit 3f2b423
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/panoramix.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,57 @@

#include "panoramix.h"

#include <bits/pthreadtypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "debug.h"
#include "utils.h"

int panoramix(UNUSED int argc, UNUSED char **argv)
char HELP_MSG[] =
"USAGE:\n"
"\t./panoramix <nb_villagers> <pot_size> <nb_fights> <nb_refills>\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;
}
18 changes: 18 additions & 0 deletions src/panoramix.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,22 @@

#pragma once

#include <bits/pthreadtypes.h>

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);

0 comments on commit 3f2b423

Please sign in to comment.