Barracuda Application Server C/C++ Reference
NO
DynBuffer Struct Reference

Detailed Description

A dynamic buffer.

You either Subclass and implement the DynBuffer_OnAllocError method or set OnAllocError to NULL.

#include <DynBuffer.h>

Inheritance diagram for DynBuffer:

Public Member Functions

 DynBuffer (int startSize, int expandSize, AllocatorIntf *alloc=0, DynBuffer_OnAllocError onAllocError=0)
 Create a dynamic buffer. More...
 
 ~DynBuffer ()
 destructor. More...
 
void release ()
 terminate the internal dynamic buffer by calling free
 
char * getBuf ()
 Returns a pointer to the internal buffer. More...
 
U32 getBufSize ()
 Returns current size of internal formatted data.
 
int getECode ()
 Returns the error code if memory allocation failed. More...
 
int expand (int sizeNeeded)
 force buffer to expand. More...
 
char * getCurPtr ()
 Return a pointer to the internal cursor position in the internal dynamic buffer. More...
 
void incrementCursor (int nBytes)
 Increments the internal cursor position. More...
 
- Public Member Functions inherited from BufPrint
 BufPrint (void *userData=0, BufPrint_Flush flush=0)
 BufPrint constructor. More...
 
 BufPrint (char *buf, int size, void *userData=0, BufPrint_Flush flush=0)
 BufPrint constructor. More...
 
void * getUserData ()
 Returns the user data pointer set in the constructor.
 
int vprintf (const char *fmt, va_list argList)
 The printf function's format is identical to the standard ANSI vprintf function. More...
 
int printf (const char *fmt,...)
 The printf function's format is identical to the standard ANSI printf function, but with the following extensions: More...
 
int baputc (int c)
 print character (wrapper for BufPrint_putc)
 
int write (const void *data, int len)
 Write data to the buffer. More...
 
int write (const char *buf)
 Used for sending a zero terminated string to the client. More...
 
void setBuf (char *buf, int size)
 Set the buffer used by BufPrint. More...
 
char * getBuf ()
 Returns a pointer to the internal buffer. More...
 
U32 getBufSize ()
 Returns current size of internal formatted data.
 
void erase ()
 resets the cursor, thus erasing the data in the buffer
 
int flush ()
 Flush buffer.
 
int b64Encode (const void *data, S32 slen)
 Encode binary data as Base64. More...
 
int b64urlEncode (const void *source, S32 slen, bool padding)
 Encode binary data as Base64url. More...
 
int jsonString (const char *str)
 Print and escape a string such that a browser can run the JavaScript 'eval' function and produce a string identical to the string the 'str' argument points to. More...
 

Static Public Member Functions

static const char * ecode2str (int eCode)
 Convert error code to string. More...
 

Constructor & Destructor Documentation

◆ DynBuffer()

DynBuffer::DynBuffer ( int  startSize,
int  expandSize,
AllocatorIntf alloc = 0,
DynBuffer_OnAllocError  onAllocError = 0 
)

Create a dynamic buffer.

Parameters
startSizethe size allocated by calling malloc.
expandSizechunk size used when calling realloc. Set to 0 if you do not want the buffer to dynamically grow.
allocthe allocator used by the dynamic buffer.
onAllocErrorpointer to function called on allocation error.

◆ ~DynBuffer()

DynBuffer::~DynBuffer ( )

destructor.

release memory by calling method DynBuffer::release.

Member Function Documentation

◆ ecode2str()

const char * DynBuffer::ecode2str ( int  eCode)
static

Convert error code to string.

Used by the onAllocError callback.

◆ expand()

int DynBuffer::expand ( int  sizeNeeded)

force buffer to expand.

Parameters
sizeNeededthe required expand size.
Returns
0 on success. A non zero value is returned if it is not enough memory to expand the buffer i.e. if the AllocatorIntf provided in the constructor cannot re-allocate the buffer.

◆ getBuf()

char * DynBuffer::getBuf ( )

Returns a pointer to the internal buffer.

This pointer is invalid after the DynBuffer reallocates the internal buffer. Unlike the BufPrint::getBuf method, the buffer returned by this method is zero terminated.

◆ getCurPtr()

char * DynBuffer::getCurPtr ( )

Return a pointer to the internal cursor position in the internal dynamic buffer.

See also
expand incrementCursor

◆ getECode()

int DynBuffer::getECode ( )

Returns the error code if memory allocation failed.

This method is typically used when the DynBuffer_OnAllocError is set to NULL in the constructor.

0: No error.
-1: No allocator.
-2: Malloc failed.
-3: Need to realloc buffer, but no realloc provided.
-4: Realloc failed.
-5: Buffer too large.

◆ incrementCursor()

void DynBuffer::incrementCursor ( int  nBytes)

Increments the internal cursor position.

It is possible to manually format data in the internal buffer. This method advances the internal cursor by N bytes.

example

char data[]={"My data"};
if(myBuf->expand(sizeof(data)-1) == 0) // -1: no need to store null term.
{
memcpy(myBuf->getCurPtr(), data, sizeof(data)-1);
myBuf->incrementCursor(sizeof(data)-1);