tgrekov-ft_printf
HIVE printf Feb 2024
Loading...
Searching...
No Matches
ft_printf.c File Reference

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 23:03:16 by tgrekov #+# #+# */
/* Updated: 2024/02/13 07:22:32 by tgrekov ### ########.fr */
/* */
/* ************************************************************************** */

Definition in file ft_printf.c.

#include "../../libft/libft.h"
#include "utils/utils.h"
#include <stdarg.h>
Include dependency graph for 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.
 

Function Documentation

◆ do_segment()

static void do_segment ( const char **  format,
va_list  args,
int *  fd,
int *  total 
)
static

Process a section of the format string, whether it contains text to print and / or format specifiers.

Parameters
[in,out]formatPointer to address of format string.
[in,out]argsInstance of variable argument list for the ft_printf call.
[in,out]fdFile descriptor to write on.
[in,out]totalTotal 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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_printf()

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 charsigned char *
h short unsigned shortshort *
l long unsigned longlong *
ll long long unsigned long long

long long *
j intmax_t uintmax_tintmax_t *
zsize_tsize_t *
tptrdiff_tptrdiff_t *
Parameters
[in]formatNull 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.
Return values
intNumber of characters printed, or -1 if an error occurs.

Definition at line 252 of file ft_printf.c.

References do_segment().

Here is the call graph for this function:

◆ handle_sequence()

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.

Parameters
[in,out]format
[in,out]args
[in,out]fd
[in]total
Return values
intNumber 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.

Here is the call graph for this function:
Here is the caller graph for this function: