rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
text_buf.h
Go to the documentation of this file.
1
46
47#pragma once
48
49#include <stdbool.h>
50#include <stddef.h>
51#include <stdint.h>
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
58#define TEXT_BUF_FILE_MAX 1048576u
60#define TEXT_BUF_ENDS_MAX 65536u
62#define TEXT_BUF_PROBE 4096u
64#define TEXT_BUF_TAB 8u
66#define TEXT_BUF_BOM_LEN 3u
67
77
85
90typedef struct {
91 uint8_t *mem;
92 size_t cap;
93 uint32_t *ends;
95 size_t ends_cap;
96 size_t n_ends;
97 size_t gap_at;
98 size_t gap_len;
99 size_t len;
100 size_t limit;
101 size_t cursor;
104 uint32_t revision;
105 bool bom;
106 bool dirty;
107} text_buf_t;
108
120text_buf_err_t text_buf_init(text_buf_t *tb, uint8_t *mem, size_t cap,
121 uint32_t *ends, size_t ends_cap);
122
129size_t text_buf_room(const text_buf_t *tb);
130
138uint8_t *text_buf_load_area(text_buf_t *tb);
139
153
163text_buf_err_t text_buf_load(text_buf_t *tb, const void *bytes, size_t len);
164
171size_t text_buf_size(const text_buf_t *tb);
172
183size_t text_buf_read(const text_buf_t *tb, size_t at, void *out, size_t n);
184
191size_t text_buf_len(const text_buf_t *tb);
192
200uint8_t text_buf_byte(const text_buf_t *tb, size_t pos);
201
208size_t text_buf_lines(const text_buf_t *tb);
209
217size_t text_buf_line_start(const text_buf_t *tb, size_t line);
218
226size_t text_buf_line_len(const text_buf_t *tb, size_t line);
227
238const char *text_buf_line(text_buf_t *tb, size_t line, size_t *len);
239
247size_t text_buf_line_of(const text_buf_t *tb, size_t pos);
248
258uint32_t text_buf_cp(const text_buf_t *tb, size_t pos, size_t *bytes);
259
267size_t text_buf_next(const text_buf_t *tb, size_t pos);
268
276size_t text_buf_prev(const text_buf_t *tb, size_t pos);
277
286bool text_buf_is_unit(const text_buf_t *tb, size_t pos);
287
296size_t text_buf_rows(const text_buf_t *tb, size_t line, unsigned width);
297
310void text_buf_cell_of(const text_buf_t *tb, size_t pos, unsigned width,
311 size_t *line, size_t *cell);
312
323size_t text_buf_pos_at(const text_buf_t *tb, size_t line, size_t cell,
324 unsigned width);
325
332size_t text_buf_cursor(const text_buf_t *tb);
333
341void text_buf_set_cursor(text_buf_t *tb, size_t pos);
342
349bool text_buf_left(text_buf_t *tb);
350
357bool text_buf_right(text_buf_t *tb);
358
369
378
386
394
402
409bool text_buf_mixed(const text_buf_t *tb);
410
417bool text_buf_bom(const text_buf_t *tb);
418
425bool text_buf_dirty(const text_buf_t *tb);
426
432void text_buf_saved(text_buf_t *tb);
433
441uint32_t text_buf_revision(const text_buf_t *tb);
442
443#ifdef __cplusplus
444}
445#endif
The buffer. Read it through the functions; the fields are open only so the caller can place it.
Definition text_buf.h:90
size_t gap_at
Definition text_buf.h:97
size_t ends_cap
Definition text_buf.h:95
bool bom
Definition text_buf.h:105
uint8_t * mem
Definition text_buf.h:91
uint32_t * ends
Definition text_buf.h:93
size_t cap
Definition text_buf.h:92
size_t n_ends
Definition text_buf.h:96
size_t kinds[TEXT_EOL_KINDS]
Definition text_buf.h:102
uint32_t revision
Definition text_buf.h:104
size_t limit
Definition text_buf.h:100
size_t len
Definition text_buf.h:99
text_eol_t eol
Definition text_buf.h:103
size_t cursor
Definition text_buf.h:101
bool dirty
Definition text_buf.h:106
size_t gap_len
Definition text_buf.h:98
text_eol_t text_buf_eol(const text_buf_t *tb)
The file's kind of line end, which ENTER inserts.
Definition text_buf.c:898
size_t text_buf_room(const text_buf_t *tb)
The largest file text_buf_load() and text_buf_load_done() take.
Definition text_buf.c:352
uint32_t text_buf_revision(const text_buf_t *tb)
The revision: moves on with each edit and load; compare it for equality only.
Definition text_buf.c:933
void text_buf_saved(text_buf_t *tb)
Note that the text as it is now is on the card.
Definition text_buf.c:926
bool text_buf_dirty(const text_buf_t *tb)
Whether the text was edited since the load or text_buf_saved().
Definition text_buf.c:921
text_buf_err_t text_buf_insert_eol(text_buf_t *tb)
Insert a line end at the cursor, which goes to the new line.
Definition text_buf.c:841
bool text_buf_right(text_buf_t *tb)
Move the cursor one unit right.
Definition text_buf.c:812
void text_buf_set_cursor(text_buf_t *tb, size_t pos)
Put the cursor at pos, moved back to the start of the unit it is in; past the end,...
Definition text_buf.c:789
text_buf_err_t text_buf_load_done(text_buf_t *tb, size_t len)
Take the len bytes a loader put at text_buf_load_area() as the text: the BOM, the line ends and their...
Definition text_buf.c:369
size_t text_buf_len(const text_buf_t *tb)
Text bytes, the BOM not counted.
Definition text_buf.c:464
text_buf_err_t text_buf_insert(text_buf_t *tb, uint32_t cp)
Insert code point cp at the cursor, which goes after it.
Definition text_buf.c:823
text_eol_t
A kind of line end. The numbers are stable.
Definition text_buf.h:79
@ TEXT_EOL_LF
Definition text_buf.h:80
@ TEXT_EOL_KINDS
Definition text_buf.h:83
@ TEXT_EOL_CRLF
Definition text_buf.h:81
@ TEXT_EOL_CR
Definition text_buf.h:82
uint8_t text_buf_byte(const text_buf_t *tb, size_t pos)
Byte pos of the text.
Definition text_buf.c:469
text_buf_err_t text_buf_load(text_buf_t *tb, const void *bytes, size_t len)
Copy a file into the buffer and take it, as text_buf_load_done().
Definition text_buf.c:402
bool text_buf_bom(const text_buf_t *tb)
Whether the file starts with a BOM.
Definition text_buf.c:916
text_buf_err_t text_buf_init(text_buf_t *tb, uint8_t *mem, size_t cap, uint32_t *ends, size_t ends_cap)
An empty text over mem, LF line ends, no BOM.
Definition text_buf.c:336
void text_buf_cell_of(const text_buf_t *tb, size_t pos, unsigned width, size_t *line, size_t *cell)
The line and the cell at which the unit at pos is drawn.
Definition text_buf.c:730
size_t text_buf_line_of(const text_buf_t *tb, size_t pos)
The line text offset pos is on.
Definition text_buf.c:533
bool text_buf_delete_before(text_buf_t *tb)
Remove the unit before the cursor (backspace).
Definition text_buf.c:872
size_t text_buf_rows(const text_buf_t *tb, size_t line, unsigned width)
Rows line line takes, wrapped at width cells.
Definition text_buf.c:715
text_buf_err_t
What a load or an edit answers. The numbers are stable.
Definition text_buf.h:69
@ TEXT_BUF_OK
Definition text_buf.h:70
@ TEXT_BUF_NOT_TEXT
Definition text_buf.h:74
@ TEXT_BUF_ARG
Definition text_buf.h:71
@ TEXT_BUF_FULL
Definition text_buf.h:75
@ TEXT_BUF_TOO_MANY
Definition text_buf.h:73
@ TEXT_BUF_TOO_LARGE
Definition text_buf.h:72
uint8_t * text_buf_load_area(text_buf_t *tb)
Where a loader puts a file's bytes in place, text_buf_room() of them at most, before text_buf_load_do...
Definition text_buf.c:360
size_t text_buf_read(const text_buf_t *tb, size_t at, void *out, size_t n)
Serialised bytes at to at + n of the file, the BOM in front: a save writes the file in pieces with th...
Definition text_buf.c:428
bool text_buf_is_unit(const text_buf_t *tb, size_t pos)
Whether a unit starts at pos: not inside a UTF-8 sequence and not between the CR and the LF of a CR L...
Definition text_buf.c:590
bool text_buf_left(text_buf_t *tb)
Move the cursor one unit left.
Definition text_buf.c:803
size_t text_buf_next(const text_buf_t *tb, size_t pos)
The start of the unit after the one at pos.
Definition text_buf.c:576
size_t text_buf_line_start(const text_buf_t *tb, size_t line)
Where line line starts.
Definition text_buf.c:479
size_t text_buf_prev(const text_buf_t *tb, size_t pos)
The start of the unit before pos.
Definition text_buf.c:618
uint32_t text_buf_cp(const text_buf_t *tb, size_t pos, size_t *bytes)
The code point of the unit at pos.
Definition text_buf.c:555
bool text_buf_delete_after(text_buf_t *tb)
Remove the unit at the cursor (delete).
Definition text_buf.c:885
size_t text_buf_size(const text_buf_t *tb)
Bytes text_buf_read() gives: the text and the BOM.
Definition text_buf.c:420
bool text_buf_mixed(const text_buf_t *tb)
Whether the text holds line ends of two kinds or three.
Definition text_buf.c:903
size_t text_buf_line_len(const text_buf_t *tb, size_t line)
Bytes of line line without its line end.
Definition text_buf.c:509
size_t text_buf_pos_at(const text_buf_t *tb, size_t line, size_t cell, unsigned width)
The unit drawn over cell cell of line line: the cursor position a tap there gives.
Definition text_buf.c:760
size_t text_buf_lines(const text_buf_t *tb)
Lines of the text: its line ends and one.
Definition text_buf.c:474
size_t text_buf_cursor(const text_buf_t *tb)
The cursor, a text offset at a unit's start.
Definition text_buf.c:784
const char * text_buf_line(text_buf_t *tb, size_t line, size_t *len)
Line line as one run of bytes, without its line end. Moves the gap out of the line when it is in it; ...
Definition text_buf.c:517