-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_converters.c
103 lines (88 loc) · 2.66 KB
/
base_converters.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
#include "main.h"
unsigned int convert_sbase(buffer_t *output, long int num, char *base,
unsigned char flags, int wid, int prec);
unsigned int convert_ubase(buffer_t *output,
unsigned long int num, char *base,
unsigned char flags, int wid, int prec);
/**
* convert_sbase - Converts a signed long to an inputted base and stores
* the result to a buffer contained in a struct.
* @output: A buffer_t struct containing a character array.
* @num: A signed long to be converted.
* @base: A pointer to a string containing the base to convert to.
* @flags: Flag modifiers.
* @wid: A width modifier.
* @prec: A precision modifier.
*
* Return: The number of bytes stored to the buffer.
*/
unsigned int convert_sbase(buffer_t *output, long int num, char *base,
unsigned char flags, int wid, int prec)
{
int size;
char digit, pad = '0';
unsigned int ret = 1;
for (size = 0; *(base + size);)
size++;
if (num >= size || num <= -size)
ret += convert_sbase(output, num / size, base,
flags, wid - 1, prec - 1);
else
{
for (; prec > 1; prec--, wid--) /* Handle precision */
ret += _memcpy(output, &pad, 1);
if (NEG_FLAG == 0) /* Handle width */
{
pad = (ZERO_FLAG == 1) ? '0' : ' ';
for (; wid > 1; wid--)
ret += _memcpy(output, &pad, 1);
}
}
digit = base[(num < 0 ? -1 : 1) * (num % size)];
_memcpy(output, &digit, 1);
return (ret);
}
/**
* convert_ubase - Converts an unsigned long to an inputted base and
* stores the result to a buffer contained in a struct.
* @output: A buffer_t struct containing a character array.
* @num: An unsigned long to be converted.
* @base: A pointer to a string containing the base to convert to.
* @flags: Flag modifiers.
* @wid: A width modifier.
* @prec: A precision modifier.
*
* Return: The number of bytes stored to the buffer.
*/
unsigned int convert_ubase(buffer_t *output, unsigned long int num, char *base,
unsigned char flags, int wid, int prec)
{
unsigned int size, ret = 1;
char digit, pad = '0', *lead = "0x";
for (size = 0; *(base + size);)
size++;
if (num >= size)
ret += convert_ubase(output, num / size, base,
flags, wid - 1, prec - 1);
else
{
if (((flags >> 5) & 1) == 1) /* Printing a ptr address */
{
wid -= 2;
prec -= 2;
}
for (; prec > 1; prec--, wid--) /* Handle precision */
ret += _memcpy(output, &pad, 1);
if (NEG_FLAG == 0) /* Handle width */
{
pad = (ZERO_FLAG == 1) ? '0' : ' ';
for (; wid > 1; wid--)
ret += _memcpy(output, &pad, 1);
}
if (((flags >> 5) & 1) == 1) /* Print 0x for ptr address */
ret += _memcpy(output, lead, 2);
}
digit = base[(num % size)];
_memcpy(output, &digit, 1);
return (ret);
}