ScriptVariant *ScriptVariant_Add(ScriptVariant * svar, ScriptVariant * rightChild) { static ScriptVariant retvar; static int flag = 1; DOUBLE dbl1, dbl2; CHAR buf[MAX_STR_VAR_LEN + 1]; if(flag) { ScriptVariant_Init(&retvar); flag = 0; } else ScriptVariant_Clear(&retvar); if(ScriptVariant_DecimalValue(svar, &dbl1) == S_OK && ScriptVariant_DecimalValue(rightChild, &dbl2) == S_OK) { if(svar->vt == VT_DECIMAL || rightChild->vt == VT_DECIMAL) { ScriptVariant_ChangeType(&retvar, VT_DECIMAL); retvar.dblVal = dbl1 + dbl2; } else { ScriptVariant_ChangeType(&retvar, VT_INTEGER); retvar.lVal = (LONG) (dbl1 + dbl2); } } else if(svar->vt == VT_STR || rightChild->vt == VT_STR) { ScriptVariant_ChangeType(&retvar, VT_STR); StrCache_Get(retvar.strVal)[0] = 0; ScriptVariant_ToString(svar, StrCache_Get(retvar.strVal)); ScriptVariant_ToString(rightChild, buf); strcat(StrCache_Get(retvar.strVal), buf); } return &retvar; }
// faster if it is not VT_STR void ScriptVariant_Copy(ScriptVariant * svar, ScriptVariant * rightChild) { // collect the str cache index if(svar->vt == VT_STR && rightChild->vt != VT_STR) { StrCache_Collect(svar->strVal); } switch (rightChild->vt) { case VT_INTEGER: svar->lVal = rightChild->lVal; break; case VT_DECIMAL: svar->dblVal = rightChild->dblVal; break; case VT_PTR: svar->ptrVal = rightChild->ptrVal; break; case VT_STR: // if it is not string, give it a string cache index if(svar->vt != VT_STR) svar->strVal = StrCache_Pop(); StrCache_Get(rightChild->strVal)[MAX_STR_VAR_LEN] = 0; strcpy(StrCache_Get(svar->strVal), StrCache_Get(rightChild->strVal)); break; default: //should not happen unless the variant is not intialized correctly //shutdown(1, "invalid variant type"); svar->ptrVal = NULL; break; } svar->vt = rightChild->vt; }
//strleft(char string, int i); HRESULT openbor_strleft(ScriptVariant **varlist , ScriptVariant **pretvar, int paramCount) { const char *src; char *dst; int srcLength, dstLength; if(paramCount < 2) { goto sl_error; } if(varlist[0]->vt != VT_STR || varlist[1]->vt != VT_INTEGER) { printf("\n Error, strleft({string}, {characters}): Invalid or missing parameter. Strleft must be passed valid {string} and number of {characters}.\n"); goto sl_error; } src = StrCache_Get(varlist[0]->strVal); srcLength = strlen(src); dstLength = (srcLength < varlist[1]->lVal) ? srcLength : varlist[1]->lVal; ScriptVariant_ChangeType(*pretvar, VT_STR); (*pretvar)->strVal = StrCache_Pop(dstLength); dst = StrCache_Get((*pretvar)->strVal); memcpy(dst, src, dstLength); dst[dstLength] = '\0'; return S_OK; sl_error: *pretvar = NULL; return E_FAIL; }
//strinfirst(char string, char search_string); HRESULT openbor_strinfirst(ScriptVariant **varlist , ScriptVariant **pretvar, int paramCount) { char *tempstr = NULL; if(paramCount < 2) { goto sif_error; } if(varlist[0]->vt != VT_STR || varlist[1]->vt != VT_STR) { printf("\n Error, strinfirst({string}, {search string}): Strinfirst must be passed valid {string} and {search string}. \n"); goto sif_error; } tempstr = strstr((char *)StrCache_Get(varlist[0]->strVal), (char *)StrCache_Get(varlist[1]->strVal)); if (tempstr != NULL) { ScriptVariant_ChangeType(*pretvar, VT_STR); (*pretvar)->strVal = StrCache_CreateNewFrom(tempstr); } else { ScriptVariant_ChangeType(*pretvar, VT_INTEGER); (*pretvar)->lVal = -1; } return S_OK; sif_error: *pretvar = NULL; return E_FAIL; }
//strinlast(char string, char search_string); HRESULT openbor_strinlast(ScriptVariant **varlist , ScriptVariant **pretvar, int paramCount) { char *tempstr = NULL; if(paramCount < 2) { goto sil_error; } if(varlist[0]->vt != VT_STR || varlist[1]->vt != VT_STR) { printf("\n Error, strinlast({string}, {search string}): Strinlast must be passed valid {string} and {search string}. \n"); goto sil_error; } // this definitely doesn't work??? it interprets a string cache index as a character tempstr = strrchr((char *)StrCache_Get(varlist[0]->strVal), varlist[1]->strVal); if (tempstr != NULL) { ScriptVariant_ChangeType(*pretvar, VT_STR); (*pretvar)->strVal = StrCache_CreateNewFrom(tempstr); } else { ScriptVariant_ChangeType(*pretvar, VT_INTEGER); (*pretvar)->lVal = -1; } return S_OK; sil_error: *pretvar = NULL; return E_FAIL; }
//strright(char string, int i); HRESULT openbor_strright(ScriptVariant **varlist , ScriptVariant **pretvar, int paramCount) { char *tempstr = NULL; if(paramCount < 2) { goto sr_error; } if(varlist[0]->vt != VT_STR || varlist[1]->vt != VT_INTEGER) { printf("\n Error, strright({string}, {characters}): Invalid or missing parameter. Strright must be passed valid {string} and number of {characters}.\n"); goto sr_error; } tempstr = (char *)StrCache_Get(varlist[0]->strVal); if (tempstr && tempstr[0]) { ScriptVariant_ChangeType(*pretvar, VT_STR); (*pretvar)->strVal = StrCache_CreateNewFrom(&tempstr[varlist[1]->lVal]); } else { ScriptVariant_ChangeType(*pretvar, VT_INTEGER); (*pretvar)->lVal = -1; } return S_OK; sr_error: *pretvar = NULL; return E_FAIL; }
ScriptVariant *ScriptVariant_Le(ScriptVariant * svar, ScriptVariant * rightChild) { DOUBLE dbl1, dbl2; static ScriptVariant retvar; retvar.vt = VT_INTEGER; if(ScriptVariant_DecimalValue(svar, &dbl1) == S_OK && ScriptVariant_DecimalValue(rightChild, &dbl2) == S_OK) { retvar.lVal = (dbl1 <= dbl2); } else if(svar->vt == VT_STR && rightChild->vt == VT_STR) { retvar.lVal = (strcmp(StrCache_Get(svar->strVal), StrCache_Get(rightChild->strVal)) <= 0); } else if(svar->vt == VT_PTR && rightChild->vt == VT_PTR) { retvar.lVal = (svar->ptrVal <= rightChild->ptrVal); } else if(svar->vt == VT_EMPTY || rightChild->vt == VT_EMPTY) { retvar.lVal = 0; } else { retvar.lVal = (memcmp(svar, rightChild, sizeof(ScriptVariant)) <= 0); } return &retvar; }
BOOL ScriptVariant_IsTrue(ScriptVariant * svar) { switch (svar->vt) { case VT_STR: return StrCache_Get(svar->strVal)[0] != 0; case VT_INTEGER: return svar->lVal != 0; case VT_DECIMAL: return svar->dblVal != 0.0; case VT_PTR: return svar->ptrVal != 0; default: return 0; } }
//strlength(char string); HRESULT openbor_strlength(ScriptVariant **varlist , ScriptVariant **pretvar, int paramCount) { if(paramCount < 1 || varlist[0]->vt != VT_STR) { goto strlength_error; } ScriptVariant_ChangeType(*pretvar, VT_INTEGER); (*pretvar)->lVal = strlen((char *)StrCache_Get(varlist[0]->strVal)); return S_OK; strlength_error: printf("Error, strlength({string}): Invalid or missing parameter. Strlength must be passed a valid {string}.\n"); *pretvar = NULL; return E_FAIL; }
//strwidth(char string, int font); HRESULT openbor_strwidth(ScriptVariant **varlist , ScriptVariant **pretvar, int paramCount) { LONG ltemp; if(paramCount < 2 || varlist[0]->vt != VT_STR || FAILED(ScriptVariant_IntegerValue(varlist[1], <emp))) { goto strwidth_error; } ScriptVariant_ChangeType(*pretvar, VT_INTEGER); (*pretvar)->lVal = font_string_width((int)ltemp, (char *)StrCache_Get(varlist[0]->strVal)); return S_OK; strwidth_error: printf("Error, strwidth({string}, {font}): Invalid or missing parameter.\n"); *pretvar = NULL; return E_FAIL; }
void ScriptVariant_ToString(ScriptVariant * svar, LPSTR buffer) { switch (svar->vt) { case VT_EMPTY: sprintf(buffer, "<VT_EMPTY> Unitialized"); break; case VT_INTEGER: sprintf(buffer, "%d", svar->lVal); break; case VT_DECIMAL: sprintf(buffer, "%lf", svar->dblVal); break; case VT_PTR: sprintf(buffer, "#%ld", (long) (svar->ptrVal)); break; case VT_STR: sprintf(buffer, "%s", StrCache_Get(svar->strVal)); break; default: sprintf(buffer, "<Unprintable VARIANT type.>"); break; } }
//'compile' constant to improve speed void Instruction_ConvertConstant(Instruction * pins) { ScriptVariant *pvar; if(pins->theVal) return; //already have the constant as a variant if(pins->OpCode == CONSTDBL) { pvar = (ScriptVariant *) malloc(sizeof(ScriptVariant)); ScriptVariant_Init(pvar); ScriptVariant_ChangeType(pvar, VT_DECIMAL); if(pins->theToken->theType != END_OF_TOKENS) pvar->dblVal = (DOUBLE) atof(pins->theToken->theSource); else pvar->dblVal = (DOUBLE) atof(pins->Label); } else if(pins->OpCode == CONSTINT || pins->OpCode == CHECKARG) { pvar = (ScriptVariant *) malloc(sizeof(ScriptVariant)); ScriptVariant_Init(pvar); ScriptVariant_ChangeType(pvar, VT_INTEGER); if(pins->theToken->theType != END_OF_TOKENS) { if(pins->theToken->theType == TOKEN_HEXCONSTANT) pvar->lVal = (LONG) htoi(pins->theToken->theSource); else pvar->lVal = (LONG) atoi(pins->theToken->theSource); } else { if(pins->Label[1] == 'x' || pins->Label[1] == 'X') pvar->lVal = (LONG) htoi(pins->Label); else pvar->lVal = (LONG) atoi(pins->Label); } } else if(pins->OpCode == CONSTSTR) { pvar = (ScriptVariant *) malloc(sizeof(ScriptVariant)); ScriptVariant_Init(pvar); ScriptVariant_ChangeType(pvar, VT_STR); strcpy(StrCache_Get(pvar->strVal), pins->theToken->theSource); } else return; pins->theVal = pvar; }