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