aboutsummaryrefslogtreecommitdiff
path: root/src/parse/parse_file.c
blob: 443a1b570fdc9213fc6164a91001db4d82d903d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   parse_file.c                                       :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/04 03:50:53 by cacharle          #+#    #+#             */
/*   Updated: 2020/02/04 04:28:41 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "cub3d.h"

static char	**trim_file_lines(char **lines)
{
	int		i;
	char	*tmp;

	if (lines == NULL)
		return (NULL);
	i = -1;
	while (lines[++i] != NULL)
	{
		if ((tmp = ft_strtrim(lines[i], " ")) == NULL)
			return (NULL);
		lines[i] = tmp;
	}
	return (lines);
}

char		**get_file_lines(char *filename)
{
	int		fd;
	int		ret;
	char	buf[CUB3D_BUFFER_SIZE + 1];
	char	*file;

	if ((fd = open(filename, O_RDONLY)) < 0)
		return (NULL);
	if ((file = ft_strdup("")) == NULL)
		return (NULL);
	while ((ret = read(fd, buf, CUB3D_BUFFER_SIZE)) > 0)
	{
		buf[ret] = '\0';
		if ((file = ft_strjoin(file, buf)) == NULL)
			return (NULL);
	}
	if (ret == -1)
		return (NULL);
	close(fd);
	return (trim_file_lines(ft_split(file, '\n')));
}