libft
Loading...
Searching...
No Matches
Files | Data Structures | Macros | Typedefs | Functions
Formatted Output (ft_printf)

Custom implementation of the printf function. More...

Files

file  format_and_print_numbers.c
 Handles numeric specifiers for ft_printf.
 
file  format_and_print_p.c
 Handles p format specifier for ft_printf.
 
file  format_print_csper.c
 Handles printing of c, s, and %% specifiers for ft_printf.
 
file  format_utils.c
 Utilities for handling padding, width, and precision in ft_printf.
 
file  ft_printf.c
 Custom printf implementation (ft_printf entry point).
 
file  ft_printf.h
 Internal definitions and declarations for ft_printf.
 
file  handle_precision_int.c
 Precision formatting for integer values in ft_printf.
 
file  initialize_printf_structs.c
 Initialization helpers for ft_printf internal state.
 
file  print_formatted_number.c
 Handles formatting and output of integers in ft_printf.
 
file  printf_alternative_form.c
 Handles the # flag for hexadecimal output in ft_printf.
 
file  process_format_string.c
 Parses and handles format specifiers for ft_printf.
 
file  setup_format.c
 Parses format specifications for ft_printf.
 
file  write_safely.c
 Safe low-level write utilities for ft_printf.
 

Data Structures

struct  s_fmt
 Format specifier data. More...
 
struct  s_pf
 State struct for ft_printf. More...
 

Macros

#define FLAG_HASH   (1 << 0)
 
#define FLAG_MINUS   (1 << 2)
 
#define FLAG_PLUS   (1 << 3)
 
#define FLAG_SPACE   (1 << 4)
 
#define FLAG_ZERO   (1 << 1)
 

Typedefs

typedef struct s_fmt t_fmt
 
typedef struct s_pf t_pf
 

Functions

static char * apply_plus_space_hash_flags (t_pf *pf, char *str, int *len)
 Applies +, space, and # flags to the formatted number.
 
static char * apply_precision_zero_padding (t_pf *pf, char *str, int *len, int is_neg)
 Pads integer string with leading zeros to match precision.
 
static char * convert_ptr_to_hex (void *ptr, t_pf *pf)
 Converts a pointer to a lowercase hexadecimal string.
 
void format_and_print_c (t_fmt *fmt, va_list *args, t_pf *pf)
 Formats and prints a character (c).
 
void format_and_print_id (t_fmt *fmt, va_list *args, t_pf *pf)
 Handles d and i format specifiers for ft_printf.
 
void format_and_print_p (t_fmt *fmt, va_list *args, t_pf *pf)
 Handles p format specifier for ft_printf.
 
void format_and_print_percent (t_fmt *fmt, t_pf *pf)
 Formats and prints a literal percent sign (%%).
 
void format_and_print_s (t_fmt *fmt, va_list *args, t_pf *pf)
 Formats and prints a string (s).
 
void format_and_print_u (t_fmt *fmt, va_list *args, t_pf *pf)
 Handles u format specifier for ft_printf.
 
void format_and_print_x (t_fmt *fmt, va_list *args, int upper, t_pf *pf)
 Handles x and X format specifiers for ft_printf.
 
int ft_printf (const char *format,...)
 Prints formatted output according to the format string.
 
int get_alternative_form_length (char specifier)
 Returns the prefix length for x and X when using # flag.
 
static char * handle_hashflag_zero_prec (int *len, char *str, t_pf *pf)
 Handles edge case for "0" with precision set to 0.
 
void handle_minus_flag_p (char *str, int padding, t_pf *pf)
 
static char * handle_null_ptr (t_pf *pf)
 Returns the default string for a NULL pointer.
 
char * handle_precision_int (t_fmt *fmt, char *str, int *len, t_pf *pf)
 Applies precision formatting to a numeric string.
 
char * handle_precision_p (t_fmt *fmt, char *str, int *len, t_pf *pf)
 
int handle_precision_s (t_fmt *fmt, int len)
 Applies precision limit to string length.
 
void handle_space_flag_p (t_pf *pf, int *padding)
 
int handle_width (t_fmt *fmt, size_t len)
 Computes the number of padding characters to apply.
 
static void initialize_fmt (t_fmt *fmt)
 Initializes a t_fmt (format specifier) structure.
 
