-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_printf.c
244 lines (213 loc) · 5.37 KB
/
my_printf.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>
#define UINT_MAX 4294967295
#define INT_MAX 2147483647
#define UINT_MIN 0
#define INT_MIN -2147483648
int* my_putchar(char c, int*count){
write(1, &c , 1);
*count +=1;
return count;
}
int * my_putstr(char *param, int *count){
int index = 0;
while(param[index] != '\0'){
write(1, ¶m[index], 1);
*count +=1;
index++;
}
return count;
}
char my_itoa(int number){
if(number==0){
number = 48;
}
else if(number==1){
number = 49;
}
else if(number == 2){
number = 50;
}
else if(number == 3){
number = 51;
}
else if(number == 4){
number = 52;
}
else if(number == 5){
number = 53;
}
else if(number == 6){
number = 54;
}
else if(number == 7){
number = 55;
}
else if(number == 8){
number = 56;
}
else if(number == 9){
number = 57;
}
return number;
}
void print_buffer(int *sorted_buffer, int *count){
int index = 0;
while(sorted_buffer[index] != 'e' ){
char int_char = my_itoa(sorted_buffer[index]);
my_putchar(int_char, count);
index++;
}
}
int *my_base_converter(unsigned number, int base, int *sorted_buffer){
int reverse_buffer[20];
int index = -1;
while (number != 0) {
index++;
int remainder = number%base;
reverse_buffer[index] = remainder;
number = (number - remainder) / base;
}
int buf_index = 0;
while (index != -1) {
sorted_buffer[buf_index] = reverse_buffer[index];
index--;
buf_index++;
}
sorted_buffer[buf_index] = 'e';
return sorted_buffer;
}
void hexa_print(int *sorted_buffer, int *count){
char hexa[] = "0123456789abcdef";
int index = 0;
while(sorted_buffer[index] != 'e'){
int hexa_index = sorted_buffer[index];
char hexa_char = hexa[hexa_index];
my_putchar(hexa_char, count);
index++;
}
}
void my_putd(char spec, int num, int *count){
int *sorted_buffer = malloc(128);
if(num==0){
my_putchar('0', count);
}
else if(spec == 'd'|| spec == 'i'){
if(num<0){
my_putchar('-', count);
num = num * -1;
}
my_base_converter((unsigned)num, 10, sorted_buffer);
print_buffer(sorted_buffer, count);
}
else if(spec == 'x'){
if(num<0){
num = UINT_MAX + num + 1;
}
my_base_converter((unsigned)num, 16, sorted_buffer);
hexa_print(sorted_buffer, count);
}
else if(spec == 'o'){
my_base_converter(num, 8, sorted_buffer);
print_buffer(sorted_buffer, count);
}
else if(spec == 'u'){
if(num > 0){
my_base_converter((unsigned)num, 10, sorted_buffer);
print_buffer(sorted_buffer, count);
}
else{
unsigned int unsigned_num = UINT_MAX + num + 1;
my_base_converter(unsigned_num, 10, sorted_buffer);
print_buffer(sorted_buffer, count);
}
}
free(sorted_buffer);
}
int* sequence_escape(char escape, int *count){
if(escape == 'n'){
my_putchar('\n', count);
}
else if(escape == 't'){
my_putchar('\t', count);
}
else if(escape == '\\'){
my_putchar('\\', count);
}
else if(escape == '\''){
my_putchar('\'', count);
}
else if(escape == '\"'){
my_putchar('\"', count);
}
return count;
}
int *conversion_spec(va_list a_list, char spec, int *count){
unsigned num;
char *str;
char *ptr;
if(spec == '%'){
my_putchar('%', count);
}
else if(spec == 's'){
str = va_arg(a_list, char*);
if(str){
my_putstr(str, count);
}
else{
my_putstr("(null)", count);
}
}
else if(spec == 'c'){
num = va_arg(a_list, int);
my_putchar(num, count);
}
else if(spec == 'p'){
ptr = va_arg(a_list, void *);
if(ptr){
my_putstr("0x", count);
my_putstr("_address_not_found - lazy&tired developer\n", count);
}
else{
my_putstr("(nil)", count);
}
}
else if(spec == 'd' || spec == 'i' || spec == 'x' || spec == 'o' || spec == 'u'){
num = va_arg(a_list, int);
my_putd(spec, (unsigned)num, count);
}
return count;
}
int my_printf(char *restrict format, ...){
int *count = malloc(32);
*count = 0;
va_list a_list;
va_start(a_list, format);
int i = 0;
while(format[i] != '\0'){
while (format[i] != '%'){
if(format[i] == '\\'){
i++;
sequence_escape(format[i], count);
}
my_putchar(format[i], count);
i++;
if(format[i] == '\0'){
break;
}
}
if(format[i] == '%'){
i++;
conversion_spec(a_list,format[i], count);
}
if(format[i] == '\0'){
break;
}
i++;
}
va_end(a_list);
int count_1 = *count;
free(count);
return count_1;
}