tgrekov-ft_printf
HIVE printf Feb 2024
|
Definition in file ft_printf.c.
Go to the source code of this file.
Functions | |
int | handle_sequence (const char **format, va_list args, int *fd, int total) |
Prepares and executes a sequence. | |
static void | do_segment (const char **format, va_list args, int *fd, int *total) |
Process a section of the format string, whether it contains text to print and / or format specifiers. | |
int | ft_printf (const char *format,...) |
Prints and formats a variable set of arguments. | |
|
static |
Process a section of the format string, whether it contains text to print and / or format specifiers.
[in,out] | format | Pointer to address of format string. |
[in,out] | args | Instance of variable argument list for the ft_printf call. |
[in,out] | fd | File descriptor to write on. |
[in,out] | total | Total number of characters written so far. |
Definition at line 36 of file ft_printf.c.
References ft_strchr(), ft_strlen(), handle_sequence(), and wrap_err().
int ft_printf | ( | const char * | format, |
... | |||
) |
Prints and formats a variable set of arguments.
The format for a specifier is as follows:
%[flags][width][precision][length]specifier
Specifier | Function | Example |
---|---|---|
d or i | Signed decimal integer | -42 |
u | Unsigned decimal integer | 42 |
o | Unsigned octal | 52 |
x | Unsigned hexadecimal integer | 2a |
X | Unsigned uppercase hexadecimal integer | 2A |
c | Character | * |
s | String of characters | Forty-two |
p | Pointer address | 0x2a |
% | A % chracter | % |
n | Nothing is printed. The corresponding argument must be a pointer to a signed integer, which will be set to the number of characters written so far. | |
> | Nothing is printed. The corresponding argument must be a signed integer representing the file descriptor to redirect any remaining output during this call to. |
Flag | Specifiers | Description |
---|---|---|
- | All 1 | Left-justify padding instead of right-justifying. |
+ | d i | Preceed numbers with a + sign if they are not negative. |
(space) | d i | Preceed numbers with a space if no sign is to be written. |
# | o x X | If non-zero, preceed values for o, x, or X specifiers with 0, 0x, or 0X respectively. |
0 | All 1 | Pad with zeroes instead of spaces. Cannot be mixed with precision or left-justification. |
_ | All 1 | Pad with custom string, the address of which is provided in the next function argument. Cannot be mixed with precision or left-justification if used on used with d, i, o, u, x, or X. |
1 With the exception of the n and > specfiers, as they do not print anything.
Width | Description |
---|---|
(number) | Minimum number of characters to print. Pads to reach this number. |
* | The minimum width should be retrieved from the next function argument. |
.precision | Description |
---|---|
.number | For d, i, o, u, x, or X, the minimum number of digits to write. If the value is shorter than this number, it is padded with leading zeros. For a precision of 0, nothing is written if the value is 0. For s, the maximum number of characters to be printed. |
* | The precision should be retrieved from the next function argument. |
Specifiers | |||||||
---|---|---|---|---|---|---|---|
Length | d i | u o x X | c | s | p | n | > |
(none) | int | unsigned int | int | char * | void * | int * | int |
hh | char | unsigned char | signed char * | ||||
h | short | unsigned short | short * | ||||
l | long | unsigned long | long * | ||||
ll | long long | unsigned long long | long long * | ||||
j | intmax_t | uintmax_t | intmax_t * | ||||
z | size_t | size_t * | |||||
t | ptrdiff_t | ptrdiff_t * |
[in] | format | Null terminated string containing text to print and, optionally, conversion specifiers and subspecifiers. See extended description. |
[in,out] | ... | Variable amount of arguments of various types, dependent on the format . |
int | Number of characters printed, or -1 if an error occurs. |
Definition at line 252 of file ft_printf.c.
References do_segment().
int handle_sequence | ( | const char ** | format, |
va_list | args, | ||
int * | fd, | ||
int | total | ||
) |
Prepares and executes a sequence.
Initializes and parses any subspecifiers, identifies the sequence and executes the preprocessor for it, adds up the final predicted lengths, prints padding, prefixes, and executes the processor for the sequence.
[in,out] | format | |
[in,out] | args | |
[in,out] | fd | |
[in] | total |
int | Number of characters printed, or -1 on error. |
Definition at line 77 of file handle_sequence.c.
References ft_strlen(), identify_sequence(), init_subspec(), s_subspec::min_width, s_sequence::pad_len, parse_subspec(), print(), s_sequence::sign, s_sequence::specifier, s_sequence::subspec, and s_sequence::total_len.