-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.c
78 lines (74 loc) · 1.83 KB
/
write.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
#include "main.h"
/**
* print_buffer - Prints given string and adds its length to counter
* @data: Pointer to structure containing general data
* @str: String to print
* @len: Length of @s
*/
void print_buffer(format_t *data, const char *str, size_t len)
{
data->nb_written_bytes += len;
write(1, str, len);
}
/**
* handle_buffer_overflow - Print buffer handler for longer strings
* @str: String to print
* @len: Length of @str
* @data: Pointer to structure containing general data
* Return: 0 if no error
*/
int handle_buffer_overflow(const char *str, size_t len, format_t *data)
{
while (len)
{
if (len > BUFF_SIZE - 1)
{
print_buffer(data, str, BUFF_SIZE - 1);
len -= BUFF_SIZE - 1;
str += BUFF_SIZE - 1;
}
else
{
_strcpy(data->buffer, str);
data->bufferlen = _strlen(data->buffer);
len = 0;
}
}
return (0);
}
/**
* write_buffer - Writes data into buffer, and prints and empties it when full
* @str: String to add to buffer
* @len: Size of @str
* @data: Pointer to structure containing general data
* Return: 0 if no error
*/
int write_buffer(const char *str, size_t len, format_t *data)
{
size_t i;
if (!str)
{
print_buffer(data, data->buffer, data->bufferlen);
data->buffer[0] = '\0';
data->bufferlen = 0;
}
if (len + data->bufferlen >= BUFF_SIZE - 1)
{
for (i = 0; i < len && i + data->bufferlen < BUFF_SIZE - 1; i++)
data->buffer[data->bufferlen + i] = str[i];
data->buffer[data->bufferlen + i] = '\0';
data->bufferlen += i;
print_buffer(data, data->buffer, data->bufferlen);
data->buffer[0] = '\0';
data->bufferlen = 0;
handle_buffer_overflow(str + i, len - i, data);
}
else
{
for (i = 0; i < len && i + data->bufferlen < BUFF_SIZE - 1; i++)
data->buffer[data->bufferlen + i] = str[i];
data->buffer[data->bufferlen + i] = '\0';
data->bufferlen += i;
}
return (0);
}