forked from xjuric29/ifj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
str.c
executable file
·104 lines (93 loc) · 2.33 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
//jednoducha knihovna pro praci s nekonecne dlouhymi retezci
//ZOBRANE Z JEDNODUCHEHO INTERPRETA
#include <string.h>
#include <malloc.h>
#include "str.h"
#define STR_LEN_INC 10
// konstanta STR_LEN_INC udava, na kolik bytu provedeme pocatecni alokaci pameti
// pokud nacitame retezec znak po znaku, pamet se postupne bude alkokovat na
// nasobky tohoto cisla
int strInit(string *s)
// funkce vytvori novy retezec
{
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)
// funkce uvolni retezec z pameti
{
free(s->str);
}
void strClear(string *s)
// funkce vymaze obsah retezce
{
s->str[0] = '\0';
s->length = 0;
}
int strAddChar(string *s1, char c)
// prida na konec retezce jeden znak
{
if (s1->length + 1 >= s1->allocSize)
{
// pamet nestaci, je potreba provest realokaci
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 strCopyConst(string *s, char *c){
//strClear(s);
int newLength = strlen(c);
if (newLength >= s->allocSize)
{
if ((s->str = (char*) realloc(s->str, newLength + 1)) == NULL)
return STR_ERROR;
s->allocSize = newLength + 1;
}
strcpy(s->str, c);
s->length = newLength;
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;
}
int strCmpString(string *s1, string *s2)
// porovna oba retezce a vrati vysledek
{
return strcmp(s1->str, s2->str);
}
int strCmpConstStr(string *s1, char* s2)
// porovna nas retezec s konstantnim retezcem
{
return strcmp(s1->str, s2);
}
char *strGetStr(string *s)
// vrati textovou cast retezce
{
return s->str;
}
int strGetLength(string *s)
// vrati delku daneho retezce
{
return s->length;
}