-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
90 lines (81 loc) · 1.92 KB
/
get_next_line_utils.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_next_line_utils.c :+: :+: */
/* +:+ */
/* By: jvan-hal <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/12/01 09:40:58 by jvan-hal #+# #+# */
/* Updated: 2023/01/09 11:08:18 by jvan-hal ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
int gnl_strlen(const char *s)
{
int count;
count = 0;
if (!s)
return (0);
while (*s)
{
++count;
++s;
}
return (count);
}
static char *extendmem(char *mem, int increment, int i, int prevsize)
{
char *newmem;
newmem = ft_calloc(prevsize + increment + 1, sizeof(char));
if (!newmem)
{
if (mem)
free(mem);
return (NULL);
}
while (i < prevsize)
{
newmem[i] = mem[i];
++i;
}
newmem[prevsize + increment] = '\0';
if (mem)
free(mem);
return (newmem);
}
int copytomem(char **mem, char *src, int i, int len)
{
int memlen;
memlen = gnl_strlen(*mem);
*mem = extendmem(*mem, len, 0, memlen);
if (!*mem)
return (-2);
while (src[i])
{
if (src[i] == '\n')
{
(*mem)[memlen + i] = src[i];
(*mem)[memlen + i + 1] = '\0';
return (i);
}
else
{
(*mem)[memlen + i] = src[i];
}
++i;
}
return (-1);
}
void shiftmem(char *mem, int shiftlen)
{
int i;
int memlen;
i = 0;
memlen = gnl_strlen(mem);
while (i < memlen + 1 - shiftlen)
{
mem[i] = mem[i + shiftlen];
++i;
}
}