-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.c
More file actions
65 lines (54 loc) · 1.54 KB
/
Copy pathstring.c
File metadata and controls
65 lines (54 loc) · 1.54 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
61
62
63
64
65
/**
* Projekt IFJ/IAL 2019 - Překladač imperativního jazyka IFJ19
*
* @file string.c
* @brief Knihovna pro praci s dynamickym stringem
*
* @author Daniel Pátek (xpatek08)
*/
#include "string.h"
#include "error_code.h"
#include <stdlib.h>
#include <string.h>
int stringInit(string *str) {
// memory allocation
if ( (str->value = (char*) malloc(STRING_INIT_SIZE * sizeof(char))) == NULL )
return ERROR_CODE_INTERNAL;
//inicialize structure
str->length = 0;
str->value[0] = '\0';
str->spaceAlloc = STRING_INIT_SIZE;
return ERROR_CODE_OK;
}
int stringAddChar(string *str, char c) {
// if the string is full - it needs to be reallocated
if (str->length + 1 >= str->spaceAlloc) {
if ((str->value = (char *) realloc(str->value, str->spaceAlloc + STRING_INIT_SIZE * sizeof(char))) == NULL)
return ERROR_CODE_INTERNAL;
str->spaceAlloc += STRING_INIT_SIZE;
}
// adding char to the end of string
str->value[str->length] = c;
str->value[str->length+1] = '\0';
str->length++;
return ERROR_CODE_OK;
}
int stringAddChars(string *str, char* c) {
int result_code;
for (unsigned int i = 0; i < strlen(c); i++) {
result_code = stringAddChar(str, c[i]);
}
return result_code;
}
int stringClear(string *str) {
for (int i = 0; i < str->spaceAlloc; i++) {
str->value[i] = '\0';
}
str->length = 0;
return ERROR_CODE_OK;
}
void stringDispose(string *str) {
free(str->value);
str->value = NULL;
str->length = str->spaceAlloc = 0;
}