-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr_alloc.c
46 lines (41 loc) · 1.42 KB
/
str_alloc.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_alloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alischyn <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/22 17:51:11 by alischyn #+# #+# */
/* Updated: 2017/03/22 18:01:02 by alischyn ### ########.fr */
/* */
/* ************************************************************************** */
#include "str.h"
void str_init(t_str *str)
{
str->string = NULL;
str->length = 0;
str->capacity = 0;
}
void str_realloc(t_str *str, int need)
{
int new_capacity;
char *new_string;
need += need % ALLOC_CHUNK;
new_capacity = str->capacity + need;
new_string = (char *)malloc(new_capacity);
if (str->length > 0)
{
MEMCPY(new_string, str->string, str->length);
free(str->string);
}
str->string = new_string;
str->capacity = new_capacity;
}
void str_free(t_str *str)
{
if (str->capacity)
{
free(str->string);
str_init(str);
}
}