-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_data_hand.c
More file actions
99 lines (93 loc) · 1.94 KB
/
2_data_hand.c
File metadata and controls
99 lines (93 loc) · 1.94 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
88
89
90
91
92
93
94
95
96
97
98
99
#include "0_raise.h"
/**
* file_to_array - Take out file data into an array.
* @var: Struct with common variables.
* Return: 0 Success, 1 Failed.
*/
int file_to_array(var_s *var)
{
FILE *file;
char *buff = NULL, *token;
size_t num = 0;
int i;
if (var->f_name == NULL)
{
fprintf(stderr, "Invalid file name\n");
return (1);
}
file = fopen(var->f_name, "r");
if (!file)
{
fprintf(stderr, "Could not open file\n");
return (1);
}
for (; getline(&buff, &num, file) != -1; var->row++) /* Get Size */
if (var->row == 0)
{
token = strtok(buff, " \n");
for (; token; var->col++)
token = strtok(NULL, " \n");
}
var->array = malloc(sizeof(coord *) * var->row);
if (!var->array)
{
fprintf(stderr, "Error creating malloc Row\n");
return (1);
}
for (i = 0; i < var->row; i++)
{
var->array[i] = malloc(sizeof(coord) * var->col);
if (!var->array[i])
{
fprintf(stderr, "Error creating malloc Column\n");
return (1);
}
}
free(buff), fclose(file), data_to_array(var);
return (0);
}
/**
* data_to_array - Take out file data into an array.
* @var: Struct with common variables.
* Return: 0 Success, 1 Failed.
*/
void data_to_array(var_s *var)
{
FILE *file;
char *buff = NULL, *token;
size_t num = 0;
int i, j;
file = fopen(var->f_name, "r"); /* Assign coodenates */
if (!file)
{
fprintf(stderr, "Could not open file\n");
return;
}
for (i = 0; getline(&buff, &num, file) != -1; i++)
{
token = strtok(buff, " \n");
for (j = 0; token; j++)
{
var->array[i][j].x = (SCREEN_WIDTH / (var->col + 8)) * (i + 1);
var->array[i][j].y = (SCREEN_HEIGHT / (var->row + 3)) * (j + 1);
var->array[i][j].z = atof(token);
token = strtok(NULL, " \n");
}
}
free(buff);
fclose(file);
}
/**
* free_array - Free coorditates.
* @var: Struct with common variables.
* Return: 0 Success, 1 Failed.
*/
void free_array(var_s *var)
{
int i;
for (i = 0; i < var->row; i++)
{
free(var->array[i]);
}
free(var->array);
}