home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 March / ENTER.ISO / files / fwp-0.0.6-win32-installer.exe / CVar.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-12-06  |  14.6 KB  |  702 lines

  1. #include "CVar.h"
  2. #include "Tokenizer.h"
  3.  
  4. #include <float.h>
  5. #include <limits.h>
  6. #include <stdlib.h>
  7.  
  8.  
  9. CVar::CVar(const char* name){
  10.     this->name=name;
  11.     infoStr="";//NULL;
  12.     changeStr="";//NULL;
  13.     flags=CON_FLAG_NONE_SET;
  14.     instantUpdate=false;
  15. }
  16.  
  17. /*
  18.     We give an implementation for the virtual destructor of CVar for the compiler
  19. */
  20. CVar::~CVar(){
  21. }
  22.  
  23. /*
  24.     This function writes the type of the cvar (e.g. 'integer') into buff and returns a pointer to buff. 
  25. */
  26. char* CVar::getTypeStr(char* buff){
  27.     if(type==CON_CVAR_TYPE_INT)
  28.         return strcpy(buff, "integer");
  29.     else if(type==CON_CVAR_TYPE_BOOL)
  30.         return strcpy(buff, "boolean");
  31.     else if(type==CON_CVAR_TYPE_REAL)
  32.         return strcpy(buff, "real");
  33.     else if(type==CON_CVAR_TYPE_STRING)
  34.         return strcpy(buff, "string");
  35.     else
  36.         return strcpy(buff, "unknown");
  37. }
  38.  
  39. /*
  40.     This function writes a string describing the accessability-flags of the cvar into buff and returns a pointer to buff. 
  41. */
  42. char* CVar::getFlagsStr(char* buff){
  43. /*
  44.     buff[0]= (flags & CON_FLAG_SERVER_READ) ? 'r' : '-';
  45.     buff[1]= (flags & CON_FLAG_SERVER_WRITE) ? 'w' : '-';
  46.     buff[2]= (flags & CON_FLAG_SERVER_EXECUTE) ? 'x' : '-';
  47.     buff[3]= '|';
  48.     buff[4]= (flags & CON_FLAG_CLIENT_READ) ? 'r' : '-';
  49.     buff[5]= (flags & CON_FLAG_CLIENT_WRITE) ? 'w' : '-';
  50.     buff[6]= (flags & CON_FLAG_CLIENT_EXECUTE) ? 'x' : '-';
  51.     buff[7]= '\0';
  52. */
  53.     buff[0] = '\0';
  54.     strcat(buff, "[");
  55.     if( (flags & CON_FLAG_SYSTEM) == CON_FLAG_SYSTEM )
  56.         strcat(buff, "s");
  57.     else
  58.         strcat(buff, "-");
  59.  
  60.     if( (flags & CON_FLAG_USER) == CON_FLAG_USER )
  61.         strcat(buff, "u");
  62.     else
  63.         strcat(buff, "-");
  64.  
  65.     if( (flags & CON_FLAG_READ_ONLY) == CON_FLAG_READ_ONLY )
  66.         strcat(buff, "r");
  67.     else
  68.         strcat(buff, "-");
  69.  
  70.     strcat(buff, "]");
  71.  
  72.     return buff;
  73. }
  74.  
  75.  
  76.  
  77. /*
  78.     This Constructor creates a pure cvar named name, with initial value val.
  79.     It doesn't reflect a real var.
  80. */
  81. CVarBool::CVarBool(const char* name, bool val):CVar(name){
  82.     this->type=CON_CVAR_TYPE_BOOL;
  83.     this->val=val;
  84.     this->var=NULL;
  85.     this->minVal=0;
  86.     this->maxVal=1;
  87. }
  88.  
  89. /*
  90.     This Constructor creates a cvar named name and binds it to the variable var.
  91. */
  92. CVarBool::CVarBool(const char* name, bool* var, bool instantUpdate):CVar(name){
  93.     this->type=CON_CVAR_TYPE_BOOL;
  94.     this->var=var;
  95.  
  96.  
  97.     this->val=*var;
  98.     this->instantUpdate=instantUpdate;
  99.     this->minVal=0;
  100.     this->maxVal=1;
  101. }
  102.  
  103. /*
  104.     This function takes a string and sets the cvar to the value contained in the string (if valid)
  105. */
  106. void CVarBool::setValStr(const char* valStr){
  107.     int tmp;
  108.     int check=sscanf(valStr, "%i", &tmp);
  109.  
  110.     if(check==0  || check==EOF || tmp<(int)minVal || tmp>(int)maxVal)        // buff was not a valid str for this cvar
  111.         return;
  112.  
  113. //    val=(bool)tmp;        // set local var
  114.     val = (tmp!=0);        // set local var
  115.  
  116.     if(instantUpdate)    // and the real variable if instantUpdate is true
  117.         updateVar();
  118.  
  119. }
  120.  
  121. /*
  122.     This function writes the value of the local var in buff and return a pointer to buff
  123. */
  124. char* CVarBool::getValStr(char* buff){
  125.     sprintf(buff, "%i", val);
  126.     return buff;
  127. }
  128.  
  129. /*
  130.     This function writes the value of the referenced var in buff and return a pointer to buff
  131. */
  132. char* CVarBool::getVarValStr(char* buff){
  133.     if(var!=NULL)
  134.         sprintf(buff, "%i", *var);
  135.     else
  136.         getValStr(buff);    // THINKABOUTME
  137.  
  138.     return buff;
  139. }
  140.  
  141. /*
  142.     Tests if the cvar can be set to the value in valStr
  143. */
  144. bool CVarBool::valStrIsValid(const char* valStr){
  145.     int tmp;
  146.     int check=sscanf(valStr, "%i", &tmp);
  147.  
  148.     return !(check==0  || check==EOF || tmp<(int)minVal || tmp>(int)maxVal);        // buff was not a valid str for this cvar
  149. }
  150.  
  151. /*
  152.     Cycles through all valid values of the cvar
  153. */
  154. bool CVarBool::toggle(){
  155.     val=!val;
  156.     if(instantUpdate)
  157.         updateVar();
  158.  
  159.     return true;
  160. }
  161.  
  162. bool CVarBool::updateVar(){
  163.     if(var!=NULL)
  164.         *var=val;
  165.     return true;
  166. }
  167.  
  168. /*
  169.     Sets the min/max value of the cvar.
  170.     The actual value of the cvar is untouched!!
  171. */
  172. void CVarBool::setValRange(bool min, bool max){
  173.     this->minVal=min;
  174.     this->maxVal=max;
  175. }
  176.  
  177. char* CVarBool::getValRangeStr(char* buff){
  178.     sprintf(buff, "%i - %i", minVal, maxVal);
  179.  
  180.     return buff;
  181. }
  182.  
  183. void CVarBool::setVal(bool newVal){
  184.     val = newVal;
  185.  
  186.     if(instantUpdate)
  187.         updateVar();
  188. }
  189.  
  190. bool CVarBool::getVal(){
  191.     return val;
  192. }
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. /*
  200.     This Constructor creates a pure cvar named name, with initial value val.
  201.     It doesn't reflect a real var.
  202. */
  203. CVarInt::CVarInt(const char* name, int val):CVar(name){
  204.     this->type=CON_CVAR_TYPE_INT;
  205.     this->val=val;
  206.  
  207.     this->var=NULL;
  208.     this->minVal=INT_MIN;
  209.     this->maxVal=INT_MAX;
  210. }
  211.  
  212. /*
  213.     This Constructor creates a cvar named name and binds it to the variable var.
  214. */
  215. CVarInt::CVarInt(const char* name, int* var, bool instantUpdate):CVar(name){
  216.     this->type=CON_CVAR_TYPE_INT;
  217.     this->var=var;
  218.     this->val=*var;
  219.     this->instantUpdate=instantUpdate;
  220.     this->minVal=INT_MIN;
  221.     this->maxVal=INT_MAX;
  222. }
  223.  
  224. /*
  225.     This function takes a string and sets the cvar to the value contained in the string (if valid)
  226. */
  227. void CVarInt::setValStr(const char* valStr){
  228.     int tmp;
  229.     int check=sscanf(valStr, "%i", &tmp);
  230.  
  231.     if(check==0  || check==EOF || tmp<minVal || tmp>maxVal)        // buff was not a valid str for this cvar
  232.         return;
  233.  
  234.     val=tmp;
  235.  
  236.     if(instantUpdate)
  237.         updateVar();
  238. }
  239.  
  240. /*
  241.     This function writes the value of the local var in buff and return a pointer to buff
  242. */
  243. char* CVarInt::getValStr(char* buff){
  244.     sprintf(buff, "%i", val);
  245.     
  246.     return buff;
  247. }
  248.  
  249. /*
  250.     This function writes the value of the referenced var in buff and return a pointer to buff
  251. */
  252. char* CVarInt::getVarValStr(char* buff){
  253.     if(var!=NULL)
  254.         sprintf(buff, "%i", *var);
  255.     else
  256.         getValStr(buff);    // THINKABOUTME
  257.     
  258.     return buff;
  259. }
  260.  
  261. /*
  262.     Tests if the cvar can be set to the value in valStr
  263. */
  264. bool CVarInt::valStrIsValid(const char* valStr){
  265.     int tmp;
  266.     int check=sscanf(valStr, "%i", &tmp);
  267.  
  268.     return !(check==0  || check==EOF || tmp<minVal || tmp>maxVal);        // buff was not a valid str for this cvar
  269. }
  270.  
  271. /*
  272.     Cycles through all valid values of the cvar
  273. */
  274. bool CVarInt::toggle(){
  275.     val=val+1 > maxVal ? minVal : val+1;
  276.  
  277.     if(instantUpdate)
  278.         updateVar();
  279.  
  280.     return true;
  281. }
  282.  
  283. bool CVarInt::updateVar(){
  284.     *var=val;
  285.     return true;
  286. }
  287.  
  288. /*
  289.     Sets the min/max value of the cvar.
  290.     The actual value of the cvar is untouched!!
  291. */
  292. void CVarInt::setValRange(int min, int max){
  293.     this->minVal=min;
  294.     this->maxVal=max;
  295. }
  296.  
  297. char* CVarInt::getValRangeStr(char* buff){
  298.     sprintf(buff, "%i - %i", minVal, maxVal);
  299.  
  300.     return buff;
  301. }
  302.  
  303. void CVarInt::setVal(int newVal){
  304.     val = newVal;
  305.  
  306.     if(instantUpdate)
  307.         updateVar();
  308. }
  309.  
  310. int CVarInt::getVal(){
  311.     return val;
  312. }
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319. /*
  320.     This Constructor creates a pure cvar named name, with initial value val.
  321.  
  322.     It doesn't reflect a real var.
  323. */
  324. CVarReal::CVarReal(const char* name, float val):CVar(name){
  325.     this->type=CON_CVAR_TYPE_REAL;
  326.     this->val=val;
  327.     this->var=NULL;
  328.     this->minVal=FLT_MIN;
  329.     this->maxVal=FLT_MAX;
  330. }
  331.  
  332. /*
  333.     This Constructor creates a cvar named name and binds it to the variable var.
  334. */
  335. CVarReal::CVarReal(const char* name, float* var, bool instantUpdate):CVar(name){
  336.     this->type=CON_CVAR_TYPE_REAL;
  337.     this->var=var;
  338.     this->val=*var;
  339.     this->instantUpdate=instantUpdate;
  340.     this->minVal=FLT_MIN;
  341.     this->maxVal=FLT_MAX;
  342. }
  343.  
  344. /*
  345.     This function takes a string and sets the cvar to the value contained in the string (if valid)
  346. */
  347. void CVarReal::setValStr(const char* valStr){
  348.     float tmp;
  349.     int check=sscanf(valStr, "%f", &tmp);
  350.  
  351.  
  352.     if(check==0  || check==EOF || tmp<minVal || tmp>maxVal)        // buff was not a valid str for this cvar
  353.         return;
  354.  
  355.     val=tmp;
  356.  
  357.     if(instantUpdate)
  358.         updateVar();
  359. }
  360.  
  361. /*
  362.     This function writes the value of the local var in buff and return a pointer to buff
  363. */
  364. char* CVarReal::getValStr(char* buff){
  365.     sprintf(buff, "%.2f", val);
  366.     return buff;
  367. }
  368.  
  369. /*
  370.     This function writes the value of the referenced var in buff and return a pointer to buff
  371. */
  372. char* CVarReal::getVarValStr(char* buff){
  373.     if(var!=NULL)
  374.         sprintf(buff, "%.2f", *var);
  375.     else
  376.         getValStr(buff);    // THINKABOUTME
  377.  
  378.     return buff;
  379. }
  380.  
  381. /*
  382.     Tests if the cvar can be set to the value in valStr
  383. */
  384. bool CVarReal::valStrIsValid(const char* valStr){
  385.     float tmp;
  386.     int check=sscanf(valStr, "%f", &tmp);
  387.  
  388.     return !(check==0  || check==EOF || tmp<minVal || tmp>maxVal);        // buff was not a valid str for this cvar
  389. }
  390.  
  391. /*
  392.     Cycles through all valid values of the cvar
  393. */
  394. bool CVarReal::toggle(){
  395.     val=val+1 > maxVal ? minVal : val+1;;
  396.     
  397.     if(instantUpdate)
  398.         updateVar();
  399.  
  400.     return true;
  401. }
  402.  
  403. bool CVarReal::updateVar(){
  404.     *var=val;
  405.     return true;
  406. }
  407.  
  408. /*
  409.     Sets the min/max value of the cvar.
  410.     The actual value of the cvar is untouched!!
  411. */
  412. void CVarReal::setValRange(float min, float max){
  413.     this->minVal=min;
  414.     this->maxVal=max;
  415. }
  416.  
  417. char* CVarReal::getValRangeStr(char* buff){
  418.     sprintf(buff, "%.2f - %.2f", minVal, maxVal);
  419.  
  420.     return buff;
  421. }
  422.  
  423. void CVarReal::setVal(float newVal){
  424.     val = newVal;
  425.  
  426.     if(instantUpdate)
  427.         updateVar();
  428.  
  429. }
  430.  
  431. float CVarReal::getVal(){
  432.     return val;
  433. }
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440. /*
  441.     This Constructor creates a pure cvar named name, with initial value val.
  442.     It doesn't reflect a real var.
  443. */
  444. CVarString::CVarString(const char* name, const char* val):CVar(name){
  445.  
  446.     this->type=CON_CVAR_TYPE_STRING;
  447.     /*this->val=*/strncpy(this->val, val, CON_MAX_STRING_LENGTH-1);
  448.     this->val[CON_MAX_STRING_LENGTH-1]='\0';
  449.     this->var=NULL;
  450.     this->minLength=0;
  451.     this->maxLength=CON_MAX_STRING_LENGTH-1;
  452. }
  453.  
  454. /*
  455.     This Constructor creates a cvar named name and binds it to the variable var.
  456. */
  457. CVarString::CVarString(const char* name, char** var, bool instantUpdate):CVar(name){
  458.     this->type=CON_CVAR_TYPE_STRING;
  459.     this->var=var;
  460.     /*this->val=*/strncpy(this->val, *var, CON_MAX_STRING_LENGTH-1);
  461.     this->val[CON_MAX_STRING_LENGTH-1]='\0';
  462.     this->instantUpdate=instantUpdate;
  463.     this->minLength=0;
  464.     this->maxLength=CON_MAX_STRING_LENGTH-1;
  465. }
  466.  
  467. /*
  468.     This function takes a string and sets the cvar to the value contained in the string (if valid)
  469. */
  470. void CVarString::setValStr(const char* valStr){
  471.     if((int)strlen(valStr)<minLength || (int)strlen(valStr)>maxLength)        // buff was not a valid str for this cvar
  472.         return;
  473.  
  474.     strcpy(val, valStr);        // and our local val as well
  475.  
  476.     if(instantUpdate)
  477.         updateVar();
  478. }
  479.  
  480. /*
  481.     This function writes the value of the local var in buff and return a pointer to buff
  482. */
  483. char* CVarString::getValStr(char* buff){
  484.     strcpy(buff, val);
  485.     return buff;
  486. }
  487.  
  488. /*
  489.     This function writes the value of the referenced var in buff and return a pointer to buff
  490. */
  491. char* CVarString::getVarValStr(char* buff){
  492.     if(var!=NULL)
  493.         strcpy(buff, *var);
  494.     else
  495.         getValStr(buff);    // THINKABOUTME
  496.  
  497.     return buff;
  498. }
  499.  
  500. /*
  501.     Tests if the cvar can be set to the value in valStr
  502. */
  503. bool CVarString::valStrIsValid(const char* valStr){
  504.     return !((int)strlen(valStr)<minLength || (int)strlen(valStr)>maxLength);
  505. }
  506.  
  507. /*
  508.     Cycles through all valid values of the cvar
  509. */
  510. bool CVarString::toggle(){
  511.     return false;    // string cvars cannot be toggled
  512. }
  513.  
  514. bool CVarString::updateVar(){
  515.     strncpy(*var, val, strlen(val)+1);            // THINKABOUTME: val must fit into *var!!!!
  516.     return true;
  517. }
  518. /*
  519.     Sets the min/max value of the cvar.
  520.     The actual value of the cvar is untouched!!
  521. */
  522. void CVarString::setValRange(int min, int max){
  523.     this->minLength=min;
  524.     this->maxLength=max;
  525. }
  526.  
  527. char* CVarString::getValRangeStr(char* buff){
  528.     sprintf(buff, "%i - %i chars", minLength, maxLength);
  529.  
  530.     return buff;
  531. }
  532.  
  533. char* CVarString::getVal(){
  534.     return val;
  535. }
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542.  
  543. /*
  544.     This Constructor creates a pure cvar named name, with initial value val.
  545.     It doesn't reflect a real var.
  546. */
  547. CVarRealVector::CVarRealVector(const char* name, float* val, int dimension):CVar(name){
  548.     this->type=CON_CVAR_TYPE_REAL_VECTOR;
  549.     this->dim=dimension;
  550.     this->val=new float[dim];
  551.     for(int i=0;i<dim;i++)
  552.         this->val[i]=val[i];
  553.  
  554.     this->var=NULL;
  555.     this->minVal=FLT_MIN;
  556.     this->maxVal=FLT_MAX;
  557. }
  558.  
  559. /*
  560.     This Constructor creates a cvar named name and binds it to the variable var.
  561. */
  562. CVarRealVector::CVarRealVector(const char* name, float** var, int dimension, bool instantUpdate):CVar(name){
  563.     this->type=CON_CVAR_TYPE_REAL_VECTOR;
  564.  
  565.     this->dim=dimension;
  566.     this->var=var;
  567.     this->val=new float[dim];
  568.     for(int i=0;i<dim;i++)
  569.         this->val[i]=(*var)[i];
  570.  
  571.     this->minVal=FLT_MIN;
  572.     this->maxVal=FLT_MAX;
  573.  
  574.     this->instantUpdate=instantUpdate;
  575. }
  576.  
  577. /*
  578.     This function takes a string and sets the cvar to the value contained in the string (if valid)
  579. */
  580. void CVarRealVector::setValStr(const char* valStr){
  581.     int i;
  582.     float* tmp=new float[dim];
  583.     
  584.     Tokenizer t(valStr, " ;,", "");
  585.  
  586.     if(t.tokc!=dim){
  587.         delete[] tmp;
  588.         return;
  589.     }
  590.  
  591.     for(i=0;i<t.tokc;i++){
  592.         tmp[i]=(float)atof(t.tokv[i]);
  593.         if(tmp[i]<minVal || tmp[i]>maxVal){
  594.             delete[] tmp;
  595.             return;
  596.         }
  597.     }
  598.  
  599.     for(i=0;i<dim;i++)
  600.         val[i]=tmp[i];
  601.  
  602.     if(instantUpdate)
  603.         updateVar();
  604.  
  605.     delete[] tmp;
  606. }
  607.  
  608. /*
  609.     This function writes the value of the local var in buff and return a pointer to buff
  610. */
  611. char* CVarRealVector::getValStr(char* buff){
  612.     int i;
  613.     char* p=buff;
  614.  
  615.     for(i=0;i<dim;i++){
  616.         p += sprintf(p, "%.2f ", val[i]);
  617.         
  618.     }
  619.  
  620.     int lastChar = strlen(buff) > 0 ? strlen(buff)-1 : 0;
  621.     buff[lastChar] = '\0';
  622.  
  623.     return buff;
  624. }
  625.  
  626. /*
  627.     This function writes the value of the referenced var in buff and return a pointer to buff
  628. */
  629. char* CVarRealVector::getVarValStr(char* buff){
  630.     int i;
  631.     char* p=buff;
  632.  
  633.     if(var!=NULL){
  634.         for(i=0;i<dim;i++){
  635.             int b = sprintf(p, "%.2f ", (*var)[i]);
  636.             p+=b;
  637.         }
  638.         int lastChar = strlen(buff) > 0 ? strlen(buff)-1 : 0;
  639.         buff[lastChar] = '\0';
  640.     }else{
  641.         getValStr(buff);    // THINKABOUTME
  642.     }
  643.     
  644.     return buff;
  645. }
  646.  
  647. /*
  648.     Tests if the cvar can be set to the value in valStr
  649. */
  650. bool CVarRealVector::valStrIsValid(const char* valStr){
  651.     int i;
  652.     float* tmp=new float[dim];
  653.     
  654.     Tokenizer t(valStr, " ;,", "");
  655.  
  656.     if(t.tokc!=dim){
  657.         delete[] tmp;
  658.         return false;
  659.     }
  660.  
  661.     for(i=0;i<t.tokc;i++){
  662.         tmp[i]=(float)atof(t.tokv[i]);
  663.         if(tmp[i]<minVal || tmp[i]>maxVal){
  664.             delete[] tmp;
  665.             return false;
  666.         }
  667.     }
  668.  
  669.     delete[] tmp;
  670.     return true;
  671. }
  672.  
  673. /*
  674.     Cycles through all valid values of the cvar
  675. */
  676. bool CVarRealVector::toggle(){
  677.     return false;    // string cvars cannot be toggled
  678. }
  679.  
  680. bool CVarRealVector::updateVar(){
  681. //    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]);
  682.     for(int i=0;i<dim;i++)
  683.         (*var)[i]=val[i];
  684.  
  685.     return true;
  686. }
  687. /*
  688.     Sets the min/max value of the cvar.
  689.     The actual value of the cvar is untouched!!
  690. */
  691. void CVarRealVector::setValRange(float min, float max){
  692.     this->minVal=min;
  693.     this->maxVal=max;
  694. }
  695.  
  696. char* CVarRealVector::getValRangeStr(char* buff){
  697.     sprintf(buff, "%.2f - %.2f chars", minVal, maxVal);
  698.  
  699.     return buff;
  700. }
  701.  
  702.