minishell
vect_append_range.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* vect_append_range.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: kfujita <kfujita@student.42tokyo.jp> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2023/02/06 19:12:30 by kfujita #+# #+# */
9 /* Updated: 2023/02/06 23:57:28 by kfujita ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "ft_vect.h"
14 
15 #include "../ft_math/ft_math.h"
16 #include "../ft_mem/ft_mem.h"
17 
18 static const int g_flag_string = 1;
19 static const int g_flag_not_string = 0;
20 
22  t_vect *vect, const void *value, size_t count, int is_str)
23 {
24  size_t bytes_to_copy;
25  size_t new_len;
26 
27  if (!can_mulp(vect->elemsize, count))
28  return (false);
29  if (!can_addp(vect->len, count))
30  return (false);
31  new_len = vect->len + count;
32  if (vect->cap < (new_len + is_str) && !vect_reserve(vect, new_len + is_str))
33  return (false);
34  bytes_to_copy = vect->elemsize * count;
35  ft_memmove(((unsigned char *)vect->p) + (vect->len * vect->elemsize),
36  value, bytes_to_copy);
37  vect->len = new_len;
38  if (is_str)
39  ((char *)(vect->p))[vect->len] = '\0';
40  return (true);
41 }
42 
43 bool vect_append_range(t_vect *vect, const void *value, size_t count)
44 {
45  return (vect_append_internal(vect, value, count, g_flag_not_string));
46 }
47 
48 bool vect_append_str(t_vect *vect, const char *value, size_t count)
49 {
50  return (vect_append_internal(vect, value, count, g_flag_string));
51 }
bool can_addp(size_t a, size_t b)
Definition: can_add.c:43
bool can_mulp(size_t a, size_t b)
Definition: can_mul.c:50
void * ft_memmove(void *dst, const void *src, size_t n)
Definition: ft_memmove.c:16
bool vect_reserve(t_vect *vect, size_t newcap)
Definition: vect_reserve.c:17
Definition: ft_vect.h:24
size_t cap
Definition: ft_vect.h:25
size_t elemsize
Definition: ft_vect.h:27
void * p
Definition: ft_vect.h:28
size_t len
Definition: ft_vect.h:26
static const int g_flag_not_string
static bool vect_append_internal(t_vect *vect, const void *value, size_t count, int is_str)
static const int g_flag_string
bool vect_append_str(t_vect *vect, const char *value, size_t count)
bool vect_append_range(t_vect *vect, const void *value, size_t count)