aboutsummaryrefslogtreecommitdiff
path: root/src/parse.c
blob: 4add0b362bed536b5954df29c24bd42315d14427 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   parse.c                                            :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: charles <charles.cabergs@gmail.com>        +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/05/09 11:02:00 by charles           #+#    #+#             */
/*   Updated: 2020/05/09 12:07:36 by charles          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "scop.h"

#define SCOP_VEC_DEFAULT_SIZE 32

static int	st_parse_line(char *line, t_ftvec *vertices, t_ftvec *indices)
{
	unsigned int	tmp;
	float			tmpf;

	if (*line == 'v')
	{
		tmpf = ft_strtof(line, &line);
		ft_vecpush(indices, *((void**)&tmp));
		tmpf = ft_strtof(line, &line);
		ft_vecpush(indices, *((void**)&tmp));
		tmpf = ft_strtof(line, &line);
		ft_vecpush(indices, *((void**)&tmp));
	}
	if (*line == 'f')
	{
		tmp = ft_strtol(line, &line, 10);
		ft_vecpush(indices, *((void**)&tmp));
		tmp = ft_strtol(line, &line, 10);
		ft_vecpush(indices, *((void**)&tmp));
		tmp = ft_strtol(line, &line, 10);
		ft_vecpush(indices, *((void**)&tmp));
	}


	return (0);
}

static int	st_parse_file(int fd, t_ftvec *vertices, t_ftvec *indices)
{
	char	*line;
	int		ret;

	while ((ret = ft_getline(fd, &line)) == FT_LINE)
	{
		ret = st_parse_line(line, vertices, indices);
		free(line);
		if (ret == -1)
			break ;
	}
	if (ret == FT_ERROR)
		return (-1);
	if (*line != '\0')
	{
		free(line);
		return (-1);
	}
	return (0);
}

int			parse(char *filepath, t_object *object)
{
	int		fd;
	t_ftvec	*vertices;
	t_ftvec	*indices;

	if ((fd = open(filepath, O_RDONLY)) == -1)
		return (-1);
	if ((vertices = ft_vecnew(SCOP_VEC_DEFAULT_SIZE)) == NULL)
		return (-1);
	if ((indices = ft_vecnew(SCOP_VEC_DEFAULT_SIZE)) == NULL)
		return (-1);
	st_parse_file(fd, vertices, indices);
	object->vertices = (float*)ft_vectobuf32(vertices);
	object->indices = (unsigned int*)ft_vectobuf32(indices);
	return (0);
}