modifie add_to_list pour fonctionner avec du binaire

This commit is contained in:
Hugo LAMY
2019-04-19 15:34:05 +02:00
parent 729853cea0
commit 730964a817
5 changed files with 67 additions and 123 deletions

BIN
.parse_input.c.swo Normal file

Binary file not shown.

View File

@@ -6,114 +6,61 @@
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */ /* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/14 15:20:53 by hulamy #+# #+# */ /* Created: 2019/04/14 15:20:53 by hulamy #+# #+# */
/* Updated: 2019/04/18 10:56:38 by hulamy ### ########.fr */ /* Updated: 2019/04/19 15:33:04 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "fillit.h" #include "fillit.h"
#include <stdio.h> #include <stdio.h>
#include "libft/includes/libft.h"
/* /*
** Function that fills the char **tetraminos section of the structure ** this function prints a 16 bites short
** with the most little rectangle that fit the tetraminos
*/ */
char **fill_tetraminos(char **square, int *tab) void print_bits(short line)
{ {
char **result; int mask;
int height;
int length;
int i;
int j;
i = 0; mask = 1 << 16;
height = tab[2] - tab[0] + 1; while (mask >>= 1)
length = tab[3] - tab[1] + 1; (line & mask) ? ft_putnbr(1) : ft_putnbr(0);
if (!(result = (char**)malloc(sizeof(*result) * (height + 1)))) ft_putchar('\n');
return (NULL); }
result[height] = NULL;
while (i < height) /*
** this function transforme a tetrminos char* into a short of 16 bites
** then it fills it and its reverse into the list
*/
int fill_list(char *line, t_fillist *list)
{
short tmp;
while (*line)
{ {
if (!(result[i] = (char*)malloc(sizeof(**result) * (length + 1)))) list->tetribit <<= 1;
return (NULL); if (*line == '\n')
result[i][length] = '\0'; line++;
i++; if (*(line++) == '#')
list->tetribit |= 1;
} }
i = -1; tmp = list->tetribit;
while (++i < height && (j = -1)) while (tmp)
while (++j < length) {
result[i][j] = square[tab[0] + i][tab[1] + j]; if (tmp & 1)
return (result); list->tibirtet |= 1;
list->tibirtet <<= 1;
tmp >>= 1;
}
return (0);
} }
/* /*
** This function calculate the line and columns where the tetraminos ** this function create the linked list and add a new structure linked each time needed
** start and end, by skipping the empty lines
**
** ! it has a little bug so far, i need to fix it
*/ */
void find_start_and_end(char **square, int **x) int add_to_list(char *line, t_fillist **list)
{
int i;
x[0][0] = -1;
x[0][1] = -1;
x[0][2] = 4;
x[0][3] = 4;
i = 4;
while (x[0][0] < 4 && i == 4 && !(i = 0) && square[++(x[0][0])][0] != '#')
while (i < 4 && square[*(x[0])][i] != '#')
i++;
i = 4;
while (x[0][1] < 4 && i == 4 && !(i = 0) && square[0][++(x[0][1])] != '#')
while (i < 4 && square[i][x[0][1]] != '#')
i++;
i = -1;
while (x[0][2] >= 0 && i == -1 && (i = 3) && square[--(x[0][2])][3] != '#')
while (i >= 0 && square[x[0][2]][i] != '#')
i--;
i = -1;
while (x[0][3] >= 0 && i == -1 && (i = 3) && square[3][--(x[0][3])] != '#')
while (i >= 0 && square[i][x[0][3]] != '#')
i--;
}
/*
** this function first call find_start_and_end to find the coordinates
** of start en end of the most little rectangle that fit the tetraminos
**
** it allows it to fill the structure with the size information
** (for instance :
** "##" ".#" ".#"
** is a tatraminos of 2 by 3)
** then it fills also the area information (2 * 3 = 6)
**
** and finally it calls fill_tetraminos to fill the char **tetraminos
*/
int fill_list(char **square, t_fillist *list)
{
int *tab;
tab = (int*)malloc(sizeof(int) * 4);
find_start_and_end(square, &tab);
list->size[0] = tab[3] - tab[1] + 1;
list->size[1] = tab[2] - tab[0] + 1;
list->area = list->size[0] * list->size[1];
list->tetraminos = fill_tetraminos(square, tab);
return (1);
}
/*
** this function first checks if the structure has been created
** if not, it creates the first element, else it adds an element
** and modifies the initial pointer to link to the new first element
**
** then it calls fill_list to fill the data of the structure
*/
int add_to_list(char **square, t_fillist **list)
{ {
t_fillist *tmp; t_fillist *tmp;
@@ -124,30 +71,27 @@ int add_to_list(char **square, t_fillist **list)
else else
tmp->next = *list; tmp->next = *list;
*list = tmp; *list = tmp;
fill_list(square, *list); fill_list(line, *list);
return (1); return (1);
} }
/* int main(int ac, char **av)
**int main(int ac, char **av) {
**{ static t_fillist *list = NULL; // avant d'appeller add_to_list il faut declarer un pointeur static vers la structure
** static t_fillist *list = NULL; // avant d'appeller add_to_list il faut declarer un pointeur static vers la structure int i;
** int i;
** if (ac > 1)
** if (ac > 1) {
** { add_to_list(*(++av), &list);
** add_to_list(++av, &list); // l'appel de la fonction se fait avec un carre valide de 4*4 et l'adresse du pointeur vers la liste while (list && (i = -1))
** if (ac == 9) {
** add_to_list(av += 4, &list); printf("%d\n", list->tetribit);
** while (list && (i = -1)) print_bits(list->tetribit);
** { print_bits(list->tibirtet);
** while (++i < list->size[1]) list = list->next;
** printf("%s\n", list->tetraminos[i]); }
** printf("\n"); }
** list = list->next;
** } return (0);
** } }
**
** return (0);
**}
*/

