-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer_functions.c
More file actions
89 lines (85 loc) · 1.85 KB
/
buffer_functions.c
File metadata and controls
89 lines (85 loc) · 1.85 KB
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
#include "holberton.h"
#include <unistd.h>
#include <stdlib.h>
/**
* create_buff - creates a buffer in memory and sets to 0
* @bytes: number of bytes to allocate for buffer
*
* Return: char pointer to beginning of buffer
*/
char *create_buff(unsigned int bytes)
{
char *new_buff;
new_buff = malloc(bytes);
if (!new_buff)
return (NULL);
/*for (i = 0; i < bytes; i++)*/
/* new_buff[i] = 0; */
return (new_buff);
}
/**
* copy_buff - copies a string into the buffer
* @str: string to copy into buffer
* @help_s: pointer to printh_t struct containing buffer, buff_i, etc..
*
* Return: number of bytes written
*/
unsigned int copy_buff(char *str, printh_t *help_s)
{
unsigned int i = 0;
if (!str || !help_s->buff)
return (0);
while (str[i] && help_s->buff_i < BUFF_SIZE)
{
help_s->buff[help_s->buff_i] = str[i];
help_s->buff_i++, i++;
if (help_s->buff_i == BUFF_SIZE)
{
print_buff(help_s->buff, BUFF_SIZE);
free(help_s->buff);
help_s->buff = create_buff(BUFF_SIZE);
if (!help_s->buff)
return (i);
help_s->buff_i = 0;
}
}
if (help_s->c != str) /* addresses */
free(str);
return (i);
}
/**
* print_buff - prints the contents of the buffer
* @buff: buffer to print
* @buff_size: size of buffer in bytes
*
* Return: number of bytes written
*/
unsigned int print_buff(char *buff, unsigned int buff_size)
{
unsigned int bytes_written;
bytes_written = write(1, buff, buff_size);
return (bytes_written);
}
/**
* free_all - Frees all memory in structure and args.
* @help_s: The pointer to a malloced structure.
* @args: Pointer to a va_list.
*
* Return: Void.
*/
void free_all(printh_t *help_s, va_list args)
{
if (help_s)
{
if (help_s->flags)
free(help_s->flags);
if (help_s->buff)
free(help_s->buff);
if (help_s->c)
free(help_s->c);
if (help_s->mods)
free(help_s->mods);
free(help_s);
}
va_end(args);
}