Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
BufPrint.h
Go to the documentation of this file.
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * Barracuda Embedded Web-Server
10 ****************************************************************************
11 * HEADER
12 *
13 * $Id: BufPrint.h 6097 2026-09-22 20:06:16Z gianluca $
14 *
15 * COPYRIGHT: Real Time Logic LLC, 2008 - 2026
16 *
17 * This software is copyrighted by and is the sole property of Real
18 * Time Logic LLC. All rights, title, ownership, or other interests in
19 * the software remain the property of Real Time Logic LLC. This
20 * software may only be used in accordance with the terms and
21 * conditions stipulated in the corresponding license agreement under
22 * which the software has been supplied. Any unauthorized use,
23 * duplication, transmission, distribution, or disclosure of this
24 * software is expressly forbidden.
25 *
26 * This Copyright notice may not be removed or modified without prior
27 * written consent of Real Time Logic LLC.
28 *
29 * Real Time Logic LLC. reserves the right to modify this software
30 * without notice.
31 *
32 * http://www.realtimelogic.com
33 ****************************************************************************
34 *
35 */
36
39#ifndef __BufPrint_h
40#define __BufPrint_h
41
42#include <TargConfig.h>
43
44#ifndef BA_API
45#define BA_API
46#endif
47
48#include <stdarg.h>
49#include <string.h>
50
51struct BufPrint;
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
65BA_API int basprintf(char* buf, const char* fmt, ...);
66
78BA_API int basnprintf(char* buf, int len, const char* fmt, ...);
79
80#ifdef __cplusplus
81#undef printf
82}
83#endif
84
120typedef int (*BufPrint_Flush)(struct BufPrint* o, int sizeRequired);
121
131typedef struct BufPrint
132{
133#ifdef __cplusplus
134
145 BufPrint(void* userData=0, BufPrint_Flush flush=0);
146
159 BufPrint(char* buf,int size,void* userData=0,BufPrint_Flush flush=0);
160
163 void* getUserData();
164
175 int vprintf(const char* fmt, va_list argList);
176
198 int printf(const char* fmt, ...);
199
205 int baputc(int c);
206
216 int write(const void* data, int len);
217
225 int write(const char* buf);
226
227
233 void setBuf(char* buf, int size);
234
235
242 char* getBuf();
243
245 U32 getBufSize();
246
247
249 void erase();
250
254 int flush();
255
263 int b64Encode(const void* data, S32 slen);
264
273 int b64urlEncode(const void* source, S32 slen, bool padding);
274
286 int jsonString(const char* str, size_t len);
287#endif
288 BufPrint_Flush flushCB;
289 void *userData;
290 char* buf;
291 int cursor;
292 int bufSize;
294
295#define BufPrint_putcMacro(o, c) do { \
296 if((o)->cursor == (o)->bufSize) \
297 { \
298 if((o)->flushCB(o, 1)) \
299 return -1; \
300 } \
301 (o)->buf[(o)->cursor++] = c; \
302} while(0)
303
304#ifdef __cplusplus
305extern "C" {
306#endif
309#define BufPrint_getUserData(o) (o)->userData
312#define BufPrint_erase(o) (o)->cursor=0
316#define BufPrint_getBuf(o) (o)->buf
321#define BufPrint_setBuf(o, b, size) (o)->buf=b,(o)->bufSize=size,(o)->cursor=0
324#define BufPrint_getBufSize(o) (o)->cursor
331 BufPrint* o,void* userData,BufPrint_Flush flush);
340 BufPrint* o, char* buf,int size,void* userData,BufPrint_Flush flush);
343#define BufPrint_destructor(o)
346BA_API int BufPrint_vprintf(BufPrint* o, const char* fmt, va_list argList);
349BA_API int BufPrint_printf(BufPrint* o, const char* fmt, ...);
352BA_API int BufPrint_write(BufPrint* o, const void* data, int len);
355BA_API int BufPrint_putc(BufPrint* o, int c);
364#define BufPrint_write2(o, data) BufPrint_write(o, data, -1)
370BA_API int BufPrint_b64Encode(BufPrint* o, const void* source, S32 slen);
374 BufPrint* o, const void* source, S32 slen, BaBool padding);
375
378BA_API int BufPrint_jsonString(BufPrint* o, const char* str, size_t len);
379#ifdef __cplusplus
380}
381inline void* BufPrint::getUserData() {
382 return BufPrint_getUserData(this);
383}
384inline BufPrint::BufPrint(void* userData, BufPrint_Flush flush) {
385 BufPrint_constructor(this, userData,flush); }
387 char* buf,int size,void* userData,BufPrint_Flush flush) {
388 BufPrint_constructor2(this,buf,size,userData,flush); }
389inline int BufPrint::vprintf(const char* fmt, va_list argList) {
390 return BufPrint_vprintf(this, fmt, argList); }
391inline int BufPrint::printf(const char* fmt, ...) {
392 int retv; va_list varg;
393 va_start(varg, fmt);
394 retv = BufPrint_vprintf(this, fmt, varg);
395 va_end(varg);
396 return retv;
397}
398inline char* BufPrint::getBuf() {
399 return BufPrint_getBuf(this); }
400inline void BufPrint::setBuf(char* buffer, int size) {
401 BufPrint_setBuf(this, buffer, size); }
403 return BufPrint_getBufSize(this); }
404inline void BufPrint::erase() {
405 BufPrint_erase(this); }
406inline int BufPrint::baputc(int c) {
407 return BufPrint_putc(this, c); }
408inline int BufPrint::write(const void* data, int len) {
409 return BufPrint_write(this, data, len); }
410inline int BufPrint::write(const char* data) {
411 return BufPrint_write2(this, data); }
412inline int BufPrint::flush() {
413 return BufPrint_flush(this);
414}
415inline int BufPrint::b64Encode(const void* source, S32 slen){
416 return BufPrint_b64Encode(this, source, slen);
417}
418inline int BufPrint::b64urlEncode(const void* source, S32 slen, bool padding){
419 return BufPrint_b64urlEncode(this, source, slen, padding?TRUE:FALSE);
420}
421inline int BufPrint::jsonString(const char* str, size_t len){
422 return BufPrint_jsonString(this, str, len);
423}
424#endif
425 /* end of BufPrint */
427
428
429#endif
BA_API int basnprintf(char *buf, int len, const char *fmt,...)
Format into a bounded caller-owned buffer.
BA_API int basprintf(char *buf, const char *fmt,...)
Format into caller-owned storage using BufPrint::printf conversions.
#define BufPrint_getUserData(o)
Definition: BufPrint.h:309
void erase()
resets the cursor, thus erasing the data in the buffer
Definition: BufPrint.h:404
#define BufPrint_write2(o, data)
Append a NUL-terminated string without copying its terminator.
Definition: BufPrint.h:364
BA_API void BufPrint_constructor2(BufPrint *o, char *buf, int size, void *userData, BufPrint_Flush flush)
Initialize a writer with borrowed storage.
U32 getBufSize()
Definition: BufPrint.h:402
void * getUserData()
Definition: BufPrint.h:381
char * getBuf()
Returns a pointer to the internal buffer.
Definition: BufPrint.h:398
BA_API int BufPrint_write(BufPrint *o, const void *data, int len)
Append bytes, flushing or expanding through the callback as needed.
#define BufPrint_getBuf(o)
Definition: BufPrint.h:316
BufPrint(void *userData=0, BufPrint_Flush flush=0)
BufPrint constructor.
Definition: BufPrint.h:384
BA_API int BufPrint_flush(BufPrint *o)
Deliver pending bytes to the callback with sizeRequired zero.
int jsonString(const char *str, size_t len)
Append a complete quoted JSON string.
Definition: BufPrint.h:421
int baputc(int c)
Append one byte (C function: BufPrint_putc).
Definition: BufPrint.h:406
int write(const void *data, int len)
Append bytes, flushing or expanding through the callback as needed.
Definition: BufPrint.h:408
int vprintf(const char *fmt, va_list argList)
Format arguments into this writer.
Definition: BufPrint.h:389
BA_API int BufPrint_jsonString(BufPrint *o, const char *str, size_t len)
Append a complete quoted JSON string.
BA_API int BufPrint_putc(BufPrint *o, int c)
Append one byte (C function: BufPrint_putc).
int flush()
Deliver pending bytes to the callback with sizeRequired zero.
Definition: BufPrint.h:412
#define BufPrint_getBufSize(o)
Definition: BufPrint.h:324
#define BufPrint_setBuf(o, b, size)
Replace borrowed storage and discard pending bytes.
Definition: BufPrint.h:321
BA_API int BufPrint_b64urlEncode(BufPrint *o, const void *source, S32 slen, BaBool padding)
Append Base64url using - and _, without a NUL terminator.
int b64urlEncode(const void *source, S32 slen, bool padding)
Append Base64url using - and _, without a NUL terminator.
Definition: BufPrint.h:418
int(* BufPrint_Flush)(struct BufPrint *o, int sizeRequired)
BufPrint flush callback function.
Definition: BufPrint.h:120
BA_API void BufPrint_constructor(BufPrint *o, void *userData, BufPrint_Flush flush)
Initialize a writer without storage; install a buffer before writing.
int printf(const char *fmt,...)
Format values using the compact BAS formatter.
Definition: BufPrint.h:391
#define BufPrint_erase(o)
Discard pending bytes without flushing or releasing storage.
Definition: BufPrint.h:312
struct BufPrint BufPrint
The BufPrint class, which implements a compact printf-style formatter, is a base class used by severa...
BA_API int BufPrint_vprintf(BufPrint *o, const char *fmt, va_list argList)
Format arguments into this writer.
BA_API int BufPrint_printf(BufPrint *o, const char *fmt,...)
Format values using the compact BAS formatter.
void setBuf(char *buf, int size)
Replace borrowed storage and reset the cursor, discarding pending data.
Definition: BufPrint.h:400
int b64Encode(const void *data, S32 slen)
Append standard Base64 with = padding, without a NUL terminator.
Definition: BufPrint.h:415
BA_API int BufPrint_b64Encode(BufPrint *o, const void *source, S32 slen)
Append padded standard Base64; see BufPrint::b64Encode.
int32_t S32
Signed 32-bit integer.
Definition: GenPrimT.h:85
uint32_t U32
Unsigned 32-bit integer.
Definition: GenPrimT.h:93
U8 BaBool
Boolean stored in an unsigned byte; FALSE is zero and TRUE is one.
Definition: GenPrimT.h:118
The BufPrint class, which implements a compact printf-style formatter, is a base class used by severa...
Definition: BufPrint.h:132