1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-19 16:47:14 +02:00

Fix the functions used to create buffers and extend with new ones to interact with them.

This commit is contained in:
Sandu Liviu Catalin
2016-07-09 17:21:41 +03:00
parent 953ef30c17
commit bd75ffe305
4 changed files with 262 additions and 92 deletions

View File

@ -8,6 +8,7 @@
#include "Library/Chrono/Time.hpp"
#include "Library/Chrono/Datetime.hpp"
#include "Library/Chrono/Timestamp.hpp"
#include "Library/Utils/Buffer.hpp"
// ------------------------------------------------------------------------------------------------
#include <cmath>
@ -494,8 +495,7 @@ SQRESULT SqEx_PushBuffer(HSQUIRRELVM vm, SQInteger size, SQInteger cursor)
// Attempt to push the requested instance
try
{
Var< const Buffer & >::push(vm, Buffer(ConvTo< Buffer::SzType >::From(size),
ConvTo< Buffer::SzType >::From(cursor)));
Var< const SqBuffer & >::push(vm, SqBuffer(size, cursor));
}
catch (...)
{
@ -512,8 +512,7 @@ SQRESULT SqEx_PushBufferData(HSQUIRRELVM vm, const char * data, SQInteger size,
// Attempt to push the requested instance
try
{
Var< const Buffer & >::push(vm, Buffer(data, ConvTo< Buffer::SzType >::From(size),
ConvTo< Buffer::SzType >::From(cursor)));
Var< const SqBuffer & >::push(vm, SqBuffer(data, size, cursor));
}
catch (...)
{
@ -524,6 +523,126 @@ SQRESULT SqEx_PushBufferData(HSQUIRRELVM vm, const char * data, SQInteger size,
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT SqEx_GetBufferInfo(HSQUIRRELVM vm, SQInteger idx, const char ** ptr, SQInteger * size, SQInteger * cursor)
{
// Attempt to obtain the requested information
try
{
// Attempt to retrieve the instance
Var< SqBuffer * > var(vm, idx);
// Validate the obtained buffer
if (!(var.value) || !(var.value->GetRef()) || !(*var.value->GetRef()))
{
// Should we obtain the buffer contents?
if (ptr)
{
*ptr = nullptr; // Default to null data
}
// Should we obtain the buffer length?
if (size)
{
*size = 0; // Default to 0 length
}
// Should we obtain the cursor position?
if (cursor)
{
*cursor = 0; // Default to position 0
}
}
// Grab the internal buffer
const Buffer & b = *var.value->GetRef();
// Should we obtain the buffer contents?
if (ptr)
{
*ptr = b.Data();
}
// Should we obtain the buffer length?
if (size)
{
*size = ConvTo< SQInteger >::From(b.Capacity());
}
// Should we obtain the cursor position?
if (cursor)
{
*cursor = ConvTo< SQInteger >::From(b.Position());
}
}
catch (...)
{
// Specify that we failed
return SQ_ERROR;
}
// Specify that we succeeded
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
const char * SqEx_GetBufferData(HSQUIRRELVM vm, SQInteger idx)
{
// Attempt to obtain the requested information
try
{
// Attempt to retrieve the instance
Var< SqBuffer * > var(vm, idx);
// Validate the obtained buffer and return the requested information
if ((var.value) && (var.value->GetRef()) && (*var.value->GetRef()))
{
return var.value->GetRef()->Data();
}
}
catch (...)
{
// Just ignore it...
}
// Specify that we failed
return nullptr;
}
// ------------------------------------------------------------------------------------------------
SQInteger SqEx_GetBufferSize(HSQUIRRELVM vm, SQInteger idx)
{
// Attempt to obtain the requested information
try
{
// Attempt to retrieve the instance
Var< SqBuffer * > var(vm, idx);
// Validate the obtained buffer and return the requested information
if ((var.value) && (var.value->GetRef()) && (*var.value->GetRef()))
{
return ConvTo< SQInteger >::From(var.value->GetRef()->Capacity());
}
}
catch (...)
{
// Just ignore it...
}
// Specify that we failed
return -1;
}
// ------------------------------------------------------------------------------------------------
SQInteger SqEx_GetBufferCursor(HSQUIRRELVM vm, SQInteger idx)
{
// Attempt to obtain the requested information
try
{
// Attempt to retrieve the instance
Var< SqBuffer * > var(vm, idx);
// Validate the obtained buffer and return the requested information
if ((var.value) && (var.value->GetRef()) && (*var.value->GetRef()))
{
return ConvTo< SQInteger >::From(var.value->GetRef()->Position());
}
}
catch (...)
{
// Just ignore it...
}
// Specify that we failed
return -1;
}
// ------------------------------------------------------------------------------------------------
void InitExports()
{
@ -587,6 +706,10 @@ void InitExports()
//buffer utilities
g_SqExports.PushBuffer = SqEx_PushBuffer;
g_SqExports.PushBufferData = SqEx_PushBufferData;
g_SqExports.GetBufferInfo = SqEx_GetBufferInfo;
g_SqExports.GetBufferData = SqEx_GetBufferData;
g_SqExports.GetBufferSize = SqEx_GetBufferSize;
g_SqExports.GetBufferCursor = SqEx_GetBufferCursor;
// Export them to the server
_Func->ExportFunctions(_Info->pluginId,

View File

@ -63,6 +63,33 @@ public:
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Allocate constructor.
*/
SqBuffer(SQInteger n, SQInteger c)
: m_Buffer(new Buffer(ConvTo< SzType >::From(n), ConvTo< SzType >::From(c)))
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
SqBuffer(ConstPtr p, SQInteger n)
: m_Buffer(new Buffer(p, ConvTo< SzType >::From(n)))
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
SqBuffer(ConstPtr p, SQInteger n, SQInteger c)
: m_Buffer(new Buffer(p, ConvTo< SzType >::From(n), ConvTo< SzType >::From(c)))
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Reference constructor.
*/