-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_read_and_make_instructions.c
93 lines (83 loc) · 2.01 KB
/
ft_read_and_make_instructions.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_read_and_make_instructions.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dlana <dlana@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/30 20:39:52 by dlana #+# #+# */
/* Updated: 2021/10/02 14:21:58 by dlana ### ########.fr */
/* */
/* ************************************************************************** */
#include "checker.h"
static size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
if (!s)
return (0);
while (s[i] != '\0')
i++;
return (i);
}
static void ft_killer(char **str)
{
if (*str)
{
free (*str);
*str = NULL;
}
}
static char *ft_join_str_and_ch(const char *s1, char c, int rd)
{
char *s2;
int i;
i = 0;
s2 = 0;
if (c == '\n')
ft_check_instructions(s1, rd - 1);
if (!c)
ft_error();
s2 = (char *)malloc(sizeof(char) * (ft_strlen(s1) + 2));
if (!s2)
ft_error();
while (s1[i])
{
s2[i] = s1[i];
i++;
}
s2[i] = c;
i++;
s2[i] = '\0';
return (s2);
}
static int ft_read_instructions(char **line)
{
int rd;
char ch;
char *str;
rd = 0;
*line = malloc(sizeof(char));
while (read(0, &ch, 1) > 0)
{
str = *line;
*line = ft_join_str_and_ch(str, ch, rd);
ft_killer(&str);
rd++;
}
return (rd);
}
int ft_read_and_make_instructions(t_point *points)
{
int rd;
char *line;
int count_instructions;
line = NULL;
count_instructions = 0;
rd = ft_read_instructions(&line);
if (line[0] != '\0')
count_instructions = ft_make_instructions(points, line);
free (line);
line = NULL;
return (count_instructions);
}