Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* parser.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: dcherend <dcherend@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/05/09 18:42:27 by dcherend #+# #+# */
- /* Updated: 2018/05/27 18:42:12 by dcherend ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "../inc/fdf.h"
- t_map *alloc_map(int width, int height)
- {
- t_map *map;
- int i;
- if (!(map = (t_map*)malloc(sizeof(t_map))))
- return (NULL);
- map->height = height;
- map->width = width;
- map->cords = (int**)ft_memalloc(sizeof(int*) * height);
- i = 0;
- while (i < width)
- {
- map->cords[i] = (int*)ft_memalloc(sizeof(int) * width);
- i++;
- }
- return (map);
- }
- void del_map(t_map *map)
- {
- int i;
- static int huy = 0;
- i = 0;
- if (map)
- {
- while (i < map->width)
- {
- free(map->cords[i]);
- i++;
- }
- free(map->cords);
- free(map);
- }
- }
- t_map *realloc_map(t_map *map, int width, int height)
- {
- if (!map)
- return (NULL);
- del_map(map);
- map = alloc_map(width, height);
- return (map);
- }
- void fill_map(t_map *map, char *line)
- {
- t_map *tmp;
- int i;
- int j;
- char **symbs;
- tmp = map;
- symbs = ft_strsplit(line, ' ');
- free(line);
- i = 0;
- while (i < map->height)
- {
- j = 0;
- while (j < map->width)
- {
- map->cords[i][j] = ft_atoi(symbs[j]);
- j++;
- }
- i++;
- }
- }
- t_map *ft_parser(char *name)
- {
- t_map *map;
- char *line;
- int rows;
- int fd;
- rows = 1;
- fd = open(name, O_RDONLY);
- if (fd < 0)
- throw_error("Bad file input.");
- map = alloc_map(1, 1);
- while ((get_next_line(fd, &line)) > 0)
- {
- rows++;
- map = realloc_map(map, ft_strlen(line), rows);
- fill_map(map, line);
- }
- return (map);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement