Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
variable.cpp
Go to the documentation of this file.
1 /*
2  * variable.cpp - generic variable class implementation
3  *
4  * Copyright (C) 2004, 2007 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$
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 
33 #include "logging.h"
34 #include "equation.h"
36 #include "analysis.h"
37 #include "variable.h"
38 
39 namespace qucs {
40 
41 // Constructor creates an unnamed instance of the variable class.
43  name = NULL;
44  text = NULL;
45  next = NULL;
46  type = VAR_UNKNOWN;
47  pass = true;
48 }
49 
50 // This constructor creates a named instance of the variable class.
52  name = n ? strdup (n) : NULL;
53  text = NULL;
54  next = NULL;
55  type = VAR_UNKNOWN;
56  pass = true;
57 }
58 
59 /* This copy constructor creates a instance of the variable class based
60  on the given variable. */
62  name = o.name ? strdup (o.name) : NULL;
63  text = o.text ? strdup (o.text) : NULL;
64  type = o.type;
65  next = o.next;
66  pass = o.pass;
67  value = o.value;
68 }
69 
72  if (name) free (name);
73  if (text) free (text);
74 }
75 
76 // Sets the name of the variable.
77 void variable::setName (char * n) {
78  if (name) free (name);
79  name = n ? strdup (n) : NULL;
80 }
81 
82 // Returns the name of the variable.
83 char * variable::getName (void) {
84  return name;
85 }
86 
87 // Creates textual representation of a variable.
88 char * variable::toString (void) {
89  char * str = NULL;
90  char * val = NULL;
91  if (text) { free (text); text = NULL; }
92  switch (type) {
93  case VAR_UNKNOWN:
94  text = strdup ("variable");
95  break;
96  case VAR_CONSTANT:
97  str = value.c->toString ();
98  text = (char *) malloc (strlen (str) + 11);
99  sprintf (text, "constant: %s", str);
100  break;
101  case VAR_VALUE:
102  str = value.v->toString ();
103  text = (char *) malloc (strlen (str) + 8);
104  sprintf (text, "value: %s", str);
105  break;
106  case VAR_REFERENCE:
107  str = value.r->toString ();
108  val = value.r->getResult()->toString ();
109  text = (char *) malloc (strlen (str) + strlen (val) + 15);
110  sprintf (text, "reference: %s = %s", str, val);
111  break;
112  case VAR_SUBSTRATE:
113  str = value.s->getName ();
114  text = (char *) malloc (strlen (str) + 12);
115  sprintf (text, "substrate: %s", str);
116  break;
117  case VAR_ANALYSIS:
118  str = value.a->getName ();
119  text = (char *) malloc (strlen (str) + 11);
120  sprintf (text, "analysis: %s", str);
121  break;
122  default:
123  text = strdup ("?variable?");
124  break;
125  }
126  return text;
127 }
128 
129 } // namespace qucs
eqn::constant * c
Definition: variable.h:88
n
Definition: parse_citi.y:147
char * text
Definition: variable.h:84
eqn::reference * r
Definition: variable.h:89
char * toString(void)
Definition: variable.cpp:88
variable * next
Definition: variable.h:94
char * name
Definition: variable.h:83
free($1)
char * getName(void)
Definition: variable.cpp:83
The analysis class header file.
union qucs::variable::value_t value
virtual ~variable()
Destructor deletes an instance of the variable class.
Definition: variable.cpp:71
char * getName(void)
Definition: object.cpp:84
void setName(char *)
Definition: variable.cpp:77
eqn::constant * v
Definition: variable.h:91