rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
log_text.c File Reference

A text file as LOGS shows it. See log_text.h. More...

#include "log_text.h"
#include <stdlib.h>
#include <string.h>
#include "gfx.h"

Macros

#define MARKS   ((LOG_TEXT_MAX / LOG_TEXT_MARK) + 1u)
 Marks a text of LOG_TEXT_MAX rows takes: rows 0, MARK, 2 MARK, ...

Functions

void log_text_set_allocator (void *(*alloc_fn)(size_t), void(*free_fn)(void *))
 Set the allocator the buffer comes from, for a test that refuses it.
void log_text_free (log_text_t *t)
 Free what t holds and empty it.
static size_t row_at (log_text_t *t, size_t off, size_t *end)
 The row that starts at off: where it ends and where the next one starts. Counts the bytes it reads into walked.
static size_t whole (const char *buf, size_t len)
 The length of buf without a last character cut short.
static size_t read_text (log_source_t *src, char *buf, bool *cut)
 Read src into buf, LOG_TEXT_MAX bytes at most, with the line ends made LF and a tab or a NUL made a space.
log_err_t log_text_load (log_text_t *t, log_source_t *src, int cols)
 Read src from its top into t, and count its rows.
bool log_text_row (log_text_t *t, int i, char *out, size_t n)
 Row i of the text into out.

Variables

static void *(*) s_alloc (size_t) = malloc
 Takes a text's buffer and marks (log_text_set_allocator()).
static void(*) s_free (void *) = free
 Gives them back.

Detailed Description

A text file as LOGS shows it. See log_text.h.

SPDX-License-Identifier: MIT

Function Documentation

◆ log_text_free()

void log_text_free ( log_text_t * t)

Free what t holds and empty it.

Parameters
tthe text; nothing happens for NULL

◆ log_text_load()

log_err_t log_text_load ( log_text_t * t,
log_source_t * src,
int cols )

Read src from its top into t, and count its rows.

What t held before is freed first.

Parameters
tthe text; nothing happens for NULL
srcthe file, read to its end or to LOG_TEXT_MAX bytes
colscells a row holds, 1 or more
Returns
LOG_OK; LOG_ERR_ARG for no source or cols under 1; LOG_ERR_MEMORY when the buffers are not given; LOG_ERR_READ when a read fails. t holds nothing after a failure.

◆ log_text_row()

bool log_text_row ( log_text_t * t,
int i,
char * out,
size_t n )

Row i of the text into out.

Parameters
tthe text
ithe row, 0 to rows - 1
outtakes the row, terminated; cut at n - 1 bytes, between two characters
nbytes of out
Returns
false for a row outside the text, with out empty

◆ log_text_set_allocator()

void log_text_set_allocator ( void * alloc_fn (*)(size_t),
void(* free_fn )(void *) )

Set the allocator the buffer comes from, for a test that refuses it.

Parameters
alloc_fnNULL for malloc
free_fnNULL for free

◆ read_text()

size_t read_text ( log_source_t * src,
char * buf,
bool * cut )
static

Read src into buf, LOG_TEXT_MAX bytes at most, with the line ends made LF and a tab or a NUL made a space.

Parameters
srcthe file, read from where it stands
buftakes the text; LOG_TEXT_MAX bytes and a terminator
cuttakes whether the file holds more
Returns
the bytes in buf

◆ row_at()

size_t row_at ( log_text_t * t,
size_t off,
size_t * end )
static

The row that starts at off: where it ends and where the next one starts. Counts the bytes it reads into walked.

Parameters
tthe text, off inside it
offthe row's first byte
endtakes the byte after the row's last
Returns
the next row's first byte; len after the last row

◆ whole()

size_t whole ( const char * buf,
size_t len )
static

The length of buf without a last character cut short.

Parameters
bufUTF-8 text
lenits bytes
Returns
len, or less by the bytes of a last character that misses some of its continuation bytes