1
0
Fork 0
mirror of https://github.com/suchmememanyskill/TegraExplorer.git synced 2024-11-29 23:32:06 +00:00
TegraExplorer/source/script/StringClass.c

132 lines
3.5 KiB
C
Raw Normal View History

2021-07-09 21:56:13 +01:00
#include "StringClass.h"
#include "compat.h"
#include "intClass.h"
2021-07-13 14:53:49 +01:00
#include "scriptError.h"
2021-07-25 20:39:45 +01:00
#include "parser.h"
2021-07-09 21:56:13 +01:00
#include <string.h>
char* getStringValue(Variable_t* var) {
if (var->variableType != StringClass)
return NULL;
return var->string.value;
}
// Will NOT copy the string, the pointer is taken as-is
StringClass_t createStringClass(char* in, u8 free) {
StringClass_t a = { 0 };
a.free = free;
a.value = in;
return a;
}
Variable_t newStringVariable(char *x, u8 readOnly, u8 freeOnExit) {
Variable_t var = { .variableType = StringClass, .readOnly = readOnly, .string = createStringClass(x, freeOnExit) };
return var;
}
ClassFunction(printStringVariable) {
if (caller->variableType == StringClass) {
StringClass_t* a = &caller->string;
gfx_printf("%s", a->value);
}
return &emptyClass;
}
ClassFunction(addStringVariables) {
char* s1 = getStringValue(caller);
char* s2 = getStringValue(*args);
char* n = malloc(strlen(s1) + strlen(s2) + 1);
strcpy(n, s1);
strcat(n, s2);
return newStringVariablePtr(n, 0, 1);
}
ClassFunction(getStringLength) {
char* s1 = getStringValue(caller);
return newIntVariablePtr(strlen(s1));
}
ClassFunction(stringBytes) {
Variable_t v = { 0 };
v.variableType = ByteArrayClass;
u32 len = strlen(caller->string.value);
v.solvedArray.vector = newVec(1, len);
v.solvedArray.vector.count = len;
memcpy(v.solvedArray.vector.data, caller->string.value, len);
return copyVariableToPtr(v);
}
2021-07-13 14:53:49 +01:00
ClassFunction(stringIndexGet) {
u32 len = strlen(caller->string.value);
u32 idx = args[0]->integer.value;
if (len < idx || idx < 0) {
SCRIPT_FATAL_ERR("Index of string out of range");
}
char* a = calloc(1,2);
a[0] = caller->string.value[idx];
return newStringVariablePtr(a, 0, 0);
}
2021-07-21 00:30:08 +01:00
ClassFunction(stringMinusInt){
u32 baseStrLen = strlen(caller->string.value);
if (baseStrLen < args[0]->integer.value){
SCRIPT_FATAL_ERR("Index of string out of range");
}
char* newStr = calloc(baseStrLen - args[0]->integer.value + 1, 1);
memcpy(newStr, caller->string.value, baseStrLen - args[0]->integer.value);
return newStringVariablePtr(newStr, 0, 1);
}
ClassFunction(stringEq){
return newIntVariablePtr(!strcmp(caller->string.value, args[0]->string.value));
}
ClassFunction(stringInEq){
return newIntVariablePtr(strcmp(caller->string.value, args[0]->string.value));
}
2021-07-25 20:39:45 +01:00
ClassFunction(stringSplit) {
int valLen = strlen(args[0]->string.value);
char* start = caller->string.value;
char* find = NULL;
Vector_t arr = newVec(sizeof(char**), 1);
char* temp;
while ((find = (strstr(start, args[0]->string.value))) != NULL) {
temp = utils_copyStringSize(start, find - start);
vecAdd(&arr, temp);
start = find + valLen;
}
temp = utils_copyStringSize(start, caller->string.value + strlen(caller->string.value) - start);
vecAdd(&arr, temp);
Variable_t a = { .variableType = StringArrayClass, .solvedArray.vector = arr };
return copyVariableToPtr(a);
}
2021-07-13 14:53:49 +01:00
u8 strOneIntArg[] = { IntClass };
2021-07-09 21:56:13 +01:00
u8 oneStringArg[] = { StringClass };
ClassFunctionTableEntry_t stringFunctions[] = {
{"print", printStringVariable, 0, 0},
{"+", addStringVariables, 1, oneStringArg },
{"len", getStringLength, 0, 0},
{"bytes", stringBytes, 0, 0},
2021-07-13 14:53:49 +01:00
{"get", stringIndexGet, 1, strOneIntArg},
2021-07-21 00:30:08 +01:00
{"-", stringMinusInt, 1, strOneIntArg},
{"==", stringEq, 1, oneStringArg},
{"!=", stringInEq, 1, oneStringArg},
2021-07-25 20:39:45 +01:00
{"split", stringSplit, 1, oneStringArg},
{"/", stringSplit, 1, oneStringArg},
2021-07-09 21:56:13 +01:00
};
Variable_t getStringMember(Variable_t* var, char* memberName) {
return getGenericFunctionMember(var, memberName, stringFunctions, ARRAY_SIZE(stringFunctions));
}