-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_string.c
More file actions
37 lines (34 loc) · 929 Bytes
/
Copy pathlib_string.c
File metadata and controls
37 lines (34 loc) · 929 Bytes
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
#include "lib_string.h"
int string_create(string_t** str) {
if (str == NULL) {
return err_num = INVALID_POINTER;
}
*str = (string_t*)malloc(sizeof(string_t));
if (*str == NULL) {
return err_num = NOT_ENOUGH_MEMORY;
}
(*str)->line = NULL;
(*str)->size = 0;
(*str)->capacity = 0;
return err_num = SUCCESS_RETVAL;
}
void string_destroy(string_t* str) {
free(str->line);
free(str);
}
int string_push_back(string_t* str, char symbol) {
if (str == NULL) {
return err_num = INVALID_POINTER;
}
if (str->size >= str->capacity) {
char* ptr;
str->capacity += REALLOCATION_SIZE;
ptr = (char*)realloc(str->line, str->capacity * sizeof(char));
if (ptr == NULL) {
return err_num = NOT_ENOUGH_MEMORY;
}
str->line = ptr;
}
str->line[str->size++] = symbol;
return err_num = SUCCESS_RETVAL;
}