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