-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_utils.c
135 lines (121 loc) · 3.23 KB
/
ft_printf_utils.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rlucio-l <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/21 18:18:30 by rlucio-l #+# #+# */
/* Updated: 2021/10/22 15:19:51 by rlucio-l ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/*
** DESCRIPTION
** Outputs 'character' to the given file descriptor.
** RETURN VALUE
** Returns the number of characters printed.
*/
int print_char_fd(char character, int fd)
{
write(fd, &character, sizeof(character));
return (1);
}
/*
** DESCRIPTION
** Outputs 'string' to the given file descriptor.
** RETURN VALUE
** Returns the number of characters printed.
*/
int print_str_fd(char *string, int fd)
{
char *pointer;
pointer = string;
if (pointer == NULL)
return (print_str_fd("(null)", 1));
while (*string)
{
print_char_fd(*string, fd);
string++;
}
return (string - pointer);
}
/*
** DESCRIPTION
** Outputs 'number' to the given file descriptor.
** RETURN VALUE
** Returns the number of characters printed.
*/
int print_nbr_fd(int number, int fd)
{
unsigned int unsigned_number;
int chars_printed;
chars_printed = 0;
if (number < 0)
{
chars_printed += print_char_fd('-', fd);
unsigned_number = -number;
}
else
unsigned_number = number;
if (unsigned_number > 9)
chars_printed += print_nbr_fd((unsigned_number / 10), fd);
chars_printed += print_char_fd(((unsigned_number % 10) + '0'), fd);
return (chars_printed);
}
char *reverse_string(char *string)
{
int first_index;
int last_index;
char *ptr;
char temp;
first_index = 0;
ptr = string;
while (*ptr != '\0')
ptr++;
last_index = (ptr - string) - 1;
while (first_index < last_index)
{
temp = string[first_index];
string[first_index] = string[last_index];
string[last_index] = temp;
first_index++;
last_index--;
}
return (string);
}
/*
** DESCRIPTION
** Allocates (with malloc(3)) and returns a string
** representing the integer received as an argument
** converted to a given base.
** RETURN VALUE
** The string representing the integer converted.
** NULL if the allocation fails.
*/
char *itoa_base(unsigned long number, int base, char specifier)
{
char *digits;
char *string;
int i;
int uppercase_shift;
digits = "0123456789abcdef0123456789ABCDEF";
string = malloc(42);
if (!string)
return (NULL);
i = 0;
if (specifier == 'X')
uppercase_shift = 16;
else
uppercase_shift = 0;
string[i++] = digits[(number % base) + uppercase_shift];
number /= base;
while (number > 0)
{
string[i++] = digits[(number % base) + uppercase_shift];
number /= base;
}
string[i] = '\0';
reverse_string(string);
return (string);
}