📄 int_variable.c
字号:
if (!strcmp(str,numType)) return(numType); if (!strcmp(str,intType)) return(intType); if (!strcmp(str,floatType)) return(floatType); if (!strcmp(str,varType)) return(varType); if (!strcmp(str,arrayType)) return(arrayType); if (!strcmp(str,scriptType)) return(scriptType); if (!strcmp(str,procType)) return(procType); for (i=0; i<nVarTypes;i++) { if (!strcmp(str,varTypeNames[i])) return(varTypeNames[i]); } return(NULL); } /* Get the type structure from a type */TypeStruct *GetTypeStruct(char *str){ int i; extern TypeStruct tsHash; extern TypeStruct tsNull; if (!strcmp(str,nullType)) return(&tsNull); if (!strcmp(str,"&str")) return(&tsStr); if (!strcmp(str,strType)) return(&tsStr); if (!strcmp(str,arrayType)) return(&tsHash); if (!strcmp(str,listType)) return(&tsStr); if (!strcmp(str,listvType)) return(&tsListv); if (!strcmp(str,rangeType)) return(&tsRange); if (!strcmp(str,numType)) return(&tsNum); if (!strcmp(str,intType)) return(&tsNum); if (!strcmp(str,floatType)) return(&tsNum); if (!strcmp(str,scriptType)) return(&tsScript); if (!strcmp(str,procType)) return(&tsProc); for (i=0; i<nVarTypes;i++) { if (!strcmp(str,varTypeNames[i])) return(typeStruct[i]); } return(NULL);} /* * Add a new variable type to LastWave interpreter * * typeName : the name of the type. * it should be of the form "&type" and should a be a pointer (always the same!) to a string * * type : specifies the type associated to the LastWave variable. It must be one of * tINT --> in case of integers (NUMVALUE) * tFLOAT --> in case of floats (NUMVALUE) * tSTR --> in case of strings (STRVALUE) * tVAL --> in case of pointers to VALUE * * parse : this is the main parse function. This function is used both in the case a "&type" is met * or when an explicit C-Parse is called. * The type of this function depends on the value of the 'type' parameter. * tINT --> char *parse (LEVEL level, char *str, int defVal, int *val) * tFLOAT --> char *parse (LEVEL level, char *str, LWFLOAT defVal, LWFLOAT *val) * tSTR --> char *parse (LEVEL level, char *str, char *defVal, char **val) * tVAL --> char *parse (LEVEL level, char *str, void *defVal, void **val) * In any case, the parameters are * 'level' : the level the parsing takes place in * 'str' : the string expression to be parseed * 'defVal' : the default value if parsing has failed * 'val' : the parsed value * The function returns YES or NO depending upon whether the parsing has succeeded or failed * * The 'parse' function can be NULL in which case the LastWave evaluator is called and the type of the result * is checked to be the right type. * * ts : the type structure associated to the type if any (NULL otherwise) * * This function returns the code tTYPE for the ParseArgv function (tTYPE_ MUST be set to be tTYPE_+1) * */static int AddVariableType_(char *typeName, int type, void *parse, TypeStruct *ts){ if (typeName == NULL) Errorf("AddVariableType_() : cannot add NULL type"); if (typeName[0] != '&') Errorf("AddVariableType_() : type name (%s) should start with a '&'",typeName); if (GetArgType(typeName) != NULL) Errorf("AddVariableType_() : type '%s' already exists",typeName); if (nVarTypes >= NVarTypes) Errorf("AddVariableType_() : sorry too many types already defined"); /* Managing type */ varTypeNames[nVarTypes] = typeName; if (parse == NULL) parse = (void *) ParseValLevel_; varTypeParseFunctions[nVarTypes] = parse; varTypes[nVarTypes] = type; typeStruct[nVarTypes] = ts; if (toplevelCur->packageName != NULL) typePackage[nVarTypes] = CopyStr(toplevelCur->packageName); else typePackage[nVarTypes] = CopyStr("kernel"); nVarTypes++; return((nVarTypes-1)*2+tLAST);}/* * The corresponding routines */ int AddVariableTypeInt(char *type, char (*parse) (LEVEL level, char *, int, int *)){ return (AddVariableType_(type,tINT, (void *) parse,NULL));}int AddVariableTypeFloat(char *type, char (*parse) (LEVEL level, char *, LWFLOAT, LWFLOAT *)){ return (AddVariableType_(type,tFLOAT, (void *) parse,NULL));}int AddVariableTypeStr(char *type, char (*parse) (LEVEL level, char *, char *, char **)){ return (AddVariableType_(type,tSTR, (void *) parse,NULL));}int AddVariableTypeValue(char *type, TypeStruct *ts, char (*parse) (LEVEL level, char *, void *, void **)){ return (AddVariableType_(type,tVAL, (void *) parse,ts));}void AddVariableType(char *type, char (*f) (LEVEL, char *, void *, void **)){ Warningf("AddVariableType(%s) : Old function",type);}/* * Getting a (simple) variable at a given level. * flagCreate = 0 ==> nothing is done if variable does not exist * flagCreate = 1 ==> variable is created and initialized with nullValue * flagCreate = 2 ==> variable is created and is an array */ static VARIABLE GetSimpleVariableHT(HASHTABLE t,char *name, char flagCreate){ VARIABLE variable; variable = (VARIABLE) GetElemHashTable(t,name); if (flagCreate && variable == NULL) { variable = NewVariable(); variable->name = CopyStr(name); variable->hashTable = t; if (flagCreate == 1) { variable->content = nullValue; nullValue->nRef++; } else variable->content = (VALUE) NewHashTable(5); AddElemHashTable(t,(AHASHELEM) variable); RemoveRefValue( variable); } return(variable);}/* * Getting a variable in a given hashtable. * The variable is of the form a.b.c where a and a.b are arrays * * * It returns : * * the last variable it found * *left is the character it stopped parsing the name at * *flag is 0 if it went well and stopped at a character which is not a '.' * is 1 if it stopped because the variable is not an array and it should be or any other bad error * is 2 if variable does not exist (in which case it returns NULL) * in any case 't' is changed and contains the hashtable the returned variable is in * * Does not generate an error */ VARIABLE GetVariableHT(HASHTABLE *t, char flagCreate, char *name, char **left, char *flag){ char *name1,*name2; char field[MaxNameLength]; VARIABLE variable; VALUE c; *flag = 0; variable = NULL; name1 = name2 = name; if (IsValidSymbolChar1(*name2)) { name2++; while (IsValidSymbolChar(*name2)) name2++; } if (*name2 == '\0') { variable = GetSimpleVariableHT(*t,name1,flagCreate); if (variable == NULL) { SetErrorf("Variable '%s' does not exist",name1); *left = name1; *flag = 2; } else *left = name2; return(variable); } if (name2-name1+1 > MaxNameLength) { SetErrorf("Variable or field name is too long (you should increase MaxNameLength in int_variable.c"); *flag = 1; *left = name1; return(NULL); } strncpy(field,name1,name2-name1); field[name2-name1] = '\0'; if (flagCreate) variable = GetSimpleVariableHT(*t,field,2); else variable = GetSimpleVariableHT(*t,field,0); if (variable == NULL) { SetErrorf("Variable '%s' does not exist",name1); *left = name1; *flag = 2; return(NULL); } *left = name2; while(1) { /* Next character is a '.' */ if (*name2 != '.') { *left = name2; *flag = 0; return(variable); } name2++; /* Getting the array corresponding to the variable */ c = ValueOf((VALUE) variable); if (GetTypeValue(c) != arrayType) { SetErrorf("Expect array right before '%s'",name1); *left = name1; *flag = 1; return(NULL); } *t = ((HASHTABLE) c); /* Looking for the next field */ name1 = name2; while (IsValidSymbolChar(*name2)) name2++; if (name2-name1+1 > MaxNameLength) { SetErrorf("Variable or field name is too long (you should increase MaxNameLength in int_variable.c"); *flag = 1; *left = name1; return(NULL); } strncpy(field,name1,name2-name1); field[name2-name1] = '\0'; /* Getting the new variable */ if (flagCreate) { if (*name2 == '.') variable = GetSimpleVariableHT(*t,field,2); else variable = GetSimpleVariableHT(*t,field,1); } else variable = GetSimpleVariableHT(*t,field,0); if (variable == NULL) { SetErrorf("Index array '%s' does not exist",field); *flag = 2; *left = name1; return(NULL); } /* Are we done ? */ if (*name2 == '\0') { *left = name2; return(variable); } }}/* * Same as above but at a given (eventually current) level * * Returns the variable if it exists or NULL if it does not * * If there is a syntax error --> Generate an error */ /* same as below but does not generate an error */VARIABLE GetVariableLevel_(LEVEL level,char *name){ VARIABLE v; HASHTABLE t; char *left,flag; while (level->levelVar != level) level = level->levelVar; t = level->theVariables; v = GetVariableHT(&t,0,name,&left,&flag); if (flag == 1 || *left != '\0') return(NULL); return(v); }VARIABLE GetVariableLevel(LEVEL level,char *name){ VARIABLE v = GetVariableLevel_(level,name); if (v == NULL) Errorf1(""); return(v);}/* * Same as above but at the current level */VARIABLE GetVariable(char *name){ return(GetVariableLevel(levelCur,name));}/* * Does the variable'name' of a given type exist at a given level ? * * The 'name' can be one of * - a simple variable like 'me' * - an array field like 'me.house.kitchen' */VALUE ExistVariableLevel(LEVEL level, char *name, char *type){ VARIABLE variable; VALUE value,*pcont; char *type1; char *left,flag; HASHTABLE t; /* Get the variable */ while (level->levelVar != level) level = level->levelVar; t = level->theVariables; variable = GetVariableHT(&t,0,name,&left,&flag); if (variable == NULL || *left != '\0') return(NULL); variable = GetVariableLevel(level,name); /* If does not exist --> return */ if (variable == NULL) return(NULL); /* Get a pointer to the variable content and the associated content */ pcont = GetVariablePContent(variable,NULL); value = *pcont; /* If content is NULL --> return NULL */ if (value == NULL) return(NULL); /* If type is not NULL we must check the type */ if (type != NULL) { type1 = GetTypeValue(value); if (type1 == type) return(value); if (type1 == signaliType && type == signalType) return(value); if (type1 == imageiType && type == imageType) return(value); if (type1 == numType && type == floatType) return(value); if (type1 == numType && type == intType && ((NUMVALUE) value)->f == (int) ((NUMVALUE) value)->f) return(value); return(NULL); } return(value);} /* * Same as above but at the current level */VALUE ExistVariable(char *name,char *type){ return(ExistVariableLevel(levelCur,name,type));}/* * Get the pointer to the variable's content (after following all the eventual indirections) */VALUE *GetVariablePContent(VARIABLE variable, char *pflagTrace){ if (variable == NULL) return(NULL); while (variable->content != NULL && IsVariable(variable->content)) { variable = (VARIABLE) variable->content; } return(&(variable->content));} /******************************************************************************* * * Getting variable contents (this is different from evaluation) * * ?????????????? A SUPPRIMER */VALUE GetVariableContentLevel_(LEVEL level, char *name, char *type) { Errorf("GetVariableContentLevel_ A Supprimer");} VALUE GetVariableContentLevel(LEVEL level, char *name, char *type) { Errorf("GetVariableContentLevel A Supprimer");}VALUE GetVariableContent(char *name, char *type) { Errorf("GetVariableContent A Supprimer");}/******************************************************************************** * * Parsing variables ????????????? * * A SUPPRIMER * ********************************************************************************/char ParseVariableLevel_(LEVEL level, char *name,VALUE def, VALUE *val,char *type, char flagTemp) { Errorf("ParseVariableLevel_ A Supprimer");} void ParseVariableLevel(LEVEL level, char *name, VALUE *val,char *type, char flagTemp) { Errorf("ParseVariableLevel A Supprimer");} void ParseVariable(char *name, VALUE *val,char *type, char flagTemp) { Errorf("ParseVariable A Supprimer");} char ParseVariable_(char *name, VALUE def, VALUE *val,char *type, char flagTemp) { Errorf("ParseVariable_ A Supprimer");} VARIABLE GetSimpleVariableHashTable(HASHTABLE t,char *name){ Errorf("GetSimpleVariableHashTable A Supprimer");}VARIABLE CGetVariableLevel(LEVEL level,char *name){ Errorf("CGetVariableLevel A Supprimer");}VARIABLE CGetVariable(char *name){ Errorf("CGetVariable A Supprimer");} /**************************************************************************************** * * * Functions to manage evaluation of a variable (with eventual fields/extraction * * ****************************************************************************************/#define Clean(val) if ((val) != NULL) {(val) = NULL;}extern char GetEventVariable2(char *name, char **str, LWFLOAT *f);extern char * LookForBracketScript(char *theLine, char *line);extern unsigned char TGetVariableContentLevelExpr(LEVEL level, char *begin, char **left, LWFLOAT *resFlt, VALUE *resVC, unsigned char flagType, unsigned char flagSimple, ExprDefParam *defParam);extern char IsErrorMsge(void);/* * Send a GetField message to the content 'value' with the field 'field' the FSIList 'fsiList' and put the result * in f,str or val * It returns what the message returned */static char *GetField(VALUE value, char *field, FSIList *fsiList,LWFLOAT *f,char **str, VALUE *val){ void *arg[5]; Field *fi; arg[0] = field; arg[1] = fsiList; arg[2] = f; arg[3] = str; *str = NULL; arg[4] = val; *val = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -