-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstr.c
More file actions
60 lines (54 loc) · 1.39 KB
/
str.c
File metadata and controls
60 lines (54 loc) · 1.39 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
/**
* School project to subject IFJ (Formal Languages and Compilers)
* Compiler implementation of imperative language IFJ18
*
* Module for operations with string, inspired by the one used in "jednoduchy_interpret"
*/
#include <string.h>
#include <stdlib.h>
#include "str.h"
#define STR_LEN_INC 8
int strInit(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;
}
void strFree(string *s)
{
free(s->str);
}
void strClear(string *s)
{
s->str[0] = '\0';
s->length = 0;
}
int strAddChar(string *s1, char c)
{
if (s1->length + 1 >= s1->allocSize) {
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;
}
int strCopyString(string *s1, string *s2)
// prekopiruje retezec s2 do s1
{
int newLength = s2->length;
if (newLength >= s1->allocSize) {
// pamet nestaci, je potreba provest realokaci
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;
}