My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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: variable.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 
33 #include "logging.h"
34 #include "equation.h"
36 #include "analysis.h"
37 #include "variable.h"
38 
39 
40 // Constructor creates an unnamed instance of the variable class.
42  name = NULL;
43  text = NULL;
44  next = NULL;
45  type = VAR_UNKNOWN;
46  pass = true;
47 }
48 
49 // This constructor creates a named instance of the variable class.
51  name = n ? strdup (n) : NULL;
52  text = NULL;
53  next = NULL;
54  type = VAR_UNKNOWN;
55  pass = true;
56 }
57 
58 /* This copy constructor creates a instance of the variable class based
59  on the given variable. */
61  name = o.name ? strdup (o.name) : NULL;
62  text = o.text ? strdup (o.text) : NULL;
63  type = o.type;
64  next = o.next;
65  pass = o.pass;
66  value = o.value;
67 }
68 
71  if (name) free (name);
72  if (text) free (text);
73 }
74 
75 // Sets the name of the variable.
76 void variable::setName (char * n) {
77  if (name) free (name);
78  name = n ? strdup (n) : NULL;
79 }
80 
81 // Returns the name of the variable.
82 char * variable::getName (void) {
83  return name;
84 }
85 
86 // Creates textual representation of a variable.
87 char * variable::toString (void) {
88  char * str = NULL;
89  char * val = NULL;
90  if (text) { free (text); text = NULL; }
91  switch (type) {
92  case VAR_UNKNOWN:
93  text = strdup ("variable");
94  break;
95  case VAR_CONSTANT:
96  str = value.c->toString ();
97  text = (char *) malloc (strlen (str) + 11);
98  sprintf (text, "constant: %s", str);
99  break;
100  case VAR_VALUE:
101  str = value.v->toString ();
102  text = (char *) malloc (strlen (str) + 8);
103  sprintf (text, "value: %s", str);
104  break;
105  case VAR_REFERENCE:
106  str = value.r->toString ();
107  val = value.r->getResult()->toString ();
108  text = (char *) malloc (strlen (str) + strlen (val) + 15);
109  sprintf (text, "reference: %s = %s", str, val);
110  break;
111  case VAR_SUBSTRATE:
112  str = value.s->getName ();
113  text = (char *) malloc (strlen (str) + 12);
114  sprintf (text, "substrate: %s", str);
115  break;
116  case VAR_ANALYSIS:
117  str = value.a->getName ();
118  text = (char *) malloc (strlen (str) + 11);
119  sprintf (text, "analysis: %s", str);
120  break;
121  default:
122  text = strdup ("?variable?");
123  break;
124  }
125  return text;
126 }