summaryrefslogtreecommitdiff
path: root/src/core/buffer.c
blob: 16a9dfe3424d11503b96f616dbaa6459e1563cd4 (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "core/buffer.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define INITIAL_CAPACITY 1024

static size_t gap_size(const Buffer *buffer) {
  return buffer->gap_end - buffer->gap_start;
}

static size_t physical_index(const Buffer *buffer, size_t logical) {
  if (logical < buffer->gap_start) {
    return logical;
  }
  return logical + gap_size(buffer);
}

static bool ensure_gap(Buffer *buffer, size_t needed) {
  if (gap_size(buffer) >= needed) {
    return true;
  }

  size_t old_gap = gap_size(buffer);
  size_t old_capacity = buffer->capacity;
  size_t new_capacity = old_capacity;
  while ((new_capacity - buffer->gap_start - (old_capacity - buffer->gap_end)) < needed) {
    new_capacity *= 2;
  }

  char *new_data = (char *)malloc(new_capacity);
  if (!new_data) {
    return false;
  }

  memcpy(new_data, buffer->data, buffer->gap_start);
  size_t tail_size = old_capacity - buffer->gap_end;
  size_t new_gap_end = new_capacity - tail_size;
  memcpy(new_data + new_gap_end, buffer->data + buffer->gap_end, tail_size);

  free(buffer->data);
  buffer->data = new_data;
  buffer->capacity = new_capacity;
  buffer->gap_end = new_gap_end;

  (void)old_gap;
  return true;
}

static void move_gap(Buffer *buffer, size_t target) {
  if (target == buffer->gap_start) {
    return;
  }

  if (target < buffer->gap_start) {
    size_t move = buffer->gap_start - target;
    memmove(buffer->data + buffer->gap_end - move, buffer->data + target, move);
    buffer->gap_start = target;
    buffer->gap_end -= move;
    return;
  }

  size_t move = target - buffer->gap_start;
  memmove(buffer->data + buffer->gap_start, buffer->data + buffer->gap_end, move);
  buffer->gap_start += move;
  buffer->gap_end += move;
}

bool buffer_init(Buffer *buffer) {
  buffer->data = (char *)malloc(INITIAL_CAPACITY);
  if (!buffer->data) {
    return false;
  }
  buffer->capacity = INITIAL_CAPACITY;
  buffer->gap_start = 0;
  buffer->gap_end = INITIAL_CAPACITY;
  buffer->path[0] = '\0';
  buffer->modified = false;
  return true;
}

void buffer_free(Buffer *buffer) {
  free(buffer->data);
  buffer->data = NULL;
  buffer->capacity = 0;
  buffer->gap_start = 0;
  buffer->gap_end = 0;
}

size_t buffer_length(const Buffer *buffer) {
  return buffer->capacity - gap_size(buffer);
}

size_t buffer_point(const Buffer *buffer) {
  return buffer->gap_start;
}

void buffer_set_point(Buffer *buffer, size_t point) {
  size_t len = buffer_length(buffer);
  if (point > len) {
    point = len;
  }
  move_gap(buffer, point);
}

bool buffer_insert_char(Buffer *buffer, char c) {
  if (!ensure_gap(buffer, 1)) {
    return false;
  }
  buffer->data[buffer->gap_start++] = c;
  buffer->modified = true;
  return true;
}

bool buffer_insert_cstr(Buffer *buffer, const char *text) {
  size_t len = strlen(text);
  if (!ensure_gap(buffer, len)) {
    return false;
  }
  memcpy(buffer->data + buffer->gap_start, text, len);
  buffer->gap_start += len;
  if (len > 0) {
    buffer->modified = true;
  }
  return true;
}

bool buffer_backspace(Buffer *buffer) {
  if (buffer->gap_start == 0) {
    return false;
  }
  buffer->gap_start--;
  buffer->modified = true;
  return true;
}

bool buffer_delete_forward(Buffer *buffer) {
  if (buffer->gap_end >= buffer->capacity) {
    return false;
  }
  buffer->gap_end++;
  buffer->modified = true;
  return true;
}

char buffer_char_at(const Buffer *buffer, size_t index) {
  size_t len = buffer_length(buffer);
  if (index >= len) {
    return '\0';
  }
  return buffer->data[physical_index(buffer, index)];
}

bool buffer_load_file(Buffer *buffer, const char *path) {
  FILE *fp = fopen(path, "rb");
  if (!fp) {
    return false;
  }

  if (fseek(fp, 0, SEEK_END) != 0) {
    fclose(fp);
    return false;
  }

  long file_size = ftell(fp);
  if (file_size < 0) {
    fclose(fp);
    return false;
  }
  rewind(fp);

  buffer->gap_start = 0;
  buffer->gap_end = buffer->capacity;

  if (!ensure_gap(buffer, (size_t)file_size + 1)) {
    fclose(fp);
    return false;
  }

  size_t read = fread(buffer->data, 1, (size_t)file_size, fp);
  fclose(fp);
  if (read != (size_t)file_size) {
    return false;
  }

  buffer->gap_start = (size_t)file_size;
  buffer->gap_end = buffer->capacity;

  strncpy(buffer->path, path, sizeof(buffer->path) - 1);
  buffer->path[sizeof(buffer->path) - 1] = '\0';
  buffer->modified = false;
  return true;
}

bool buffer_save_file(Buffer *buffer, const char *path) {
  const char *out_path = path;
  if (!out_path || out_path[0] == '\0') {
    out_path = buffer->path;
  }

  if (!out_path || out_path[0] == '\0') {
    return false;
  }

  FILE *fp = fopen(out_path, "wb");
  if (!fp) {
    return false;
  }

  size_t before = buffer->gap_start;
  size_t after = buffer->capacity - buffer->gap_end;

  if (before > 0 && fwrite(buffer->data, 1, before, fp) != before) {
    fclose(fp);
    return false;
  }

  if (after > 0 && fwrite(buffer->data + buffer->gap_end, 1, after, fp) != after) {
    fclose(fp);
    return false;
  }

  fclose(fp);

  if (path && path[0] != '\0') {
    strncpy(buffer->path, path, sizeof(buffer->path) - 1);
    buffer->path[sizeof(buffer->path) - 1] = '\0';
  }

  buffer->modified = false;

  return true;
}

const char *buffer_path(const Buffer *buffer) {
  return buffer->path;
}

bool buffer_modified(const Buffer *buffer) {
  return buffer->modified;
}

void buffer_index_to_line_col(const Buffer *buffer, size_t index, int *line, int *col) {
  size_t len = buffer_length(buffer);
  if (index > len) {
    index = len;
  }

  int l = 0;
  int c = 0;
  for (size_t i = 0; i < index; ++i) {
    char ch = buffer_char_at(buffer, i);
    if (ch == '\n') {
      l++;
      c = 0;
    } else {
      c++;
    }
  }

  *line = l;
  *col = c;
}

size_t buffer_line_col_to_index(const Buffer *buffer, int target_line, int target_col) {
  if (target_line < 0) {
    target_line = 0;
  }
  if (target_col < 0) {
    target_col = 0;
  }

  size_t len = buffer_length(buffer);
  int line = 0;
  int col = 0;

  for (size_t i = 0; i < len; ++i) {
    if (line == target_line && col == target_col) {
      return i;
    }

    char ch = buffer_char_at(buffer, i);
    if (ch == '\n') {
      if (line == target_line) {
        return i;
      }
      line++;
      col = 0;
    } else {
      col++;
    }
  }

  return len;
}