My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
property.cpp
Go to the documentation of this file.
1 /*
2  * property.cpp - generic property class implementation
3  *
4  * Copyright (C) 2003-2009 Stefan Jahn <stefan@lkcc.org>
5  *
6  * This is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This software is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this package; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  * $Id: property.cpp 1825 2011-03-11 20:42:14Z ela $
22  *
23  */
24 
25 #if HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <math.h>
34 
35 #include "complex.h"
36 #include "variable.h"
37 #include "property.h"
38 
39 // Constructor creates an unnamed instance of the property class.
41  type = PROPERTY_UNKNOWN;
42  name = NULL;
43  value = 0.0;
44  str = NULL;
45  txt = NULL;
46  var = NULL;
47  next = NULL;
48  def = false;
49 }
50 
51 // Constructor creates a named instance of the property class.
52 property::property (const char * n) {
53  type = PROPERTY_UNKNOWN;
54  name = n ? strdup (n) : NULL;
55  value = 0.0;
56  str = NULL;
57  txt = NULL;
58  var = NULL;
59  next = NULL;
60  def = false;
61 }
62 
63 /* This full qualified constructor creates an instance of the property
64  class containing both the key and the value of the property. */
65 property::property (const char * n, const char * val) {
66  type = PROPERTY_STR;
67  name = n ? strdup (n) : NULL;
68  str = val ? strdup (val) : NULL;
69  value = 0.0;
70  txt = NULL;
71  var = NULL;
72  next = NULL;
73  def = false;
74 }
75 
76 /* This full qualified constructor creates an instance of the property
77  class containing both the key and the value of the property. */
78 property::property (const char * n, nr_double_t val) {
79  type = PROPERTY_DOUBLE;
80  name = n ? strdup (n) : NULL;
81  value = val;
82  str = NULL;
83  txt = NULL;
84  var = NULL;
85  next = NULL;
86  def = false;
87 }
88 
89 /* This full qualified constructor creates an instance of the property
90  class containing both the key and the value of the property. */
91 property::property (const char * n, variable * val) {
92  type = PROPERTY_VAR;
93  name = n ? strdup (n) : NULL;
94  var = val;
95  value = 0.0;
96  txt = NULL;
97  str = NULL;
98  next = NULL;
99  def = false;
100 }
101 
102 /* The copy constructor creates a new instance of the property class
103  based on the given property object. */
105  type = p.type;
106  name = p.name ? strdup (p.name) : NULL;
107  str = p.str ? strdup (p.str) : NULL;
108  value = p.value;
109  txt = p.txt ? strdup (p.txt) : NULL;
110  next = p.next;
111  var = p.var;
112  def = p.def;
113 }
114 
115 // Destructor deletes the property object.
117 #if 0 /* FIXME: do this at another code location */
118  if (type == PROPERTY_VAR) {
119  constant * c = var->getConstant ();
120  if (c->getType () == TAG_VECTOR) {
121  delete c;
122  delete var;
123  }
124  }
125 #endif
126  if (name) free (name);
127  if (str) free (str);
128  if (txt) free (txt);
129 }
130 
131 // Sets the name of the property.
132 void property::setName (char * n) {
133  if (name) free (name);
134  name = n ? strdup (n) : NULL;
135 }
136 
137 // Returns the name of the property.
138 char * property::getName (void) {
139  return name;
140 }
141 
142 /* Goes through the chained list of the properties and looks for a
143  property matching the given key and returns its value if possible.
144  If there is no such property the function returns NULL. */
145 property * property::findProperty (const char * n) {
146  for (property * p = this; p != NULL; p = p->getNext ()) {
147  if (!strcmp (p->getName (), n)) return p;
148  }
149  return NULL;
150 }
151 
152 // Short macro in order to obtain the correct constant value.
153 #define D(con) ((constant *) (con))->d
154 #define S(con) ((constant *) (con))->s
155 #define V(con) ((constant *) (con))->v
156 
157 // Returns the property's value as vector.
159  if (var != NULL) {
160  if (var->getType () == VAR_CONSTANT)
161  return V (var->getConstant ());
162  else if (var->getType () == VAR_REFERENCE)
163  return V (var->getReference()->getResult ());
164  }
165  return NULL;
166 }
167 
168 // Returns the property's value as string.
169 char * property::getString (void) {
170  if (var != NULL) return S (var->getConstant ());
171  return str;
172 }
173 
174 // Returns the property's reference if it is a variable.
175 char * property::getReference (void) {
176  if (var != NULL) return var->getName ();
177  return str;
178 }
179 
180 // Returns the property's value as double.
181 nr_double_t property::getDouble (void) {
182  if (var != NULL) {
183  if (var->getType () == VAR_CONSTANT)
184  return D (var->getConstant ());
185  else if (var->getType () == VAR_REFERENCE)
186  return D (var->getReference()->getResult ());
187  }
188  return value;
189 }
190 
191 // Returns the property's value as integer.
193  if (var != NULL) return (int) floor (D (var->getConstant ()));
194  return (int) floor (value);
195 }
196 
197 // Sets the property's value being a double.
198 void property::set (nr_double_t val) {
199  type = PROPERTY_DOUBLE;
200  value = val;
201 }
202 
203 // Sets the property's value being an integer.
204 void property::set (int val) {
205  type = PROPERTY_INT;
206  value = val;
207 }
208 
209 // Sets the property's value being a variable.
210 void property::set (variable * val) {
211  type = PROPERTY_VAR;
212  var = val;
213 }
214 
215 // Sets the property's value being a string.
216 void property::set (char * val) {
217  type = PROPERTY_STR;
218  if (str) free (str);
219  str = val ? strdup (val) : NULL;
220 }
221 
222 // This function returns a text representation of the property object.
223 char * property::toString (void) {
224  char text[256];
225  if (txt) free (txt);
226  switch (type) {
227  case PROPERTY_UNKNOWN:
228  txt = strdup ("(no such type)");
229  break;
230  case PROPERTY_INT:
231  sprintf (text, "%d", (int) floor (value));
232  txt = strdup (text);
233  break;
234  case PROPERTY_STR:
235  txt = strdup (str);
236  break;
237  case PROPERTY_DOUBLE:
238  sprintf (text, "%g", (double) value);
239  txt = strdup (text);
240  break;
241  case PROPERTY_VAR:
242  sprintf (text, "%s", var->getName ());
243  txt = strdup (text);
244  break;
245  }
246  return txt;
247 }