Institut de Robòtica i Informàtica Industrial
KRD Group

The CuikSuite Project

constants.c

Go to the documentation of this file.
00001 #include "constants.h"
00002 
00003 #include "error.h"
00004 #include "boolean.h"
00005 #include "defines.h"
00006 
00007 #include <stdlib.h>
00008 #include <string.h>
00009 
00018 /*
00019  * Init the set of constants.
00020  */
00021 void InitConstants(Tconstants *cts)
00022 {
00023   cts->max_constants=INIT_NUM_CONSTANTS;
00024 
00025   NEW(cts->cts,cts->max_constants,Tconstant);
00026 
00027   cts->n=0;
00028 }
00029 
00030 /*
00031  * Returns the number of constants includes in the set 'vs'.
00032  */
00033 unsigned int NConstants(Tconstants *cts)
00034 {
00035   return(cts->n);
00036 }
00037 
00038 /*
00039  * Adds a new constant with name 'name' value 'v' and degrees flag 'd' in the set 'vs'
00040  * NOTE: It does not test whether or not there is another constant with the same name!!!!
00041  * This must be tested before using AddConstant calling the function 'GetConstantWithName'
00042  */
00043 unsigned int AddConstant(char *name,double v,Tconstants *cts)
00044 {
00045   unsigned int num;
00046 
00047   if (cts->n==cts->max_constants)
00048     MEM_DUP(cts->cts,cts->max_constants,Tconstant);
00049   
00050   num=cts->n;
00051 
00052   NEW(cts->cts[num].name,strlen(name)+1,char);
00053   strcpy(cts->cts[num].name,name);
00054   cts->cts[num].vs=v;
00055 
00056   cts->n++;
00057 
00058   return(num);
00059 }
00060 
00061 /*
00062  * Returns the identifier of the constant with name 'name' in the set 'vs'.
00063  * If there is no constant with that name, it returns (-1).
00064  * This function can be used to test if a constant with a given name has been already defined.
00065  */
00066 unsigned int GetConstantWithName(char *name,Tconstants *cts)
00067 {
00068   boolean found;
00069   unsigned int i;
00070 
00071   found=FALSE;
00072   i=0;
00073   while((!found)&&(i<cts->n))
00074     {
00075       if (strcmp(name,cts->cts[i].name)==0)
00076         found=TRUE;
00077       else
00078         i++;
00079     }
00080 
00081   if (found)
00082     return(i);
00083   else
00084     return(NO_UINT);
00085 }
00086 
00087 /*
00088  * Returns the value of constant number 'n' in the set 'vs'.
00089  * If there is no constant number 'n' it issues an error.
00090  */
00091 double GetConstantValue(unsigned int n,Tconstants *cts)
00092 {
00093   if (n<cts->n)
00094     return(cts->cts[n].vs);
00095   else
00096     Error("Undefined constant referenced in function GetConstant");
00097   return(0.0);
00098 }
00099 
00100 /*
00101  * Prints the information of constant set 'vs' (Identifier and name for all constants)
00102  * on file 'f'.
00103  */
00104 void PrintConstants(FILE *f,Tconstants *cts)
00105 {
00106   unsigned int i;
00107   
00108   for(i=0;i<cts->n;i++)
00109     fprintf(f,"%3u  %s=%f\n",i,cts->cts[i].name,cts->cts[i].vs);
00110 }
00111 
00112 /*
00113  * Deletes the constant set 'vs'.
00114  */
00115 void DeleteConstants(Tconstants *cts)
00116 {
00117   unsigned int i;
00118 
00119   for(i=0;i<cts->n;i++)
00120     free(cts->cts[i].name);
00121   free(cts->cts);
00122 }