-
Notifications
You must be signed in to change notification settings - Fork 1
/
str.c
126 lines (118 loc) · 3.12 KB
/
str.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/************************************************************************
*
* Compiler implementation for imperative programming language IFJ18
*
* Autors:
* Sasák Tomáš - xsasak01
* Venkrbec Tomáš - xvenkr01
* Krajči Martin - xkrajc21
* Dižová Natália - xdizov00
*
***********************************************************************/
#include <string.h>
#include <malloc.h>
#include "str.h"
/**
* Function initalizes string before it's first use.
* @param s Pointer to the string.
*/
int str_init(string *s)
{
if ((s->str = (char*) malloc(STR_LEN_INC)) == NULL)
return STR_ERROR;
s->str[0] = '\0';
s->length = 0;
s->allocSize = STR_LEN_INC;
return STR_SUCCESS;
}
/**
* Function free's out the string used memory.
* @param s Pointer to the string.
*/
void str_free(string *s)
{
free(s->str);
}
/**
* Function appends char array on the end of string.
* @param s1 Pointer to the string.
* @param c Pointer to the string array which will be appended.
*/
int str_add_string(string *s1, char* c)
{
int length = strlen(c);
for(int i = 0; i < length; i++)
{
str_add_char(s1, c[i]);
}
return STR_SUCCESS;
}
/**
* Function appends one char on the end of string.
* @param s1 Pointer to the string.
* @param c Char which will be appended at the end of s1.
*/
int str_add_char(string *s1, char c)
{
if (s1->length + 1 >= s1->allocSize) { // Not enough memory, we need to reallocate
if ((s1->str = (char*) realloc(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;
}
/**
* Function copies second string into first string.
* @param s1 Pointer to the string (destination).
* @param s2 Pointer to the string (source).
*/
int str_copy_string(string *s1, string *s2)
{
int newLength = s2->length;
if (newLength >= s1->allocSize) { // Not enough memory, we need to reallocate
if ((s1->str = (char*) realloc(s1->str, newLength + 1)) == NULL)
return STR_ERROR;
s1->allocSize = newLength + 1;
}
strcpy(s1->str, s2->str);
s1->length = newLength;
return STR_SUCCESS;
}
/**
* Function copies constant string array into string.
* @param s1 Pointer to the string (destination).
* @param s2 Pointer to the constant char array (source).
*/
int str_copy_const_string(string *s1, char *s2)
{
int newLength = strlen(s2);
if (newLength >= s1->allocSize) { // Not enough memory, we need to reallocate
if ((s1->str = (char*) realloc(s1->str, newLength + 1)) == NULL)
return STR_ERROR;
s1->allocSize = newLength + 1;
}
strcpy(s1->str, s2);
s1->length = newLength;
return STR_SUCCESS;
}
/**
* Function compares right string with left string and returns result.
* @param s1 Pointer to the left string.
* @param s2 Pointer to the right string.
*/
int str_cmp_string(string *s1, string *s2)
{
return strcmp(s1->str, s2->str);
}
/**
* Function compares left string with constant char array and returns result.
* @param s1 Pointer to the left string.
* @param s2 Constant char array.
*/
int str_cmp_const_str(string *s1, char* s2)
{
return strcmp(s1->str, s2);
}