aboutsummaryrefslogtreecommitdiff
path: root/src/io/ft_printf/extract.c
blob: c56a777b1018c67f0236868a81b8b5c819190c4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   extract.c                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2019/10/29 00:10:36 by cacharle          #+#    #+#             */
/*   Updated: 2019/11/10 10:33:33 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "ft_vasprintf.h"

const char	*extract_flags(t_pformat *pformat, const char *fmt)
{
	if (*fmt == '\0')
		return (fmt);
	while (ft_strchr(FLAGS_STR, *fmt) != NULL)
	{
		if (*fmt == '0')
			pformat->flags |= FLAG_ZERO;
		if (*fmt == '-')
			pformat->flags |= FLAG_MINUS;
		if (*fmt == '+')
			pformat->flags |= FLAG_SIGNED;
		if (*fmt == ' ')
			pformat->flags |= FLAG_SPACE;
		if (*fmt == '#')
			pformat->flags |= FLAG_ALTERNATE;
		if (*fmt == '\'')
			;
		fmt++;
	}
	if (pformat->flags & FLAG_SIGNED)
		pformat->flags &= ~FLAG_SPACE;
	return (fmt);
}

const char	*extract_width(t_pformat *pformat, const char *fmt)
{
	if (*fmt == '\0')
		return (fmt);
	if (*fmt == '*')
	{
		pformat->flags |= FLAG_WIDTH_WILDCARD;
		fmt++;
	}
	if (!ft_isdigit(*fmt))
		return (fmt);
	pformat->width = ft_atoi(fmt);
	while (*fmt && ft_isdigit(*fmt))
		fmt++;
	if (pformat->flags & FLAG_WIDTH_WILDCARD)
		pformat->flags |= FLAG_WIDTH_OVERWRITE;
	return (fmt);
}

const char	*extract_precision(t_pformat *pformat, const char *fmt)
{
	if (*fmt == '\0' || *fmt != '.')
		return (fmt);
	fmt++;
	if (*fmt == '*')
	{
		pformat->flags |= FLAG_PRECISION_WILDCARD;
		fmt++;
	}
	pformat->precision = ft_atoi(fmt);
	while (*fmt && ft_isdigit(*fmt))
		fmt++;
	return (fmt);
}

const char	*extract_length_modifier(t_pformat *pformat, const char *fmt)
{
	if (fmt[0] && fmt[0] == 'l')
	{
		if (fmt[1] && fmt[1] == 'l')
		{
			pformat->flags |= FLAG_LONG_LONG;
			return (fmt + 2);
		}
		pformat->flags |= FLAG_LONG;
		return (fmt + 1);
	}
	if (fmt[0] && fmt[0] == 'h')
	{
		if (fmt[1] && fmt[1] == 'h')
		{
			pformat->flags |= FLAG_SHORT_SHORT;
			return (fmt + 2);
		}
		pformat->flags |= FLAG_SHORT;
		return (fmt + 1);
	}
	return (fmt);
}