From 6be9cc0cbc8e86a8d88aea247fbd5b30d9588a2f Mon Sep 17 00:00:00 2001 From: Gregory Tertyshny Date: Thu, 15 Dec 2016 19:16:26 +0200 Subject: [PATCH] new check for raw string (doesn't work) --- Makefile | 3 +- inc/fillit.h | 6 +- src/check_raw_string.c | 34 ++++++++++ src/main.c | 149 ++++++++++------------------------------- src/read_file.c | 2 +- src/test!_check.c | 6 +- 6 files changed, 78 insertions(+), 122 deletions(-) create mode 100644 src/check_raw_string.c diff --git a/Makefile b/Makefile index 54405ef..aa615d1 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,8 @@ SRC_FILES = main.c \ to_letters.c \ test!_check.c \ test!_print_one_string.c \ - test!_print_table.c + test!_print_table.c \ + check_raw_string.c OBJ_FILES = $(SRC_FILES:.c=.o) diff --git a/inc/fillit.h b/inc/fillit.h index fc43bd4..1c1a18b 100644 --- a/inc/fillit.h +++ b/inc/fillit.h @@ -36,7 +36,6 @@ typedef struct s_map unsigned int figure_amount; } t_map; -// obsolete, rewrite for unit16 typedef struct s_tetrominoes_templates { char figure[17]; @@ -64,10 +63,13 @@ void move_up_left(char **table); void print_one_string(char **glued); // compare 19 templates with each tetromino in *ttr table -void test_check(char **ttr, t_tetrominoes_templates *tamplates); +void test_check(char **ttr, char **tamplates); // change hashes ('#') with letters // obsolete, rewrite for structs array void to_letters(char **ttr); +// проверка считаной строки +int check_raw_string(char *raw_string); + #endif diff --git a/src/check_raw_string.c b/src/check_raw_string.c new file mode 100644 index 0000000..9376456 --- /dev/null +++ b/src/check_raw_string.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_raw_string.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/15 18:32:54 by gtertysh #+# #+# */ +/* Updated: 2016/12/15 18:32:58 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int check_raw_string(char *raw_string) +{ + int n_count; + + n_count = 0; + if (*raw_string == 0 || *raw_string == '\n') + return (1); + while(*raw_string) + { + if (*raw_string == '\n') + n_count++; + if ((n_count + 1) % 4 == 0) + if (*(raw_string + 1) != '\0') + if (*(raw_string + 1) != '\n' && !(*(raw_string + 2) == '.' || *(raw_string + 2) == '#')) + return (1); + raw_string++; + + } + if (n_count < 3) + return (1); + return (0); +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index e9610f4..de91053 100644 --- a/src/main.c +++ b/src/main.c @@ -12,103 +12,26 @@ #include "fillit.h" -// bad global variable -t_tetrominoes_templates templates[19] = -{ - { "#..." - "#..." - "#..." - "#..." }, - - { "####" - "...." - "...." - "...." }, - - { "#..." - "#..." - "##.." - "...." }, - - { "..#." - "###." - "...." - "...." }, - - { "##.." - ".#.." - ".#.." - "...." }, - - { "###." - "#..." - "...." - "...." }, - - { ".#.." - ".#.." - "##.." - "...." }, - - { "###." - "..#." - "...." - "...." }, - - { "##.." - "#..." - "#..." - "...." }, - - { "#..." - "###." - "...." - "...." }, - - { "###." - ".#.." - "...." - "...." }, - - { "#..." - "##.." - "#..." - "...." }, - - { ".#.." - "###." - "...." - "...." }, - - { ".#.." - "##.." - ".#.." - "...." }, - - { "##.." - "##.." - "...." - "...." }, - - { "##.." - ".##." - "...." - "...." }, - - { ".#.." - "##.." - "#..." - "...." }, - - { ".##." - "##.." - "...." - "...." }, - - { "#..." - "##.." - ".#.." - "...." } +char *str_templates[19] = +{ "#...#...#...#...", + "####............", + "#...#...##......", + "..#.###.........", + "##...#...#......", + "###.#...........", + ".#...#..##......", + "###...#.........", + "##..#...#.......", + "#...###.........", + "###..#..........", + "#...##..#.......", + ".#..###.........", + ".#..##...#......", + "##..##..........", + "##...##.........", + ".#..##..#.......", + ".##.##..........", + "#...##...#......" }; int main(int argc, char **argv) @@ -124,31 +47,27 @@ int main(int argc, char **argv) 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); - + if (check_raw_string(string)) + { + ft_putstr("error\n"); + return (1); + } table = to_table(&string); - printf("print_table:\n\n\n"); - print_table(table); - + // if (check_string_table(table)) + // { + // ft_putstr("error\n"); + // return (1); + // } 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, templates); - - to_letters(ttr); - printf("to latters:\n\n"); - print_one_string(ttr); + //print_one_string(ttr); + test_check(ttr, str_templates); + //to_letters(ttr); + // print_one_string(ttr); return (0); } diff --git a/src/read_file.c b/src/read_file.c index 6dfb512..62b1ebf 100644 --- a/src/read_file.c +++ b/src/read_file.c @@ -32,7 +32,7 @@ char *read_file(char *path) string = ft_memalloc(sizeof(char) * BUF_S); while ((readed = read(fd, buf, BUF_S))) { - buf[BUF_S] = '\0'; + buf[readed] = '\0'; old_size = ft_strlen(string); string = ft_realloc(string, old_size + readed, old_size); ft_strcat(string, buf); diff --git a/src/test!_check.c b/src/test!_check.c index 373200e..999272d 100644 --- a/src/test!_check.c +++ b/src/test!_check.c @@ -14,7 +14,7 @@ // Test func. Compare figures from file // with templates -void test_check(char **ttr, t_tetrominoes_templates *tamplates) +void test_check(char **ttr, char **tamplates) { int i; int j; @@ -28,8 +28,8 @@ void test_check(char **ttr, t_tetrominoes_templates *tamplates) 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("%s\n", tamplates[j]); + if ((ft_memcmp(ttr[i], tamplates[j], 17) == 0)) printf(" match!\n"); j++; }