ft_printf / src / parser / parse_utils_bonus.c 79 commits
02-05-2026
93 lines 2.4 kB
 1/* ************************************************************************** */
 2/*                                                                            */
 3/*                                                        :::      ::::::::   */
 4/*   parse_utils_bonus.c                                :+:      :+:    :+:   */
 5/*                                                    +:+ +:+         +:+     */
 6/*   By: mleitao <mleitao@student.42.fr>            +#+  +:+       +#+        */
 7/*                                                +#+#+#+#+#+   +#+           */
 8/*   Created: 2026/03/30 18:11:57 by mleitao           #+#    #+#             */
 9/*   Updated: 2026/05/02 00:51:57 by mleitao          ###   ########.fr       */
10/*                                                                            */
11/* ************************************************************************** */
12
13#include "parser_bonus.h"
14#include "utils.h"
15
16void	set_flags(t_data *d)
17{
18	char	flag;
19
20	while (in(FLAGS, *d->fmt))
21	{
22		flag = *d->fmt;
23		if (flag == '-')
24			d->format.minus = 1;
25		else if (flag == '0')
26			d->format.zero = 1;
27		else if (flag == '#')
28			d->format.hash = 1;
29		else if (flag == ' ')
30			d->format.space = 1;
31		else if (flag == '+')
32			d->format.plus = 1;
33		d->fmt++;
34	}
35}
36
37void	set_width(t_data *d)
38{
39	if (*d->fmt == '*')
40	{
41		d->format.width = (long)va_arg(d->args, int);
42		if (d->format.width < 0)
43		{
44			d->format.minus = 1;
45			d->format.width = -d->format.width;
46		}
47		d->fmt++;
48		return ;
49	}
50	d->format.width = 0;
51	while (ft_isdigit(*d->fmt))
52	{
53		d->format.width = d->format.width * 10 + (*d->fmt - '0');
54		d->fmt++;
55	}
56}
57
58void	set_precision(t_data *d)
59{
60	if (*d->fmt == '*')
61	{
62		d->format.precision = (long)va_arg(d->args, int);
63		if (d->format.precision < 0)
64			d->format.precision = -1;
65		d->fmt++;
66		return ;
67	}
68	d->format.precision = 0;
69	while (ft_isdigit(*d->fmt))
70	{
71		d->format.precision = d->format.precision * 10 + (*d->fmt - '0');
72		d->fmt++;
73	}
74}
75
76int	set_specifier(t_data *d)
77{
78	if (!in(SPECIFIERS, *d->fmt))
79		return (-1);
80	return (d->format.spec = *d->fmt++, 0);
81}
82
83void	normalize_format(t_data *d)
84{
85	if (d->format.minus)
86		d->format.zero = 0;
87	if (d->format.precision >= 0 && (in("diuxX", d->format.spec)))
88		d->format.zero = 0;
89	if (d->format.plus)
90		d->format.space = 0;
91	if (!(in("xX", d->format.spec)))
92		d->format.hash = 0;
93}