-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_errors.c
89 lines (81 loc) · 2.1 KB
/
handle_errors.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_errors.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anel-bou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/21 19:13:45 by anel-bou #+# #+# */
/* Updated: 2020/01/21 21:01:09 by anel-bou ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
#define ERR_MSG "Data error: one of the given lines is smaller than the first"
void ft_usage(void)
{
ft_putendl("Usage : ./fdf <filename>");
exit(0);
}
int check_error(char *file)
{
int fd;
char *line;
fd = open(file, O_RDONLY);
if (get_next_line(fd, &line) <= 0)
{
if (get_next_line(fd, &line) == 0)
{
ft_putendl("no data file");
exit(0);
}
perror("");
exit(0);
}
ft_strdel(&line);
return (1);
}
int calc_line_len(char **line)
{
int value;
int n;
value = 0;
n = (*(line)[0] == ' ' ? 1 : 0);
while ((*line)[n])
{
value++;
while (((*line)[n] && (*line)[n] >= '0' &&
(*line)[n] <= '9') || (*line)[n] == '-')
n++;
if ((*line)[n] == ',')
while ((*line)[n] && (*line)[n] != ' ')
n++;
while ((*line)[n] && (*line)[n] == ' ')
n++;
}
ft_strdel(line);
return (value);
}
int check_line_alignment(char *file)
{
char *line;
int fd;
int first_line;
fd = open(file, O_RDONLY);
get_next_line(fd, &line);
if (line[0] == 0)
{
ft_putendl("the fist line is empty");
ft_strdel(&line);
exit(0);
}
first_line = calc_line_len(&line);
while (get_next_line(fd, &line))
{
if (first_line > calc_line_len(&line))
{
ft_putendl(ERR_MSG);
exit(0);
}
}
return (1);
}