-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuffer.cpp
More file actions
103 lines (75 loc) · 1.81 KB
/
Buffer.cpp
File metadata and controls
103 lines (75 loc) · 1.81 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
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
//
// Created by mano on 10.12.16.
//
#include "Buffer.h"
Buffer::Buffer(size_t size) {
this->size = size;
end = 0;
buf = (char*) malloc(size);
if (NULL == buf) {
perror("malloc");
flag_correct_buffer = false;
exit(EXIT_FAILURE);
}
flag_correct_buffer = true;
flag_finished = false;
flag_finished_correct = true;
}
Buffer::Buffer() {
size = DEFAULT_BUFFER_SIZE;
end = 0;
buf = (char*) malloc(size);
if (NULL == buf) {
perror("malloc");
flag_correct_buffer = false;
exit(EXIT_FAILURE);
}
flag_correct_buffer = true;
flag_finished = false;
flag_finished_correct = true;
}
int Buffer::do_resize(size_t new_size) {
char * new_buf = (char *)realloc(buf, new_size);
if (NULL == new_buf) {
perror("realloc");
free(buf);
exit(EXIT_FAILURE);
}
buf = new_buf;
size = new_size;
return RESULT_CORRECT;
}
int Buffer::do_move_end(ssize_t received) {
fprintf(stderr, "Move end\n");
end += received;
if (end == size) {
return do_resize(size * 2);
}
fprintf(stderr, "Done moving\n");
return RESULT_CORRECT;
}
void Buffer::add_data_to_end(const char * from, size_t size_data) {
while (end + size_data > size) {
do_resize(size * 2);
}
memcpy(buf + end, from, size_data);
end += size_data;
}
void Buffer::add_symbol_to_end(char c) {
while (end + 1 > size) {
do_resize(size * 2);
}
buf[end++] = c;
}
Buffer::~Buffer() {
fprintf(stderr, "Destructor buffer\n");
fprintf(stderr, "Size: %ld\n", size);
if (flag_correct_buffer) {
free(buf);
perror("free");
}
else {
fprintf(stderr, "Destructor buffer not correct\n");
}
fprintf(stderr, "Done buffer destructor\n");
}