Skip to content

Commit cd322cd

Browse files
committed
Improving default flush mechanism
1 parent 140916d commit cd322cd

File tree

3 files changed

+63
-28
lines changed

3 files changed

+63
-28
lines changed

inc/BufPrint.h

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
****************************************************************************
1111
* HEADER
1212
*
13-
* $Id: BufPrint.h 4915 2021-12-01 18:26:55Z wini $
13+
* $Id: BufPrint.h 5029 2022-01-16 21:32:09Z wini $
1414
*
1515
* COPYRIGHT: Real Time Logic LLC, 2008 - 2022
1616
*
@@ -85,25 +85,38 @@ BA_API int basnprintf(char* buf, int len, const char* fmt, ...);
8585

8686
/** BufPrint flush callback function.
8787
88-
A BufPrint instance calls the flush callback function when buffer is
89-
full. The callback can either extend the buffer or flush and reset the
90-
buffer.
88+
A BufPrint instance calls the flush callback function when the buffer
89+
is full or when #BufPrint::flush is called. The callback can either
90+
extend the buffer or flush and reset the buffer.
91+
92+
The following default callback is set if no callback is installed when
93+
calling the BufPrint constructor:
94+
95+
\code
96+
BufPrint_defaultFlush(struct BufPrint* bp, int sizeRequired)
97+
{
98+
bp->cursor=0; // Reset
99+
baAssert(sizeRequired == 0); // Program error in code calling BufPrint_xxx
100+
return sizeRequired ? -1 : 0;
101+
}
102+
\endcode
91103
92104
\param o the object. BufPrint is typically upcasted to the derived object.
93-
\param sizeRequired the required expands size if the callback is
94-
expanding the buffer. Not used when flushing and resetting the buffer.
105+
\param sizeRequired the minimum size the buffer must expand. Note that
106+
sizeRequired will be zero when the callback is called via
107+
BufPrint::flush
108+
95109
*/
96110
typedef int (*BufPrint_Flush)(struct BufPrint* o, int sizeRequired);
97111

