-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils_bonus.c
113 lines (105 loc) · 3.42 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nkawaguc <nkawaguc@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/05 22:07:41 by nkawaguc #+# #+# */
/* Updated: 2024/05/05 22:07:41 by nkawaguc ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
// gnl_realloc: Reallocate memory with new_size.
// If the reallocation fails, free the original pointer and return NULL.
// If the reallocation succeeds, copy the original data to the new pointer.
// Return: the new pointer.
void *gnl_realloc(void *ptr, size_t new_size, size_t old_size)
{
void *new_ptr;
size_t i;
if (new_size == 0)
while (new_size == 0 || ((char *)ptr)[new_size - 1])
new_size++;
new_ptr = malloc(new_size);
if (new_ptr == NULL)
return (free(ptr), NULL);
i = 0;
while (i < old_size && i < new_size)
{
((char *)new_ptr)[i] = ((char *)ptr)[i];
i++;
}
free(ptr);
return (new_ptr);
}
// gnl_strncat: Concatenate src to dst.
// If the new_size of dst is not enough, it is reallocated in gnl_realloc.
// The size of the concatenated string of src is stored in cat_size.
// Return: the size of the concatenated string of src.
int gnl_strncat(char **dst, const char *src, int *line_size, int *cat_size)
{
int i;
int j;
i = -1;
while ((*dst)[++i])
;
j = 0;
while (src[j] && (j == 0 || src[j - 1] != '\n'))
{
if (i + j + 1 >= *line_size)
{
*dst = gnl_realloc(*dst, *line_size * 2, *line_size);
if (*dst == NULL)
{
*cat_size = -1;
return (-1);
}
*line_size *= 2;
}
(*dst)[i + j] = src[j];
j++;
}
(*dst)[i + j] = '\0';
*cat_size = j;
return (j);
}
// gnl_bzero: Initialize buf with '\0'.
// Return: None
void gnl_bzero(void *buf, size_t size)
{
while (size)
((char *)buf)[--size] = '\0';
}
// gnl_read: Read from fd to buf and concatenate buf to line.
// If line is not empty, concatenate buf to line.
// If '\n' is not found in buf, read until '\n' is found or EOF is reached.
// Move the characters after first '\n' to the beginning of buf.
// Return: whether the reading is successful. (1: success, 0: EOF, -1: error)
int gnl_read(int fd, char **line, int *line_size, char *buf)
{
int read_size;
int cat_size;
int i;
cat_size = 0;
if (line[0] && gnl_strncat(line, buf, line_size, &cat_size) == -1)
return (-1);
if (!buf[cat_size] && (cat_size == 0 || buf[cat_size - 1] != '\n'))
{
while (1)
{
read_size = read(fd, buf, BUFFER_SIZE);
if (read_size == -1 || read_size == 0)
return (read_size);
buf[read_size] = '\0';
if (gnl_strncat(line, buf, line_size, &cat_size) <= 0)
return (cat_size);
if (buf[cat_size - 1] == '\n')
break ;
}
}
i = -1;
while (++i <= BUFFER_SIZE - cat_size)
buf[i] = buf[cat_size + i];
return (1);
}