-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_printf.h
216 lines (204 loc) · 7.15 KB
/
ft_printf.h
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/06 15:12:29 by apuchill #+# #+# */
/* Updated: 2020/06/01 14:57:00 by apuchill ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
/*
** -.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-
** HEADERS
*/
# include <unistd.h>
# include <stdlib.h>
# include <stdarg.h>
# include <stdint.h>
/*
** -.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-
** MACROS
*/
# define FLAGS "-+#0 "
# define ALL_FL "-+#0 *.0123456789lh"
# define FSPECS "cspdiuxX%onfge"
# define DIGITS "0123456789"
# define HEXALOW "0123456789abcdef"
# define HEXAUPP "0123456789ABCDEF"
# define OCTAL "01234567"
/*
** -.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-
** STRUCT DECLARATIONS
*/
/*
** FORMAT SPECIFIER STRUCT
**
** .set: stores all collected format specifiers.
** .spe_c: specifier character - i.e. variable type (ex.: %c %s %p %d %i).
** .pad_c: padding character - could be eiter ' ' (default) or '0' ('0' flag).
** .minus: '-' flag - left-justifies within the given field width; right
** justification is the default.
** .plus: '+' flag - preceeds positive numbers with the plus sign; by default,
** only negative numbers are preceeded with a '-' sign.
** .space: ' ' flag - a blank space is inserted before unsigned values.
** .hash: '#' flag - for %o %x %X, preceeds not null values with 0, 0x or 0X
** respectively; for %a %A %e %E %f %F %g %G, prints decimal point
** even if no more digits follow. By default, if no digits follow, no
** decimal point is printed.
** .width: min number of characters to be printed, with blank space padding if
** value is shorter than width.
** .point: '1' if precision is given, '0' if not.
** .precision: for %d %i %o %u %x %X, min number of digits to be printed, with
** with 0 padding if value is shorter than precision; for %a %A %e %E
** %f %F, number of digits to be printed after the decimal point (by
** default, this is 6); for %g %G, max number of significants digits
** to be printed; for %s, max number of characters to be printed (by
** default, all characters are printed until '\0'); note: '.' = '.0'.
** .length: modifies the length of the data type as follows:
** | %d %i (int) | %u %o %x %X (unsigned int)| %n (int*) |
** 'l' | long int | unsigned long int | long int* |
** 'll'| long long int | unsigned long long int | long long int*|
** 'h' | short int | unsigned short int | short int* |
** 'hh | signed char | unsigned char | signed char* |
** Reference: http://www.cplusplus.com/reference/cstdio/printf/
*/
typedef struct s_flags
{
char set[20];
char spe_c;
char pad_c;
char minus;
char plus;
char space;
char hash;
int width;
char point;
int precision;
char length;
char print_n0;
char sign;
unsigned long long int ulli;
long long int lli;
long double f;
int e_nbr;
char *a;
char *d;
char e[5];
char *tmp;
int size;
size_t strlen;
} t_flags;
/*
** ft_ftoa_rnd STRUCT & UNION
*/
typedef struct s_ftoa
{
long double n;
short int dec_len;
short int rnd;
unsigned long long int int_part;
long double dec_part;
unsigned long long int dec_int;
int dec_int_size;
char *a;
char *d;
char *tmp;
char z0[20];
} t_ftoa;
typedef struct s_dbl
{
uint64_t mantisa :52;
uint64_t exponent :11;
uint64_t sign :1;
} t_dbl;
union u_dbl
{
double f;
t_dbl bits;
} u_double_bit;
/*
** -.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-
** MAIN FUNCTIONS
*/
/*
** FILE: ft_printf.c
** Initialization function: start/end variadic arguments functions <stdarg.h>;
** in between that, goes through the input string printing plain characters or
** collecting + treating format specifiers to the FORMAT SPECIFIER STRUCT thus
** printing each occurrance with the corresponding function.
*/
int ft_printf(const char *str, ...);
/*
** FILE: ft_printf_csp_pct.c
** Outputs the input variable (collected by 'va_arg' function) as a pointer to a
** string to be printed by the 'print_flags' function.
*/
void print_spec_c(int *len, t_flags fl, char c);
void print_spec_s(int *len, t_flags fl, char *s);
void print_spec_pct(int *len, t_flags fl);
/*
** FILE: ft_printf_iduxo.c
** Outputs the input variable (collected by 'va_arg' function) as a pointer to a
** string to be printed by the 'print_flags' function.
*/
void print_spec_i_d_u(int *len, t_flags fl, va_list args);
void print_spec_x(int *len, t_flags fl, va_list args);
void print_spec_o(int *len, t_flags fl, va_list args);
void print_spec_p(int *len, t_flags fl, unsigned long int p);
/*
** FILE: ft_printf_f_g.c
** Outputs the input variable (collected by 'va_arg' function) as a pointer to a
** string to be printed by the 'print_flags' function.
*/
void print_spec_f_e_g(int *len, t_flags fl, double n);
t_flags print_spec_f(t_flags fl, double n);
t_flags print_spec_g(t_flags fl, double n, int p);
/*
** FILE: ft_printf_e.c
** Outputs the input variable (collected by 'va_arg' function) as a pointer to a
** string to be printed by the 'print_flags' function.
*/
t_flags print_spec_e(t_flags fl, double n);
/*
** FILE: ft_printf_flags.c
** Prints each format specifier function's outputted string with formatting in
** accordance with the collected format specifiers.
*/
void print_flags(int *len, t_flags fl);
/*
** -.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-.-'-
** UTILS FUNCTIONS
**
** Basic Libc functions - note: some were adapted and thus named with a suffix
** to indicate the nature of the customization.
*/
/*
** FILE: ft_printf_utils.c
*/
size_t ft_strlen(const char *s);
int ft_strchr_01(char *s, char c);
void ft_putchar_len(char c, int *len);
void ft_putcstr_len(char *s, int *len, int size);
/*
** FILE: ft_printf_utils_2.c
*/
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize);
size_t ft_strlcat(char *dst, const char *src, size_t dstsize);
char *ft_strdup(const char *s1);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_substr(char const *s, unsigned int start, size_t len);
/*
** FILE: ft_printf_utils_3.c
*/
long double ft_pow(long double n, unsigned int pow);
char *ft_ullitoa_base(unsigned long long int n, char *base);
long double ft_fmod(long double n, long double mod);
/*
** FILE: ft_ftoa_rnd.c
*/
char *ft_ftoa_rnd(long double n, short int dec_len, short int rnd);
#endif