Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
check_csv.cpp
Go to the documentation of this file.
1 /*
2  * check_csv.cpp - checker for CSV files
3  *
4  * Copyright (C) 2007, 2009 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 #include <ctype.h>
33 #include <cmath>
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_csv.h"
45 
46 using namespace qucs;
47 
48 strlist * csv_header = NULL;
50 dataset * csv_result = NULL;
51 
52 /* Removes temporary data items from memory if necessary. */
53 static void csv_finalize (void) {
54  qucs::vector * root, * next;
55  for (root = csv_vector; root != NULL; root = next) {
56  next = (qucs::vector *) root->getNext ();
57  delete root;
58  }
59  csv_vector = NULL;
60  if (csv_header != NULL) {
61  delete csv_header;
62  csv_header = NULL;
63  }
64  csv_lex_destroy ();
65 }
66 
67 /* Validates a data vector identifier. */
68 static void csv_validate_str (char * n) {
69  char * p = n;
70  if (!isalpha (*p)) *p = '_';
71  p++;
72  while (*p) {
73  if (!isalnum (*p) && *p != '.' && *p != ',' && *p != '[' && *p != ']')
74  *p = '_';
75  p++;
76  }
77 }
78 
79 /* Creates dataset from CSV vectors. */
80 static void csv_create_dataset (int len) {
81  qucs::vector * dep, * indep, * v;
82  char * n, depn[256];
83  strlist * s;
84 
85  // create dataset
86  csv_result = new dataset ();
87 
88  // add dependency vector
89  indep = new qucs::vector ();
91  s = new strlist ();
92  n = csv_header ? csv_header->get (0) : (char *) "x";
93  csv_validate_str (n);
94  s->add (n);
95  indep->setName (n);
96 
97  // create variable vector(s)
98  for (int i = 1; i < len; i++) {
99  v = new qucs::vector ();
100  n = csv_header ? csv_header->get (i) : NULL;
101  if (n == NULL) {
102  sprintf (depn, "y%d", i);
103  n = depn;
104  }
105  csv_validate_str (n);
106  v->setName (n);
107  v->setDependencies (new strlist (*s));
108  csv_result->addVariable (v);
109  }
110 
111  // fill all vectors in dataset
112  for (v = csv_vector; v != NULL; v = (qucs::vector *) v->getNext ()) {
113  dep = csv_result->getVariables ();
114  int l;
115  for (l = 0; l < v->getSize () - 1; l++) {
116  dep->add (v->get (l));
117  dep = (qucs::vector *) dep->getNext ();
118  }
119  indep->add (v->get (l));
120  }
121 
122  // cleanup
123  delete s;
124 }
125 
126 /* This function is the checker routine for a parsed CSV. It returns
127  zero on success or non-zero if the parsed csv contained errors. */
128 int csv_check (void) {
129 
130  int len = -1, errors = 0;
131 
132  // no data
133  if (csv_vector == NULL) {
134  logprint (LOG_ERROR, "checker error, no data in csv file\n");
135  errors++;
136  }
137  // data lines available
138  else {
139  // check number of columns in each data line
140  for (qucs::vector * v = csv_vector; v != NULL; v = (qucs::vector *) v->getNext ()) {
141  if (len == -1) len = v->getSize ();
142  else {
143  if (v->getSize () != len) {
144  logprint (LOG_ERROR, "checker error, different cols (%d != %d) in "
145  "csv data line\n", v->getSize (), len);
146  errors++;
147  }
148  }
149  }
150  // check number of columns in data and header
151  if (csv_header && csv_header->length () != len) {
152  logprint (LOG_ERROR, "checker error, different cols (%d != %d) in "
153  "data and header lines\n", csv_header->length (), len);
154  errors++;
155  }
156  // create dataset if possible
157  if (!errors) {
158  csv_create_dataset (len);
159  }
160  }
161 
162  /* free temporary memory */
163  csv_finalize ();
164 
165  return errors ? -1 : 0;
166 }
167 
168 
169 // Destroys data used by the CSV file lexer, parser and checker.
170 void csv_destroy (void) {
171  if (csv_result != NULL) {
172  // delete associated dataset
173  delete csv_result;
174  csv_result = NULL;
175  }
176  if (csv_vector != NULL) {
177  csv_finalize ();
178  csv_vector = NULL;
179  }
180 }
181 
182 // Initializes the CSV file checker.
183 void csv_init (void) {
184  csv_result = NULL;
185  csv_vector = NULL;
186  csv_header = NULL;
187 }
188 
l
Definition: parse_vcd.y:213
qucs::vector * getVariables(void)
Definition: dataset.h:63
int getSize(void) const
Definition: vector.cpp:192
Global physical constants header file.
n
Definition: parse_citi.y:147
object * getNext(void)
Definition: object.h:59
static void csv_create_dataset(int len)
Definition: check_csv.cpp:80
void setDependencies(strlist *)
Definition: vector.cpp:143
i
Definition: parse_mdl.y:516
next
Definition: parse_spice.y:859
void setName(const char *)
Definition: object.cpp:78
void add(nr_complex_t)
Definition: vector.cpp:151
void csv_init(void)
Definition: check_csv.cpp:183
qucs::vector * csv_vector
Definition: check_csv.cpp:49
void csv_destroy(void)
Definition: check_csv.cpp:170
strlist * csv_header
Definition: check_csv.cpp:48
int length(void)
Definition: strlist.cpp:100
static void csv_finalize(void)
Definition: check_csv.cpp:53
Dense matrix class header file.
void addVariable(qucs::vector *)
Definition: dataset.cpp:153
dataset * csv_result
Definition: check_csv.cpp:50
v
Definition: parse_zvr.y:141
void appendDependency(qucs::vector *)
Definition: dataset.cpp:128
int csv_lex_destroy(void)
#define LOG_ERROR
Definition: logging.h:28
static void csv_validate_str(char *n)
Definition: check_csv.cpp:68
int csv_check(void)
Definition: check_csv.cpp:128
char * get(int)
Definition: strlist.cpp:129
void logprint(int level, const char *format,...)
Definition: logging.c:37
nr_complex_t get(int)
Definition: vector.cpp:179