tgrekov-ft_printf
HIVE printf Feb 2024
Loading...
Searching...
No Matches
parse_subspec.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* parse_subspec.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/01/25 17:52:45 by tgrekov #+# #+# */
9/* Updated: 2024/02/13 13:43:57 by tgrekov ### ########.fr */
10/* */
11/* ************************************************************************** */
12
20#include <stdarg.h>
21#include "subspec.h"
22#include "../utils/utils.h"
23#include "../../../libft/libft.h"
24
31void init_subspec(t_subspec *subspec)
32{
33 subspec->left_justify = 0;
34 subspec->forced_sign = 0;
35 subspec->force_decimal = 0;
36 subspec->pad_str = " ";
37 subspec->min_width = 0;
38 subspec->precision = -1;
39 subspec->lenmod = none;
40}
41
48static void parse_lenmod(const char **format, t_subspec *subspec)
49{
50 if (**format == 'h' && (*format)++)
51 {
52 subspec->lenmod = h;
53 if (**format == 'h' && (*format)++)
54 subspec->lenmod = hh;
55 }
56 else if (**format == 'l' && (*format)++)
57 {
58 subspec->lenmod = l;
59 if (**format == 'l' && (*format)++)
60 subspec->lenmod = ll;
61 }
62 else if (**format == 'j' && (*format)++)
63 subspec->lenmod = j;
64 else if (**format == 'z' && (*format)++)
65 subspec->lenmod = z;
66 else if (**format == 't' && (*format)++)
67 subspec->lenmod = t;
68}
69
77static int subspec_parse_width_or_precision(const char **format, va_list args)
78{
79 int res;
80
81 if (**format == '*')
82 {
83 (*format)++;
84 return (va_arg(args, int));
85 }
86 res = ft_atoi(*format);
87 while (ft_isdigit(**format))
88 (*format)++;
89 return (res);
90}
91
99void parse_subspec(const char **format, t_subspec *subspec, va_list args)
100{
101 while (**format && ft_strchr("-+ #0_", **format))
102 {
103 if (**format == '-')
104 subspec->left_justify = 1;
105 else if (**format == '+')
106 subspec->forced_sign = "+";
107 else if (**format == ' ')
108 subspec->forced_sign = " ";
109 else if (**format == '#')
110 subspec->force_decimal = 1;
111 else if (**format == '0' && !subspec->left_justify)
112 subspec->pad_str = "0000000000";
113 else if (**format == '_')
114 subspec->pad_str = va_arg(args, char *);
115 (*format)++;
116 }
117 if (**format == '*' || ft_isdigit(**format))
118 subspec->min_width = subspec_parse_width_or_precision(format, args);
119 if (**format == '.')
120 {
121 (*format)++;
122 subspec->precision = subspec_parse_width_or_precision(format, args);
123 }
124 parse_lenmod(format, subspec);
125}
int ft_atoi(const char *str)
Converts ASCII string str to integer representation.
Definition ft_atoi.c:42
int ft_isdigit(int c)
Is c a numeric character.
Definition ft_isdigit.c:26
char * ft_strchr(const char *s, int c)
Finds first occurence of character c in string s.
Definition ft_strchr.c:30
static void parse_lenmod(const char **format, t_subspec *subspec)
Parse the length modifier for the sequence.
void init_subspec(t_subspec *subspec)
Sets initial values for a s_subspec that are not always set elsewhere.
void parse_subspec(const char **format, t_subspec *subspec, va_list args)
Parse flags, width, precision, and length.
static int subspec_parse_width_or_precision(const char **format, va_list args)
Parse inline or variable argument width or precision.
Holds subspecifier options for a format specifier.
Definition subspec.h:65
int precision
Minimum number of digits to be printed, or maximum characters for strings.
Definition subspec.h:71
int min_width
Minimum number of characters to be printed.
Definition subspec.h:70
char force_decimal
1 or 0, whether or not to prefix octals or hexadecimals with their respective prefixes....
Definition subspec.h:68
char * forced_sign
String to print before the value. Set before preprocessing for a format handler. Will be overriden,...
Definition subspec.h:67
t_lenmod lenmod
Represents the length modifiers for a format specifier's variable argument.
Definition subspec.h:72
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