tgrekov-ft_printf
HIVE printf Feb 2024
Loading...
Searching...
No Matches
handle_sequence.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* handle_sequence.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2023/11/01 00:41:01 by tgrekov #+# #+# */
9/* Updated: 2024/02/13 07:11:50 by tgrekov ### ########.fr */
10/* */
11/* ************************************************************************** */
12
20#include <stdarg.h>
21#include "sequence.h"
22#include "../utils/utils.h"
23#include "../../../libft/libft.h"
24
25void init_subspec(t_subspec *subspec);
26void identify_sequence(va_list args, t_sequence *seq);
27void parse_subspec(const char **format, t_subspec *subspec, va_list args);
28
39static int print(t_sequence seq, int *fd, int total)
40{
41 int res;
42
43 res = 0;
44 if (seq.pad_len && !seq.subspec.left_justify
45 && seq.subspec.pad_str[0] != '0'
46 && !wrap_err(repeat_str_n(seq.subspec.pad_str, seq.pad_len, *fd), &res))
47 return (-1);
48 if (seq.sign && !wrap_err(write(*fd, seq.sign, ft_strlen(seq.sign)), &res))
49 return (-1);
50 if (seq.pad_len && !seq.subspec.left_justify
51 && seq.subspec.pad_str[0] == '0'
52 && !wrap_err(repeat_str_n(seq.subspec.pad_str, seq.pad_len, *fd), &res))
53 return (-1);
54 if (!wrap_err(seq.process(seq, fd, total + res), &res))
55 return (-1);
56 if (seq.pad_len && seq.subspec.left_justify
57 && !wrap_err(repeat_str_n(seq.subspec.pad_str, seq.pad_len, *fd), &res))
58 return (-1);
59 return (res);
60}
61
77int handle_sequence(const char **format, va_list args, int *fd, int total)
78{
79 t_sequence seq;
80
82 parse_subspec(format, &seq.subspec, args);
83 seq.specifier = **format;
84 identify_sequence(args, &seq);
85 if (!seq.specifier)
86 return (-1);
87 if (seq.sign)
88 seq.total_len += ft_strlen(seq.sign);
89 if (seq.subspec.min_width > seq.total_len)
90 seq.pad_len = seq.subspec.min_width - seq.total_len;
91 seq.total_len += seq.pad_len;
92 (*format)++;
93 return (print(seq, fd, total));
94}
void init_subspec(t_subspec *subspec)
Sets initial values for a s_subspec that are not always set elsewhere.
void identify_sequence(va_list args, t_sequence *seq)
Initialize a s_sequence and determine which specifier it needs to handle, and runs the preprocessing ...
void parse_subspec(const char **format, t_subspec *subspec, va_list args)
Parse flags, width, precision, and length.
static int print(t_sequence seq, int *fd, int total)
Print appropriate padding characters on either side, as well as the sign / prefix,...
int handle_sequence(const char **format, va_list args, int *fd, int total)
Prepares and executes a sequence.
int wrap_err(int n, int *total)
Wrapper for handling functions that return a positive integer on success and -1 on failure....
Definition wrap_err.c:29
size_t ft_strlen(const char *str)
Get length of str.
Definition ft_strlen.c:28
int repeat_str_n(const char *str, int n, int fd)
Write exactly n characters from str on descriptor fd. Repeats string if ft_strlen(str) is greater tha...
Holds information regarding the current format specifier sequence.
Definition sequence.h:49
int(* process)(struct s_sequence, int *, int)
Function to use for processing this sequence's value.
Definition sequence.h:56
char specifier
The specifier character for this sequence.
Definition sequence.h:50
t_subspec subspec
Holds subspecifier options for a format specifier.
Definition sequence.h:51
int total_len
Total number of characters after padding.
Definition sequence.h:54
char * sign
String containing the prefix for the value. Set during preprocessing for a format handler.
Definition sequence.h:52
int pad_len
Number of characters to pad.
Definition sequence.h:55
Holds subspecifier options for a format specifier.
Definition subspec.h:65
int min_width
Minimum number of characters to be printed.
Definition subspec.h:70
char left_justify
1 or 0, whether padding should left justify.
Definition subspec.h:66
char * pad_str
String used when padding values.
Definition subspec.h:69