-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_number.c
94 lines (85 loc) · 2.31 KB
/
parse_number.c
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
88
89
90
91
92
93
94
/* ************************************************************************** */
/* */
/* :::::::: */
/* parse_number.c :+: :+: */
/* +:+ */
/* By: dnoom <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/04/06 11:25:33 by dnoom #+# #+# */
/* Updated: 2022/04/06 11:25:33 by dnoom ######## odam.nl */
/* */
/* ************************************************************************** */
#include "miniRT.h"
void digit_error(t_parse_line *line)
{
printf("Expected digit at line %d, column %d, found '%c'\n",
line->line_nr, line->i + 1, line->line[line->i]);
error("Parse error");
}
void parse_char(t_parse_line *line, char *c)
{
*c = line->line[line->i];
line->i++;
}
void parse_int(t_parse_line *line, int *i)
{
int sign;
char c;
long l;
l = 0;
sign = 1;
c = line->line[line->i];
if (c == '-')
sign = -1;
if (c == '-' || c == '+')
c = line_next(line);
if (!ft_isdigit(c))
digit_error(line);
while (ft_isdigit(c))
{
l = l * 10 + sign * (c - '0');
c = line_next(line);
check_long_over_int(l, line);
}
*i = l;
}
void parse_decimals(t_parse_line *line, double *f, float sign)
{
char c;
float after_dot;
float after_dot_mul;
c = line_next(line);
after_dot = 0;
after_dot_mul = sign;
while (ft_isdigit(c))
{
after_dot_mul /= 10;
after_dot = after_dot * 10 + (c - '0');
c = line_next(line);
}
*f += after_dot * after_dot_mul;
}
void parse_float(t_parse_line *line, float *f)
{
float sign;
char c;
double d;
d = 0;
sign = 1;
c = line->line[line->i];
if (c == '-')
sign = -1;
if (c == '-' || c == '+')
c = line_next(line);
if (!ft_isdigit(c) && !(c == '.' && ft_isdigit(line->line[line->i + 1])))
digit_error(line);
while (ft_isdigit(c))
{
d = d * 10 + sign * (c - '0');
c = line_next(line);
check_double_over_float(d, line);
}
if (c == '.')
parse_decimals(line, &d, sign);
*f = d;
}