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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* Very simple C string buffer implementation
*
* Copyright Johan Malm 2020
*/
#ifndef LABWC_BUF_H
#define LABWC_BUF_H
struct buf {
/**
* Pointer to underlying string buffer. If alloc != 0, then
* this was allocated via malloc().
*/
char *data;
/**
* Allocated length of buf. If zero, data was not allocated
* (either NULL or literal empty string).
*/
int alloc;
/**
* Length of string contents (not including terminating NUL).
* Currently this must be zero if alloc is zero (i.e. non-empty
* literal strings are not allowed).
*/
int len;
};
/** Value used to initialize a struct buf to an empty string */
#define BUF_INIT ((struct buf){.data = ""})
/**
* buf_expand_tilde - expand ~ in buffer
* @s: buffer
*/
void buf_expand_tilde(struct buf *s);
/**
* buf_expand_shell_variables - expand $foo and ${foo} in buffer
* @s: buffer
* Note: $$ is not handled
*/
void buf_expand_shell_variables(struct buf *s);
/**
* buf_add_fmt - add format string to C string buffer
* @s: buffer
* @fmt: format string to be added
*/
void buf_add_fmt(struct buf *s, const char *fmt, ...);
/**
* buf_add_hex_color - add rgb color as hex string to C string buffer
* @s: buffer
* @color: rgb color to be added
*
* For example:
* - With the input 'red' (defined as red[4] = { 1.0f, 0.0f, 0.0f, 1.0f}) the
* string "#ff0000ff" will be written to the buffer.
*/
void buf_add_hex_color(struct buf *s, float color[4]);
/**
* buf_add - add data to C string buffer
* @s: buffer
* @data: data to be added
*/
void buf_add(struct buf *s, const char *data);
/**
* buf_add_char - add single char to C string buffer
* @s: buffer
* @ch: char to be added
*/
void buf_add_char(struct buf *s, char ch);
/**
* buf_clear - clear the buffer, internal allocations are preserved
* @s: buffer
*
* The buffer will be set to a NUL-terminated empty string.
*
* This is the appropriate function to call to re-use the buffer
* in a loop or similar situations as it reuses the existing heap
* allocation.
*/
void buf_clear(struct buf *s);
/**
* buf_reset - reset the buffer, internal allocations are free'd
* @s: buffer
*
* The buffer will be re-initialized to BUF_INIT (empty string).
*
* Inside a loop, consider using buf_clear() instead, as it allows
* reusing the existing heap allocation. buf_reset() should still be
* called after exiting the loop.
*/
void buf_reset(struct buf *s);
/**
* buf_move - move the contents of src to dst, freeing any previous
* allocation of dst and resetting src to BUF_INIT.
*
* dst must either have been initialized with BUF_INIT
* or zeroed out (e.g. created by znew() or on the stack
* with something like struct buf foo = {0}).
*/
void buf_move(struct buf *dst, struct buf *src);
#endif /* LABWC_BUF_H */
|