char *lcStringAdd_( char *sIn, char *sFormat, ... ){ va_list arg; char *sNewString; char *buffer; int iFormatLen; va_start( arg, sFormat ); iFormatLen = vasprintf(&sNewString,sFormat,arg); va_end( arg ); buffer = malloc(iFormatLen+lcStrlen(sIn)+1); sprintf(buffer,"%s%s",sIn,sNewString); sIn = realloc(sIn,lcStrlen(buffer)+1); sIn[0] = '\0'; strcpy(sIn,buffer); lcFree( buffer ); lcFree( sNewString ); return sIn; }
struct lcTemplate *lcTemplateLoad(char *sTemplate, struct lcUser *User){ char *sFilePath = NULL; char *sHTMLPath = "HTML/"; int iTemplateSize = -1; FILE *fTemplate ; struct lcTemplate *tpl = malloc(sizeof(struct lcTemplate)); tpl->vars = malloc(sizeof(struct variableContainer)); tpl->vars->Variables = malloc(sizeof(struct variable)); tpl->vars->iVariables = 0; tpl->sText = malloc(1); sFilePath = malloc(strlen(sHTMLPath)+strlen(CFG.WorkingDirectory)+strlen(sTemplate)+2); sprintf(sFilePath,"%s%s%s",CFG.WorkingDirectory,sHTMLPath,sTemplate); if (lcFileExists(sFilePath)==FALSE) { debug("Template %s does not exist\n\r",sFilePath); lcFree(sFilePath); return NULL; } // load template into a string iTemplateSize = lcFileSize(sFilePath); fTemplate=fopen(sFilePath,"r"); if(fTemplate==NULL){ perror("fopen"); exit(-1); } tpl->sText = realloc(tpl->sText,iTemplateSize+1); fread (tpl->sText, 1, iTemplateSize, fTemplate); tpl->sText[iTemplateSize] = '\0'; fclose(fTemplate); lcFree(sFilePath); lcTemplateAddVariableString(tpl,"User.Login",User->login==TRUE?"true":"false"); lcTemplateAddVariableString(tpl,"User.Admin",User->UserType==lcADMIN?"true":"false"); lcTemplateAddVariableString(tpl,"User.Name",User->sName); lcTemplateAddVariableString(tpl,"User.Root",strcmp(User->sName,CFG.sRootUser)==0?"true":"false"); return tpl; }
char *lcStrReplace_(char *string,char *replace, char *with){ int iNext = -1; char *pos = string; int iFindLen = (int)strlen(replace); int iReplaceLen = (int)strlen(with); char *sRet = NULL; int iCnt = 0; while((iNext=lcStrStr(pos,replace))!=-1){ iCnt++; pos +=iNext+iFindLen; } int iTmp = (int)strlen(string)-iFindLen*iCnt + iReplaceLen*iCnt +2; sRet = malloc(iTmp); memset(sRet, '\0', iTmp); pos = string; while((iNext=lcStrStr(pos,replace))!=-1){ strncat(sRet,pos,iNext); strncat(sRet,with,iReplaceLen); pos +=iNext+iFindLen; } // and add the last rest if there is nothing to find more strcat(sRet,pos); string = realloc(string,strlen(sRet)+2); memset(string, '\0', strlen(sRet)+2); strncat(string,sRet,strlen(sRet)); lcFree(sRet); return string; }
static void objectDataDealloc(LCObjectRef object) { if (object->data) { if(object->type->dealloc) { object->type->dealloc(object); } else { lcFree(object->data); } object->data = NULL; } }
LCObjectRef objectRelease(LCObjectRef object) { if (object) { object->rCount = object->rCount - 1; if (object->rCount == 0) { objectDataDealloc(object); lcFree(object); return NULL; } } return object; }
static void _objectSetHash(LCObjectRef object, char hash[HASH_LENGTH]) { if (!hash) { if (object->hash) { lcFree(object->hash); } object->hash = NULL; } if (!object->hash) { object->hash = malloc(sizeof(char)*HASH_LENGTH); } strcpy(object->hash, hash); }
void lcTemplateClean(struct lcTemplate *tpl){ // free vars int i; for(i=0;i<tpl->vars->iVariables;i++){ lcFree(tpl->vars->Variables[i]->sName); lcFree(tpl->vars->Variables[i]->sValue); lcFree(tpl->vars->Variables[i]); } lcFree(tpl->vars->Variables); lcFree(tpl->vars); lcFree(tpl->sText); lcFree(tpl); }
int _lcTemplateCheckCodeblock(struct variableContainer *vars,codeblock *con){ char *sIfCondition = con->sCondition; int iConditionsFound = 0; lcSymbol ConditionType = lcIsTrue; // check what to do (==,<,<=,>=,>,!,!=) if(lcStrStr(sIfCondition, "==") != -1){ ConditionType = lcEquals; iConditionsFound++; } if(lcStrStr(sIfCondition, "<=") != -1){ ConditionType = lcSmallerEqual; iConditionsFound++; }else if(lcStrStr(sIfCondition, "<") != -1){ ConditionType = lcSmaller; iConditionsFound++; } if(lcStrStr(sIfCondition, ">=") != -1){ ConditionType = lcBiggerEqual; iConditionsFound++; }else if(lcStrStr(sIfCondition, ">") != -1){ ConditionType = lcBigger; iConditionsFound++; } if(lcStrStr(sIfCondition, "!=") != -1){ ConditionType = lcNotEqual; iConditionsFound++; }else if(lcStrStr(sIfCondition, "!") != -1){ ConditionType = lcIsFalse; iConditionsFound++; } if(lcStrStr(sIfCondition, "isset(") != -1){ ConditionType = lcIsSet; iConditionsFound++; } if(iConditionsFound > 1){ sprintf(con->sErrorMsg,"invalid amount of conditions! '%i'",iConditionsFound); debug("%s\n",con->sErrorMsg); return -1; } char *sVarName = NULL; char *sValue = NULL; int iLen = 0; // we have only the name if it's an boolean if(ConditionType == lcIsTrue){ asprintf(&sValue,"true"); asprintf(&sVarName,con->sCondition); }else if (ConditionType == lcIsFalse){ // remove the ! asprintf(&sValue,"false"); iLen = strlen(con->sCondition); sVarName = malloc(iLen+1); strncpy(sVarName,con->sCondition+1,iLen); sVarName[iLen]='\0'; }else if(ConditionType == lcIsSet){ int iStartPos = lcStrStr(sIfCondition,"("); int iEndPos = lcStrStr(sIfCondition,")"); iStartPos++; // remove the ( iLen = iEndPos-iStartPos; sVarName = malloc(iLen+1); strncpy(sVarName,con->sCondition+iStartPos,iLen); sVarName[iLen]='\0'; }else{ // with value (==,!=,<,<=,>,>=) char *sDelimiter; switch(ConditionType){ case lcEquals: sDelimiter = "=="; break; case lcNotEqual: sDelimiter = "!="; break; case lcBiggerEqual: sDelimiter = ">="; break; case lcBigger: sDelimiter = ">"; break; case lcSmallerEqual:sDelimiter = "<="; break; case lcSmaller: sDelimiter = "<"; break; default: sprintf(con->sErrorMsg,"invalid conditiontype"); debug("%s\n",con->sErrorMsg); return -1; break; } if(strlen(sDelimiter) == 1){ asprintf(&sVarName,"%s",strtok(con->sCondition,sDelimiter)); asprintf(&sValue ,"%s",strtok(NULL,sDelimiter)); }else{ int iPos = lcStrStr(con->sCondition,sDelimiter); con->sCondition[iPos] = '\0'; con->sCondition[iPos+1] = '\0'; asprintf(&sVarName,"%s",con->sCondition); asprintf(&sValue ,"%s",con->sCondition+iPos+2); } if(sVarName == NULL || sValue == NULL){ sprintf(con->sErrorMsg,"invalid value(%s) or name(%s)",sValue,sVarName); debug("%s\n",con->sErrorMsg); return -1; } } int i; int iFound = 0; for(i=0;i<vars->iVariables;i++){ if(strcmp(sVarName,vars->Variables[i]->sName)==0){ iFound++; debug("found: %s valuecmp:%s valuesrc:%s\n",sVarName,sValue,vars->Variables[i]->sValue); break; } } if(iFound != 1 && ConditionType != lcIsSet){ sprintf(con->sErrorMsg,"undefined variable %s",sVarName); debug("%s\n",con->sErrorMsg); free(sVarName); free(sValue); return -1; } int iReturn = 0; double a = 0.0 ,b = 0.0; char *end; /* * 1 ^= OK * 0 ^= NOK * -1 ^= Error */ switch(ConditionType){ case lcEquals: if(strcmp(vars->Variables[i]->sValue,sValue)!=0) iReturn = 0; break; case lcNotEqual: if(strcmp(vars->Variables[i]->sValue,sValue)!=0) iReturn = 1; break; case lcBiggerEqual: a = strtod(vars->Variables[i]->sValue,&end); if(end == vars->Variables[i]->sValue){ sprintf(con->sErrorMsg,"value(%s) isn't a number",vars->Variables[i]->sValue); iReturn = -1; break; } b = strtod(sValue,&end); if(end == vars->Variables[i]->sValue){ sprintf(con->sErrorMsg,"value(%s) isn't a number",sValue); iReturn = -1; break; } if(a>=b) iReturn = 1; break; case lcBigger: a = strtod(vars->Variables[i]->sValue,&end); if(end == vars->Variables[i]->sValue){ sprintf(con->sErrorMsg,"value(%s) isn't a number",vars->Variables[i]->sValue); iReturn = -1; break; } b = strtod(sValue,&end); if(end == vars->Variables[i]->sValue){ sprintf(con->sErrorMsg,"value(%s) isn't a number",sValue); iReturn = -1; break; } if(a>b) iReturn = 1; break; case lcSmallerEqual: a = strtod(vars->Variables[i]->sValue,&end); if(end == vars->Variables[i]->sValue){ sprintf(con->sErrorMsg,"value(%s) isn't a number",vars->Variables[i]->sValue); iReturn = -1; break; } b = strtod(sValue,&end); if(end == vars->Variables[i]->sValue){ sprintf(con->sErrorMsg,"value(%s) isn't a number",sValue); iReturn = -1; break; } if(a>=b) iReturn = 1; break; case lcSmaller: a = strtod(vars->Variables[i]->sValue,&end); if(end == vars->Variables[i]->sValue){ sprintf(con->sErrorMsg,"value(%s) isn't a number",vars->Variables[i]->sValue); iReturn = -1; break; } b = strtod(sValue,&end); if(end == vars->Variables[i]->sValue){ sprintf(con->sErrorMsg,"value(%s) isn't a number",sValue); iReturn = -1; break; } if(a>=b) iReturn = 1; break; case lcIsFalse: case lcIsTrue: if(strcmp(vars->Variables[i]->sValue,sValue)==0) iReturn = 1; break; case lcIsSet: if(iFound>0) iReturn = 1; break; } debug("con->sErrorMsg: %s\n",con->sErrorMsg); lcFree(sVarName); lcFree(sValue); return iReturn; }