aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRudy Bousset <rbousset@z2r5p2.le-101.fr>2020-01-27 19:41:18 +0100
committerRudy Bousset <rbousset@z2r5p2.le-101.fr>2020-01-27 19:41:18 +0100
commit329519abf2280039d53eaa6965d712e214d7dc34 (patch)
treedaff9da992921d955c663cbf39570747057f64e9
parentCleaner (diff)
download42-cub3d-329519abf2280039d53eaa6965d712e214d7dc34.tar.gz
42-cub3d-329519abf2280039d53eaa6965d712e214d7dc34.tar.bz2
42-cub3d-329519abf2280039d53eaa6965d712e214d7dc34.tar.xz
42-cub3d-329519abf2280039d53eaa6965d712e214d7dc34.tar.zst
42-cub3d-329519abf2280039d53eaa6965d712e214d7dc34.zip
cool
Diffstat (limited to '')
-rw-r--r--inc/cub3d.h2
-rw-r--r--src/ft_get_map.c43
-rw-r--r--src/ft_init_lists.c2
3 files changed, 26 insertions, 21 deletions
diff --git a/inc/cub3d.h b/inc/cub3d.h
index 9462035..f0b8181 100644
--- a/inc/cub3d.h
+++ b/inc/cub3d.h
@@ -38,7 +38,7 @@ typedef struct s_cub
int f_color;
int c_color;
char **map;
- size_t map_width;
+ size_t map_w;
struct s_win *wlist;
} t_cub;
diff --git a/src/ft_get_map.c b/src/ft_get_map.c
index 735e31e..06f3353 100644
--- a/src/ft_get_map.c
+++ b/src/ft_get_map.c
@@ -42,17 +42,33 @@ ft_check_err(char *line, size_t size)
static char
*ft_get_first_line(char *line, t_cub *clist)
{
- size_t len;
char *mapl;
- len = ft_strlen(line);
- if (len < 2 || ft_check_err(line, len) < 0)
+ clist->map_w = ft_strlen(line);
+ if (clist->map_w <= 2 || ft_check_err(line, clist->map_w) < 0)
return (NULL);
- if (!(mapl = (char*)malloc(((len / 2) + 2) * sizeof(char))))
+ if (!(mapl = (char*)malloc(((clist->map_w / 2) + 2) * sizeof(char))))
return (NULL);
ft_linecpy(line, mapl, 0);
ft_memdel(line);
- clist->map_width = len;
+ return (mapl);
+}
+
+static char
+*ft_get_core_map(char *line, char *mapl, size_t i, t_cub *clist)
+{
+ if (ft_check_err(line, clist->map_w) < 0)
+ {
+ ft_memdel(line);
+ ft_memdel(mapl);
+ return (NULL);
+ }
+ if (!(mapl = (char*)ft_nrealloc(mapl, (((clist->map_w / 2) + 2) * i)
+ * sizeof(char), ((((clist->map_w / 2) + 2) * i) +
+ (clist->map_w / 2) + 2) * sizeof(char))))
+ return (NULL);
+ ft_linecpy(line, mapl, ((clist->map_w / 2) + 2) * i);
+ ft_memdel(line);
return (mapl);
}
@@ -60,36 +76,25 @@ int
ft_get_map(int fd, t_cub *clist)
{
size_t i;
- size_t len;
int ret;
char *line;
char *mapl;
- if (get_next_line(fd, &line) <= 0 ||
+ if ((ret = get_next_line(fd, &line)) <= 0 ||
!(mapl = ft_get_first_line(line, clist)))
{
ft_memdel(line);
return (-1);
}
i = 1;
- ret = 1;
- len = clist->map_width;
while (ret > 0)
{
ret = get_next_line(fd, &line);
- if (ft_check_err(line, len) < 0)
- {
- ft_memdel(line);
- ft_memdel(mapl);
+ if (!(mapl = ft_get_core_map(line, mapl, i, clist)))
return (-1);
- }
- mapl = (char*)ft_nrealloc(mapl, (((len / 2) + 2) * i) * sizeof(char),
- ((((len / 2) + 2) * i) + (len / 2) + 2) * sizeof(char));
- ft_linecpy(line, mapl, ((len / 2) + 2) * i);
- ft_memdel(line);
i++;
}
- mapl[(i * ((len / 2) + 2)) - 1] = '\0';
+ mapl[(i * ((clist->map_w / 2) + 2)) - 1] = '\0';
ft_free_words(clist->map, NULL);
clist->map = ft_split(mapl, '\n');
ft_memdel(mapl);
diff --git a/src/ft_init_lists.c b/src/ft_init_lists.c
index 3554e5e..9e66e2e 100644
--- a/src/ft_init_lists.c
+++ b/src/ft_init_lists.c
@@ -37,6 +37,6 @@ t_cub
if (!(clist->map[0] = (char*)ft_calloc(1, sizeof(char))))
return (NULL);
clist->map[1] = 0;
- clist->map_width = 0;
+ clist->map_w = 0;
return (clist);
}