-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf_utils_convert_string_precision.c
More file actions
93 lines (83 loc) · 2.99 KB
/
ft_printf_utils_convert_string_precision.c
File metadata and controls
93 lines (83 loc) · 2.99 KB
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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_printf_utils_convert_string_precision.c :+: :+: */
/* +:+ */
/* By: ohengelm <ohengelm@student.42.fr> +#+ */
/* +#+ */
/* Created: 2022/04/02 21:14:32 by ohengelm #+# #+# */
/* Updated: 2023/08/04 19:37:21 by ohengelm ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
// void *ft_calloc(size_t count, size_t size)
// size_t ft_strlen(const char *s)
// char *ft_strchr(const char *s, int c)
// void *ft_memset(void *b, int c, size_t len)
// void *ft_memcpy(void *dst, const void *src, size_t n)
// size_t ft_strlen(const char *s)
#include "ft_printf_utils.h"
// t_flags
#include <stdlib.h>
// void free(void *)
static char *calloc_presicion_size(t_flags *flags, char *string);
static void prepare_new_str(t_flags *flags, char *new_str, char *str, int size);
static void cpy_string_to_new_str(t_flags *flags, char *str, char *new_str);
char *ftp_modify_precision(t_flags *flags, char *string)
{
char *new_str;
new_str = calloc_presicion_size(flags, string);
if (new_str != NULL)
cpy_string_to_new_str(flags, string, new_str);
free(string);
return (new_str);
}
static char *calloc_presicion_size(t_flags *flags, char *string)
{
char *new_str;
int len;
int size;
if (string[0] == '0' && flags->prec == 0)
return (ft_calloc(sizeof(char), 1));
len = ft_strlen(string);
size = len;
if (ft_strchr("diouxXp", flags->conv) != NULL && size <= flags->prec)
{
size = flags->prec;
if (string[0] == '-')
size++;
}
else if (flags->prec >= 0 && flags->prec < len && flags->conv == 's')
size = flags->prec;
if (flags->conv == 'p')
size += 2;
new_str = (char *)ft_calloc(sizeof(char), size + 1);
if (new_str != NULL)
prepare_new_str(flags, new_str, string, size);
return (new_str);
}
static void prepare_new_str(t_flags *flags, char *new_str, char *str, int size)
{
if (flags->conv == 's')
ft_memset(new_str, ' ', size);
else
ft_memset(new_str, '0', size);
if (str[0] == '-' && ft_strchr("dio", flags->conv))
new_str[0] = '-';
if (flags->conv == 'p')
ft_memcpy(new_str, "0x", 2);
}
static void cpy_string_to_new_str(t_flags *flags, char *str, char *new_str)
{
int loc_str;
int len_newstr;
int len_str;
loc_str = 0;
if (ft_strchr("dio", flags->conv) && str[0] == '-')
loc_str++;
len_str = ft_strlen(&str[loc_str]);
len_newstr = ft_strlen(new_str);
if (len_newstr < len_str)
len_str = len_newstr;
ft_memcpy(&new_str[len_newstr - len_str], &str[loc_str], len_str);
}