Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
csv_producer.cpp
Go to the documentation of this file.
1 /*
2  * csv_producer.cpp - the CSV data file producer
3  *
4  * Copyright (C) 2006, 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 <time.h>
32 #include <string.h>
33 #include "dataset.h"
34 
35 #include "csv_producer.h"
36 
37 using namespace qucs;
38 
39 /* Global variables. */
41 /* FILE * csv_out = NULL; -- already defined in CSV lexer */
42 
43 struct csv_data {
44  char type; // type of variable
45  vector * v; // appropriate data vector
46  int idx; // index into vector
47  int skip; // skip length
48  int len; // length of vector
49 };
50 
51 /* Definition of line separator. */
52 #ifdef __MINGW32__
53 #define csv_crlf "\n"
54 #else
55 #define csv_crlf "\r\n"
56 #endif
57 
58 /* The CSV data printer. */
59 void csv_print (struct csv_data * data, int vectors, const char * sep) {
60 
61  int len = 0;
62 
63  // print header
64  for (int i = 0; i < vectors; i++) {
65  if (data[i].type == 'c')
66  fprintf (csv_out, "\"r %s\"%s\"i %s\"", data[i].v->getName (),
67  sep, data[i].v->getName ());
68  else
69  fprintf (csv_out, "\"%s\"", data[i].v->getName ());
70  fprintf (csv_out, "%s", i != vectors - 1 ? sep : csv_crlf);
71  // find longest vector
72  if (len < data[i].len) len = data[i].len;
73  }
74 
75  // print data
76  for (int k = 0; k < len; k++) {
77  for (int i = 0; i < vectors; i++) {
78  if (data[i].type == 'c')
79  fprintf (csv_out, "%+." NR_DECS "e%s%+." NR_DECS "e",
80  (double) real (data[i].v->get (data[i].idx)), sep,
81  (double) imag (data[i].v->get (data[i].idx)));
82  else
83  fprintf (csv_out, "%+." NR_DECS "e",
84  (double) real (data[i].v->get (data[i].idx)));
85  fprintf (csv_out, "%s", i != vectors - 1 ? sep : csv_crlf);
86  data[i].idx = ((k + 1) / data[i].skip) % data[i].len;
87  }
88  }
89 }
90 
91 /* This is the overall CSV producer. */
92 void csv_producer (char * variable, const char * sep) {
93  vector * v;
94  // save variable including its dependencies
95  if (variable && (v = qucs_data->findVariable (variable)) != NULL) {
96 
97  // prepare variable + dependency structures
98  strlist * deps = v->getDependencies ();
99  int vectors = 1 + (deps ? deps->length () : 0);
100  struct csv_data * data = new struct csv_data[vectors];
101  int i = vectors - 1;
102  data[i].type = real (sum (norm (imag (*v)))) > 0.0 ? 'c' : 'r';
103  data[i].v = v;
104  data[i].idx = 0;
105  data[i].skip = 1;
106  data[i].len = v->getSize ();
107 
108  int a = v->getSize ();
109  for (i = vectors - 2; i >= 0; i--) {
110  vector * d = qucs_data->findDependency (deps->get (i));
111  data[i].type = real (sum (norm (imag (*d)))) > 0.0 ? 'c' : 'r';
112  data[i].v = d;
113  data[i].idx = 0;
114  a /= d->getSize ();
115  data[i].skip = a;
116  data[i].len = d->getSize ();
117  }
118 
119  csv_print (data, vectors, sep);
120  delete[] data;
121  }
122  // save dependency + all variable depending on it
123  else if (variable && (v = qucs_data->findDependency (variable)) != NULL) {
124 
125  // prepare dependency + variables structures
126  vector * vars;
127  int vectors = 1;
128  for (vars = qucs_data->getVariables (); vars != NULL;
129  vars = (vector *) vars->getNext ()) {
130  strlist * deps = vars->getDependencies ();
131  if (deps->contains (v->getName ()))
132  vectors++;
133  }
134  struct csv_data * data = new struct csv_data[vectors];
135 
136  data[0].type = real (sum (norm (imag (*v)))) > 0.0 ? 'c' : 'r';
137  data[0].v = v;
138  data[0].idx = 0;
139  data[0].skip = 1;
140  data[0].len = v->getSize ();
141  int i = 1;
142  for (vars = qucs_data->getVariables (); vars != NULL;
143  vars = (vector *) vars->getNext ()) {
144  strlist * deps = vars->getDependencies ();
145  if (deps->contains (v->getName ())) {
146  vector * d = vars;
147  data[i].type = real (sum (norm (imag (*d)))) > 0.0 ? 'c' : 'r';
148  data[i].v = d;
149  data[i].idx = 0;
150  data[i].skip = 1;
151  data[i].len = d->getSize ();
152  i++;
153  }
154  }
155 
156  csv_print (data, vectors, sep);
157  delete[] data;
158  }
159  // no such data found
160  else {
161  fprintf (stderr, "no such data variable `%s' found\n", variable);
162  }
163 }
vector * v
FILE * csv_out
matrix real(matrix a)
Real part matrix.
Definition: matrix.cpp:568
qucs::vector * getVariables(void)
Definition: dataset.h:63
nr_complex_t sum(vector)
Definition: vector.cpp:251
object * getNext(void)
Definition: object.h:59
i
Definition: parse_mdl.y:516
matrix imag(matrix a)
Imaginary part matrix.
Definition: matrix.cpp:581
#define csv_crlf
type
Definition: parse_vcd.y:164
v
Definition: parse_zvr.y:141
nr_double_t norm(const nr_complex_t z)
Compute euclidian norm of complex number.
Definition: complex.cpp:283
void csv_print(struct csv_data *data, int vectors, const char *sep)
char * getName(void)
Definition: object.cpp:84
qucs::vector * findDependency(const char *)
Definition: dataset.cpp:281
qucs::dataset * qucs_data
qucs::vector * findVariable(const char *)
Definition: dataset.cpp:292
void csv_producer(char *variable, const char *sep)
data
Definition: parse_citi.y:117