-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.c
65 lines (60 loc) · 1.14 KB
/
strings.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
/**
* @project IFJ17
* @file strings.c
* @author Jan Bartosek (xbarto92)
* @brief Operations with string structure.
*/
#include "strings.h"
#include <malloc.h>
#include <string.h>
#include "garbage_collector.h"
/**
* @copydoc strInit
*/
int strInit(string *s) {
if ((s->str = (char *) gcmalloc(STR_LEN_INC)) == NULL) {
return STR_ERROR;
}
s->str[0] = '\0';
s->length = 0;
s->allocSize = STR_LEN_INC;
return STR_SUCCESS;
}
/**
* @copydoc strFree
*/
void strFree(string *s) {
gcfree(s->str);
}
/**
* @copydoc strClear
*/
void strClear(string *s) {
strFree(s);
strInit(s);
}
/**
* @copydoc strAddChar
*/
int strAddChar(string *s1, char c) {
if (s1->length + 1 >= s1->allocSize) {
if ((s1->str = (char *) gcrealloc(s1->str, s1->length + STR_LEN_INC)) == NULL) {
return STR_ERROR;
}
s1->allocSize = s1->length + STR_LEN_INC;
}
s1->str[s1->length] = c;
s1->length++;
s1->str[s1->length] = '\0';
return STR_SUCCESS;
}
/**
* @copydoc strSizeUp
*/
int strSizeUp(string *s1) {
if ((s1->str = (char *) gcrealloc(s1->str, s1->length * 2)) == NULL) {
return STR_ERROR;
}
s1->allocSize = s1->length * 2;
return STR_SUCCESS;
}