Migrated the host module to C++ exceptions as well.

Also enabled the latest C++ revision in the project.
Replaced the Random library with the one provided by C++11.
Implemented a simple AES256 encryption class.
Various other fixes and improvements.
This commit is contained in:
Sandu Liviu Catalin
2016-03-10 05:57:13 +02:00
parent 3162221e7f
commit 70e5f0ba21
124 changed files with 14873 additions and 14062 deletions
+111 -145
View File
@@ -7,10 +7,10 @@
#include <sqstdstring.h>
// ------------------------------------------------------------------------------------------------
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// ------------------------------------------------------------------------------------------------
#include <algorithm>
@@ -21,137 +21,117 @@ namespace SqMod {
// ------------------------------------------------------------------------------------------------
CSStr LeftStr(CSStr t, SQChar f, Uint32 w)
{
// Allocate a buffer with the requested width
Buffer b(w * sizeof(SQChar));
Uint32 n = !t ? 0 : strlen(t);
// Populate the entire buffer with the fill character
memset(b.Data(), f, w * sizeof(SQChar));
// Is the specified width valid?
if (w >= b.Size< SQChar >())
{
SqThrow("Invalid width specified: %d > %d", w, b.Size< SQChar >());
w = 0;
}
if (!w)
return _SC("");
// Allocate a buffer with the requested width
Buffer b(w);
// Is the specified string valid?
else if (n == 0)
{
SqThrow("Invalid string length: %d < 0", n);
}
// Is the specified string within width range?
else if (n > w)
{
SqThrow("String is out of bounds: %d > %d", n, w);
}
// Insert the specified string
if (!t || *t == 0)
// Insert only the fill character
memset(b.Data(), f, w);
else
{
// Calculate the string length
const Uint32 n = strlen(t);
// Insert only the fill character first
memset(b.Data(), f, w);
// Overwrite with the specified string
strncpy(b.Data(), t, n);
}
// End the resulted string
b.At< SQChar >(w) = 0;
b.At(w) = 0;
// Return the resulted string
return b.Get< SQChar >();
}
CSStr LeftStr(CSStr t, SQChar f, Uint32 w, Uint32 o)
{
// Allocate a buffer with the requested width
Buffer b(w * sizeof(SQChar));
Uint32 n = !t ? 0 : strlen(t);
// Populate the entire buffer with the fill character
memset(b.Data(), f, w * sizeof(SQChar));
// Is the specified width valid?
if (w >= b.Size< SQChar >())
{
SqThrow("Invalid width specified: %d > %d", w, b.Size< SQChar >());
w = 0;
}
if (!w)
return _SC("");
// Is the specified offset within width range?
else if (o > w)
SqThrowF("Offset is out of bounds");
// Allocate a buffer with the requested width
Buffer b(w);
// Is the specified string valid?
else if (n == 0)
{
SqThrow("Invalid string length: %d < 0", n);
}
// Is the specified string within width range?
else if ((o+n) > w)
{
SqThrow("String is out of bounds: (%d+%d) > %d", o, n, w);
}
// Insert the specified string
if (!t || *t == 0)
// Insert only the fill character
memset(b.Data(), f, w);
else
{
strncpy(b.Get< SQChar >() + o, t, n);
// Clculate the string length
const Uint32 n = strlen(t);
// Insert the fill character first
memset(b.Data(), f, w);
// Overwrite with the specified string
strncpy(b.Data() + o, t, w - n);
}
// End the resulted string
b.At< SQChar >(w) = 0;
b.At(w) = 0;
// Return the resulted string
return b.Get< SQChar >();
return b.Get();
}
// ------------------------------------------------------------------------------------------------
CSStr RightStr(CSStr t, SQChar f, Uint32 w)
{
// Allocate a buffer with the requested width
Buffer b(w * sizeof(SQChar));
Uint32 n = !t ? 0 : strlen(t);
// Populate the entire buffer with the fill character
memset(b.Data(), f, w * sizeof(SQChar));
// Is the specified width valid?
if (w >= b.Size< SQChar >())
{
SqThrow("Invalid width specified: %d > %d", w, b.Size< SQChar >());
w = 0;
}
if (!w)
return _SC("");
// Allocate a buffer with the requested width
Buffer b(w);
// Is the specified string valid?
else if (n == 0)
{
SqThrow("Invalid string length: %d < 0", n);
}
// Is the specified string within width range?
else if (n > w)
{
SqThrow("String is out of bounds: %d > %d", n, w);
}
// Insert the specified string
if (!t || *t == 0)
// Insert only the fill character
memset(b.Data(), f, w);
else
{
strncpy(b.Get< SQChar >() + (w-n), t, n);
// Calculate the string length
const Uint32 n = strlen(t);
// Insert the fill character first
memset(b.Data(), f, w);
// Overwrite with the specified string
if (n >= w)
strncpy(b.Data(), t, w);
else
strncpy(b.Data() + (w - n), t, n);
}
// End the resulted string
b.At< SQChar >(w) = 0;
b.At(w) = 0;
// Return the resulted string
return b.Get< SQChar >();
}
CSStr RightStr(CSStr t, SQChar f, Uint32 w, Uint32 o)
{
// Allocate a buffer with the requested width
Buffer b(w * sizeof(SQChar));
Uint32 n = !t ? 0 : strlen(t);
// Populate the entire buffer with the fill character
memset(b.Data(), f, w * sizeof(SQChar));
// Is the specified width valid?
if (w >= b.Size< SQChar >())
{
SqThrow("Invalid width specified: %d > %d", w, b.Size< SQChar >());
w = 0;
}
if (!w)
return _SC("");
// Is the specified offset within width range?
else if (o > w)
SqThrowF("Offset is out of bounds");
// Allocate a buffer with the requested width
Buffer b(w);
// Is the specified string valid?
else if (n == 0)
{
SqThrow("Invalid string length: %d < 0", n);
}
// Is the specified string within width range?
else if ((n+o) > w)
{
SqThrow("String is out of bounds: (%d+%d) > %d", n, o, w);
}
// Insert the specified string
if (!t || *t == 0)
// Insert only the fill character
memset(b.Data(), f, w);
else
{
strncpy(b.Get< SQChar >() + ((w-n)-o), t, n);
// Calculate the string length
const Uint32 n = strlen(t);
// Insert the fill character first
memset(b.Data(), f, w);
// Overwrite with the specified string
if (n >= w || (n + o) >= w)
strncpy(b.Data(), t, w - o);
else
strncpy(b.Data() + ((w - n) - o), t, n);
}
// End the resulted string
b.At< SQChar >(w) = 0;
b.At(w) = 0;
// Return the resulted string
return b.Get< SQChar >();
}
@@ -159,34 +139,26 @@ CSStr RightStr(CSStr t, SQChar f, Uint32 w, Uint32 o)
// ------------------------------------------------------------------------------------------------
CSStr CenterStr(CSStr t, SQChar f, Uint32 w)
{
// Allocate a buffer with the requested width
Buffer b(w * sizeof(SQChar));
Uint32 n = !t ? 0 : strlen(t);
// Populate the entire buffer with the fill character
memset(b.Data(), f, w * sizeof(SQChar));
// Is the specified width valid?
if (w >= b.Size< SQChar >())
{
SqThrow("Invalid width specified: %d > %d", w, b.Size< SQChar >());
w = 0;
}
if (!w)
return _SC("");
// Allocate a buffer with the requested width
Buffer b(w);
// Is the specified string valid?
else if (n == 0)
{
SqThrow("Invalid string length: %d < 0", n);
}
// Is the specified string within width range?
else if (n > w)
{
SqThrow("String is out of bounds: %d > %d", n, w);
}
// Insert the specified string
if (!t || *t == 0)
// Insert only the fill character
memset(b.Data(), f, w);
else
{
strncpy(b.Get< SQChar >() + (w/2 - n/2), t, n);
// Calculate the string length
const Uint32 n = strlen(t);
// Insert only the fill character first
memset(b.Data(), f, w);
// Overwrite with the specified string
strncpy(b.Data() + ((w/2) - (n/2)), t, n);
}
// End the resulted string
b.At< SQChar >(w) = 0;
b.At(w) = 0;
// Return the resulted string
return b.Get< SQChar >();
}
@@ -194,12 +166,11 @@ CSStr CenterStr(CSStr t, SQChar f, Uint32 w)
// ------------------------------------------------------------------------------------------------
CSStr StrJustAlphaNum(CSStr str)
{
Uint32 size = 0;
// See if we actually have something to search for
if(!str || (size = strlen(str)) <= 0)
{
if(!str || *str == 0)
return _SC("");
}
// Calculate the string length
Uint32 size = strlen(str);
// Obtain a temporary buffer
Buffer b(size);
// Resulted string size
@@ -211,13 +182,11 @@ CSStr StrJustAlphaNum(CSStr str)
{
// Is this an alpha-numeric character?
if (isalnum(c) != 0)
{
// Save it and move to the next one
b.At< SQChar >(n++) = c;
}
b.At(n++) = c;
}
// End the resulted string
b.At< SQChar >(n) = 0;
b.At(n) = 0;
// Return the string
return b.Get< SQChar >();
}
@@ -225,12 +194,11 @@ CSStr StrJustAlphaNum(CSStr str)
// ------------------------------------------------------------------------------------------------
CSStr StrToLowercase(CSStr str)
{
Uint32 size = 0;
// See if we actually have something to search for
if(!str || (size = strlen(str)) <= 0)
{
if(!str || *str == 0)
return _SC("");
}
// Calculate the string length
Uint32 size = strlen(str);
// Obtain a temporary buffer
Buffer b(size);
// Resulted string size
@@ -239,12 +207,10 @@ CSStr StrToLowercase(CSStr str)
SQChar c = 0;
// Process characters
while ((c = *(str++)) != 0)
{
// Convert it and move to the next one
b.At< SQChar >(n++) = tolower(c);
}
b.At(n++) = tolower(c);
// End the resulted string
b.At< SQChar >(n) = 0;
b.At(n) = 0;
// Return the string
return b.Get< SQChar >();
}
@@ -252,12 +218,11 @@ CSStr StrToLowercase(CSStr str)
// ------------------------------------------------------------------------------------------------
CSStr StrToUppercase(CSStr str)
{
Uint32 size = 0;
// See if we actually have something to search for
if(!str || (size = strlen(str)) <= 0)
{
if(!str || *str == 0)
return _SC("");
}
// Calculate the string length
Uint32 size = strlen(str);
// Obtain a temporary buffer
Buffer b(size);
// Resulted string size
@@ -266,12 +231,10 @@ CSStr StrToUppercase(CSStr str)
SQChar c = 0;
// Process characters
while ((c = *(str++)) != 0)
{
// Convert it and move to the next one
b.At< SQChar >(n++) = toupper(c);
}
b.At(n++) = toupper(c);
// End the resulted string
b.At< SQChar >(n) = 0;
b.At(n) = 0;
// Return the string
return b.Get< SQChar >();
}
@@ -280,18 +243,16 @@ CSStr StrToUppercase(CSStr str)
static CSStr FromArray(Array & arr)
{
// Determine array size
const Int32 length = (Int32)arr.Length();
const Int32 length = static_cast< Int32 >(arr.Length());
// Obtain a temporary buffer
Buffer b(length * sizeof(Int32));
// Get array elements as integers
arr.GetArray< Int32 >(b.Get< Int32 >(), length);
// Overwrite integers with characters
for (Int32 n = 0; n < length; ++n)
{
b.At< SQChar >(n) = (SQChar)b.At< Int32 >(n);
}
b.At(n) = static_cast< SQChar >(b.At< Int32 >(n));
// Terminate the resulted string
b.At< SQChar >(length) = 0;
b.At(length) = 0;
// Return the string
return b.Get< SQChar >();
}
@@ -301,9 +262,14 @@ static SQInteger StdPrintF(HSQUIRRELVM vm)
{
CStr msg = NULL;
SQInteger length = 0;
if(SQ_FAILED(sqstd_format(vm, 2, &length, &msg)))
return -1;
// Attempt to run the specified format and save the result
const SQRESULT res = sqstd_format(vm, 2, &length, &msg);
// Validate the result for errors and propagate them to the VM
if(SQ_FAILED(res))
return res;
// Send the resulted string to console as a user message
LogUsr("%s", msg);
// This function doesn't return anything
return 0;
}