-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_multijoin.c
81 lines (74 loc) · 2.07 KB
/
ft_multijoin.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_multijoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbetz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/02 16:08:04 by rbetz #+# #+# */
/* Updated: 2023/02/22 10:57:21 by rbetz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
// static char *copy_input(bool tofr, int n, char **input)
// {
// int i;
// int len;
// char *str;
// i = 0;
// len = 0;
// while (i < n)
// len += ft_strlen(input[i++]);
// str = ft_calloc(len + 1, sizeof(char));
// i = 0;
// len = 0;
// while (str != NULL && i < n)
// {
// ft_memcpy(&str[len], input[i], ft_strlen(input[i]));
// len += ft_strlen(input[i]);
// if (tofr == true)
// {
// free(input[i]);
// input[i] = NULL;
// }
// i++;
// }
// return (str);
// }
static char **read_input(int n, va_list args)
{
int i;
char **input;
i = 0;
input = ft_calloc(n + 1, sizeof(char *));
if (input == NULL)
return (NULL);
input[n] = NULL;
while (i < n)
{
input[i] = va_arg(args, char *);
i++;
}
return (input);
}
/*tofr, frees the input if true, n is the amount of strings,*/
/*accepts endless strings to join together*/
char *ft_multijoin(bool tofr, int n, ...)
{
va_list args;
char **input;
char **cpy;
char *str;
if (n < 2)
return (NULL);
va_start(args, n);
input = read_input(n, args);
if (input == NULL)
return (NULL);
cpy = input;
str = ft_multijoin_array(input);
va_end(args);
while (tofr && cpy)
free(cpy++);
return (str);
}