-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_printf.c
139 lines (129 loc) · 3.65 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
/******************************************************************************
*
* File Name : my_printf.c
* Created By : Hugo POULIQUEN
* Creation Date : 03 13th, 2016
* Last Changed By : Hugo POULIQUEN
* Last Change : 03 30th, 2016
*
*******************************************************************************/
#include "include/puts/puts_char.h"
#include "include/puts/puts_nbr.h"
#include "include/str/str_len.h"
#include <stdarg.h>
void (*str_functions[4])(char *) = {
my_puts, my_puts_ptr, my_puts_extand, my_puts
};
void (*int_functions[7])(int) = {
my_puts_nbr, my_puts_nbr, my_puts_octal, my_puts_hexa, my_puts_hexa_maj,
my_puts_binary, my_puts_nbr
};
struct flag{
int index_flag;
int str_list;
};
/*
** Function to search function index in array
** Param: char for search
*/
struct flag search_index_flags(char search){
struct flag result;
char *flags_str, *flags_int;
flags_str = "spS%";
flags_int = "dioxXbu";
result.index_flag = result.str_list = -1;
for (int i = 0; flags_str[i]; i++) {
if (flags_str[i] == search){
result.str_list = 1;
result.index_flag = i;
}
}
if (result.str_list == -1) {
for (int i = 0; flags_int[i]; i++) {
if (flags_int[i] == search){
result.str_list = 0;
result.index_flag = i;
}
}
}
return result;
}
/*void format_function(char *format, int index){
char *flags_format;
int index_format;
flags_format = "#+-";
for (int i = index; format[i]; i++){
for (int j = 0; flags_format[j]; j++) {
}
}
}*/
/*
** Function to select the correct function
** Param: number, struct, struct
*/
va_list va;
void select_function(int format, va_list va, struct flag result){
if(format == '%')
my_puts_char((char)(format));
else if(format == 'c')
my_puts_char(va_arg(va, int));
else if(result.str_list == 1)
str_functions[result.index_flag](va_arg(va, char*));
else if(result.str_list == 0)
int_functions[result.index_flag](va_arg(va, int));
else
my_puts_char(format);
}
/*
** Function to add padding
** Param: number
*/
int padding(int search){
char *flags_nbr;
int padding_ok, flags_nbr_size;
flags_nbr = "0123456789";
padding_ok = 0;
flags_nbr_size = my_strlen(flags_nbr);
for(int i=0; i < flags_nbr_size; i++){
if(flags_nbr[i] == search){
padding_ok = 1;
for(int j = 0; j < i; j++){
my_puts(" ");
}
}
}
return padding_ok;
}
int my_printf(char *format, ...){
struct flag result;
int padding_ok;
va_start (va, format);
for (int i = 0; format[i]; i++){
if(format[i] == '%'){
i++;
if((format[i] == '#' && format[i+1] == 'd'))
i++;
else if((format[i] == '#' && format[i+1] == 'x')){
my_puts("0x");
i++;
}else if((format[i] == '#' && format[i+1] == 'o')){
my_puts_nbr(0);
i++;
}else if((format[i] == '0' && format[i+1] == 'd'))
i++;
else if((format[i] == '+' && format[i+1] == 'd')){
my_puts("+");
i++;
} else if((format[i] == '-' && format[i+1] == 'd'))
i++;
padding_ok = padding(format[i]);
if(padding_ok == 1)
i++;
result = search_index_flags(format[i]);
select_function(format[i], va, result);
} else
my_puts_char(format[i]);
}
va_end (va);
return 0;
}