-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_write_to_buffer.c
54 lines (53 loc) · 969 Bytes
/
_write_to_buffer.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
#include "holberton.h"
/**
* _write - Add char to buffer, write and reset buffer if full.
* @b_r: buffer to write
* @c: char to write
*/
void _write(buffer *b_r, char c)
{
if (b_r->bp == 1024)
{
write(1, b_r->buf, b_r->bp);
b_r->printed += b_r->bp;
b_r->bp = 0;
}
b_r->buf[b_r->bp++] = c;
}
/**
* _write_str - Add string to buffer, write and reset buffer if full.
* @b_r: buffer to write
* @s: string to write
*/
void _write_str(buffer *b_r, char *s)
{
while (*s != '\0')
{
if (b_r->bp == 1024)
{
write(1, b_r->buf, b_r->bp);
b_r->printed += b_r->bp;
b_r->bp = 0;
}
b_r->buf[b_r->bp++] = *s++;
}
}
/**
* _write_tmpbuf - Add tmpbuf to buffer, write and reset buffer if full.
* @b_r: buffer to write
*/
void _write_tmpbuf(buffer *b_r)
{
int i;
i = 0;
while (i < b_r->tp)
{
if (b_r->bp == 1024)
{
write(1, b_r->buf, b_r->bp);
b_r->printed += b_r->bp;
b_r->bp = 0;
}
b_r->buf[b_r->bp++] = b_r->tmpbuf[i];
}
}