-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_multijoin_array.c
More file actions
73 lines (66 loc) · 1.99 KB
/
ft_multijoin_array.c
File metadata and controls
73 lines (66 loc) · 1.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_multijoin_array.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbetz <rbetz@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/02 16:08:04 by rbetz #+# #+# */
/* Updated: 2023/02/22 10:58:28 by rbetz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *copy_input(char **array, size_t *lengths, size_t len)
{
char *str;
size_t i;
size_t pos;
str = ft_calloc(len + 1, sizeof(char));
if (!str)
return (NULL);
i = 0;
pos = 0;
while (array[i] != NULL)
{
ft_memcpy(&str[pos], array[i], lengths[i]);
pos += lengths[i];
i++;
}
return (str);
}
static size_t get_total_length(char **array, size_t *lengths)
{
size_t total_len;
size_t i;
total_len = 0;
i = 0;
while (array[i] != NULL)
{
lengths[i] = ft_strlen(array[i]);
total_len += lengths[i];
i++;
}
return (total_len);
}
/*Combines every string inside array into a single string. Array needs to be
terminated with NULL pointer.
Will not free any string inside the array, but frees the array.*/
char *ft_multijoin_array(char **array)
{
size_t len;
size_t *lengths;
size_t size;
char *str;
if (array == NULL)
return (NULL);
size = ft_arraycount(array);
lengths = ft_calloc(size, sizeof(size_t));
if (lengths == NULL)
return (NULL);
len = get_total_length(array, lengths);
str = copy_input(array, lengths, len);
free(lengths);
free(array);
array = NULL;
return (str);
}