This repository was archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strxjoin.c
74 lines (68 loc) · 2.09 KB
/
ft_strxjoin.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_strxjoin.c :+: :+: */
/* +:+ */
/* By: fbes <fbes@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2022/02/09 16:23:26 by fbes #+# #+# */
/* Updated: 2022/04/09 00:43:05 by fbes ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stddef.h>
static char *xerror(t_ft_va_list strs, char *res)
{
ft_va_end(strs);
ft_free(res);
return (NULL);
}
static void xjoin(size_t amount, t_ft_va_list strs, char *res)
{
t_ft_va_list strx;
char *temp;
size_t i;
i = 0;
temp = res;
ft_va_start(&strx, strs);
while (i < amount)
{
ft_memcpy(temp, (*strx)->item, (*strx)->size - 1);
temp += (*strx)->size - 1;
ft_va_arg(&strx);
i++;
}
}
/**
* Join many strings into one big string
* @param[in] amount The amount of strings to join
* @param[in] strs Strings to join into the newly formed string,
* stored in ft_va_list format. Warning: this list always
* freed by ft_va_end after running this method.
* @return A pointer to the newly allocated joined string,
* or NULL on error
*/
char *ft_strxjoin(size_t amount, t_ft_va_list strs)
{
t_ft_va_list strx;
size_t i;
size_t joint_len;
char *res;
res = NULL;
if (amount == 0)
return (xerror(strs, res));
joint_len = 0;
i = 0;
ft_va_start(&strx, strs);
while (i < amount)
{
joint_len += (*strx)->size - 1;
ft_va_arg(&strx);
i++;
}
res = ft_stralloc(joint_len);
if (joint_len > 0)
xjoin(amount, strs, res);
ft_va_end(strs);
return (res);
}