constants.c
Go to the documentation of this file.
1 #include "constants.h"
2 
3 #include "error.h"
4 #include "boolean.h"
5 #include "defines.h"
6 
7 #include <stdlib.h>
8 #include <string.h>
9 
18 /*
19  * Init the set of constants.
20  */
22 {
24 
25  NEW(cts->cts,cts->max_constants,Tconstant);
26 
27  cts->n=0;
28 }
29 
30 void CopyConstants(Tconstants *cts_dst,Tconstants *cts_src)
31 {
32  unsigned int i;
33 
34  cts_dst->max_constants=cts_src->max_constants;
35  NEW(cts_dst->cts,cts_dst->max_constants,Tconstant);
36  cts_dst->n=0;
37  for(i=0;i<cts_dst->n;i++)
38  AddConstant(cts_src->cts[i].name,cts_src->cts[i].vs,cts_dst);
39 }
40 
41 
43 {
44  unsigned int i;
45 
46  CopyConstants(cout,c1);
47 
48  for(i=0;i<c2->n;i++)
49  AddConstant(c2->cts[i].name,c2->cts[i].vs,cout);
50 }
51 
52 /*
53  * Returns the number of constants includes in the set 'vs'.
54  */
55 unsigned int NConstants(Tconstants *cts)
56 {
57  return(cts->n);
58 }
59 
60 /*
61  * Adds a new constant with name 'name' value 'v' and degrees flag 'd' in the set 'vs'
62  * NOTE: It does not test whether or not there is another constant with the same name!!!!
63  * This must be tested before using AddConstant calling the function 'GetConstantWithName'
64  */
65 unsigned int AddConstant(char *name,double v,Tconstants *cts)
66 {
67  unsigned int num;
68 
69  if (cts->n==cts->max_constants)
71 
72  num=cts->n;
73 
74  NEW(cts->cts[num].name,strlen(name)+1,char);
75  strcpy(cts->cts[num].name,name);
76  cts->cts[num].vs=v;
77 
78  cts->n++;
79 
80  return(num);
81 }
82 
83 /*
84  * Returns the identifier of the constant with name 'name' in the set 'vs'.
85  * If there is no constant with that name, it returns (-1).
86  * This function can be used to test if a constant with a given name has been already defined.
87  */
88 unsigned int GetConstantWithName(char *name,Tconstants *cts)
89 {
90  boolean found;
91  unsigned int i;
92 
93  found=FALSE;
94  i=0;
95  while((!found)&&(i<cts->n))
96  {
97  if (strcmp(name,cts->cts[i].name)==0)
98  found=TRUE;
99  else
100  i++;
101  }
102 
103  if (found)
104  return(i);
105  else
106  return(NO_UINT);
107 }
108 
109 /*
110  * Returns the value of constant number 'n' in the set 'vs'.
111  * If there is no constant number 'n' it issues an error.
112  */
113 double GetConstantValue(unsigned int n,Tconstants *cts)
114 {
115  if (n<cts->n)
116  return(cts->cts[n].vs);
117  else
118  Error("Undefined constant referenced in function GetConstant");
119  return(0.0);
120 }
121 
122 /*
123  * Prints the information of constant set 'vs' (Identifier and name for all constants)
124  * on file 'f'.
125  */
126 void PrintConstants(FILE *f,Tconstants *cts)
127 {
128  unsigned int i;
129 
130  for(i=0;i<cts->n;i++)
131  fprintf(f,"%3u %s=%f\n",i,cts->cts[i].name,cts->cts[i].vs);
132 }
133 
134 /*
135  * Deletes the constant set 'vs'.
136  */
138 {
139  unsigned int i;
140 
141  for(i=0;i<cts->n;i++)
142  free(cts->cts[i].name);
143  free(cts->cts);
144 }
Definition of the boolean type.
void InitConstants(Tconstants *cts)
Initializes a constant set.
Definition: constants.c:21
#define FALSE
FALSE.
Definition: boolean.h:30
double GetConstantValue(unsigned int n, Tconstants *cts)
Retrives a the value of a constant.
Definition: constants.c:113
A table of constants.
Definition: constants.h:53
#define NEW(_var, _n, _type)
Allocates memory space.
Definition: defines.h:385
void PrintConstants(FILE *f, Tconstants *cts)
Prints a set of constants.
Definition: constants.c:126
Definition of a table of Tconstants.
#define TRUE
TRUE.
Definition: boolean.h:21
void Error(const char *s)
General error function.
Definition: error.c:80
void CopyConstants(Tconstants *cts_dst, Tconstants *cts_src)
Copies a set of constants.
Definition: constants.c:30
Error and warning functions.
Tconstant * cts
Definition: constants.h:56
Definitions of constants and macros used in several parts of the cuik library.
unsigned int NConstants(Tconstants *cts)
Number of constants.
Definition: constants.c:55
char * name
Definition: constants.h:37
unsigned int AddConstant(char *name, double v, Tconstants *cts)
Add a constant.
Definition: constants.c:65
double vs
Definition: constants.h:38
#define INIT_NUM_CONSTANTS
Initial room for constants.
Definition: constants.h:26
unsigned int max_constants
Definition: constants.h:54
void MergeConstants(Tconstants *c1, Tconstants *c2, Tconstants *cout)
Fuses constant sets.
Definition: constants.c:42
#define MEM_DUP(_var, _n, _type)
Duplicates a previously allocated memory space.
Definition: defines.h:414
#define NO_UINT
Used to denote an identifier that has not been initialized.
Definition: defines.h:435
unsigned int GetConstantWithName(char *name, Tconstants *cts)
Retrives a constant from the set.
Definition: constants.c:88
A constant.
Definition: constants.h:36
void DeleteConstants(Tconstants *cts)
Destructor.
Definition: constants.c:137
unsigned int n
Definition: constants.h:55