-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgap_buffer.h
More file actions
89 lines (79 loc) · 2.54 KB
/
gap_buffer.h
File metadata and controls
89 lines (79 loc) · 2.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef GAP_BUFFER_H
#define GAP_BUFFER_H
#include <stddef.h>
#include <stdbool.h>
#include <sys/cdefs.h>
__BEGIN_DECLS
/**
* Simple Gap Buffer structure for handling insertion and deletion of text.
*/
struct gap_buffer {
size_t cap;
size_t len;
size_t cursor_start;
size_t cursor_end;
char *buffer;
};
/**
* Initialize the gap buffer with the given size.
* @param gb The gap buffer.
* @param buf_size The size of the initial buffer.
* @returns True if successful, false otherwise.
*/
bool gap_buffer_init(struct gap_buffer *gb, size_t buf_size) __THROWNL __nonnull((1));
/**
* Move the cursor of the buffer to the given position.
* @param gb The gap buffer.
* @param pos The position offset to move to.
* @returns True if successful, false otherwise.
*/
bool gap_buffer_move_cursor(struct gap_buffer *gb, size_t pos) __THROWNL __nonnull((1));
/**
* Get the char at the given position.
* @param gb The gap buffer.
* @param pos The position offset.
* @param[out] out The char variable to be populated.
*/
void gap_buffer_get_char(struct gap_buffer *gb, size_t pos, char* out) __THROWNL __nonnull((1));
/**
* Get the length of the gap buffer.
* This does not include the gap length only the text length.
* @param gb The gap buffer.
* @returns The length of the gap buffer.
*/
size_t gap_buffer_get_len(struct gap_buffer *gb) __THROWNL __nonnull((1));
/**
* Insert a character at the current position.
* @param gb The gap buffer.
* @param c The given character.
* @returns True if successful, false otherwise.
*/
bool gap_buffer_insert(struct gap_buffer *gb, char c) __THROWNL __nonnull((1));
/**
* Insert a word at the given position.
* @param gb The gap buffer.
* @param pos The position offset.
* @param input The given input string.
* @returns True if successful, false otherwise.
*/
bool gap_buffer_insert_word(struct gap_buffer *gb, size_t pos, char* input) __THROWNL __nonnull((1,3));
/**
* Delete a character to the left of the current position.
* @param gb The gap buffer.
* @returns True if successful, false otherwise.
*/
bool gap_buffer_delete(struct gap_buffer *gb) __THROWNL __nonnull((1));
/**
* Delete a sequence of characters to the left of the current position.
* @param gb The gap buffer.
* @param n The size of the sequence.
* @returns True if successful, false otherwise.
*/
bool gap_buffer_delete_seq(struct gap_buffer *gb, size_t n) __THROWNL __nonnull((1));
/**
* Free the gap buffer internals.
* @param gb The gap buffer.
*/
void gap_buffer_free(struct gap_buffer *gb) __THROWNL __nonnull((1));
__END_DECLS
#endif