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.c
1/* =============================================================================
2 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
3 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
4 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
5 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
6 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
7
8 Copyright (c) 2021 Pieter Conradie <https://piconomix.com>
9
10 License: MIT
11 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
12
13 Title: px_sbuf.h : Safe buffer string appending functions that avoid overflow
14 Author(s): Pieter Conradie
15 Creation Date: 2021-04-01
16
17============================================================================= */
18
19/* _____STANDARD INCLUDES____________________________________________________ */
20#include <stdarg.h>
21#include <stdio.h>
22
23/* _____PROJECT INCLUDES_____________________________________________________ */
24#include "px_sbuf.h"
25
26/* _____LOCAL DEFINITIONS____________________________________________________ */
27
28/* _____MACROS_______________________________________________________________ */
29
30/* _____GLOBAL VARIABLES_____________________________________________________ */
31
32/* _____LOCAL VARIABLES______________________________________________________ */
33
34/* _____LOCAL FUNCTION DECLARATIONS__________________________________________ */
35
36/* _____LOCAL FUNCTIONS______________________________________________________ */
37
38/* _____GLOBAL FUNCTIONS_____________________________________________________ */
39void px_sbuf_init(px_sbuf_t * sbuf, char * buf, size_t buf_size)
40{
41 sbuf->buf = buf;
42 sbuf->buf_size = buf_size;
43 sbuf->index = 0;
44 sbuf->buf[0] = '\0';
45}
46
48{
49 sbuf->index = 0;
50 sbuf->buf[0] = '\0';
51}
52
53bool px_sbuf_is_empty(const px_sbuf_t * sbuf)
54{
55 if(sbuf->index == 0)
56 {
57 return true;
58 }
59 else
60 {
61 return false;
62 }
63}
64
65bool px_sbuf_is_full(const px_sbuf_t * sbuf)
66{
67 if(sbuf->index >= (sbuf->buf_size - 1))
68 {
69 return true;
70 }
71 else
72 {
73 return false;
74 }
75}
76
77void px_sbuf_putchar(px_sbuf_t * sbuf, char c)
78{
79 // Buffer full?
80 if(sbuf->index >= (sbuf->buf_size - 1))
81 {
82 return;
83 }
84 // Append char
85 sbuf->buf[sbuf->index] = c;
86 // Adjust to new position
87 sbuf->index++;
88 // Zero terminate
89 sbuf->buf[sbuf->index] = '\0';
90}
91
92void px_sbuf_puts(px_sbuf_t * sbuf, const char * str)
93{
94 px_sbuf_print(sbuf, str);
95 px_sbuf_putchar(sbuf, '\n');
96}
97
98void px_sbuf_print(px_sbuf_t * sbuf, const char * str)
99{
100 char c;
101
102 // Buffer full?
103 if(sbuf->index >= (sbuf->buf_size - 1))
104 {
105 return;
106 }
107 // Append string
108 while(true)
109 {
110 // Read char
111 c = *str++;
112 // End of string?
113 if(c == '\0')
114 {
115 break;
116 }
117 // Write char
118 sbuf->buf[sbuf->index] = c;
119 // Next index
120 sbuf->index++;
121 // End of buffer?
122 if(sbuf->index == (sbuf->buf_size - 1))
123 {
124 break;
125 }
126 }
127 // Zero terminate
128 sbuf->buf[sbuf->index] = '\0';
129}
130
131void px_sbuf_printf(px_sbuf_t * sbuf, const char * format, ...)
132{
133 va_list args;
134 size_t rem_buf_size;
135 int i;
136
137 // Buffer full?
138 if(sbuf->index >= (sbuf->buf_size - 1))
139 {
140 return;
141 }
142 // Calculate remaining size
143 rem_buf_size = sbuf->buf_size - sbuf->index;
144 // Append formatted string
145 va_start(args, format);
146 i = vsnprintf(&sbuf->buf[sbuf->index], rem_buf_size, format, args);
147 va_end(args);
148 // Error?
149 if(i <= 0)
150 {
151 // Zero terminate to remove error
152 sbuf->buf[sbuf->index] = '\0';
153 return;
154 }
155 // Overflow?
156 if(i > rem_buf_size)
157 {
158 i = rem_buf_size;
159 }
160 // Adjust to new position
161 sbuf->index += i;
162}
163
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