From 346e386d8de6a8ba2247992c47c34dcdbd65623c Mon Sep 17 00:00:00 2001 From: Kolomiets Yaroslav Date: Tue, 13 Dec 2016 22:23:17 +0200 Subject: [PATCH] Grisha start --- main.c | 365 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 365 insertions(+) create mode 100644 main.c diff --git a/main.c b/main.c new file mode 100644 index 0000000..aaa3380 --- /dev/null +++ b/main.c @@ -0,0 +1,365 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/10 16:07:25 by gtertysh #+# #+# */ +/* Updated: 2016/12/10 21:35:59 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include +#include +#include + +#define BUF_S 8192 + +typedef struct s_tetrominoes +{ + char figure[17]; +} t_ttr; + +t_ttr ttr_tamplates[19] = +{ + { "#..." + "#..." + "#..." + "#..." }, + + { "####" + "...." + "...." + "...." }, + + { "#..." + "#..." + "##.." + "...." }, + + { "..#." + "###." + "...." + "...." }, + + { "##.." + ".#.." + ".#.." + "...." }, + + { "###." + "#..." + "...." + "...." }, + + { ".#.." + ".#.." + "##.." + "...." }, + + { "###." + "..#." + "...." + "...." }, + + { "##.." + "#..." + "#..." + "...." }, + + { "#..." + "###." + "...." + "...." }, + + { "###." + ".#.." + "...." + "...." }, + + { "#..." + "##.." + "#..." + "...." }, + + { ".#.." + "###." + "...." + "...." }, + + { ".#.." + "##.." + ".#.." + "...." }, + + { "##.." + "##.." + "...." + "...." }, + + { "##.." + ".##." + "...." + "...." }, + + { ".#.." + "##.." + "#..." + "...." }, + + { ".##." + "##.." + "...." + "...." }, + + { "#..." + "##.." + ".#.." + "...." } +}; + +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); +} + +char **to_table(char *string) +{ + char **table; + + table = ft_strsplit(string, '\n'); + free(string); + string = NULL; + return (table); +} + +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++; + } +} + +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++; + } +} + +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); +} + +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++; + } +} + +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++; + } +} + +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++; + } +} + +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); +}