-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils_bonus.c
118 lines (107 loc) · 2.18 KB
/
get_next_line_utils_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: xbeheydt <xbeheydt@42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/11 19:05:04 by xbeheydt #+# #+# */
/* Updated: 2023/01/11 19:05:08 by xbeheydt ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
char *ft_strjoin(char *s1, char *s2)
{
char *ptr;
int i;
int j;
i = 0;
j = 0;
ptr = ft_calloc(sizeof(char), (ft_strlen(s1) + ft_strlen(s2) + 1));
if (ptr == NULL)
return (NULL);
while (s1[i] != '\0')
{
ptr[i] = s1[i];
i++;
}
while (s2[j] != '\0')
{
ptr[i] = s2[j];
i++;
j++;
}
ptr[i] = '\0';
return (ptr);
}
size_t ft_strlen(char *s)
{
int i;
if (s == NULL)
return (0);
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char *freeandreplace(char *oldstr, char *stradd, int ind)
{
char *nptr;
if (ind >= 0)
{
nptr = ft_strjoin("", &stradd[ind + 1]);
if (oldstr != NULL)
{
free(oldstr);
oldstr = NULL;
}
return (nptr);
}
else
{
nptr = ft_strjoin(oldstr, stradd);
free (oldstr);
return (nptr);
}
}
void *ft_calloc(size_t nmemb, size_t size)
{
void *ptr;
size_t i;
i = 0;
ptr = malloc(nmemb * size);
if (ptr == NULL)
return (NULL);
while (i < (nmemb * size))
{
((char *)(ptr))[i] = '\0';
i++;
}
return (ptr);
}
int readforterm(char *buf, int toggle)
{
int i;
i = 0;
if (toggle == 1)
{
while (buf[i] != '\0')
{
if (buf[i] == '\n')
return (i);
i++;
}
return (-1);
}
else
{
i = 0;
while (i < BUFFER_SIZE)
{
if (buf[i] == '\n' || buf[i] == '\0')
return (i);
i++;
}
return (-1);
}
}