px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_sbuf.h
1#ifndef __PX_SBUF_H__
2#define __PX_SBUF_H__
3/* =============================================================================
4 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
5 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
6 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
7 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
8 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
9
10 Copyright (c) 2021 Pieter Conradie <https://piconomix.com>
11
12 License: MIT
13 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
14
15 Title: px_sbuf.h : Safe buffer string appending functions that avoid overflow
16 Author(s): Pieter Conradie
17 Creation Date: 2021-04-01
18
19============================================================================= */
20
21/**
22 * @ingroup UTILS
23 * @defgroup PX_SBUF px_sbuf.h : Safe buffer string appending functions that avoid overflow
24 *
25 * Safe buffer string appending functions that avoid overflow.
26 *
27 * File(s):
28 * - utils/inc/px_sbuf.h
29 * - utils/src/px_sbuf.c
30 *
31 * This module provides a mechanism to provide a fixed size character buffer
32 * and append characters and strings safely and avoiding buffer overflows.
33 *
34 * Example:
35 *
36 * @include utils/test/px_sbuf_test.c
37 *
38 * @{
39 */
40
41/* _____STANDARD INCLUDES____________________________________________________ */
42
43/* _____PROJECT INCLUDES_____________________________________________________ */
44#include "px_defs.h"
45
46#ifdef __cplusplus
47extern "C" {
48#endif
49/* _____DEFINITIONS__________________________________________________________ */
50
51/* _____TYPE DEFINITIONS_____________________________________________________ */
52typedef struct
53{
54 char * buf; ///< Pointer to character buffer
55 size_t buf_size; ///< Size of character buffer
56 size_t index; ///< Index of next character to be copied into buffer
57} px_sbuf_t;
58
59/* _____GLOBAL VARIABLES_____________________________________________________ */
60
61/* _____GLOBAL FUNCTION DECLARATIONS_________________________________________ */
62/**
63 * Initialise safe buffer
64 *
65 * @param sbuf Pointer to safe buffer object
66 * @param buf Pointer to character buffer
67 * @param buf_size Size of character buffer
68 */
69void px_sbuf_init(px_sbuf_t * sbuf, char * buf, size_t buf_size);
70
71/**
72 * Reset safe buffer
73 *
74 * @param sbuf Pointer to buffer object
75 */
76void px_sbuf_reset(px_sbuf_t * sbuf);
77
78/**
79 * Is safe buffer empty?
80 *
81 * @param sbuf Pointer to safe buffer object
82 *
83 * @retval true safe buffer is empty
84 * @retval false safe buffer is not empty
85 */
86bool px_sbuf_is_empty(const px_sbuf_t * sbuf);
87
88/**
89 * Is safe buffer full
90 *
91 * @param sbuf Pointer to safe buffer object
92 *
93 * @retval true safe buffer is full
94 * @retval false safe buffer is empty
95 */
96bool px_sbuf_is_full(const px_sbuf_t * sbuf);
97
98/**
99 * Append a character to safe buffer
100 *
101 * @param sbuf Pointer to safe buffer object
102 * @param c Character to append
103 */
104void px_sbuf_putchar(px_sbuf_t * sbuf, char c);
105
106/**
107 * Append a string and newline character to safe buffer
108 *
109 * @param sbuf Pointer to safe buffer object
110 * @param str String to append
111 */
112void px_sbuf_puts(px_sbuf_t * sbuf, const char * str);
113
114/**
115 * Append a string to safe buffer
116 *
117 * @param sbuf Pointer to safe buffer object
118 * @param str String to append
119 */
120void px_sbuf_print(px_sbuf_t * sbuf, const char * str);
121
122/**
123 * Append a formatted string to safe buffer
124 *
125 * @param sbuf Pointer to safe buffer object
126 * @param format Printf format string
127 */
128void px_sbuf_printf(px_sbuf_t * sbuf, const char * format, ...);
129
130/* _____MACROS_______________________________________________________________ */
131
132#ifdef __cplusplus
133}
134#endif
135
136/// @}
137#endif
size_t index
Index of next character to be copied into buffer.
Definition: px_sbuf.h:56
char * buf
Pointer to character buffer.
Definition: px_sbuf.h:54
size_t buf_size
Size of character buffer.
Definition: px_sbuf.h:55
void px_sbuf_puts(px_sbuf_t *sbuf, const char *str)
Append a string and newline character to safe buffer.
Definition: px_sbuf.c:92
bool px_sbuf_is_full(const px_sbuf_t *sbuf)
Is safe buffer full.
Definition: px_sbuf.c:65
void px_sbuf_putchar(px_sbuf_t *sbuf, char c)
Append a character to safe buffer.
Definition: px_sbuf.c:77
bool px_sbuf_is_empty(const px_sbuf_t *sbuf)
Is safe buffer empty?
Definition: px_sbuf.c:53
void px_sbuf_reset(px_sbuf_t *sbuf)
Reset safe buffer.
Definition: px_sbuf.c:47
void px_sbuf_printf(px_sbuf_t *sbuf, const char *format,...)
Append a formatted string to safe buffer.
Definition: px_sbuf.c:131
void px_sbuf_print(px_sbuf_t *sbuf, const char *str)
Append a string to safe buffer.
Definition: px_sbuf.c:98
void px_sbuf_init(px_sbuf_t *sbuf, char *buf, size_t buf_size)
Initialise safe buffer.
Definition: px_sbuf.c:39