2016-12-13 22:23:17 +02:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* main.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2016/12/10 16:07:25 by gtertysh #+# #+# */
|
|
|
|
/* Updated: 2016/12/10 21:35:59 by gtertysh ### ########.fr */
|
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#include "libft.h"
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#define BUF_S 8192
|
|
|
|
|
|
|
|
typedef struct s_tetrominoes
|
|
|
|
{
|
|
|
|
char figure[17];
|
|
|
|
} t_ttr;
|
|
|
|
|
|
|
|
t_ttr ttr_tamplates[19] =
|
|
|
|
{
|
|
|
|
{ "#..."
|
|
|
|
"#..."
|
|
|
|
"#..."
|
|
|
|
"#..." },
|
|
|
|
|
|
|
|
{ "####"
|
|
|
|
"...."
|
|
|
|
"...."
|
|
|
|
"...." },
|
|
|
|
|
|
|
|
{ "#..."
|
|
|
|
"#..."
|
|
|
|
"##.."
|
|
|
|
"...." },
|
|
|
|
|
|
|
|
{ "..#."
|
|
|
|
"###."
|
|
|
|
"...."
|
|
|
|
"...." },
|
|
|
|
|
|
|
|
{ "##.."
|
|
|
|
".#.."
|
|
|
|
".#.."
|
|
|
|
"...." },
|
|
|
|
|
|
|
|
{ "###."
|
|
|
|
"#..."
|
|
|
|
"...."
|
|
|
|
"...." },
|
|
|
|
|
|
|
|
{ ".#.."
|
|
|
|
".#.."
|
|
|
|
"##.."
|
|
|
|
"...." },
|
|
|
|
|
|
|
|
{ "###."
|
|
|
|
"..#."
|
|
|
|
"...."
|
|
|
|
"...." },
|
|
|
|
|
|
|
|
{ "##.."
|
|
|
|
"#..."
|
|
|
|
"#..."
|
|
|
|
"...." },
|
|
|
|
|
|
|
|
{ "#..."
|
|
|
|
"###."
|
|
|
|
"...."
|
|
|
|
"...." },
|
|
|
|
|
|
|
|
{ "###."
|
|
|
|
".#.."
|
|
|
|
"...."
|
|
|
|
"...." },
|
|
|
|
|
|
|
|
{ "#..."
|
|
|
|
"##.."
|
|
|
|
"#..."
|
|
|
|
"...." },
|
|
|
|
|
|
|
|
{ ".#.."
|
|
|
|
"###."
|
|
|
|
"...."
|
|
|
|
"...." },
|
|
|
|
|
|
|
|
{ ".#.."
|
|
|
|
"##.."
|
|
|
|
".#.."
|
|
|
|
"...." },
|
|
|
|
|
|
|
|
{ "##.."
|
|
|
|
"##.."
|
|
|
|
"...."
|
|
|
|
"...." },
|
|
|
|
|
|
|
|
{ "##.."
|
|
|
|
".##."
|
|
|
|
"...."
|
|
|
|
"...." },
|
|
|
|
|
|
|
|
{ ".#.."
|
|
|
|
"##.."
|
|
|
|
"#..."
|
|
|
|
"...." },
|
|
|
|
|
|
|
|
{ ".##."
|
|
|
|
"##.."
|
|
|
|
"...."
|
|
|
|
"...." },
|
|
|
|
|
|
|
|
{ "#..."
|
|
|
|
"##.."
|
|
|
|
".#.."
|
|
|
|
"...." }
|
|
|
|
};
|
|
|
|
|
2016-12-14 12:24:46 +02:00
|
|
|
|
|
|
|
// read from file. Returns string.
|
2016-12-13 22:23:17 +02:00
|
|
|
char *read_file(char *path)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int readed;
|
|
|
|
int old_size;
|
|
|
|
char buf[BUF_S + 1];
|
|
|
|
char *string;
|
|
|
|
|
|
|
|
readed = 0;
|
|
|
|
string = 0;
|
|
|
|
old_size = 0;
|
|
|
|
if (!(fd = open(path, O_RDONLY)))
|
|
|
|
{
|
|
|
|
ft_putstr("error\n");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
string = ft_memalloc(sizeof(char) * BUF_S);
|
|
|
|
while ((readed = read(fd, buf, BUF_S)))
|
|
|
|
{
|
|
|
|
buf[BUF_S] = '\0';
|
|
|
|
old_size = ft_strlen(string);
|
|
|
|
string = ft_realloc(string, old_size + readed, old_size);
|
|
|
|
ft_strcat(string, buf);
|
|
|
|
}
|
|
|
|
return (string);
|
|
|
|
}
|
|
|
|
|
2016-12-14 12:24:46 +02:00
|
|
|
// Split string to table by newline character. Each string contain one line of the file
|
2016-12-13 22:23:17 +02:00
|
|
|
char **to_table(char *string)
|
|
|
|
{
|
|
|
|
char **table;
|
|
|
|
|
|
|
|
table = ft_strsplit(string, '\n');
|
|
|
|
free(string);
|
|
|
|
string = NULL;
|
|
|
|
return (table);
|
|
|
|
}
|
|
|
|
|
2016-12-14 12:24:46 +02:00
|
|
|
// Test func. Print string table.
|
2016-12-13 22:23:17 +02:00
|
|
|
void print_table(char **table)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (table[i])
|
|
|
|
{
|
|
|
|
ft_putstr(table[i]);
|
|
|
|
ft_putstr("\n");
|
|
|
|
if ((i + 1) % 4 == 0)
|
|
|
|
ft_putchar('\n');
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 12:24:46 +02:00
|
|
|
// Test func. Print table but one string contain whole figure.
|
2016-12-13 22:23:17 +02:00
|
|
|
void print_one_string(char **glued)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (glued[i])
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
while (glued[i][j])
|
|
|
|
{
|
|
|
|
ft_putchar(glued[i][j]);
|
|
|
|
if ((j + 1) % 4 == 0)
|
|
|
|
ft_putchar('\n');
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
ft_putstr("\n");
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 12:24:46 +02:00
|
|
|
// This function creates string table in which one line represent one figure
|
|
|
|
// by "glueing" evry 4 strings from table
|
2016-12-13 22:23:17 +02:00
|
|
|
char **glue_figure(char **table)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int size;
|
|
|
|
char **ttr_table;
|
|
|
|
|
|
|
|
size = 0;
|
|
|
|
i = 0;
|
|
|
|
j = 0;
|
|
|
|
while(table[size])
|
|
|
|
size++;
|
|
|
|
ttr_table = ft_memalloc(sizeof(char *) * (size / 4 + 1));
|
|
|
|
while (i < size / 4)
|
|
|
|
ttr_table[i++] = ft_memalloc(sizeof(char) * 17);
|
|
|
|
ttr_table[i] = 0;
|
|
|
|
i = 0;
|
|
|
|
while (i < size / 4)
|
|
|
|
{
|
|
|
|
ft_strcat(ttr_table[i], table[j]);
|
|
|
|
j++;
|
|
|
|
if (j % 4 == 0)
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
while(table[i])
|
|
|
|
free(table[i++]);
|
|
|
|
free(table);
|
|
|
|
return (ttr_table);
|
|
|
|
}
|
|
|
|
|
2016-12-14 12:24:46 +02:00
|
|
|
// Moves figures to top left corner.
|
|
|
|
// table contains strings each represent one figure
|
2016-12-13 22:23:17 +02:00
|
|
|
void move_up_left(char **table)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int k;
|
|
|
|
char tmp[17];
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (table[i])
|
|
|
|
{
|
|
|
|
ft_memcpy(tmp, table[i], 17);
|
|
|
|
j = 0;
|
|
|
|
while(table[i][j] != '#')
|
|
|
|
j++;
|
|
|
|
ft_memmove(tmp, table[i] + j, 17 - j);
|
|
|
|
k = 0;
|
|
|
|
while ( (tmp[3] == '#' && tmp[4] == '#') ||
|
|
|
|
(tmp[7] == '#' && tmp[8] == '#') ||
|
|
|
|
(tmp[11] == '#' && tmp[12] == '#'))
|
|
|
|
{
|
|
|
|
ft_memmove((tmp + k + 1), (tmp + k), 17 - j);
|
|
|
|
*(tmp + k) = '.';
|
|
|
|
j--;
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
k = 15;
|
|
|
|
while (j--)
|
|
|
|
{
|
|
|
|
*(tmp + k--) = '.';
|
|
|
|
}
|
|
|
|
ft_memcpy(table[i], tmp, 17);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 12:24:46 +02:00
|
|
|
// convert "#" symbols to letters
|
2016-12-13 22:23:17 +02:00
|
|
|
void to_letters(char **ttr)
|
|
|
|
{
|
|
|
|
char ch;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
ch = 'A';
|
|
|
|
while(ttr[i])
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
while(ttr[i][j])
|
|
|
|
{
|
|
|
|
if(ttr[i][j] == '#')
|
|
|
|
ttr[i][j] = ch;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
ch++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 12:24:46 +02:00
|
|
|
// Test func. Compare figures from file
|
|
|
|
// with templates
|
2016-12-13 22:23:17 +02:00
|
|
|
void test_check(char **ttr, t_ttr *tamplates)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (ttr[i])
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
while(j < 19)
|
|
|
|
{
|
|
|
|
printf("\n\ninput:\n");
|
|
|
|
printf("%s\n", ttr[i]);
|
|
|
|
printf("\n\ntemplate:\n");
|
|
|
|
printf("%s\n", tamplates[j].figure);
|
|
|
|
if ((ft_memcmp(ttr[i], tamplates[j].figure, 17) == 0))
|
|
|
|
printf(" match!\n");
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 12:24:46 +02:00
|
|
|
|
|
|
|
// Forgot for what this func was added:)
|
2016-12-13 22:23:17 +02:00
|
|
|
int ft_dumb_sqrt(int nb)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (i * i < nb)
|
|
|
|
i++;
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *string;
|
|
|
|
char **table;
|
|
|
|
char **ttr;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
if (argc != 2)
|
|
|
|
{
|
|
|
|
ft_putstr("error\n");
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
string = read_file(argv[1]);
|
|
|
|
if (!(string = read_file(argv[1])))
|
|
|
|
{
|
|
|
|
ft_putstr("error\n");
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
printf("string:\n\n\n%s\n", string);
|
|
|
|
table = to_table(string);
|
|
|
|
// set null to free :)
|
|
|
|
printf("print_table:\n\n\n");
|
|
|
|
print_table(table);
|
|
|
|
ttr = glue_figure(table);
|
|
|
|
printf("glued figures:\n\n\n");
|
|
|
|
print_one_string(ttr);
|
|
|
|
move_up_left(ttr);
|
|
|
|
printf("move to up left:\n\n\n");
|
|
|
|
print_one_string(ttr);
|
|
|
|
//printf("test check:\n\n");
|
|
|
|
//test_check(ttr, ttr_tamplates);
|
|
|
|
to_letters(ttr);
|
|
|
|
printf("to latters:\n\n");
|
|
|
|
print_one_string(ttr);
|
|
|
|
printf("%d\n", ft_dumb_sqrt(4));
|
|
|
|
//while (i < 19)
|
|
|
|
//{
|
|
|
|
// ft_putstr(ttr[i++].figure);
|
|
|
|
// ft_putstr("\n");
|
|
|
|
//}
|
|
|
|
//if (check(string))
|
|
|
|
//{
|
|
|
|
// ft_putstr("error\n");
|
|
|
|
// return (1);
|
|
|
|
//}
|
|
|
|
return (0);
|
|
|
|
}
|