-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgap_buffer.c
195 lines (169 loc) · 4.91 KB
/
gap_buffer.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "gap_buffer.h"
#include "macro.h"
static void resize(gap_buffer_t* ptr);
void init_gap_buffer(gap_buffer_t* ptr)
{
// ptr = (gap_buffer_t*)malloc(sizeof(gap_buffer_t));
ptr->left = (char*) calloc(MIN_SIZE_GAP_BUFFER_HALF, sizeof(char));
ptr->left_index = 0;
ptr->right = (char*) calloc(MIN_SIZE_GAP_BUFFER_HALF, sizeof(char));
ptr->right_index = MIN_SIZE_GAP_BUFFER_HALF - 1;
ptr->length = MIN_SIZE_GAP_BUFFER;
ptr->cursor_pos = 0;
printf("ciao: %u\n",ptr->cursor_pos);
}
void free_gap_buffer(gap_buffer_t* ptr)
{
free(ptr->left);
ptr->left = NULL;
free(ptr->right);
ptr->right = NULL;
}
void reset_gap_buffer(gap_buffer_t* ptr)
{
free_gap_buffer(ptr);
ptr->left_index = 0;
ptr->right_index = 0;
ptr->length = 0;
}
// double the size if too small, half it if too big
static void resize(gap_buffer_t* ptr)
{
// compute length of text written in buffer
unsigned int length_tmp = LEFT_LENGTH(ptr) + RIGHT_LENGTH(ptr);
if(DOUBLE(length_tmp) >= ptr->length)
{
//double the size of the buffer and copy the data
char* buf_left_tmp = (char*)calloc(ptr->length, sizeof(char));
memcpy(buf_left_tmp, ptr->left, ptr->left_index);
char* buf_right_tmp = (char*)calloc(ptr->length, sizeof(char));
// copy a string only if there is something in the buffer
if (HALF(ptr->length) > ptr->right_index + 1)
{
memcpy(buf_right_tmp + HALF(ptr->length) + ptr->right_index + 1,
ptr->right + ptr->right_index + 1,
RIGHT_LENGTH(ptr));
}
free_gap_buffer(ptr);
ptr->left = buf_left_tmp;
ptr->right = buf_right_tmp;
ptr->right_index = ptr->right_index + HALF(ptr->length);
ptr->length = DOUBLE(ptr->length);
}
else if(QUADRUPLE(length_tmp) < ptr->length && ptr->length > MIN_SIZE_GAP_BUFFER)
{
unsigned int new_half_buf_length = QUARTER(ptr->length);
char* buf_left_tmp = (char*)calloc(new_half_buf_length,sizeof(char));
memcpy(buf_left_tmp, ptr->left, ptr->left_index);
char* buf_right_tmp = (char*)calloc(new_half_buf_length,sizeof(char));
// copy a string only if there is something in the buffer
if(HALF(ptr->length) > ptr->right_index + 1)
{
memcpy(buf_right_tmp + ptr->right_index - new_half_buf_length,
ptr->right + ptr->right_index,
RIGHT_LENGTH(ptr));
}
free_gap_buffer(ptr);
ptr->left = buf_left_tmp;
ptr->right = buf_right_tmp;
ptr->right_index = ptr->right_index - new_half_buf_length;
ptr->length = HALF(ptr->length);
}
}
void insert_char(gap_buffer_t* ptr, char ch)
{
*(ptr->left + ptr->left_index++) = ch;
resize(ptr);
}
//TODO
void insert_string(const char* ch)
{
}
void update_gap_buffer(gap_buffer_t* ptr)
{
if(ptr->cursor_pos >= ptr->length)
{
printf("Error: cursor_pos greater than length in gap_buffer\n");
return;
}
if(ptr->cursor_pos < ptr->left_index)
{
(void)memcpy(ptr->right + ptr->right_index + 1 - (ptr->left_index - ptr->cursor_pos),
ptr->left + ptr->cursor_pos,
ptr->left_index - ptr->cursor_pos);
(void)memset(ptr->left + ptr->cursor_pos, 0, ptr->left_index - ptr->cursor_pos);
ptr->right_index = ptr->right_index - (ptr->left_index - ptr->cursor_pos);
ptr->left_index = ptr->cursor_pos;
}
else if(ptr->cursor_pos > ptr->left_index)
{
(void)memcpy(ptr->left + ptr->left_index,
ptr->right + ptr->right_index + 1,
ptr->cursor_pos - ptr->left_index);
(void)memset(ptr->right, 0, ptr->cursor_pos - ptr->left_index);
ptr->right_index = ptr->right_index + (ptr->cursor_pos - ptr->left_index);
ptr->left_index = ptr->cursor_pos;
}
}
/*
void set_cursor_at(gap_buffer_t* ptr, unsigned int index)
{
if(index >= ptr->length)
{
printf("Error: function %s tried to access out of range value\n", __func__);
return;
}
if(index < ptr->left_index)
{
(void)memcpy(ptr->right + ptr->right_index - (ptr->left_index - index),
ptr->left + index,
ptr->left_index - index);
(void)memset(ptr->left + index, 0, ptr->left_index - index);
ptr->left_index = index;
ptr->right_index = ptr->right_index - (ptr->left_index - index);
}
else if(index > ptr->left_index)
{
(void)memcpy(ptr->left + ptr->left_index,
ptr->right + ptr->right_index,
index - ptr->left_index);
(void)memset(ptr->right, 0, index - ptr->left_index);
ptr->left_index = index;
ptr->right_index = ptr->right_index + (index - ptr->left_index);
}
}
*/
void save_file(const char* filename, gap_buffer_t* ptr)
{
FILE* file = fopen(filename,"w");
if(file == NULL)
{
printf("Error opening %s!\\n",filename);
}
scanf("%s",ptr->left);
scanf("%s",ptr->right + ptr->right_index + 1);
fclose(file);
}
const char* get_left_message(gap_buffer_t* ptr)
{
return ptr->left;
}
const char* get_right_message(gap_buffer_t* ptr)
{
return ptr->right + ptr->right_index + 1;
}
void delete_char(gap_buffer_t* ptr)
{
if (ptr->left_index > 0)
{
*(ptr->left + ptr->left_index--) = '\0';
resize(ptr);
}
}
// TODO
void delete_range(unsigned int from, unsigned int to)
{
}