mirror of
				https://github.com/labwc/labwc.git
				synced 2025-11-03 09:01:51 -05:00 
			
		
		
		
	There is at least one user of the buffer API that reuse a single buffer by just resetting `buf.len` to `0`. This works as long as the new user of the buffer actually adds something to the buffer. However, if we don't add anything but still provide `buf.buf` to a consumer, the old content will be re-used. This patch thus adds two new clearing variants to the buffer API: - `buf_clear()` which doesn't reset the internal allocations - `buf_reset()` which does free the internal allocations Additionally, this patch makes `buffer_add_char()` public which allows adding single characters to an existing buffer. This will be used in a future PR which implements custom format strings for the OSD.
		
			
				
	
	
		
			67 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/* 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
 | 
						|
 | 
						|
#include <stdio.h>
 | 
						|
#include <stdlib.h>
 | 
						|
#include <string.h>
 | 
						|
 | 
						|
struct buf {
 | 
						|
	char *buf;
 | 
						|
	int alloc;
 | 
						|
	int len;
 | 
						|
};
 | 
						|
 | 
						|
/**
 | 
						|
 * 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_init - allocate NULL-terminated C string buffer
 | 
						|
 * @s: buffer
 | 
						|
 * Note: use free(s->buf) to free it
 | 
						|
 */
 | 
						|
void buf_init(struct buf *s);
 | 
						|
 | 
						|
/**
 | 
						|
 * 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
 | 
						|
 * @data: char to be added
 | 
						|
 */
 | 
						|
void buf_add_char(struct buf *s, char data);
 | 
						|
 | 
						|
/**
 | 
						|
 * buf_clear - clear the buffer, internal allocations are preserved
 | 
						|
 * @s: buffer
 | 
						|
 */
 | 
						|
void buf_clear(struct buf *s);
 | 
						|
 | 
						|
/**
 | 
						|
 * buf_reset - reset the buffer, internal allocations are free'd
 | 
						|
 * @s: buffer
 | 
						|
 */
 | 
						|
void buf_reset(struct buf *s);
 | 
						|
 | 
						|
#endif /* LABWC_BUF_H */
 |