aboutsummaryrefslogtreecommitdiff
path: root/convert_char.c
blob: e15457e0f9c8d8efe57417869c25b09a29a71c84 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   convert_char.c                                     :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2019/10/30 23:22:29 by cacharle          #+#    #+#             */
/*   Updated: 2019/10/30 23:37:29 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

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

static char	*handle_padding_char(t_pformat *pformat, char *str)
{
	char	*tmp;
	int		i;

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

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

	if ((str = ft_strnew(2)) == NULL)
		return (NULL);
	str[0] = va_arg(ap, int);
	str[1] = '\0';
	return (handle_padding_char(pformat, str));
}