View File

@@ -6,7 +6,7 @@
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */ /* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/01 13:34:46 by vmanzoni #+# #+# */ /* Created: 2019/03/01 13:34:46 by vmanzoni #+# #+# */
/* Updated: 2019/04/18 11:46:51 by vmanzoni ### ########.fr */ /* Updated: 2019/04/19 14:52:10 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -28,11 +28,9 @@
typedef struct s_fillist typedef struct s_fillist
{ {
int id; short tetribit;
char **tetraminos; short tibirtet;
int position[2]; int position;
int size[2];
int area;
struct s_fillist *next; struct s_fillist *next;
} t_fillist; } t_fillist;
@@ -46,6 +44,6 @@ void parse_input(char *input);
int check_file_errors(char *file); int check_file_errors(char *file);
int check_tetri_errors(char *tetri); int check_tetri_errors(char *tetri);
int check_tetri_errors2(char *tetri); int check_tetri_errors2(char *tetri);
int add_to_list(char **square, t_fillist **list); int add_to_list(char *square, t_fillist **list);
#endif #endif

View File

@@ -6,7 +6,7 @@
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */ /* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/15 14:48:14 by vmanzoni #+# #+# */ /* Created: 2019/04/15 14:48:14 by vmanzoni #+# #+# */
/* Updated: 2019/04/18 13:07:56 by vmanzoni ### ########.fr */ /* Updated: 2019/04/19 12:53:40 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -87,6 +87,8 @@ void parse_input(char *input)
short test; //DELETE BEFORE EVAL short test; //DELETE BEFORE EVAL
i = 0; i = 0;
printf("input: %s\n", input);
printf("end\n");
while (input[i]) while (input[i])
{ {
j = 0; j = 0;

View File

@@ -6,7 +6,7 @@
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */ /* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/13 12:09:46 by vmanzoni #+# #+# */ /* Created: 2019/04/13 12:09:46 by vmanzoni #+# #+# */
/* Updated: 2019/04/15 14:48:36 by vmanzoni ### ########.fr */ /* Updated: 2019/04/19 12:50:32 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */