JsonParserGeneratorRK
Public Member Functions | Protected Attributes
JsonBuffer Class Reference

Base class for managing a static or dynamic buffer, used by both JsonParser and JsonWriter. More...

#include <JsonParserGeneratorRK.h>

Inheritance diagram for JsonBuffer:
JsonParser JsonWriter JsonParserStatic< BUFFER_SIZE, MAX_TOKENS > JsonModifier JsonWriterStatic< BUFFER_SIZE >

Public Member Functions

 JsonBuffer ()
 Construct a JsonBuffer object with no external buffer specified.
 
virtual ~JsonBuffer ()
 Destructor. Destroying the object does not delete any underlying buffer!
 
 JsonBuffer (char *buffer, size_t bufferLen)
 Construct a JsonBuffer with an external buffer of a given size. More...
 
void setBuffer (char *buffer, size_t bufferLen)
 Sets the buffers to the specified buffer and length. More...
 
bool allocate (size_t len)
 Allocate the buffer using malloc/realloc. More...
 
bool addString (const char *data)
 Add a c-string to the end of the buffer. More...
 
bool addData (const char *data, size_t dataLen)
 Add a string to the end of the buffer. More...
 
char * getBuffer () const
 Gets a pointer to the internal buffer. More...
 
size_t getOffset () const
 Gets the current offset for writing.
 
void setOffset (size_t offset)
 swets the current offset for writing
 
size_t getBufferLen () const
 Gets the current length of the buffer. More...
 
void clear ()
 Clears the current buffer for writing. More...
 

Protected Attributes

char * buffer
 The buffer to to read from or write to. This is not null-terminated.
 
size_t bufferLen
 The length of the buffer in bytes,.
 
size_t offset
 The read or write offset.
 
bool staticBuffers
 True if the buffers were passed in and should not freed or reallocated.
 

Detailed Description

Base class for managing a static or dynamic buffer, used by both JsonParser and JsonWriter.

Constructor & Destructor Documentation

◆ JsonBuffer()

JsonBuffer::JsonBuffer ( char *  buffer,
size_t  bufferLen 
)

Construct a JsonBuffer with an external buffer of a given size.

Parameters
bufferPointer to the buffer
bufferLenThe length of the buffer

This buffer will not be deleted when the object is destructed.

Member Function Documentation

◆ addData()

bool JsonBuffer::addData ( const char *  data,
size_t  dataLen 
)

Add a string to the end of the buffer.

Parameters
dataPointer to the string bytes. Does not need to be null-terminated
dataLenLength of the data in bytes. For UTF-8, this is the number of bytes, not characters!

◆ addString()

bool JsonBuffer::addString ( const char *  data)
inline

Add a c-string to the end of the buffer.

Parameters
dataPointer to a c-string (null terminated).

◆ allocate()

bool JsonBuffer::allocate ( size_t  len)

Allocate the buffer using malloc/realloc.

Parameters
lenThe length of the buffer in bytes
Returns
true if the allocation/reallocation was successful or false if there was not enough free memory.

There's also a version that takes a pointer and length to use a static buffer instead of a dynamically allocated one.

◆ clear()

void JsonBuffer::clear ( )

Clears the current buffer for writing.

This only sets the offset to 0, it does not clear the bytes.

◆ getBuffer()

char* JsonBuffer::getBuffer ( ) const
inline

Gets a pointer to the internal buffer.

Note: The internal buffer is not null-terminated!

◆ getBufferLen()

size_t JsonBuffer::getBufferLen ( ) const
inline

Gets the current length of the buffer.

The buffer length is either the bufferLen passed to the constructor that takes a buffer and bufferLen or the length allocated using allocate(len).

◆ setBuffer()

void JsonBuffer::setBuffer ( char *  buffer,
size_t  bufferLen 
)

Sets the buffers to the specified buffer and length.

Parameters
bufferPointer to the buffer
bufferLenThe length of the buffer

This buffer will not be deleted when the object is destructed.


The documentation for this class was generated from the following files: