-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_values.c
121 lines (111 loc) · 3.16 KB
/
read_values.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_values.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emende <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/02 13:08:03 by emende #+# #+# */
/* Updated: 2022/04/09 20:41:16 by emende ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
static void update_count(t_vars *v, int fd, int ret, char *line)
{
ret = get_next_line(fd, &line);
if (ret < 1)
panic("error: File was empty or directory\n", v);
v->split = ft_strsplit(line, ' ');
if (!v->split)
panic("error: malloc failed.\n", v);
ft_strdel(&line);
v->col_count = (int) ft_arrlen((const void **) v->split) - 1;
free_strarr(&v->split);
while (ret)
{
ret = get_next_line(fd, &line);
if (ret)
{
v->split = ft_strsplit((char const *) line, ' ');
if (!v->split)
panic("error: malloc failed.\n", v);
ft_strdel(&line);
if ((ft_arrlen((const void **) v->split) - 1) \
!= (size_t) v->col_count)
panic("error: Differences on lines.\n", v);
free_strarr(&v->split);
}
v->row_count++;
}
}
static int *atoi_splits(char **splits, int col)
{
int *split;
int i;
split = (int *) malloc(sizeof(int) * (size_t)(col + 2));
if (!split)
return (NULL);
i = 0;
while (i <= col)
{
split[i] = ft_atoi(splits[i]);
i++;
}
return (split);
}
static void altitudes_to_array(t_vars *v, int fd)
{
char *line;
char **splits;
int i;
v->arr = (int **) malloc(sizeof(int *) * (size_t)v->row_count);
if (!v->arr)
panic("error: malloc fail.\n", v);
i = 0;
while (i < v->row_count)
{
if (get_next_line(fd, &line) < 1)
panic("error: get_next_line error\n", v);
splits = ft_strsplit((char const *) line, ' ');
if (!splits)
panic("error: ft_strsplit failed.\n", v);
ft_strdel(&line);
v->arr[i] = atoi_splits(splits, v->col_count);
if (!(v->arr[i]) && free_intarr(&v->arr, i) && free_strarr(&splits))
panic("error: malloc failed.\n", v);
free_strarr(&splits);
i++;
}
close(fd);
}
static void count_altitudes(t_vars *v, int **points)
{
int row;
int col;
v->max_altitude = points[0][0];
v->min_altitude = points[0][0];
row = -1;
while (++row < v->row_count)
{
col = -1;
while (++col < v->col_count)
{
if (points[row][col] > v->max_altitude)
v->max_altitude = points[row][col];
if (points[row][col] > v->max_altitude)
v->min_altitude = points[row][col];
}
}
}
void read_values(int fd, char *argv, t_vars *v)
{
char *line;
line = NULL;
update_count(v, fd, 0, line);
close(fd);
fd = open(argv, O_RDONLY);
if (fd < 0)
panic("error: Open failed.\n", v);
altitudes_to_array(v, fd);
count_altitudes(v, v->arr);
}