diff options
| author | Cabergs Charles <cacharle@e-r6-p7.s19.be> | 2019-07-24 18:46:39 +0200 |
|---|---|---|
| committer | Cabergs Charles <cacharle@e-r6-p7.s19.be> | 2019-07-24 18:46:58 +0200 |
| commit | 79e6c8152252bac1cd632e224ac496a1823da1a5 (patch) | |
| tree | 1e69b9330430438fa499f94e864dcfb4d83ea007 /bsq/parse_helper.c | |
| parent | e7acdc820fefa41ae00a7c776388e3d17250a2e9 (diff) | |
| download | piscine-79e6c8152252bac1cd632e224ac496a1823da1a5.tar.gz piscine-79e6c8152252bac1cd632e224ac496a1823da1a5.tar.bz2 piscine-79e6c8152252bac1cd632e224ac496a1823da1a5.zip | |
bsq and c13 passed
Diffstat (limited to 'bsq/parse_helper.c')
| -rw-r--r-- | bsq/parse_helper.c | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/bsq/parse_helper.c b/bsq/parse_helper.c new file mode 100644 index 0000000..56d0f6d --- /dev/null +++ b/bsq/parse_helper.c @@ -0,0 +1,101 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/23 19:24:06 by cacharle #+# #+# */ +/* Updated: 2019/07/24 12:04:33 by samzur ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> +#include "include.h" + +/* +** initialise the `y` row of the matrix +*/ + +int *set_row(char *file, t_parsed_terrain *pterrain, int y) +{ + int i; + int x; + int *row; + + if ((row = (int*)malloc(sizeof(int) * pterrain->terrain->columns)) == NULL) + return (NULL); + i = ft_line_len(file) + 1; + i += (ft_line_len(&file[i]) + 1) * y; + x = 0; + while (x < pterrain->terrain->columns) + { + if (file[i] == pterrain->empty) + row[x] = EMPTY; + else if (file[i] == pterrain->obstacle) + row[x] = OBSTACLE; + i++; + x++; + } + return (row); +} + +/* +** set dimensions of the terrain and check they correspond with the terrain, +** also check the character used for the cell types +** return -1 in case of error, 0 otherwise. +*/ + +int set_dimensions(char *file, t_parsed_terrain *pterrain) +{ + int i; + int j; + int first_line_len; + int line_len; + + first_line_len = ft_line_len(file); + if (first_line_len == 0) + return (-1); + i = 0; + while (file[i]) + { + line_len = ft_line_len(&file[i]); + if (line_len != first_line_len) + return (-1); + j = -1; + while (++j < line_len) + if (file[i + j] != pterrain->empty + && file[i + j] != pterrain->obstacle) + return (-1); + i += line_len + 1; + } + pterrain->terrain->columns = first_line_len; + return (0); +} + +/* +** Parse header and check if everything is ok (number, obstacles, duplicates) +*/ + +int parse_header(char *file, t_parsed_terrain *pterrain) +{ + int i; + + i = ft_line_len(file); + if (i < 4) + return (-1); + pterrain->filled = file[--i]; + pterrain->obstacle = file[--i]; + pterrain->empty = file[--i]; + if (pterrain->filled == pterrain->obstacle + || pterrain->filled == pterrain->empty + || pterrain->obstacle == pterrain->empty) + return (-1); + if ((pterrain->terrain->rows = ft_natoi(file, i)) < 0) + return (-1); + i = ft_line_len(file) + 1; + if (pterrain->terrain->rows != count_lines(&file[i]) + || pterrain->terrain->rows == 0) + return (-1); + return (i); +} |
