aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_get_map.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ft_get_map.c')
-rw-r--r--src/ft_get_map.c74
1 files changed, 42 insertions, 32 deletions
diff --git a/src/ft_get_map.c b/src/ft_get_map.c
index a8a6fe0..e41ffd6 100644
--- a/src/ft_get_map.c
+++ b/src/ft_get_map.c
@@ -3,31 +3,26 @@
#include <stddef.h>
#include <stdlib.h>
-/* static char */
-/* **ft_split_lines(char *line, int count) */
-/* { */
-/* } */
-
static int
-ft_linecpy(char *line, char *mapL, size_t start)
+ft_linecpy(char *line, char *mapl, size_t start)
{
size_t i;
size_t j;
i = 0;
j = start;
- while (i < ft_strlen(line))
+ while (i < ft_strlen(line) && line[i])
{
- mapL[j] = line[i];
+ mapl[j] = line[i];
i += 2;
j++;
}
- mapL[j + 1] = '\n';
+ mapl[j] = '\n';
return (0);
}
static int
-ft_check_err(char *line, size_t size, t_win *wlist)
+ft_check_err(char *line, size_t size)
{
size_t i;
@@ -37,45 +32,60 @@ ft_check_err(char *line, size_t size, t_win *wlist)
while (i < ft_strlen(line) && line[i])
{
if (line[i] != ' ')
- return (ft_map_error(11, wlist));
+ return (-1);
i += 2;
}
return (0);
}
+
+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)
+ return (NULL);
+ if (!(mapl = (char*)malloc(((len / 2) + 2) * sizeof(char))))
+ return (NULL);
+ ft_linecpy(line, mapl, 0);
+ ft_memdel(line);
+ clist->map_width = len;
+ return (mapl);
+}
+
int
-ft_get_map(int fd, t_win *wlist)
+ft_get_map(int fd, t_cub *clist)
{
size_t i;
size_t len;
+ int ret;
char *line;
- char *mapL;
+ char *mapl;
- ft_free_words(wlist->clist->map, NULL);
- /* if (!(wlist->clist->map = (char**)malloc(40 * sizeof(char*)))) */
- /* return (-1); */
- if (get_next_line(fd, &line) > 0)
+ if (get_next_line(fd, &line) <= 0 ||
+ !(mapl = ft_get_first_line(line, clist)))
{
- len = ft_strlen(line);
- ft_check_err(line, len, wlist);
- if (!(mapL = (char*)malloc((len + 1) * sizeof(char))))
- return (-1);
- ft_linecpy(line, mapL, 0);
ft_memdel(line);
- }
- else
return (-1);
+ }
i = 1;
- while (get_next_line(fd, &line) > 0)
+ ret = 1;
+ len = clist->map_width;
+ while (ret > 0)
{
- ft_check_err(line, len, wlist);
- mapL = (char*)ft_nrealloc(mapL, ((len + 1) * i) * sizeof(char), (((len + 1) * i) + ft_strlen(line) + 1) * sizeof(char));
- ft_linecpy(line, mapL, (len + 1) * i);
+ ret = get_next_line(fd, &line);
+ ft_check_err(line, len);
+ mapl = (char*)ft_nrealloc(mapl, (((len / 2) + 2) * i) * sizeof(char),
+ ((((len / 2) + 2) * i) + (ft_strlen(line) / 2) + 2) * sizeof(char));
+ ft_linecpy(line, mapl, ((len / 2) + 2) * i);
ft_memdel(line);
i++;
}
- mapL[(i * (len + 1)) - 1] = '\0';
- ft_printf("mapL [%s]\n", mapL);
- wlist->clist->map = ft_split(mapL, '\n');
- ft_memdel(mapL);
+ mapl[(i * ((len / 2) + 2)) - 1] = '\0';
+ ft_free_words(clist->map, NULL);
+ clist->map = ft_split(mapl, '\n');
+ ft_memdel(mapl);
return (0);
}