mirror of
https://github.com/LucasCodeur/alcu.git
synced 2026-04-28 17:44:34 +02:00
FEAT: add vector, fix standard input
This commit is contained in:
+11
-10
@@ -12,30 +12,31 @@
|
||||
|
||||
#include "../libft/libft.h"
|
||||
#include "get_next_line.h"
|
||||
#include "vector.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int check_input(int fd)
|
||||
bool check_input(t_vector* lines)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
char *line;
|
||||
char* line;
|
||||
|
||||
line = NULL;
|
||||
line = get_next_line(fd);
|
||||
j = 0;
|
||||
while (line != NULL)
|
||||
line = NULL;
|
||||
while (j < lines->pfVectorTotal(lines))
|
||||
{
|
||||
line = (char*)lines->pfVectorGet(lines, j);
|
||||
i = -1;
|
||||
while (line[++i])
|
||||
if (!(line[i] >= '0' && line[i] <= '9') && line[i] != '\n')
|
||||
return (free(line), -1);
|
||||
return (false);
|
||||
if (ft_atoi(line) < 1 || ft_atoi(line) > 10000)
|
||||
return (free(line), -1);
|
||||
free(line);
|
||||
return (false);
|
||||
j++;
|
||||
line = get_next_line(fd);
|
||||
}
|
||||
return (j);
|
||||
return (true);
|
||||
}
|
||||
|
||||
@@ -1,35 +1,51 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* fill_array.c :+: :+: :+: */
|
||||
/* fill_vector.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/28 13:21:46 by lud-adam #+# #+# */
|
||||
/* Updated: 2026/03/28 20:53:02 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2026/03/29 14:58:09 by lud-adam ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../libft/libft.h"
|
||||
|
||||
#include "get_next_line.h"
|
||||
|
||||
int *fill_array(int fd, int size)
|
||||
{
|
||||
int *res;
|
||||
char *line;
|
||||
int i;
|
||||
#include "alcu.h"
|
||||
|
||||
bool fill_vector(t_vector* lines, int fd)
|
||||
{
|
||||
char *line;
|
||||
|
||||
res = malloc(sizeof(int) * size);
|
||||
if (res == NULL)
|
||||
return (NULL);
|
||||
line = get_next_line(fd);
|
||||
i = 0;
|
||||
while (line != NULL)
|
||||
{
|
||||
lines->pfVectorAdd(lines, line);
|
||||
line = get_next_line(fd);
|
||||
}
|
||||
return (true);
|
||||
}
|
||||
|
||||
int* fill_array(t_vector* lines, int* size)
|
||||
{
|
||||
int* res;
|
||||
int i;
|
||||
char* line;
|
||||
|
||||
i = 0;
|
||||
line = NULL;
|
||||
*size = lines->pfVectorTotal(lines);
|
||||
res = malloc(sizeof(int) * *size);
|
||||
if (!res)
|
||||
return (NULL);
|
||||
while (i < *size)
|
||||
{
|
||||
line = (char*)lines->pfVectorGet(lines, i);
|
||||
res[i] = ft_atoi(line);
|
||||
i++;
|
||||
free(line);
|
||||
line = get_next_line(fd);
|
||||
}
|
||||
return (res);
|
||||
}
|
||||
@@ -69,6 +69,8 @@ static void player_turn(int *line)
|
||||
if (temp)
|
||||
free(temp);
|
||||
}
|
||||
if (temp)
|
||||
free(temp);
|
||||
}
|
||||
|
||||
static void play_turns(int *lines, int i, int *ai_turn, int size)
|
||||
|
||||
+23
-12
@@ -6,14 +6,14 @@
|
||||
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/28 12:30:29 by lud-adam #+# #+# */
|
||||
/* Updated: 2026/03/28 21:41:29 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2026/03/29 14:58:10 by lud-adam ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../libft/libft.h"
|
||||
#include "alcu.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int get_fd(int argc, char **argv)
|
||||
@@ -32,27 +32,38 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
int fd;
|
||||
int size;
|
||||
int *lines;
|
||||
int* lines_int;
|
||||
VECTOR_INIT(lines);
|
||||
|
||||
fd = get_fd(argc, argv);
|
||||
lines_int = NULL;
|
||||
size = 0;
|
||||
if (fd < 0)
|
||||
{
|
||||
ft_putstr_fd("ERROR", 2);
|
||||
return (1);
|
||||
}
|
||||
size = check_input(fd);
|
||||
if (size < 0)
|
||||
fill_vector(&lines, fd);
|
||||
if (check_input(&lines) == false)
|
||||
{
|
||||
lines.pfVectorFree(&lines);
|
||||
ft_putstr_fd("ERROR", 2);
|
||||
return (1);
|
||||
|
||||
}
|
||||
if (argc == 1)
|
||||
fd = open("/dev/tty", O_RDONLY);
|
||||
// // fd = get_fd(argc, argv);
|
||||
lines_int = fill_array(&lines, &size);
|
||||
if (!lines_int)
|
||||
{
|
||||
lines.pfVectorFree(&lines);
|
||||
ft_putstr_fd("ERROR", 2);
|
||||
return (1);
|
||||
}
|
||||
game(lines_int, size);
|
||||
close(fd);
|
||||
fd = open(argv[1], O_RDONLY);
|
||||
lines = fill_array(fd, size);
|
||||
if (!lines)
|
||||
return (1);
|
||||
game(lines, size);
|
||||
close(fd);
|
||||
free(lines);
|
||||
free(lines_int);
|
||||
lines.pfVectorFree(&lines);
|
||||
return (0);
|
||||
}
|
||||
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vector.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/29 14:23:01 by lud-adam #+# #+# */
|
||||
/* Updated: 2026/03/29 14:50:19 by lud-adam ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "vector.h"
|
||||
#include "libft.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
int vectorResize(t_vector *v, int capacity);
|
||||
int vectorPushBack(t_vector *v, void *item);
|
||||
int vectorSet(t_vector *v, int index, void *item);
|
||||
void *vectorGet(t_vector *v, int index);
|
||||
int vectorDelete(t_vector *v, int index);
|
||||
int vectorFree(t_vector *v);
|
||||
int vectorTotal(t_vector *v);
|
||||
|
||||
void vector_init(t_vector *v)
|
||||
{
|
||||
// Initialize function pointers
|
||||
v->pfVectorTotal = vectorTotal;
|
||||
v->pfVectorResize = vectorResize;
|
||||
v->pfVectorAdd = vectorPushBack;
|
||||
v->pfVectorSet = vectorSet;
|
||||
v->pfVectorGet = vectorGet;
|
||||
v->pfVectorFree = vectorFree;
|
||||
v->pfVectorDelete = vectorDelete;
|
||||
// Allocate memory and check for failure
|
||||
v->vector_list.capacity = VECTOR_INIT_CAPACITY;
|
||||
v->vector_list.total = 0;
|
||||
v->vector_list.items = malloc(sizeof(void *) * v->vector_list.capacity);
|
||||
if (!v->vector_list.items)
|
||||
{
|
||||
ft_putstr_fd("Memory allocation failed in vector_init\n", 2);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
int vectorTotal(t_vector *v)
|
||||
{
|
||||
int totalCount = UNDEFINE;
|
||||
if(v)
|
||||
{
|
||||
totalCount = v->vector_list.total;
|
||||
}
|
||||
return totalCount;
|
||||
}
|
||||
|
||||
int vectorResize(t_vector *v, int capacity)
|
||||
{
|
||||
int status = UNDEFINE;
|
||||
if(v)
|
||||
{
|
||||
void **items = realloc(v->vector_list.items, sizeof(void *) * capacity);
|
||||
if (items)
|
||||
{
|
||||
v->vector_list.items = items;
|
||||
v->vector_list.capacity = capacity;
|
||||
status = SUCCESS;
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
int vectorPushBack(t_vector *v, void *item)
|
||||
{
|
||||
int status = UNDEFINE;
|
||||
if(v)
|
||||
{
|
||||
if (v->vector_list.capacity == v->vector_list.total)
|
||||
{
|
||||
status = vectorResize(v, v->vector_list.capacity * 2);
|
||||
if(status != UNDEFINE)
|
||||
{
|
||||
v->vector_list.items[v->vector_list.total++] = item;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
v->vector_list.items[v->vector_list.total++] = item;
|
||||
status = SUCCESS;
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
int vectorSet(t_vector *v, int index, void *item)
|
||||
{
|
||||
int status = UNDEFINE;
|
||||
if(v)
|
||||
{
|
||||
if ((index >= 0) && (index < v->vector_list.total))
|
||||
{
|
||||
v->vector_list.items[index] = item;
|
||||
status = SUCCESS;
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
void *vectorGet(t_vector *v, int index)
|
||||
{
|
||||
void *readData = NULL;
|
||||
if(v)
|
||||
{
|
||||
if ((index >= 0) && (index < v->vector_list.total))
|
||||
{
|
||||
readData = v->vector_list.items[index];
|
||||
}
|
||||
}
|
||||
return readData;
|
||||
}
|
||||
|
||||
int vectorDelete(t_vector *v, int index)
|
||||
{
|
||||
int status = UNDEFINE;
|
||||
int i = 0;
|
||||
if(v)
|
||||
{
|
||||
if ((index < 0) || (index >= v->vector_list.total))
|
||||
return status;
|
||||
v->vector_list.items[index] = NULL;
|
||||
for (i = index; (i < v->vector_list.total - 1); ++i)
|
||||
{
|
||||
v->vector_list.items[i] = v->vector_list.items[i + 1];
|
||||
v->vector_list.items[i + 1] = NULL;
|
||||
}
|
||||
v->vector_list.total--;
|
||||
if ((v->vector_list.total > 0) && ((v->vector_list.total) == (v->vector_list.capacity / 4)))
|
||||
{
|
||||
vectorResize(v, v->vector_list.capacity / 2);
|
||||
}
|
||||
status = SUCCESS;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
int vectorFree(t_vector *v)
|
||||
{
|
||||
int status = UNDEFINE;
|
||||
int total = v->pfVectorTotal(v);
|
||||
if(v)
|
||||
{
|
||||
for (int i = 0; i < total; i++)
|
||||
{
|
||||
void * temp = v->pfVectorGet(v, i);
|
||||
free (temp);
|
||||
}
|
||||
free(v->vector_list.items);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
Reference in New Issue
Block a user