static void initialize_pf (t_pf *pf)
 Initializes a t_pf (printf state) structure.
 
int initialize_printf_structs (t_pf *pf)
 Allocates and initializes the structures required by ft_printf.
 
int is_valid_flag (char c)
 Checks if a character is a valid format flag.
 
static void parse_and_handle_specifier (t_fmt *fmt, va_list *args, t_pf *pf)
 Delegates the specifier to the appropriate formatter.
 
static void parse_flags (const char **format, t_fmt *fmt)
 Parses format flags and sets corresponding bits.
 
static void parse_precision (const char **format, t_fmt *fmt)
 Parses precision (e.g., %.5d).
 
static int parse_specifier (const char **format, t_fmt *fmt)
 Validates and stores the conversion specifier.
 
static void parse_width (const char **format, t_fmt *fmt)
 Parses field width (e.g., %10d).
 
char * prepend_alternative_form (char *str, char specifier, t_pf *pf)
 Prepends the 0x or 0X prefix to a hexadecimal string.
 
static char * prepend_sign (char *str, char sign, t_pf *pf)
 Prepends a sign character (+ or space) to a number string.
 
void print_formatted_number (t_fmt *fmt, char *str, int len, t_pf *pf)
 Final formatter and dispatcher for integer output.
 
void print_padding (int padding, char pad_char, t_pf *pf)
 Prints a series of padding characters.
 
void print_right_aligned_p (t_fmt *fmt, char *str, int padding, t_pf *pf)
 
static void print_with_padding (t_fmt *fmt, char *str, t_pf *pf)
 Prints the pointer string with formatting and padding.
 
static void print_with_padding (t_pf *pf, char *str, int padding)
 Writes the number string with left/right padding.
 
static void print_zero_padding_int (int padding, char **str_ptr, t_pf *pf)
 Writes zero-padding and handles printing the sign first if needed.
 
static void process_conv_spec (const char **format, va_list *args, t_pf *pf)
 Parses a single conversion specifier after %.
 
void process_format_string (const char *format, va_list *args, t_pf *pf)
 Walks through the format string and dispatches format handlers.
 
int requires_alternative_form (char specifier, char *str)
 Checks whether the alternative form (#) should be applied.
 
static void reset_fmt (t_fmt *fmt)
 Resets the format context for the next conversion.
 
int setup_format (const char **format, t_fmt *fmt)
 Parses a full format specification (flags, width, precision, specifier).
 
void write_char_safely (int fd, char c, t_pf *pf)
 Safely writes a single character to a file descriptor.
 
void write_safely (int fd, char *str, t_pf *pf)
 Safely writes a null-terminated string to a file descriptor.
 
void write_safely_len (int fd, char *str, size_t len, t_pf *pf)
 Safely writes len bytes from a string to a file descriptor.
 

Detailed Description

Custom implementation of the printf function.

This group includes all functions involved in the implementation of ft_printf, including:


Data Structure Documentation

◆ s_fmt

struct s_fmt

Format specifier data.

Stores parsed flags, field width, precision, and the actual type specifier.

Data Fields

int flags
 
int precision
 
char specifier
 
int width
 

Field Documentation

◆ flags

int s_fmt::flags

◆ precision

int s_fmt::precision

◆ specifier

char s_fmt::specifier

◆ width

int s_fmt::width

◆ s_pf

struct s_pf

State struct for ft_printf.

Keeps track of total characters written, and internal errors.

+ Collaboration diagram for s_pf:

Data Fields

t_fmtfmt
 
int malloc_error
 
int total
 
int write_error
 

Field Documentation

◆ fmt

t_fmt* s_pf::fmt

◆ malloc_error

int s_pf::malloc_error

◆ total

int s_pf::total

◆ write_error

int s_pf::write_error

Macro Definition Documentation

◆ FLAG_HASH

#define FLAG_HASH   (1 << 0)

#include <include/ft_printf.h>

'#' flag: alternate form

◆ FLAG_MINUS

#define FLAG_MINUS   (1 << 2)

#include <include/ft_printf.h>

'-' flag: left-justify

◆ FLAG_PLUS

#define FLAG_PLUS   (1 << 3)

#include <include/ft_printf.h>

'+' flag: show plus sign

◆ FLAG_SPACE

#define FLAG_SPACE   (1 << 4)

#include <include/ft_printf.h>

' ' flag: leading space

◆ FLAG_ZERO

#define FLAG_ZERO   (1 << 1)

#include <include/ft_printf.h>

'0' flag: zero padding

Typedef Documentation

◆ t_fmt

typedef struct s_fmt t_fmt

#include <include/ft_printf.h>

◆ t_pf

typedef struct s_pf t_pf

#include <include/ft_printf.h>

Function Documentation

◆ apply_plus_space_hash_flags()

static char * apply_plus_space_hash_flags ( t_pf pf,
char *  str,
int *  len 
)
static

#include <srcs/ft_printf/print_formatted_number.c>

Applies +, space, and # flags to the formatted number.

Prepends signs or prefixes to the string depending on format specifier and flags. Updates string length accordingly.

Parameters
pfThe current printf state.
strPointer to the number string (malloc'd).
lenPointer to its length (will be updated).
Returns
Newly formatted string with all flags applied, or NULL on error.
See also
prepend_sign
prepend_alternative_form
requires_alternative_form
get_alternative_form_length

◆ apply_precision_zero_padding()

static char * apply_precision_zero_padding ( t_pf pf,
char *  str,
int *  len,
int  is_neg 
)
static

#include <srcs/ft_printf/handle_precision_int.c>

Pads integer string with leading zeros to match precision.

If precision is greater than the number of digits, this function inserts leading zeros. Handles negative numbers by preserving the - sign at the start of the result.

Parameters
pfPointer to the ft_printf global state.
strMalloc’d number string (will be freed if changed).
lenPointer to length of string (updated if modified).
is_neg1 if the number is negative (i.e., has - prefix), 0 otherwise.
Returns
A new malloc’d string with applied precision, or NULL on error.
See also
ft_calloc
ft_memset
ft_strlcpy

◆ convert_ptr_to_hex()

static char * convert_ptr_to_hex ( void *  ptr,
t_pf pf 
)
static

#include <srcs/ft_printf/format_and_print_p.c>

Converts a pointer to a lowercase hexadecimal string.

Converts the pointer address to a lowercase hexadecimal string, prepends the "0x" prefix, and returns a newly allocated result.

Parameters
ptrPointer to convert.
pfPrint context to set error flag if needed.
Returns
Formatted pointer string or NULL on allocation error.
See also
ft_itoa_base
ft_strjoin

◆ format_and_print_c()

void format_and_print_c ( t_fmt fmt,
va_list *  args,
t_pf pf 
)

#include <include/ft_printf.h>

Formats and prints a character (c).

Retrieves a single character from the variadic list and prints it with optional alignment and width padding.

Parameters
fmtFormat specifier structure.
argsArgument list containing the character.
pfPrint context structure.
See also
write_char_safely
handle_width
print_padding

◆ format_and_print_id()

void format_and_print_id ( t_fmt fmt,
va_list *  args,
t_pf pf 
)

#include <include/ft_printf.h>

Handles d and i format specifiers for ft_printf.

Converts an integer value to string using ft_itoa, applies formatting using handle_precision_int, and writes the result using print_formatted_number. If a memory allocation fails, the malloc_error flag is set in the state struct.

Parameters
fmtFormat descriptor (flags, width, precision).
argsPointer to va_list with the argument.
pfState struct tracking write progress and error flags.
See also
ft_itoa
handle_precision_int
print_formatted_number

◆ format_and_print_p()

void format_and_print_p ( t_fmt fmt,
va_list *  args,
t_pf pf 
)

#include <srcs/ft_printf/format_and_print_p.c>

Handles p format specifier for ft_printf.

This function formats and prints a pointer argument. It converts the pointer to hexadecimal, applies precision and alignment based on flags, and handles the null case gracefully.

Parameters
fmtFormat descriptor (flags, width, precision).
argsArgument list (va_list) from ft_printf.
pfPrint context structure.
See also
print_with_padding
handle_null_ptr
convert_ptr_to_hex

◆ format_and_print_percent()

void format_and_print_percent ( t_fmt fmt,
t_pf pf 
)

#include <include/ft_printf.h>

Formats and prints a literal percent sign (%%).

Prints a single percent character with optional width padding. Padding is done with ‘’ 'or'0', and alignment respects the FLAG_MINUS` flag.

Parameters
fmtFormat descriptor (width and flags).
pfPrint context for error tracking and byte count.
See also
handle_width
print_padding
write_char_safely

◆ format_and_print_s()

void format_and_print_s ( t_fmt fmt,
va_list *  args,
t_pf pf 
)

#include <include/ft_printf.h>

Formats and prints a string (s).

Retrieves a string from the variadic list and prints it with optional width, alignment, and precision. NULL strings are replaced by "(null)".

If a precision is set, the string is truncated to that length. Alignment follows the FLAG_MINUS flag.

Parameters
fmtFormat descriptor for width and precision.
argsVariadic arguments (string to print).
pfPrint state and error tracking.
See also
handle_precision_s
write_safely_len
handle_width
print_padding

◆ format_and_print_u()

void format_and_print_u ( t_fmt fmt,
va_list *  args,
t_pf pf 
)

#include <include/ft_printf.h>

Handles u format specifier for ft_printf.

Converts an unsigned integer to string using ft_utoa, applies precision, and writes the formatted result using print_formatted_number.

Parameters
fmtFormat descriptor (flags, width, precision).
argsPointer to va_list with the argument.
pfState struct tracking write progress and error flags.
See also
ft_utoa
handle_precision_int
print_formatted_number

◆ format_and_print_x()

void format_and_print_x ( t_fmt fmt,
va_list *  args,
int  upper,
t_pf pf 
)

#include <include/ft_printf.h>

Handles x and X format specifiers for ft_printf.

Converts an unsigned integer to hexadecimal using ft_itoa_base, applies precision, and prints the formatted result. If upper is non-zero, the hexadecimal letters are uppercase (for X).

Parameters
fmtFormat descriptor (flags, width, precision).
argsPointer to va_list with the argument.
upperIf non-zero, use uppercase letters in output.
pfState struct tracking write progress and error flags.
See also
ft_itoa_base
handle_precision_int
print_formatted_number

◆ ft_printf()

int ft_printf ( const char *  format,
  ... 
)

#include <include/ft_printf.h>

Prints formatted output according to the format string.

Mimics the behavior of the standard printf function:

  • Parses the format string
  • Processes and prints each format specifier with its argument
  • Uses internal state via t_pf to manage memory and write errors

If a memory allocation or write operation fails, returns -1.

Parameters
formatThe format string (e.g. "Hello %s\n").
...Variadic arguments matching format specifiers.
Returns
The number of characters written, or -1 on error.
Note
This function must be passed valid format and matching arguments.
See also
initialize_printf_structs
process_format_string

◆ get_alternative_form_length()

int get_alternative_form_length ( char  specifier)

#include <include/ft_printf.h>

Returns the prefix length for x and X when using # flag.

The prefix for alternative hexadecimal output is always two characters: 0x or 0X.

Parameters
specifierThe current format specifier.
Returns
Length of the prefix (2 if applicable, 0 otherwise).

◆ handle_hashflag_zero_prec()

static char * handle_hashflag_zero_prec ( int *  len,
char *  str,
t_pf pf 
)
static

#include <srcs/ft_printf/handle_precision_int.c>

Handles edge case for "0" with precision set to 0.

When precision is explicitly 0 and the value is also 0, the result is normally an empty string unless the # flag is set for hex.

Parameters
lenPointer to the string length (will be updated).
strThe current numeric string.
pfPointer to the ft_printf global state.
Returns
New string or NULL on allocation failure.
Note
Returns "(null)" if memory allocation fails.

◆ handle_minus_flag_p()

void handle_minus_flag_p ( char *  str,
int  padding,
t_pf pf 
)

#include <include/ft_printf.h>

◆ handle_null_ptr()

static char * handle_null_ptr ( t_pf pf)
static

#include <srcs/ft_printf/format_and_print_p.c>

Returns the default string for a NULL pointer.

Allocates and returns the literal string (nil) to represent null pointers in p format.

Parameters
pfPrint state tracker to set malloc error flag if needed.
Returns
A newly allocated string or NULL on allocation failure.
See also
ft_strdup

◆ handle_precision_int()

char * handle_precision_int ( t_fmt fmt,
char *  str,
int *  len,
t_pf pf 
)

#include <include/ft_printf.h>

Applies precision formatting to a numeric string.

This is the main function used for formatting d, i, u, x, X according to the precision field of the format string. It takes care of:

  • Adding leading zeros
  • Skipping output for "0" when precision is 0
  • Handling negative sign in integer
Parameters
fmtCurrent format specifier.
strNumeric string to process (must be malloc’d).
lenPointer to the string length (updated).
pfPointer to the ft_printf global state.
Returns
The updated string (malloc’d), or NULL on error.
See also
apply_precision_zero_padding
handle_hashflag_zero_prec

◆ handle_precision_p()

char * handle_precision_p ( t_fmt fmt,
char *  str,
int *  len,
t_pf pf 
)

#include <include/ft_printf.h>

◆ handle_precision_s()

int handle_precision_s ( t_fmt fmt,
int  len 
)

#include <include/ft_printf.h>

Applies precision limit to string length.

Truncates the given string length to the precision value if one is specified (non-negative). Used when formatting s.

Parameters
fmtFormat specifier structure.
lenOriginal string length.
Returns
Truncated length if precision applies, otherwise original length.

◆ handle_space_flag_p()

void handle_space_flag_p ( t_pf pf,
int *  padding 
)

#include <include/ft_printf.h>

◆ handle_width()

int handle_width ( t_fmt fmt,
size_t  len 
)

#include <include/ft_printf.h>

Computes the number of padding characters to apply.

Calculates how many characters must be padded to the left or right to meet the field width requirement.

Parameters
fmtFormat descriptor.
lenLength of the current content to be printed.
Returns
Number of characters to pad (may be zero).

◆ initialize_fmt()

static void initialize_fmt ( t_fmt fmt)
static

#include <srcs/ft_printf/initialize_printf_structs.c>

Initializes a t_fmt (format specifier) structure.

Sets formatting fields to their default values:

  • flags = 0
  • width = 0
  • precision = -1 (unset)
  • specifier = 0 (null character)
Parameters
fmtPointer to the t_fmt structure to initialize.

◆ initialize_pf()

static void initialize_pf ( t_pf pf)
static

#include <srcs/ft_printf/initialize_printf_structs.c>

Initializes a t_pf (printf state) structure.

Sets total written characters, write error, and malloc error tracking fields to zero.

Parameters
pfPointer to the t_pf structure to initialize.

◆ initialize_printf_structs()

int initialize_printf_structs ( t_pf pf)

#include <include/ft_printf.h>

Allocates and initializes the structures required by ft_printf.

This function sets up the main state structure (t_pf) and allocates a t_fmt format descriptor. All fields are initialized to their default values. If allocation fails, returns -1.

Parameters
pfPointer to the t_pf structure that will be initialized.
Returns
0 on success, or -1 if allocation fails.
See also
initialize_pf
initialize_fmt

◆ is_valid_flag()

int is_valid_flag ( char  c)

#include <include/ft_printf.h>

Checks if a character is a valid format flag.

Recognized flags include:

  • #: alternate form
  • 0: zero padding
  • -: left-justify
  • +: show plus sign
  • ‘’ '` (space): leading space for positive numbers
Parameters
cThe character to evaluate.
Returns
1 if the character is a valid flag, 0 otherwise.

◆ parse_and_handle_specifier()

static void parse_and_handle_specifier ( t_fmt fmt,
va_list *  args,
t_pf pf 
)
static

#include <srcs/ft_printf/process_format_string.c>

Delegates the specifier to the appropriate formatter.

Parameters
fmtCurrent format context (t_fmt).
argsActive variadic argument list.
pfPrint state tracker (t_pf).
See also
format_and_print_c
format_and_print_s
format_and_print_id
format_and_print_u
format_and_print_x
format_and_print_percent

◆ parse_flags()

static void parse_flags ( const char **  format,
t_fmt fmt 
)
static

#include <srcs/ft_printf/setup_format.c>

Parses format flags and sets corresponding bits.

Flags include: #, 0, -, +, and space.

Parameters
formatFormat string pointer (will be advanced).
fmtTarget format context.
See also
is_valid_flag

◆ parse_precision()

static void parse_precision ( const char **  format,
t_fmt fmt 
)
static

#include <srcs/ft_printf/setup_format.c>

Parses precision (e.g., %.5d).

Precision is stored in fmt->precision. If the . is present but no digits follow, precision is set to 0.

Parameters
formatFormat string pointer (will be advanced).
fmtTarget format context.
See also
ft_atoi

◆ parse_specifier()

static int parse_specifier ( const char **  format,
t_fmt fmt 
)
static

#include <srcs/ft_printf/setup_format.c>

Validates and stores the conversion specifier.

Accepted specifiers: c, s, p, d, i, u, x, X, %

Parameters
formatFormat string pointer (will be advanced).
fmtTarget format context.
Returns
0 if valid, -1 if unknown specifier.
See also
ft_strchr

◆ parse_width()

static void parse_width ( const char **  format,
t_fmt fmt 
)
static

#include <srcs/ft_printf/setup_format.c>

Parses field width (e.g., %10d).

Stores the integer width value in fmt->width.

Parameters
formatFormat string pointer (will be advanced).
fmtTarget format context.
See also
ft_atoi

◆ prepend_alternative_form()

char * prepend_alternative_form ( char *  str,
char  specifier,
t_pf pf 
)

#include <include/ft_printf.h>

Prepends the 0x or 0X prefix to a hexadecimal string.

Allocates a new string containing the appropriate prefix followed by the original string. Frees the original string and updates the printf context if an allocation error occurs.

Parameters
strThe original hexadecimal string (must be malloc'd).
specifierEither ‘'x’or'X'`, determines the prefix casing.
pfPointer to the printf context for error tracking.
Returns
A newly allocated string with prefix, or NULL on error.
See also
ft_strlcpy

◆ prepend_sign()

static char * prepend_sign ( char *  str,
char  sign,
t_pf pf 
)
static

#include <srcs/ft_printf/print_formatted_number.c>

Prepends a sign character (+ or space) to a number string.

Allocates and returns a new string with the specified sign inserted at the beginning. Frees the old string. Used for d / i formatting.

Parameters
strThe numeric string to modify (will be freed).
signThe character to prepend (+ or ‘’ '`).
pfPointer to the printf context (for error tracking).
Returns
Newly allocated string with prepended sign, or NULL on error.
See also
ft_strlcpy

◆ print_formatted_number()

void print_formatted_number ( t_fmt fmt,
char *  str,
int  len,
t_pf pf 
)

#include <include/ft_printf.h>

Final formatter and dispatcher for integer output.

This function prepares a number string for printing by applying:

  • flags (+, , #)
  • width / zero-padding
  • left/right alignment

It prints the fully formatted number and handles memory cleanup.

Parameters
fmtThe current format descriptor.
strMalloc'd number string to format.
lenInitial string length.
pfThe printf context for output state tracking.

◆ print_padding()

void print_padding ( int  padding,
char  pad_char,
t_pf pf 
)

#include <include/ft_printf.h>

Prints a series of padding characters.

This function prints padding number of characters (typically ‘’ 'or '0') usingwrite_char_safely`. It is used to align formatted output to the desired width.

If a write error occurs, it sets pf->write_error.

Parameters
paddingNumber of characters to pad.
pad_charCharacter used for padding (usually ' ' or '0').
pfPrint context to track output and errors.
See also
write_char_safely

◆ print_right_aligned_p()

void print_right_aligned_p ( t_fmt fmt,
char *  str,
int  padding,
t_pf pf 
)

#include <include/ft_printf.h>

◆ print_with_padding() [1/2]

static void print_with_padding ( t_fmt fmt,
char *  str,
t_pf pf 
)
static

#include <srcs/ft_printf/format_and_print_p.c>

Prints the pointer string with formatting and padding.

This function applies precision and width formatting to the pointer string str, processes spacing or alignment flags, and prints the formatted result. It frees the input string once printed.

Parameters
fmtFormat descriptor (flags, width, precision).
strString representation of the pointer.
pfPointer to print context (error and length tracker).
See also
handle_precision_p
handle_width
handle_space_flag_p
handle_minus_flag_p
print_right_aligned_p

◆ print_with_padding() [2/2]

static void print_with_padding ( t_pf pf,
char *  str,
int  padding 
)
static

#include <srcs/ft_printf/print_formatted_number.c>

Writes the number string with left/right padding.

Parameters
pfThe current printf context.
strThe string to print.
paddingNumber of padding spaces.
See also
write_safely
print_padding

◆ print_zero_padding_int()

static void print_zero_padding_int ( int  padding,
char **  str_ptr,
t_pf pf 
)
static

#include <srcs/ft_printf/print_formatted_number.c>

Writes zero-padding and handles printing the sign first if needed.

Parameters
paddingAmount of zeros to pad.
str_ptrPointer to the string pointer (adjusted if sign is printed).
pfThe printf state.
See also
write_char_safely

◆ process_conv_spec()

static void process_conv_spec ( const char **  format,
va_list *  args,
t_pf pf 
)
static

#include <srcs/ft_printf/process_format_string.c>

Parses a single conversion specifier after %.

This function processes the format string starting from the current cursor. It resets the format context, sets up the format descriptor, and dispatches the appropriate formatter. If the format is invalid, it prints the raw characters until the next % or end of string.

Parameters
formatPointer to the current cursor in the format string.
argsArgument list used for conversions.
pfPrint state context.
See also
setup_format
reset_fmt
parse_and_handle_specifier
write_char_safely

◆ process_format_string()

void process_format_string ( const char *  format,
va_list *  args,
t_pf pf 
)

#include <include/ft_printf.h>

Walks through the format string and dispatches format handlers.

This is the main format processing loop for ft_printf. It iterates through the characters in the format string. When encountering %, it parses and processes a specifier. Otherwise, it prints the character.

Parameters
formatThe format string to parse.
argsThe active variadic arguments.
pfPrint state context.
See also
process_conv_spec
write_char_safely

◆ requires_alternative_form()

int requires_alternative_form ( char  specifier,
char *  str 
)

#include <include/ft_printf.h>

Checks whether the alternative form (#) should be applied.

Alternative form is only applied if the specifier is ‘'x’or'X'and the value is non-zero (e.g., not"0"`).

Parameters
specifierThe format specifier character (x or X).
strString representation of the value to print.
Returns
1 if prefix should be applied, 0 otherwise.

◆ reset_fmt()

static void reset_fmt ( t_fmt fmt)
static

#include <srcs/ft_printf/process_format_string.c>

Resets the format context for the next conversion.

Parameters
fmtThe format descriptor to reset.

◆ setup_format()

int setup_format ( const char **  format,
t_fmt fmt 
)

#include <include/ft_printf.h>

Parses a full format specification (flags, width, precision, specifier).

Parses from % until the final specifier character. Populates the given t_fmt structure accordingly.

Parameters
formatFormat string pointer (will be advanced).
fmtPointer to the format structure to populate.
Returns
0 on success, -1 on invalid specifier.
See also
parse_flags
parse_width
parse_precision
parse_specifier

◆ write_char_safely()

void write_char_safely ( int  fd,
char  c,
t_pf pf 
)

#include <include/ft_printf.h>

Safely writes a single character to a file descriptor.

Attempts to write c to fd. If interrupted (EINTR), retries. Updates total byte count or sets write error in pf on failure.

Parameters
fdFile descriptor to write to.
cCharacter to write.
pfPointer to printf state context (t_pf).

◆ write_safely()

void write_safely ( int  fd,
char *  str,
t_pf pf 
)

#include <include/ft_printf.h>

Safely writes a null-terminated string to a file descriptor.

This function ensures the full content of str is written to fd, handling partial writes and interruptions caused by signals (EINTR). Updates the total field in t_pf and sets write_error on failure.

Parameters
fdFile descriptor to write to.
strNull-terminated string to write.
pfPointer to printf state context (t_pf).
See also
ft_strlen

◆ write_safely_len()

void write_safely_len ( int  fd,
char *  str,
size_t  len,
t_pf pf 
)

#include <include/ft_printf.h>

Safely writes len bytes from a string to a file descriptor.

Unlike write_safely, this function writes a fixed number of bytes (useful for non-null-terminated data). Handles EINTR and write errors.

Parameters
fdFile descriptor to write to.
strBuffer containing data to write.
lenNumber of bytes to write.
pfPointer to printf state context (t_pf).