-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
127 lines (117 loc) · 3.17 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
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
119
120
121
122
123
124
125
126
127
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bnidia <bnidia@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/23 14:27:18 by bnidia #+# #+# */
/* Updated: 2022/01/19 19:58:35 by bnidia ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
/*
* check_file(fd, t_file)
* check for presence of opened file in t_file ring structure
* if t_file not found return NULL
*/
t_file *check_file(int fd, t_file **file)
{
t_file *initial_position;
if ((*file)->fd == fd)
return (*file);
initial_position = *file;
*file = (*file)->next;
while (*file != initial_position)
{
if ((*file)->fd == fd)
return (*file);
*file = (*file)->next;
}
return (NULL);
}
/*
* init_file(t_file **file, int fd)
* creating a struct file, were we store data about read file
* and used buffer. It is circled struct.
* Line length research says that line length fall between 45 and
* 75 characters per line, medium is 66 chars (letters and spaces)
*/
t_file *init_file(int fd, t_file **file)
{
t_file *new_file;
new_file = (t_file *)malloc(sizeof(t_file));
if (new_file == NULL)
return (NULL);
new_file->fd = fd;
new_file->rb_size = 0;
new_file->next = new_file;
if (*file == NULL)
return (new_file);
new_file->next = (*file)->next;
(*file)->next = new_file;
*file = new_file;
return (*file);
}
void make_string(t_file *file)
{
while (42)
{
if (file->rb_size == 0)
{
file->rb_size = read(file->fd, file->rb_ar, BUFFER_SIZE);
if (file->rb_size <= 0)
return ;
file->rb_pos = 0;
}
if (file->s_size == 0 || file->s_size + 8 >= file->s_capacity)
ft_realloc(file);
if (file->str == NULL)
return ;
file->str[file->s_size++] = file->rb_ar[file->rb_pos++];
file->rb_size--;
if (file->rb_ar[file->rb_pos - 1] == '\n')
{
file->str[file->s_size] = '\0';
return ;
}
}
}
void ft_realloc(t_file *file)
{
char *return_string;
size_t i;
i = 0;
if (file->s_size == 0)
file->s_capacity = 128;
else if (file->s_capacity <= 33554432)
file->s_capacity *= 2;
else
file->s_capacity += 33554432;
return_string = (char *)malloc(file->s_capacity);
while (i < file->s_size && return_string != NULL)
{
return_string[i] = file->str[i];
i++;
}
free(file->str);
file->str = return_string;
}
char *delete_file(t_file **file)
{
t_file *del_file;
if (*file == (*file)->next)
{
free(*file);
*file = NULL;
return (NULL);
}
del_file = *file;
*file = (*file)->next;
while (del_file != (*file)->next)
*file = (*file)->next;
(*file)->next = del_file->next;
free(del_file);
del_file = NULL;
return (NULL);
}