-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.c
More file actions
87 lines (79 loc) · 2.19 KB
/
parser.c
File metadata and controls
87 lines (79 loc) · 2.19 KB
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
84
85
86
87
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fvonsovs <fvonsovs@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/22 09:48:04 by fvonsovs #+# #+# */
/* Updated: 2024/09/14 15:20:23 by fvonsovs ### ########.fr */
/* */
/* ************************************************************************** */
#include "minirt.h"
// free map later!
t_map *parser(char *filename)
{
t_map *map;
int fd;
char *line;
map = malloc_map();
fd = open_file(filename);
test_map(fd);
close(fd);
fd = open_file(filename);
line = get_next_line(fd);
while (line != NULL)
{
line = sanitize(line);
parse_line(map, line);
free(line);
line = get_next_line(fd);
}
close(fd);
return (map);
}
char *sanitize(char *line)
{
int i;
char *str;
i = -1;
str = line;
while (str && str[++i])
{
if (str[i] == '\t' || str[i] == '\n')
str[i] = '\t';
}
line = ft_strtrim(str, "\t");
free(str);
return (line);
}
int parse_line(t_map *map, char *line)
{
if (ft_strncmp(line, "A", 1) == 0)
return (parse_ambient(map, line));
if (ft_strncmp(line, "C", 1) == 0)
return (parse_camera(map, line));
if (ft_strncmp(line, "L", 1) == 0)
return (parse_light(map, line));
if (ft_strncmp(line, "sp", 2) == 0)
return (parse_sphere(map, line));
if (ft_strncmp(line, "pl", 2) == 0)
return (parse_plane(map, line));
if (ft_strncmp(line, "cy", 2) == 0)
return (parse_cylinder(map, line));
return (0);
}
int parse_float(char *str, float *num)
{
if (!is_float(str))
return (1);
*num = str_to_float(str);
return (0);
}
int parse_ulong(char *str, size_t *num)
{
if (!is_ulong(str))
return (1);
*num = (size_t)ft_atoi(str);
return (0);
}