2021-06-09 18:42:26 +08:00
|
|
|
/*
|
2022-04-03 02:39:57 +08:00
|
|
|
* Copyright (c) 2020-2022 Thakee Nathees
|
|
|
|
* Copyright (c) 2021-2022 Pocketlang Contributors
|
2021-06-09 18:42:26 +08:00
|
|
|
* Distributed Under The MIT License
|
|
|
|
*/
|
|
|
|
|
2021-06-21 21:02:20 +08:00
|
|
|
#include "internal.h"
|
2021-06-09 18:42:26 +08:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t* data;
|
|
|
|
uint32_t count;
|
|
|
|
uint32_t capacity;
|
|
|
|
} ByteBuffer;
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* BYTE BUFFER */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2021-06-10 07:35:14 +08:00
|
|
|
// Initialize a new buffer int instance.
|
2021-06-09 18:42:26 +08:00
|
|
|
void byteBufferInit(ByteBuffer* buffer);
|
|
|
|
|
2021-06-10 06:40:24 +08:00
|
|
|
// Clears the allocated elements from the VM's realloc function.
|
2021-06-09 18:42:26 +08:00
|
|
|
void byteBufferClear(ByteBuffer* buffer);
|
|
|
|
|
2021-06-11 15:46:55 +08:00
|
|
|
// Ensure the capacity is greater than [size], if not resize.
|
2021-06-09 18:42:26 +08:00
|
|
|
void byteBufferReserve(ByteBuffer* buffer, size_t size);
|
|
|
|
|
2021-06-11 15:46:55 +08:00
|
|
|
// Fill the buffer at the end of it with provided data if the capacity
|
2021-06-09 18:42:26 +08:00
|
|
|
// isn't enough using VM's realloc function.
|
|
|
|
void byteBufferFill(ByteBuffer* buffer, uint8_t data, int count);
|
|
|
|
|
|
|
|
// Write to the buffer with provided data at the end of the buffer.
|
|
|
|
void byteBufferWrite(ByteBuffer* buffer, uint8_t data);
|
|
|
|
|
|
|
|
// Add all the characters to the buffer, byte buffer can also be used as a
|
|
|
|
// buffer to write string (like a string stream). Note that this will not
|
|
|
|
// add a null byte '\0' at the end.
|
|
|
|
void byteBufferAddString(ByteBuffer* buffer, const char* str, uint32_t length);
|