|
libft
|
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. | |
Custom implementation of the printf function.
This group includes all functions involved in the implementation of ft_printf, including:
| struct s_fmt |
| 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_fmt * | fmt |
| int | malloc_error |
| int | total |
| int | write_error |
| t_fmt* s_pf::fmt |
| int s_pf::malloc_error |
| int s_pf::total |
| int s_pf::write_error |
| #define FLAG_HASH (1 << 0) |
#include <include/ft_printf.h>
'#' flag: alternate form
| #define FLAG_MINUS (1 << 2) |
#include <include/ft_printf.h>
'-' flag: left-justify
| #define FLAG_PLUS (1 << 3) |
#include <include/ft_printf.h>
'+' flag: show plus sign
| #define FLAG_SPACE (1 << 4) |
#include <include/ft_printf.h>
' ' flag: leading space
| #define FLAG_ZERO (1 << 1) |
#include <include/ft_printf.h>
'0' flag: zero padding
#include <include/ft_printf.h>
#include <include/ft_printf.h>
|
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.
| pf | The current printf state. |
| str | Pointer to the number string (malloc'd). |
| len | Pointer to its length (will be updated). |
NULL on error.
|
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.
| pf | Pointer to the ft_printf global state. |
| str | Malloc’d number string (will be freed if changed). |
| len | Pointer to length of string (updated if modified). |
| is_neg | 1 if the number is negative (i.e., has - prefix), 0 otherwise. |
NULL on error.
|
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.
| ptr | Pointer to convert. |
| pf | Print context to set error flag if needed. |
#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.
| fmt | Format specifier structure. |
| args | Argument list containing the character. |
| pf | Print context structure. |
#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.
| fmt | Format descriptor (flags, width, precision). |
| args | Pointer to va_list with the argument. |
| pf | State struct tracking write progress and error flags. |
#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.
| fmt | Format descriptor (flags, width, precision). |
| args | Argument list (va_list) from ft_printf. |
| pf | Print context structure. |
#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.
| fmt | Format descriptor (width and flags). |
| pf | Print context for error tracking and byte count. |
#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.
| fmt | Format descriptor for width and precision. |
| args | Variadic arguments (string to print). |
| pf | Print state and error tracking. |
#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.
| fmt | Format descriptor (flags, width, precision). |
| args | Pointer to va_list with the argument. |
| pf | State struct tracking write progress and error flags. |
#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).
| fmt | Format descriptor (flags, width, precision). |
| args | Pointer to va_list with the argument. |
| upper | If non-zero, use uppercase letters in output. |
| pf | State struct tracking write progress and error flags. |
| 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:
t_pf to manage memory and write errorsIf a memory allocation or write operation fails, returns -1.
| format | The format string (e.g. "Hello %s\n"). |
| ... | Variadic arguments matching format specifiers. |
-1 on error.| 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.
| specifier | The current format specifier. |
|
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.
| len | Pointer to the string length (will be updated). |
| str | The current numeric string. |
| pf | Pointer to the ft_printf global state. |
"(null)" if memory allocation fails. | void handle_minus_flag_p | ( | char * | str, |
| int | padding, | ||
| t_pf * | pf | ||
| ) |
#include <include/ft_printf.h>
|
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.
| pf | Print state tracker to set malloc error flag if needed. |
#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:
"0" when precision is 0| fmt | Current format specifier. |
| str | Numeric string to process (must be malloc’d). |
| len | Pointer to the string length (updated). |
| pf | Pointer to the ft_printf global state. |
NULL on error.#include <include/ft_printf.h>
| 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.
| fmt | Format specifier structure. |
| len | Original string length. |
| void handle_space_flag_p | ( | t_pf * | pf, |
| int * | padding | ||
| ) |
#include <include/ft_printf.h>
| 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.
| fmt | Format descriptor. |
| len | Length of the current content to be printed. |
|
static |
#include <srcs/ft_printf/initialize_printf_structs.c>
Initializes a t_fmt (format specifier) structure.
Sets formatting fields to their default values:
flags = 0width = 0precision = -1 (unset)specifier = 0 (null character)| fmt | Pointer to the t_fmt structure to initialize. |
|
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.
| pf | Pointer to the t_pf structure to initialize. |
| 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.
| pf | Pointer to the t_pf structure that will be initialized. |
| int is_valid_flag | ( | char | c | ) |
#include <include/ft_printf.h>
Checks if a character is a valid format flag.
Recognized flags include:
#: alternate form0: zero padding-: left-justify+: show plus sign| c | The character to evaluate. |
#include <srcs/ft_printf/process_format_string.c>
Delegates the specifier to the appropriate formatter.
| fmt | Current format context (t_fmt). |
| args | Active variadic argument list. |
| pf | Print state tracker (t_pf). |
|
static |
#include <srcs/ft_printf/setup_format.c>
Parses format flags and sets corresponding bits.
Flags include: #, 0, -, +, and space.
| format | Format string pointer (will be advanced). |
| fmt | Target format context. |
|
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.
| format | Format string pointer (will be advanced). |
| fmt | Target format context. |
|
static |
#include <srcs/ft_printf/setup_format.c>
Validates and stores the conversion specifier.
Accepted specifiers: c, s, p, d, i, u, x, X, %
| format | Format string pointer (will be advanced). |
| fmt | Target format context. |
|
static |
#include <srcs/ft_printf/setup_format.c>
Parses field width (e.g., %10d).
Stores the integer width value in fmt->width.
| format | Format string pointer (will be advanced). |
| fmt | Target format context. |
| 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.
| str | The original hexadecimal string (must be malloc'd). |
| specifier | Either ‘'x’or'X'`, determines the prefix casing. |
| pf | Pointer to the printf context for error tracking. |
|
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.
| str | The numeric string to modify (will be freed). |
| sign | The character to prepend (+ or ‘’ '`). |
| pf | Pointer to the printf context (for error tracking). |
NULL on error.#include <include/ft_printf.h>
Final formatter and dispatcher for integer output.
This function prepares a number string for printing by applying:
+, , #)It prints the fully formatted number and handles memory cleanup.
| fmt | The current format descriptor. |
| str | Malloc'd number string to format. |
| len | Initial string length. |
| pf | The printf context for output state tracking. |
| 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.
| padding | Number of characters to pad. |
| pad_char | Character used for padding (usually ' ' or '0'). |
| pf | Print context to track output and errors. |
#include <include/ft_printf.h>
#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.
| fmt | Format descriptor (flags, width, precision). |
| str | String representation of the pointer. |
| pf | Pointer to print context (error and length tracker). |
|
static |
#include <srcs/ft_printf/print_formatted_number.c>
Writes the number string with left/right padding.
| pf | The current printf context. |
| str | The string to print. |
| padding | Number of padding spaces. |
|
static |
#include <srcs/ft_printf/print_formatted_number.c>
Writes zero-padding and handles printing the sign first if needed.
| padding | Amount of zeros to pad. |
| str_ptr | Pointer to the string pointer (adjusted if sign is printed). |
| pf | The printf state. |
|
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.
| format | Pointer to the current cursor in the format string. |
| args | Argument list used for conversions. |
| pf | Print state context. |
| 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.
| format | The format string to parse. |
| args | The active variadic arguments. |
| pf | Print state context. |
| 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"`).
| specifier | The format specifier character (x or X). |
| str | String representation of the value to print. |
|
static |
#include <srcs/ft_printf/process_format_string.c>
Resets the format context for the next conversion.
| fmt | The format descriptor to reset. |
| 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.
| format | Format string pointer (will be advanced). |
| fmt | Pointer to the format structure to populate. |
| 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.
| fd | File descriptor to write to. |
| c | Character to write. |
| pf | Pointer to printf state context (t_pf). |
| 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.
| fd | File descriptor to write to. |
| str | Null-terminated string to write. |
| pf | Pointer to printf state context (t_pf). |
| 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.
| fd | File descriptor to write to. |
| str | Buffer containing data to write. |
| len | Number of bytes to write. |
| pf | Pointer to printf state context (t_pf). |