aboutsummaryrefslogtreecommitdiff
path: root/printer.c
blob: a4da1a19efc4e0c9cc7c0fe674b73fc9f6841ee9 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   printer.c                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2019/10/28 23:19:24 by cacharle          #+#    #+#             */
/*   Updated: 2019/10/30 04:13:18 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include "header.h"


char	*convert(t_pformat *pformat, va_list ap)
{
	char			*str;

	if (pformat->flags & FLAG_MIN_WIDTH_WILDCARD)
	{
		if (pformat->flags & FLAG_MIN_WIDTH_OVERWRITE)
			(void)va_arg(ap, int);
		else
			pformat->min_width = va_arg(ap, int);
		if (pformat->min_width < 0)
		{
			pformat->flags |= FLAG_LEFT_ADJUSTED;
			pformat->min_width *= -1;
		}
	}
	if (pformat->flags & FLAG_PRECISION_WILDCARD)
		pformat->precision = va_arg(ap, int);
	if ((str = convert_type(ap, pformat)) == NULL)
		return (NULL);
	return (str);
}

char	*convert_type(va_list ap, t_pformat *pformat)
{
	if (pformat->type == 'c')
		return (convert_char(ap, pformat));
	if (pformat->type == 's')
		return (convert_str(ap, pformat));
	if (pformat->type == 'p')
		return (convert_ptr(ap, pformat));
	if (pformat->type == 'd' || pformat->type == 'i')
		return (convert_int(ap, pformat));
	if (pformat->type == 'u')
		return (convert_uint(ap, pformat));
	if (pformat->type == 'x')
		return (convert_hex_low(ap, pformat));
	if (pformat->type == 'X')
		return (convert_hex_up(ap, pformat));
	if (pformat->type == '%')
		return (convert_percent(ap, pformat));
	return (NULL);
}

char	*handle_padding(t_pformat *pformat, char *str)
{
	char	*tmp;
	int		len;
	int		i;

	if ((len = ft_strlen(str)) >= pformat->min_width)
		return (str);
	if ((tmp = (char*)malloc(sizeof(char) * (pformat->min_width + 1))) == NULL)
		return (NULL);
	if (pformat->flags & FLAG_LEFT_ADJUSTED)
	{
		i = len;
		ft_strcpy(tmp, str);
		while (i < pformat->min_width)
			tmp[i++] = ' ';
		tmp[i] = 0;
	}
	else
	{
		i = 0;
		while (i <= pformat->min_width - len)
			tmp[i++] = pformat->flags & FLAG_ZERO_PADDING ? '0' : ' ';
		ft_strcpy(tmp + i - 1, str);
	}
	free(str);
	return (tmp);
}

/* char *handle_nbr_padding(t_pformat *pformat, char *str) */
/* { */
/* 	char *tmp; */
/* 	int len = ft_strlen(str); */
/* 	int i; */
/*  */
/* 	if (!(IN_STR("+-", str[0]) && pformat->flags & FLAG_ZERO_PADDING)) */
/* 		return (str); */
/* 	tmp[0] = str[0]; */
/* 	len--; */
/* 	i = 1; */
/* 	while (i < pformat->min_width - len) */
/* 		tmp[i++] = '0'; */
/* 	ft_strcpy(tmp + i , str + 1); */
/* 	ft_strcpy(str, tmp); */
/* 	free(tmp); */
/* 	return (str); */
/* } */

char	*handle_precision(t_pformat *pformat, char *str)
{
	int		len;
	char	*tmp;

	len = ft_strlen(str);
	if (pformat->precision == 0 && str[0] == '0')
		return (ft_strdup(""));
	else if (IN_STR("diuxXp", pformat->type) && len < pformat->precision)
	{
		if ((tmp = (char*)malloc(sizeof(char) * (pformat->precision + 1))) == NULL)
			return (NULL);
		if (IN_STR("+-", str[0]))
		{
			tmp[0] = str[0];
			len--;
			ft_strcpy(tmp + pformat->precision - len + 1, str + 1);
			while (pformat->precision-- > len)
				tmp[pformat->precision - len + 1] = '0';
			ft_strcpy(str, tmp);
			free(tmp);
			return (str);
		}
		ft_strcpy(tmp + pformat->precision - len, str);
		while (pformat->precision-- > len)
			tmp[pformat->precision - len] = '0';
		ft_strcpy(str, tmp);
		free(tmp);
	}
	return (str);
}