My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
check_citi.cpp
Go to the documentation of this file.
1 /*
2  * check_citi.cpp - checker for CITIfiles
3  *
4  * Copyright (C) 2006 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: check_citi.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 "logging.h"
36 #include "complex.h"
37 #include "object.h"
38 #include "vector.h"
39 #include "matrix.h"
40 #include "matvec.h"
41 #include "dataset.h"
42 #include "strlist.h"
43 #include "constants.h"
44 #include "check_citi.h"
45 
47 struct citi_package_t * citi_root = NULL;
48 
49 /* Returns the number of vectors in a package. */
50 static int citi_count_vectors (struct citi_package_t * p) {
51  int i = 0;
52  for (vector * v = p->data; v != NULL; v = (vector *) v->getNext ()) i++;
53  return i;
54 }
55 
56 /* Returns the number of variables in a package. */
57 static int citi_count_variables (struct citi_package_t * p) {
58  int i = 0;
59  for (struct citi_header_t * h = p->head; h != NULL; h = h->next) {
60  if (h->var != NULL) i++;
61  }
62  return i;
63 }
64 
65 /* Returns the n-th vector in the package. */
66 static vector * citi_get_vector (struct citi_package_t * p, int n) {
67  vector * v = p->data;
68  for (int i = 0; v != NULL; v = (vector *) v->getNext (), i++) {
69  if (i == n) return v;
70  }
71  return NULL;
72 }
73 
74 /* Returns the number of packages. */
75 static int citi_count_packages (struct citi_package_t * p) {
76  int i = 0;
77  for (; p != NULL; p = p->next) i++;
78  return i;
79 }
80 
81 /* Find name of the package in its headers. */
82 static char * citi_get_package (struct citi_package_t * p) {
83  for (struct citi_header_t * h = p->head; h != NULL; h = h->next) {
84  if (h->package != NULL) return h->package;
85  }
86  return NULL;
87 }
88 
89 /* Create a valid vector for the dataset. */
90 static vector * citi_create_vector (struct citi_package_t * p, int i,
91  char * n, char * type) {
92  vector * vec;
93  vec = citi_get_vector (p, i); // fetch vector
94  vec = new vector (*vec); // copy vector
95  vec->reverse (); // reverse vector
96 
97  // convert data if necessary
98  if (!strcmp (type, "MAGANGLE")) {
99  for (int i = 0; i < vec->getSize (); i++) {
100  nr_complex_t val = vec->get (i);
101  val = polar (real (val), rad (imag (val)));
102  vec->set (val, i);
103  }
104  }
105  else if (!strcmp (type, "DBANGLE")) {
106  for (int i = 0; i < vec->getSize (); i++) {
107  nr_complex_t val = vec->get (i);
108  val = polar (pow (10.0, real (val) / 20.0), rad (imag (val)));
109  vec->set (val, i);
110  }
111  }
112 
113  // return named vector
114  vec->setName (n);
115  return vec;
116 }
117 
118 /* Returns dependent variable length for the given dependencies. */
119 static int citi_vector_length (strlist deps) {
120  int n = 1;
121  // no dependencies
122  if (deps.length () <= 0)
123  return 0;
124  // calculate length of resulting dependent variable
125  for (int i = 0; i < deps.length(); i++) {
126  vector * v = citi_result->findDependency (deps.get (i));
127  if (v != NULL) n *= v->getSize ();
128  }
129  return n;
130 }
131 
132 /* Checks length of variable vectors. */
133 static int citi_check_dep_length (vector * v, strlist deps, char * package) {
134  int rlength = v->getSize ();
135  int dlength = citi_vector_length (deps);
136  if (rlength != dlength) {
137  logprint (LOG_ERROR, "checker error, invalid vector `%s' length "
138  "(%d != %d) in package `%s'\n", v->getName (), rlength, dlength,
139  package);
140  return 1;
141  }
142  return 0;
143 }
144 
145 // Destroys data used by the CITIfile parser.
146 void citi_finalize (void) {
147  struct citi_package_t * p, * pn;
148  /* go through all packages */
149  for (p = citi_root; p != NULL; p = pn) {
150  struct citi_header_t * h, * hn;
151  /* go through each header */
152  for (h = p->head; h != NULL; h = hn) {
153  if (h->package) free (h->package);
154  if (h->var) free (h->var);
155  if (h->type) free (h->type);
156  hn = h->next;
157  free (h);
158  }
159  vector * v, * vn;
160  /* go through each vector */
161  for (v = p->data; v != NULL; v = vn) {
162  vn = (vector *) v->getNext ();
163  delete v;
164  }
165  pn = p->next;
166  free (p);
167  }
168  citi_lex_destroy ();
169 }
170 
171 /* This function is the checker routine for a parsed CITIfile. It
172  returns zero on success or non-zero if it contained errors. */
173 int citi_check (void) {
174 
175  int errors = 0;
176 
177  /* create dataset */
178  citi_result = new dataset ();
179  /* get root package */
180  struct citi_package_t * p = citi_root;
181  /* count packages */
182  int packages = citi_count_packages (p);
183 
184  /* go through all packages */
185  for (p = citi_root; p != NULL; p = p->next) {
186  struct citi_header_t * h;
187  char * package = citi_get_package (p);
188  int n = 0;
189  strlist deps;
190 
191  /* check number of defined variables and vectors */
192  int cvec = citi_count_vectors (p);
193  int cvar = citi_count_variables (p);
194  if (cvec != cvar) {
195  logprint (LOG_ERROR, "checker error, no. of vectors (%d) does not equal "
196  "no. of variables (%d) in package `%s'\n", cvec, cvar,
197  package);
198  errors++;
199  break;
200  }
201 
202  /* no package info if there is just one */
203  char opack[256];
204  if (packages < 2) {
205  opack[0] = '\0';
206  } else {
207  sprintf (opack, "%s.", package);
208  }
209 
210  /* go through each header */
211  for (h = p->head; h != NULL; h = h->next) {
212  vector * v;
213  if (h->var != NULL) {
214  char txt[256];
215  if (h->i1 >= 0) {
216  /* dependent variables */
217  if (h->i2 >= 0) {
218  sprintf (txt, "%s%s[%d,%d]", opack, h->var, h->i1, h->i2);
219  v = citi_create_vector (p, n, txt, h->type);
220  v->setDependencies (new strlist (deps));
221  errors += citi_check_dep_length (v, deps, package);
222  citi_result->addVariable (v);
223  n++;
224  } else {
225  sprintf (txt, "%s%s[%d]", opack, h->var, h->i1);
226  v = citi_create_vector (p, n, txt, h->type);
227  v->setDependencies (new strlist (deps));
228  errors += citi_check_dep_length (v, deps, package);
229  citi_result->addVariable (v);
230  n++;
231  }
232  } else if (h->n >= 0) {
233  /* independent variable */
234  sprintf (txt, "%s%s", opack, h->var);
235  v = citi_create_vector (p, n, txt, h->type);
236  deps.add (txt);
237  if (!citi_result->findDependency (txt)) {
238  /* add independent vectors only once */
239  citi_result->addDependency (v);
240  }
241  n++;
242  // check length of independent vector
243  if (v->getSize () != h->n) {
244  logprint (LOG_ERROR, "checker error, vector `%s' length (%d) "
245  "does not equal defined length (%d) in package `%s'\n",
246  h->var, v->getSize (), h->n, package);
247  errors++;
248  }
249  } else {
250  /* dependent variables, no indices */
251  sprintf (txt, "%s%s", opack, h->var);
252  v = citi_create_vector (p, n, txt, h->type);
253  v->setDependencies (new strlist (deps));
254  errors += citi_check_dep_length (v, deps, package);
255  citi_result->addVariable (v);
256  n++;
257  }
258  }
259  }
260  }
261 
262  citi_finalize ();
263  citi_root = NULL;
264  return errors ? -1 : 0;
265 }
266 
267 // Destroys data used by the CITIfile parser and checker.
268 void citi_destroy (void) {
269  if (citi_result != NULL) {
270  // delete associated dataset
271  delete citi_result;
272  citi_result = NULL;
273  }
274  if (citi_root != NULL) {
275  citi_finalize ();
276  citi_root = NULL;
277  }
278 }
279 
280 // Initializes the CITIfile checker.
281 void citi_init (void) {
282  citi_result = NULL;
283  citi_root = NULL;
284 }