From 4dbf280012521e6bf6bc7d546cb35ce797ef1de1 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sun, 22 Mar 2020 20:19:25 +0900 Subject: [PATCH] [util] Add a means to open a gap in a text buffer for loading files into the buffer. --- include/QF/txtbuffer.h | 14 ++++++++++++++ libs/util/txtbuffer.c | 15 +++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/QF/txtbuffer.h b/include/QF/txtbuffer.h index e73c7257a..8e4723fb9 100644 --- a/include/QF/txtbuffer.h +++ b/include/QF/txtbuffer.h @@ -59,6 +59,20 @@ txtbuffer_t *TextBuffer_Create (void); */ void TextBuffer_Destroy (txtbuffer_t *buffer); +/** Open a gap for writing at the specified offset. + + Text after the offset is moved to be after the opened gap. + The buffer is resized as necessary. + + \param buffer The buffer to be updated + \param offset The offset in the buffer at which to insert the text block + \param text_len The size of the gap to be opened + \return Pointr to beginning of gap if successful, 0 if failure + (offset not valid or out of memory) +*/ +char *TextBuffer_OpenGap (txtbuffer_t *buffer, size_t offset, + size_t text_len); + /** Insert a block of text at the specified offset. Text after the offset is moved to be after the inserted block of text. diff --git a/libs/util/txtbuffer.c b/libs/util/txtbuffer.c index 59ee0da16..0edc785b5 100644 --- a/libs/util/txtbuffer.c +++ b/libs/util/txtbuffer.c @@ -159,6 +159,21 @@ TextBuffer_Destroy (txtbuffer_t *buffer) FREE (txtbuffers, buffer); } +VISIBLE char * +TextBuffer_OpenGap (txtbuffer_t *buffer, size_t offset, size_t text_len) +{ + char *dst; + + if (offset > buffer->textSize) { + return 0; + } + dst = txtbuffer_open_gap (buffer, offset, text_len); + if (!dst) { + return 0; + } + return dst; +} + VISIBLE int TextBuffer_InsertAt (txtbuffer_t *buffer, size_t offset, const char *text, size_t text_len)