98112
/** The BufPrint class, which implements an ANSI compatible printf
99-
method, is an abstract class used as a base for many of the
100-
Barracuda classes.
101-
102-
The output from printf is formatted in an internal buffer. This
103-
class does not allocate memory for the buffer. Thus, any class
104-
using BufPrint must provide a buffer BufPrint can use. BufPrint
105-
calls the callback function BufPrint_Flush when the buffer is
106-
full.
113+
method, is a base class used by several other classes.
114+
115+
This class does not allocate memory for the buffer. Thus, any
116+
class using BufPrint must provide a buffer BufPrint can use. The
117+
output from printf is formatted in the buffer passed into the
118+
constructor. BufPrint calls the callback function BufPrint_Flush
119+
when the buffer is full. See #BufPrint_Flush for additional details.
107120
*/
108121
typedef struct BufPrint
109122
{
@@ -114,7 +127,8 @@ typedef struct BufPrint
114127
115128
\param userData an optional argument stored in the BufPrint
116129
object and accessible in the flush callback.
117-
\param flush a pointer to the flush callback function.
130+
\param flush a pointer to the flush callback function. See
131+
#BufPrint_Flush for details.
118132
119133
\sa setBuf(), getUserData()
120134
*/
@@ -127,7 +141,8 @@ typedef struct BufPrint
127141
\param size the buffer size
128142
\param userData an optional argument stored in the BufPrint
129143
object and accessible in the flush callback.
130-
\param flush a pointer to the flush callback function.
144+
\param flush a pointer to the flush callback function. See
145+
#BufPrint_Flush for details.
131146
132147
\sa setBuf(), getUserData()
133148
*/

inc/JEncoder.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
****************************************************************************
1212
* HEADER
1313
*
14-
* $Id: JEncoder.h 4915 2021-12-01 18:26:55Z wini $
14+
* $Id: JEncoder.h 5029 2022-01-16 21:32:09Z wini $
1515
*
16-
* COPYRIGHT: Real Time Logic LLC, 2006-2018
16+
* COPYRIGHT: Real Time Logic LLC, 2006-2022
1717
*
1818
* This software is copyrighted by and is the sole property of Real
1919
* Time Logic LLC. All rights, title, ownership, or other interests in
@@ -49,7 +49,19 @@
4949
/** The JEncoder can serialize a JSON JVAL syntax tree to the JSON
5050
text format. The JEncoder can also be used for assembling JSON
5151
text from calling the primitive methods in this class.
52-
*/
52+
53+
Example:
54+
\code
55+
JErr err;
56+
char buf[40]; //Must be sufficiently large for the JSON string
57+
BufPrint jBuf(buf,sizeof(buf));
58+
JEncoder jEnc(&err,&jBuf);
59+
jEnc.set("{d}", "The number of the day is", 5);
60+
//jBuf.buf == buf
61+
buf[jBuf.cursor]=0; // So we can use printf
62+
printf("%s\n",buf); // Prints: {"The number of the day is":5}
63+
\endcode
64+
*/
5365
typedef struct JEncoder
5466
{
5567
#ifdef __cplusplus

src/BufPrint.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
****************************************************************************
1111
* PROGRAM MODULE
1212
*
13-
* $Id: BufPrint.c 4975 2021-12-29 01:54:26Z wini $
13+
* $Id: BufPrint.c 5029 2022-01-16 21:32:09Z wini $
1414
*
1515
* COPYRIGHT: Real Time Logic, 2002 - 2022
1616
*
@@ -46,11 +46,15 @@
4646
***************************************************************************/
4747

4848
static int
49-
sprintfIsFull(struct BufPrint* print, int sizeRequired)
49+
BufPrint_defaultFlush(struct BufPrint* bp, int sizeRequired)
5050
{
51-
(void)print; /* not used */
52-
(void)sizeRequired; /* not used */
53-
return -1; /* Failed, buffer is full */
51+
bp->cursor=0; /* Reset */
52+
/* Buffer is full if sizeRequired set (if not a
53+
* BufPrint_flush() call)
54+
* This indicates a program error.
55+
*/
56+
baAssert(sizeRequired == 0); /* Program error in code calling BufPrint_xxx */
57+
return sizeRequired ? -1 : 0;
5458
}
5559

5660

@@ -61,7 +65,7 @@ basnprintf(char* buf, int len, const char* fmt, ...)
6165
va_list varg;
6266
BufPrint bufPrint;
6367
/* (len -1) -> -1 -> space for the string terminator */
64-
BufPrint_constructor2(&bufPrint, buf, (len -1), 0, sprintfIsFull);
68+
BufPrint_constructor2(&bufPrint, buf, (len -1), 0, BufPrint_defaultFlush);
6569
va_start(varg, fmt);
6670
retv = BufPrint_vprintf(&bufPrint, fmt, varg);
6771
if( retv >= 0 )
@@ -82,7 +86,7 @@ basprintf(char* buf, const char* fmt, ...)
8286
BufPrint bufPrint;
8387
/* We have no idea what the size is so let's set it to fffff */
8488
BufPrint_constructor2(
85-
&bufPrint, buf, (int)((unsigned int)(~0)/2u), 0, sprintfIsFull);
89+
&bufPrint, buf, (int)((unsigned int)(~0)/2u), 0, BufPrint_defaultFlush);
8690
bufPrint.cursor = 0; /* Start position is beginning of buf */
8791
va_start(varg, fmt);
8892
retv = BufPrint_vprintf(&bufPrint, fmt, varg);
@@ -204,7 +208,7 @@ BufPrint_constructor(BufPrint* o, void* userData, BufPrint_Flush flush)
204208
{
205209
memset(o, 0, sizeof(BufPrint));
206210
o->userData = userData;
207-
o->flushCB = flush ? flush : sprintfIsFull;
211+
o->flushCB = flush ? flush : BufPrint_defaultFlush;
208212
}
209213

210214
BA_API void
@@ -1098,6 +1102,10 @@ BufPrint_flush(BufPrint* o)
10981102
if(!o)
10991103
return -1;
11001104
if(o->cursor)
1101-
return o->flushCB(o, 0);
1105+
{
1106+
int rsp = o->flushCB(o, 0);
1107+
o->cursor=0;
1108+
return rsp;
1109+
}
11021110
return 0;
11031111
}

0 commit comments

Comments
 (0)