My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
object.cpp
Go to the documentation of this file.
1 /*
2  * object.cpp - generic object class implementation
3  *
4  * Copyright (C) 2003, 2004, 2005, 2006, 2008 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: object.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 <assert.h>
33 
34 #include "logging.h"
35 #include "complex.h"
36 #include "property.h"
37 #include "object.h"
38 #include "variable.h"
39 
40 
41 // Constructor creates an unnamed instance of the object class.
43  name = NULL;
44  prev = next = NULL;
45  prop = NULL;
46  ptxt = NULL;
47 }
48 
49 // This constructor creates a named instance of the object class.
50 object::object (const char * n) {
51  name = strdup (n);
52  prev = next = NULL;
53  prop = NULL;
54  ptxt = NULL;
55 }
56 
57 /* This copy constructor creates a instance of the object class based
58  on the given object. */
59 object::object (const object & o) {
60  name = o.name ? strdup (o.name) : NULL;
61  next = o.next;
62  prev = o.prev;
63  ptxt = o.ptxt ? strdup (o.ptxt) : NULL;
64  copyProperties (o.prop);
65 }
66 
67 // Destructor deletes an instance of the object class.
69  if (name) free (name);
70  if (ptxt) free (ptxt);
72 }
73 
74 // Sets the name of the object.
75 void object::setName (const char * n) {
76  if (name) free (name);
77  name = n ? strdup (n) : NULL;
78 }
79 
80 // Returns the name of the object.
81 char * object::getName (void) {
82  return name;
83 }
84 
85 // The function adds a complete property to the object property list.
87  p->setNext (prop);
88  prop = p;
89 }
90 
91 /* This function adds a property consisting of a key and a string
92  value to the object. */
93 property * object::addProperty (const char * n, const char * val) {
94  property * p = new property (n, val);
95  addProperty (p);
96  return p;
97 }
98 
99 /* This function sets the specified property consisting of a key and a
100  string value in the object. */
101 void object::setProperty (const char * n, char * val) {
102  property * p = prop->findProperty (n);
103  if (p != NULL)
104  p->set (val);
105  else
106  addProperty (n, val);
107 }
108 
109 /* This function adds a property consisting of a key and a double
110  value to the object. */
111 property * object::addProperty (const char * n, nr_double_t val) {
112  property * p = new property (n, val);
113  addProperty (p);
114  return p;
115 }
116 
117 /* This function sets the specified property consisting of a key and a
118  double value in the object. */
119 void object::setProperty (const char * n, nr_double_t val) {
120  property * p = prop->findProperty (n);
121  if (p != NULL)
122  p->set (val);
123  else
124  addProperty (n, val);
125 }
126 
127 /* Th function sets the specified property consisting of a key and a
128  double value in the object. The property is marked a scalability
129  property. */
130 void object::setScaledProperty (const char * n, nr_double_t val) {
131  char prop[64];
132  sprintf (prop, "Scaled:%s", n);
133  setProperty (prop, val);
134 }
135 
136 /* This function adds a property consisting of a key and a variable
137  value to the object. */
138 property * object::addProperty (const char * n, variable * val) {
139  property * p = new property (n, val);
140  addProperty (p);
141  return p;
142 }
143 
144 /* Returns the requested property value which has been previously
145  added as its vector representation. If there is no such property
146  the function returns NULL. */
148  property * p = prop->findProperty (n);
149  if (p != NULL) return p->getVector ();
150  return NULL;
151 }
152 
153 /* Returns the requested property value which has been previously
154  added as its text representation. If there is no such property the
155  function returns NULL. */
156 char * object::getPropertyString (const char * n) {
157  property * p = prop->findProperty (n);
158  if (p != NULL) return p->getString ();
159  return NULL;
160 }
161 
162 /* Returns the requested property reference variable name. If there
163  is no such property the function returns NULL. */
164 char * object::getPropertyReference (const char * n) {
165  property * p = prop->findProperty (n);
166  if (p != NULL) return p->getReference ();
167  return NULL;
168 }
169 
170 /* Returns the requested property value which has been previously
171  added as its double representation. If there is no such property
172  the function returns zero. */
173 nr_double_t object::getPropertyDouble (const char * n) {
174  property * p = prop->findProperty (n);
175  if (p != NULL) return p->getDouble ();
176  return 0.0;
177 }
178 
179 /* The functions returns the requested (scalability) property value
180  which has been previously added. If there is no such scaled
181  property the function returns the standard property or zero. */
182 nr_double_t object::getScaledProperty (const char * n) {
183  char txt[64];
184  sprintf (txt, "Scaled:%s", n);
185  // try to find scaled property
186  property * p = prop->findProperty (txt);
187  if (p != NULL) return p->getDouble ();
188  // default to standard property
189  return getPropertyDouble (n);
190 }
191 
192 /* Returns the requested property value which has been previously
193  added as its integer representation. If there is no such property
194  the function returns zero. */
195 int object::getPropertyInteger (const char * n) {
196  property * p = prop->findProperty (n);
197  if (p != NULL) return p->getInteger ();
198  return 0;
199 }
200 
201 /* The function checks whether the object has got a certain property
202  value. If so it returns non-zero, otherwise it returns zero. */
203 bool object::hasProperty (const char * n) {
204  return (prop && prop->findProperty (n)) ? true : false;
205 }
206 
207 /* The function checks whether the object has got a certain property
208  value and if this has its default value. If so it returns non-zero,
209  otherwise it returns zero. */
210 bool object::isPropertyGiven (const char * n) {
211  if (prop != NULL) {
212  property * p = prop->findProperty (n);
213  if (p != NULL && !p->isDefault ()) return true;
214  }
215  return false;
216 }
217 
218 /* This function copies all properties in the given property list into
219  an object. */
221  prop = NULL;
222  while (org != NULL) {
223  addProperty (new property (*org));
224  org = org->getNext ();
225  }
226 }
227 
228 // Deletes all properties of an object.
230  property * n;
231  for (property * p = prop; p != NULL; p = n) {
232  n = p->getNext ();
233  delete p;
234  }
235  prop = NULL;
236 }
237 
238 // The function returns the number of properties in the object.
240  int res = 0;
241  for (property * p = prop; p != NULL; p = p->getNext ()) res++;
242  return res;
243 }
244 
245 // This function returns a text representation of the objects properties.
246 char * object::propertyList (void) {
247  if (ptxt) free (ptxt);
248  int len = countProperties () + 1;
249  ptxt = (char *) malloc (len); ptxt[0] = '\0';
250  for (property * p = prop; p != NULL; p = p->getNext ()) {
251  char * n = p->getName ();
252  char * val = p->toString ();
253  char * text = (char *) malloc (strlen (n) + strlen (val) + 4);
254  sprintf (text, "%s=\"%s\"", n, val);
255  len += strlen (text);
256  ptxt = (char *) realloc (ptxt, len);
257  strcat (ptxt, text);
258  if (p->getNext () != NULL) strcat (ptxt, " ");
259  free (text);
260  }
261  return ptxt;
262 }