-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.c
114 lines (101 loc) · 1.98 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
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
#include "main.h"
/**
* nod_by_base - a function that calculate the number of
* digit needed to print the given int in a specific base
* @n: unsigned int
* @base: unsigned int
* Return: int
*/
int nod_by_base(unsigned int n, unsigned int base)
{
unsigned int i;
for (i = 0; n > 0; i++)
{
n /= base;
}
return (i);
}
/**
* store_base_reverse - store a number converted in a base in reverse
*
* @n: number to convert in base
*
* @base: the base to convert to
*
* @upper: flag for printing in upper case for base greater than 10
*
* @buffer: buffer to store result before printing
*
* Return: the buffer that contain the number converted in base
* stored in reverse
*
*/
char *store_base_reverse(unsigned int n, int base, int upper,
char *buffer)
{
int i, r;
i = 0;
while (n > 0)
{
r = n % base;
if (r < 10)
buffer[i++] = r + '0';
else
{
if (upper)
buffer[i++] = r + 55;
else
buffer[i++] = r + 87;
}
n /= base;
}
return (buffer);
}
/**
* search_spec_to_print - search a valid specifier to use to print
*
* @format: string to search for a valid specifier
*
* @i: position of start of a specifier
*
* @counter: number of bytes already written to stdout
*
* @chartof: a structure that map characters to functions
*
* @args: variable argument
*
* @buffer: buffer to store result before printing
*
* Return: the current index in the structure `chartof`
*/
int search_spec_to_print(const char *format, int i, int *counter,
fspec *chartof, va_list args, char *buffer)
{
int j;
j = 0;
while (chartof[j].c)
{
if (format[i + 1] == chartof[j].c)
{
*counter += chartof[j].func(args, buffer);
break;
}
j++;
}
return (j);
}
/**
* flush_buffer - send buffer content to stdout
*
* @buffer: a buffer that holds data
*
* Return: the number of bytes send to stdout
*/
int flush_buffer(char *buffer)
{
int i, j;
i = write(1, buffer, _strlen(buffer));
for (j = 0; buffer[j] != '\0'; j++)
buffer[j] = '\0';
return (i);
}