-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter_specifiers.c
128 lines (112 loc) · 2.9 KB
/
character_specifiers.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
#include "main.h"
char hex_to_ascii(int digit);
/**
* str_format_handler - Handles the %s format specifier and
* prints the argument corresponding to it
* @ap: Argument pointer
* @count: The number of printed characters
* @total: A pointer to the total number of characters printed
* @buffer: A pointer to the buffer holding the characters to be printed
* @field_width: The field width
*/
void str_format_handler(va_list ap, int *count, int *total, char *buffer,
int field_width)
{
int i, len;
char *string = va_arg(ap, char *);
if (string == NULL)
string = "(null)";
len = find_length(string);
if (field_width > 0)
{
field_width -= len;
field_width_handler(field_width, buffer, count, total);
}
for (i = 0; i < len; i++)
{
buffer[(*count)++] = string[i];
buffer_status_handler(count, total, buffer);
}
}
/**
* char_format_handler - Handles the %c format specifier and
* prints the argument corresponding to it
* @ap: Argument pointer
* @count: A pointer to the number of printed characters
* @total: A pointer to the total number of characters printed
* @buffer: A pointer to the buffer holding the characters to be printed
* @field_width: The field width
*/
void char_format_handler(va_list ap, int *count, int *total, char *buffer,
int field_width)
{
char character = (char)va_arg(ap, int);
if (field_width > 0)
{
field_width -= 1;
field_width_handler(field_width, buffer, count, total);
}
buffer[(*count)++] = character;
buffer_status_handler(count, total, buffer);
}
/**
* custom_S_handler - prints the string
* (non printable characters displayed as /x, followed
* by ASCII code value in hexadecimal)
*
* @ap: Argument pointer
* @count: number of printed characters
* @total: A pointer to the total number of characters printed
* @buffer: A pointer to the buffer holding the characters to be printed
* @field_width: The field width
*/
void custom_S_handler(va_list ap, int *count, int *total, char *buffer,
int field_width)
{
int i, len;
char *string = va_arg(ap, char *);
UNUSED(field_width);
if (string != NULL)
{
len = find_length(string);
for (i = 0; i < len; i++)
{
if (string[i] < 32 || string[i] >= 127)
{
buffer[*count] = '\\';
(*count)++;
buffer[*count] = 'x';
(*count)++;
buffer[*count] = hex_to_ascii(string[i] / 16);
(*count)++;
buffer[*count] = hex_to_ascii(string[i] % 16);
(*count)++;
}
else
{
buffer[*count] = string[i];
(*count)++;
}
if (*count == 1024)
{
*total += write(1, (const void *)buffer, *count);
*count = 0;
}
}
}
}
/**
* hex_to_ascii - Converts a hexadecimal digit to its ASCII
* @digit: The hexadecimal digit (0-15)
*
* Return: The ASCII representation of the hexadecimal digit
*/
char hex_to_ascii(int digit)
{
if (digit >= 0 && digit <= 9)
return (digit + '0');
else if (digit >= 10 && digit <= 15)
return (digit - 10 + 'A');
else
return ('\0');
}