-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.c
79 lines (67 loc) · 1.3 KB
/
helpers.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
#include "main.h"
/**
* init_bufff - Initialize the buffer and handle error
*
* Return: the created buffer if successful, NULL otherwise
*/
buf *init_buff()
{
buf *my_buffer = malloc(sizeof(buf));
if (!my_buffer)
return (NULL);
my_buffer->final_str = malloc(1024 * sizeof(char));
if (!my_buffer->final_str)
{
free(my_buffer);
return (NULL);
}
my_buffer->head = my_buffer->final_str;
my_buffer->count = 0;
my_buffer->tmp = 0;
return (my_buffer);
}
/**
* update_buff - update the buffer and/or reset buffer if full
*
* @my_buffer: the buffer
* @value: value to add to buffer
*/
void update_buff(buf *my_buffer, char value)
{
if (my_buffer->tmp == 1024)
{
write(STDOUT_FILENO, my_buffer->head, my_buffer->tmp);
my_buffer->final_str = my_buffer->head;
my_buffer->tmp = 0;
}
*(my_buffer->final_str) = value;
(my_buffer->count)++;
(my_buffer->tmp)++;
(my_buffer->final_str)++;
}
/**
* cleanup_buff - free all allocated memory to buff
*
* @my_buffer: buffer to clean up
*/
void cleanup_buff(buf *my_buffer)
{
free(my_buffer->head);
free(my_buffer);
}
/**
* get_int_len - Get the length of an integer
*
* @value: integer to evaluate
* Return: leength of the integer
*/
int get_int_len(int value)
{
int len = 1;
while (value > 9)
{
len++;
value /= 10;
}
return (len);
}