home *** CD-ROM | disk | FTP | other *** search
- #include "CVar.h"
- #include "Tokenizer.h"
-
- #include <float.h>
- #include <limits.h>
- #include <stdlib.h>
-
-
- CVar::CVar(const char* name){
- this->name=name;
- infoStr="";//NULL;
- changeStr="";//NULL;
- flags=CON_FLAG_NONE_SET;
- instantUpdate=false;
- }
-
- /*
- We give an implementation for the virtual destructor of CVar for the compiler
- */
- CVar::~CVar(){
- }
-
- /*
- This function writes the type of the cvar (e.g. 'integer') into buff and returns a pointer to buff.
- */
- char* CVar::getTypeStr(char* buff){
- if(type==CON_CVAR_TYPE_INT)
- return strcpy(buff, "integer");
- else if(type==CON_CVAR_TYPE_BOOL)
- return strcpy(buff, "boolean");
- else if(type==CON_CVAR_TYPE_REAL)
- return strcpy(buff, "real");
- else if(type==CON_CVAR_TYPE_STRING)
- return strcpy(buff, "string");
- else
- return strcpy(buff, "unknown");
- }
-
- /*
- This function writes a string describing the accessability-flags of the cvar into buff and returns a pointer to buff.
- */
- char* CVar::getFlagsStr(char* buff){
- /*
- buff[0]= (flags & CON_FLAG_SERVER_READ) ? 'r' : '-';
- buff[1]= (flags & CON_FLAG_SERVER_WRITE) ? 'w' : '-';
- buff[2]= (flags & CON_FLAG_SERVER_EXECUTE) ? 'x' : '-';
- buff[3]= '|';
- buff[4]= (flags & CON_FLAG_CLIENT_READ) ? 'r' : '-';
- buff[5]= (flags & CON_FLAG_CLIENT_WRITE) ? 'w' : '-';
- buff[6]= (flags & CON_FLAG_CLIENT_EXECUTE) ? 'x' : '-';
- buff[7]= '\0';
- */
- buff[0] = '\0';
- strcat(buff, "[");
- if( (flags & CON_FLAG_SYSTEM) == CON_FLAG_SYSTEM )
- strcat(buff, "s");
- else
- strcat(buff, "-");
-
- if( (flags & CON_FLAG_USER) == CON_FLAG_USER )
- strcat(buff, "u");
- else
- strcat(buff, "-");
-
- if( (flags & CON_FLAG_READ_ONLY) == CON_FLAG_READ_ONLY )
- strcat(buff, "r");
- else
- strcat(buff, "-");
-
- strcat(buff, "]");
-
- return buff;
- }
-
-
-
- /*
- This Constructor creates a pure cvar named name, with initial value val.
- It doesn't reflect a real var.
- */
- CVarBool::CVarBool(const char* name, bool val):CVar(name){
- this->type=CON_CVAR_TYPE_BOOL;
- this->val=val;
- this->var=NULL;
- this->minVal=0;
- this->maxVal=1;
- }
-
- /*
- This Constructor creates a cvar named name and binds it to the variable var.
- */
- CVarBool::CVarBool(const char* name, bool* var, bool instantUpdate):CVar(name){
- this->type=CON_CVAR_TYPE_BOOL;
- this->var=var;
-
-
- this->val=*var;
- this->instantUpdate=instantUpdate;
- this->minVal=0;
- this->maxVal=1;
- }
-
- /*
- This function takes a string and sets the cvar to the value contained in the string (if valid)
- */
- void CVarBool::setValStr(const char* valStr){
- int tmp;
- int check=sscanf(valStr, "%i", &tmp);
-
- if(check==0 || check==EOF || tmp<(int)minVal || tmp>(int)maxVal) // buff was not a valid str for this cvar
- return;
-
- // val=(bool)tmp; // set local var
- val = (tmp!=0); // set local var
-
- if(instantUpdate) // and the real variable if instantUpdate is true
- updateVar();
-
- }
-
- /*
- This function writes the value of the local var in buff and return a pointer to buff
- */
- char* CVarBool::getValStr(char* buff){
- sprintf(buff, "%i", val);
- return buff;
- }
-
- /*
- This function writes the value of the referenced var in buff and return a pointer to buff
- */
- char* CVarBool::getVarValStr(char* buff){
- if(var!=NULL)
- sprintf(buff, "%i", *var);
- else
- getValStr(buff); // THINKABOUTME
-
- return buff;
- }
-
- /*
- Tests if the cvar can be set to the value in valStr
- */
- bool CVarBool::valStrIsValid(const char* valStr){
- int tmp;
- int check=sscanf(valStr, "%i", &tmp);
-
- return !(check==0 || check==EOF || tmp<(int)minVal || tmp>(int)maxVal); // buff was not a valid str for this cvar
- }
-
- /*
- Cycles through all valid values of the cvar
- */
- bool CVarBool::toggle(){
- val=!val;
- if(instantUpdate)
- updateVar();
-
- return true;
- }
-
- bool CVarBool::updateVar(){
- if(var!=NULL)
- *var=val;
- return true;
- }
-
- /*
- Sets the min/max value of the cvar.
- The actual value of the cvar is untouched!!
- */
- void CVarBool::setValRange(bool min, bool max){
- this->minVal=min;
- this->maxVal=max;
- }
-
- char* CVarBool::getValRangeStr(char* buff){
- sprintf(buff, "%i - %i", minVal, maxVal);
-
- return buff;
- }
-
- void CVarBool::setVal(bool newVal){
- val = newVal;
-
- if(instantUpdate)
- updateVar();
- }
-
- bool CVarBool::getVal(){
- return val;
- }
-
-
-
-
-
-
- /*
- This Constructor creates a pure cvar named name, with initial value val.
- It doesn't reflect a real var.
- */
- CVarInt::CVarInt(const char* name, int val):CVar(name){
- this->type=CON_CVAR_TYPE_INT;
- this->val=val;
-
- this->var=NULL;
- this->minVal=INT_MIN;
- this->maxVal=INT_MAX;
- }
-
- /*
- This Constructor creates a cvar named name and binds it to the variable var.
- */
- CVarInt::CVarInt(const char* name, int* var, bool instantUpdate):CVar(name){
- this->type=CON_CVAR_TYPE_INT;
- this->var=var;
- this->val=*var;
- this->instantUpdate=instantUpdate;
- this->minVal=INT_MIN;
- this->maxVal=INT_MAX;
- }
-
- /*
- This function takes a string and sets the cvar to the value contained in the string (if valid)
- */
- void CVarInt::setValStr(const char* valStr){
- int tmp;
- int check=sscanf(valStr, "%i", &tmp);
-
- if(check==0 || check==EOF || tmp<minVal || tmp>maxVal) // buff was not a valid str for this cvar
- return;
-
- val=tmp;
-
- if(instantUpdate)
- updateVar();
- }
-
- /*
- This function writes the value of the local var in buff and return a pointer to buff
- */
- char* CVarInt::getValStr(char* buff){
- sprintf(buff, "%i", val);
-
- return buff;
- }
-
- /*
- This function writes the value of the referenced var in buff and return a pointer to buff
- */
- char* CVarInt::getVarValStr(char* buff){
- if(var!=NULL)
- sprintf(buff, "%i", *var);
- else
- getValStr(buff); // THINKABOUTME
-
- return buff;
- }
-
- /*
- Tests if the cvar can be set to the value in valStr
- */
- bool CVarInt::valStrIsValid(const char* valStr){
- int tmp;
- int check=sscanf(valStr, "%i", &tmp);
-
- return !(check==0 || check==EOF || tmp<minVal || tmp>maxVal); // buff was not a valid str for this cvar
- }
-
- /*
- Cycles through all valid values of the cvar
- */
- bool CVarInt::toggle(){
- val=val+1 > maxVal ? minVal : val+1;
-
- if(instantUpdate)
- updateVar();
-
- return true;
- }
-
- bool CVarInt::updateVar(){
- *var=val;
- return true;
- }
-
- /*
- Sets the min/max value of the cvar.
- The actual value of the cvar is untouched!!
- */
- void CVarInt::setValRange(int min, int max){
- this->minVal=min;
- this->maxVal=max;
- }
-
- char* CVarInt::getValRangeStr(char* buff){
- sprintf(buff, "%i - %i", minVal, maxVal);
-
- return buff;
- }
-
- void CVarInt::setVal(int newVal){
- val = newVal;
-
- if(instantUpdate)
- updateVar();
- }
-
- int CVarInt::getVal(){
- return val;
- }
-
-
-
-
-
-
- /*
- This Constructor creates a pure cvar named name, with initial value val.
-
- It doesn't reflect a real var.
- */
- CVarReal::CVarReal(const char* name, float val):CVar(name){
- this->type=CON_CVAR_TYPE_REAL;
- this->val=val;
- this->var=NULL;
- this->minVal=FLT_MIN;
- this->maxVal=FLT_MAX;
- }
-
- /*
- This Constructor creates a cvar named name and binds it to the variable var.
- */
- CVarReal::CVarReal(const char* name, float* var, bool instantUpdate):CVar(name){
- this->type=CON_CVAR_TYPE_REAL;
- this->var=var;
- this->val=*var;
- this->instantUpdate=instantUpdate;
- this->minVal=FLT_MIN;
- this->maxVal=FLT_MAX;
- }
-
- /*
- This function takes a string and sets the cvar to the value contained in the string (if valid)
- */
- void CVarReal::setValStr(const char* valStr){
- float tmp;
- int check=sscanf(valStr, "%f", &tmp);
-
-
- if(check==0 || check==EOF || tmp<minVal || tmp>maxVal) // buff was not a valid str for this cvar
- return;
-
- val=tmp;
-
- if(instantUpdate)
- updateVar();
- }
-
- /*
- This function writes the value of the local var in buff and return a pointer to buff
- */
- char* CVarReal::getValStr(char* buff){
- sprintf(buff, "%.2f", val);
- return buff;
- }
-
- /*
- This function writes the value of the referenced var in buff and return a pointer to buff
- */
- char* CVarReal::getVarValStr(char* buff){
- if(var!=NULL)
- sprintf(buff, "%.2f", *var);
- else
- getValStr(buff); // THINKABOUTME
-
- return buff;
- }
-
- /*
- Tests if the cvar can be set to the value in valStr
- */
- bool CVarReal::valStrIsValid(const char* valStr){
- float tmp;
- int check=sscanf(valStr, "%f", &tmp);
-
- return !(check==0 || check==EOF || tmp<minVal || tmp>maxVal); // buff was not a valid str for this cvar
- }
-
- /*
- Cycles through all valid values of the cvar
- */
- bool CVarReal::toggle(){
- val=val+1 > maxVal ? minVal : val+1;;
-
- if(instantUpdate)
- updateVar();
-
- return true;
- }
-
- bool CVarReal::updateVar(){
- *var=val;
- return true;
- }
-
- /*
- Sets the min/max value of the cvar.
- The actual value of the cvar is untouched!!
- */
- void CVarReal::setValRange(float min, float max){
- this->minVal=min;
- this->maxVal=max;
- }
-
- char* CVarReal::getValRangeStr(char* buff){
- sprintf(buff, "%.2f - %.2f", minVal, maxVal);
-
- return buff;
- }
-
- void CVarReal::setVal(float newVal){
- val = newVal;
-
- if(instantUpdate)
- updateVar();
-
- }
-
- float CVarReal::getVal(){
- return val;
- }
-
-
-
-
-
-
- /*
- This Constructor creates a pure cvar named name, with initial value val.
- It doesn't reflect a real var.
- */
- CVarString::CVarString(const char* name, const char* val):CVar(name){
-
- this->type=CON_CVAR_TYPE_STRING;
- /*this->val=*/strncpy(this->val, val, CON_MAX_STRING_LENGTH-1);
- this->val[CON_MAX_STRING_LENGTH-1]='\0';
- this->var=NULL;
- this->minLength=0;
- this->maxLength=CON_MAX_STRING_LENGTH-1;
- }
-
- /*
- This Constructor creates a cvar named name and binds it to the variable var.
- */
- CVarString::CVarString(const char* name, char** var, bool instantUpdate):CVar(name){
- this->type=CON_CVAR_TYPE_STRING;
- this->var=var;
- /*this->val=*/strncpy(this->val, *var, CON_MAX_STRING_LENGTH-1);
- this->val[CON_MAX_STRING_LENGTH-1]='\0';
- this->instantUpdate=instantUpdate;
- this->minLength=0;
- this->maxLength=CON_MAX_STRING_LENGTH-1;
- }
-
- /*
- This function takes a string and sets the cvar to the value contained in the string (if valid)
- */
- void CVarString::setValStr(const char* valStr){
- if((int)strlen(valStr)<minLength || (int)strlen(valStr)>maxLength) // buff was not a valid str for this cvar
- return;
-
- strcpy(val, valStr); // and our local val as well
-
- if(instantUpdate)
- updateVar();
- }
-
- /*
- This function writes the value of the local var in buff and return a pointer to buff
- */
- char* CVarString::getValStr(char* buff){
- strcpy(buff, val);
- return buff;
- }
-
- /*
- This function writes the value of the referenced var in buff and return a pointer to buff
- */
- char* CVarString::getVarValStr(char* buff){
- if(var!=NULL)
- strcpy(buff, *var);
- else
- getValStr(buff); // THINKABOUTME
-
- return buff;
- }
-
- /*
- Tests if the cvar can be set to the value in valStr
- */
- bool CVarString::valStrIsValid(const char* valStr){
- return !((int)strlen(valStr)<minLength || (int)strlen(valStr)>maxLength);
- }
-
- /*
- Cycles through all valid values of the cvar
- */
- bool CVarString::toggle(){
- return false; // string cvars cannot be toggled
- }
-
- bool CVarString::updateVar(){
- strncpy(*var, val, strlen(val)+1); // THINKABOUTME: val must fit into *var!!!!
- return true;
- }
- /*
- Sets the min/max value of the cvar.
- The actual value of the cvar is untouched!!
- */
- void CVarString::setValRange(int min, int max){
- this->minLength=min;
- this->maxLength=max;
- }
-
- char* CVarString::getValRangeStr(char* buff){
- sprintf(buff, "%i - %i chars", minLength, maxLength);
-
- return buff;
- }
-
- char* CVarString::getVal(){
- return val;
- }
-
-
-
-
-
-
-
- /*
- This Constructor creates a pure cvar named name, with initial value val.
- It doesn't reflect a real var.
- */
- CVarRealVector::CVarRealVector(const char* name, float* val, int dimension):CVar(name){
- this->type=CON_CVAR_TYPE_REAL_VECTOR;
- this->dim=dimension;
- this->val=new float[dim];
- for(int i=0;i<dim;i++)
- this->val[i]=val[i];
-
- this->var=NULL;
- this->minVal=FLT_MIN;
- this->maxVal=FLT_MAX;
- }
-
- /*
- This Constructor creates a cvar named name and binds it to the variable var.
- */
- CVarRealVector::CVarRealVector(const char* name, float** var, int dimension, bool instantUpdate):CVar(name){
- this->type=CON_CVAR_TYPE_REAL_VECTOR;
-
- this->dim=dimension;
- this->var=var;
- this->val=new float[dim];
- for(int i=0;i<dim;i++)
- this->val[i]=(*var)[i];
-
- this->minVal=FLT_MIN;
- this->maxVal=FLT_MAX;
-
- this->instantUpdate=instantUpdate;
- }
-
- /*
- This function takes a string and sets the cvar to the value contained in the string (if valid)
- */
- void CVarRealVector::setValStr(const char* valStr){
- int i;
- float* tmp=new float[dim];
-
- Tokenizer t(valStr, " ;,", "");
-
- if(t.tokc!=dim){
- delete[] tmp;
- return;
- }
-
- for(i=0;i<t.tokc;i++){
- tmp[i]=(float)atof(t.tokv[i]);
- if(tmp[i]<minVal || tmp[i]>maxVal){
- delete[] tmp;
- return;
- }
- }
-
- for(i=0;i<dim;i++)
- val[i]=tmp[i];
-
- if(instantUpdate)
- updateVar();
-
- delete[] tmp;
- }
-
- /*
- This function writes the value of the local var in buff and return a pointer to buff
- */
- char* CVarRealVector::getValStr(char* buff){
- int i;
- char* p=buff;
-
- for(i=0;i<dim;i++){
- p += sprintf(p, "%.2f ", val[i]);
-
- }
-
- int lastChar = strlen(buff) > 0 ? strlen(buff)-1 : 0;
- buff[lastChar] = '\0';
-
- return buff;
- }
-
- /*
- This function writes the value of the referenced var in buff and return a pointer to buff
- */
- char* CVarRealVector::getVarValStr(char* buff){
- int i;
- char* p=buff;
-
- if(var!=NULL){
- for(i=0;i<dim;i++){
- int b = sprintf(p, "%.2f ", (*var)[i]);
- p+=b;
- }
- int lastChar = strlen(buff) > 0 ? strlen(buff)-1 : 0;
- buff[lastChar] = '\0';
- }else{
- getValStr(buff); // THINKABOUTME
- }
-
- return buff;
- }
-
- /*
- Tests if the cvar can be set to the value in valStr
- */
- bool CVarRealVector::valStrIsValid(const char* valStr){
- int i;
- float* tmp=new float[dim];
-
- Tokenizer t(valStr, " ;,", "");
-
- if(t.tokc!=dim){
- delete[] tmp;
- return false;
- }
-
- for(i=0;i<t.tokc;i++){
- tmp[i]=(float)atof(t.tokv[i]);
- if(tmp[i]<minVal || tmp[i]>maxVal){
- delete[] tmp;
- return false;
- }
- }
-
- delete[] tmp;
- return true;
- }
-
- /*
- Cycles through all valid values of the cvar
- */
- bool CVarRealVector::toggle(){
- return false; // string cvars cannot be toggled
- }
-
- bool CVarRealVector::updateVar(){
- // printf("update: val: %f %f %f %f var: %f %f %f %f\n", val[0], val[1], val[2], val[3], (*var)[0], (*var)[1], (*var)[2], (*var)[3]);
- for(int i=0;i<dim;i++)
- (*var)[i]=val[i];
-
- return true;
- }
- /*
- Sets the min/max value of the cvar.
- The actual value of the cvar is untouched!!
- */
- void CVarRealVector::setValRange(float min, float max){
- this->minVal=min;
- this->maxVal=max;
- }
-
- char* CVarRealVector::getValRangeStr(char* buff){
- sprintf(buff, "%.2f - %.2f chars", minVal, maxVal);
-
- return buff;
- }
-
-