-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_func.c
More file actions
159 lines (132 loc) · 2.36 KB
/
write_func.c
File metadata and controls
159 lines (132 loc) · 2.36 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "main.h"
/**
* print_char - Prints a character
*
* @c: Character to be printed
*
* Return: On success 1.
* On error, -1 is returned, and errno is set appropriately.
*/
int print_char(char c)
{
return (write(1, &c, 1));
}
/**
* print_str - Prints a string
*
* @str: Pointer to string to be printed
*
* Return: Lenght of string printed
*/
int print_str(char *str)
{
int indx;
for (indx = 0; str[indx] != '\0'; indx++)
print_char(str[indx]);
return (indx);
}
/**
* print_num - Prints an integer value
*
* @num: Integer value to print
*
* Return: Lenght of number;
*/
int print_num(size_t num)
{
int iter, ld, len, size;
size_t num_len = num;
char ch, *buf;
for (len = 0; num_len > 0; len++)
num_len /= 10;
buf = malloc(sizeof(char) * (len + 1));
if (buf == NULL)
return (0);
size = len;
for (iter = 0; num > 0; iter++)
{
ld = num % 10;
ch = ld + 48;
--size;
buf[size] = ch;
num /= 10;
}
buf[len] = '\0';
print_str(buf);
free(buf);
return (len);
}
/**
* print_hex - Prints a the hexadecimal value of an integer
*
* @num: Integer to convert to hex
* @hex: Hex value
* @hex_case: Hexadecimal case value
*
* Return: Lenght printed
*/
int print_hex(unlint_t num, int hex, int hex_case)
{
int indx, rem, num_len, size, len = 0;
unlint_t div_num;
char *buf, ch;
if (num < 10)
{
len += print_char(num + 48);
return (len);
}
else if (num > 9 && num < 16)
{
rem = num;
ch = check_hex(rem, hex_case);
len += print_char(ch);
return (len);
}
div_num = num;
for (num_len = 0; div_num != 0; num_len++)
div_num /= hex;
buf = malloc(sizeof(char) * (num_len + 1));
if (buf == NULL)
return (0);
size = num_len;
div_num = num;
for (indx = 0; div_num != 0; indx++)
{
rem = div_num % hex;
ch = check_hex(rem, hex_case);
buf[--size] = ch;
div_num /= hex;
}
buf[num_len] = '\0';
len += print_str(buf);
free(buf);
return (len);
}
/**
* check_hex - Check for upper case hex or lower case hex value
*
* @rem: Integer to check and convert to char
* @hex_case: Hexadecimal value
*
* Return: Uppercase hex value if @hex_case == 0
* else Lowercase value if @hex_case == 1
*/
char check_hex(int rem, int hex_case)
{
char ch;
if (hex_case == 1)
{
if (rem > 9)
ch = rem + 87;
else
ch = rem + 48;
}
else
{
if (rem > 9)
ch = rem + 55;
else
ch = rem + 48;
}
return (ch);
}