Files
mop3/libinput/in_gb.c
kamkow1 bf1e8e8573
All checks were successful
Build documentation / build-and-deploy (push) Successful in 3m49s
libinput Generic way of gathering user commandline input
2026-03-20 18:04:26 +01:00

136 lines
3.2 KiB
C

#include <in_gb.h>
#include <malloc.h>
#include <stddef.h>
#include <string.h>
void in_gb_init (in_gb_malloc_func_t mallocfn, void* ctx, struct in_gb* gb, size_t capacity) {
gb->buffer = mallocfn (ctx, capacity);
if (gb->buffer == NULL)
return;
memset (gb->buffer, 0, capacity);
gb->size = capacity;
gb->gap_start = 0;
gb->gap_end = capacity;
}
void in_gb_fini (in_gb_free_func_t freefn, void* ctx, struct in_gb* gb) {
freefn (ctx, gb->buffer);
memset (gb, 0, sizeof (*gb));
}
void in_gb_grow (in_gb_realloc_func_t reallocfn, void* ctx, struct in_gb* gb) {
size_t old_size = gb->size;
size_t new_size = gb->size * 2;
char* new = reallocfn (ctx, gb->buffer, old_size, new_size);
if (new == NULL)
return;
gb->buffer = new;
size_t post_gap_size = old_size - gb->gap_end;
size_t new_gap_end = new_size - post_gap_size;
memmove (gb->buffer + new_gap_end, gb->buffer + gb->gap_end, post_gap_size);
gb->gap_end = new_gap_end;
gb->size = new_size;
}
void in_gb_move (struct in_gb* gb, size_t pos) {
if (pos > (gb->size - (gb->gap_end - gb->gap_start)))
pos = (gb->size - (gb->gap_end - gb->gap_start));
while (gb->gap_start > pos) {
gb->gap_start--;
gb->gap_end--;
gb->buffer[gb->gap_end] = gb->buffer[gb->gap_start];
}
while (gb->gap_start < pos) {
gb->buffer[gb->gap_start] = gb->buffer[gb->gap_end];
gb->gap_start++;
gb->gap_end++;
}
}
void in_gb_insert (in_gb_realloc_func_t reallocfn, void* ctx, struct in_gb* gb, char c) {
if (gb->gap_start == gb->gap_end)
in_gb_grow (reallocfn, ctx, gb);
gb->buffer[gb->gap_start] = c;
gb->gap_start++;
}
void in_gb_backspace (struct in_gb* gb) {
if (gb->gap_start > 0)
gb->gap_start--;
}
size_t in_gb_length (struct in_gb* gb) { return gb->size - (gb->gap_end - gb->gap_start); }
char* in_gb_string_at (in_gb_malloc_func_t mallocfn, void* ctx, struct in_gb* gb, size_t pos) {
size_t total_size = in_gb_length (gb);
if (pos >= total_size)
return "";
size_t tail_size = total_size - pos;
char* res = mallocfn (ctx, tail_size + 1);
if (res == NULL)
return NULL;
size_t written = 0;
if (pos < gb->gap_start) {
size_t copy = gb->gap_start - pos;
if (copy > tail_size)
copy = tail_size;
memcpy (res, &gb->buffer[pos], copy);
written += copy;
pos += copy;
}
if (written < tail_size) {
size_t gap_size = gb->gap_end - gb->gap_start;
size_t phys_pos = pos + gap_size;
size_t copy = tail_size - written;
memcpy (&res[written], &gb->buffer[phys_pos], copy);
written += copy;
}
res[written] = '\0';
return res;
}
void in_gb_concat (in_gb_realloc_func_t reallocfn, void* ctx, struct in_gb* dest,
struct in_gb* src) {
size_t src_len = in_gb_length (src);
if (src_len == 0)
return;
while ((dest->gap_end - dest->gap_start) < src_len)
in_gb_grow (reallocfn, ctx, dest);
in_gb_move (dest, in_gb_length (dest));
size_t part1_len = src->gap_start;
memcpy (dest->buffer + dest->gap_start, src->buffer, part1_len);
dest->gap_start += part1_len;
size_t part2_len = src->size - src->gap_end;
if (part2_len > 0) {
memcpy (dest->buffer + dest->gap_start, src->buffer + src->gap_end, part2_len);
dest->gap_start += part2_len;
}
}