Qucs-core
0.0.19
|
00001 /* 00002 * check_csv.cpp - checker for CSV files 00003 * 00004 * Copyright (C) 2007, 2009 Stefan Jahn <stefan@lkcc.org> 00005 * 00006 * This is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2, or (at your option) 00009 * any later version. 00010 * 00011 * This software is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this package; see the file COPYING. If not, write to 00018 * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 * 00021 * $Id$ 00022 * 00023 */ 00024 00025 #if HAVE_CONFIG_H 00026 # include <config.h> 00027 #endif 00028 00029 #include <stdio.h> 00030 #include <stdlib.h> 00031 #include <string.h> 00032 #include <ctype.h> 00033 #include <cmath> 00034 00035 #include "logging.h" 00036 #include "complex.h" 00037 #include "object.h" 00038 #include "vector.h" 00039 #include "matrix.h" 00040 #include "matvec.h" 00041 #include "dataset.h" 00042 #include "strlist.h" 00043 #include "constants.h" 00044 #include "check_csv.h" 00045 00046 using namespace qucs; 00047 00048 strlist * csv_header = NULL; 00049 qucs::vector * csv_vector = NULL; 00050 dataset * csv_result = NULL; 00051 00052 /* Removes temporary data items from memory if necessary. */ 00053 static void csv_finalize (void) { 00054 qucs::vector * root, * next; 00055 for (root = csv_vector; root != NULL; root = next) { 00056 next = (qucs::vector *) root->getNext (); 00057 delete root; 00058 } 00059 csv_vector = NULL; 00060 if (csv_header != NULL) { 00061 delete csv_header; 00062 csv_header = NULL; 00063 } 00064 csv_lex_destroy (); 00065 } 00066 00067 /* Validates a data vector identifier. */ 00068 static void csv_validate_str (char * n) { 00069 char * p = n; 00070 if (!isalpha (*p)) *p = '_'; 00071 p++; 00072 while (*p) { 00073 if (!isalnum (*p) && *p != '.' && *p != ',' && *p != '[' && *p != ']') 00074 *p = '_'; 00075 p++; 00076 } 00077 } 00078 00079 /* Creates dataset from CSV vectors. */ 00080 static void csv_create_dataset (int len) { 00081 qucs::vector * dep, * indep, * v; 00082 char * n, depn[256]; 00083 strlist * s; 00084 00085 // create dataset 00086 csv_result = new dataset (); 00087 00088 // add dependency vector 00089 indep = new qucs::vector (); 00090 csv_result->appendDependency (indep); 00091 s = new strlist (); 00092 n = csv_header ? csv_header->get (0) : (char *) "x"; 00093 csv_validate_str (n); 00094 s->add (n); 00095 indep->setName (n); 00096 00097 // create variable vector(s) 00098 for (int i = 1; i < len; i++) { 00099 v = new qucs::vector (); 00100 n = csv_header ? csv_header->get (i) : NULL; 00101 if (n == NULL) { 00102 sprintf (depn, "y%d", i); 00103 n = depn; 00104 } 00105 csv_validate_str (n); 00106 v->setName (n); 00107 v->setDependencies (new strlist (*s)); 00108 csv_result->addVariable (v); 00109 } 00110 00111 // fill all vectors in dataset 00112 for (v = csv_vector; v != NULL; v = (qucs::vector *) v->getNext ()) { 00113 dep = csv_result->getVariables (); 00114 int l; 00115 for (l = 0; l < v->getSize () - 1; l++) { 00116 dep->add (v->get (l)); 00117 dep = (qucs::vector *) dep->getNext (); 00118 } 00119 indep->add (v->get (l)); 00120 } 00121 00122 // cleanup 00123 delete s; 00124 } 00125 00126 /* This function is the checker routine for a parsed CSV. It returns 00127 zero on success or non-zero if the parsed csv contained errors. */ 00128 int csv_check (void) { 00129 00130 int len = -1, errors = 0; 00131 00132 // no data 00133 if (csv_vector == NULL) { 00134 logprint (LOG_ERROR, "checker error, no data in csv file\n"); 00135 errors++; 00136 } 00137 // data lines available 00138 else { 00139 // check number of columns in each data line 00140 for (qucs::vector * v = csv_vector; v != NULL; v = (qucs::vector *) v->getNext ()) { 00141 if (len == -1) len = v->getSize (); 00142 else { 00143 if (v->getSize () != len) { 00144 logprint (LOG_ERROR, "checker error, different cols (%d != %d) in " 00145 "csv data line\n", v->getSize (), len); 00146 errors++; 00147 } 00148 } 00149 } 00150 // check number of columns in data and header 00151 if (csv_header && csv_header->length () != len) { 00152 logprint (LOG_ERROR, "checker error, different cols (%d != %d) in " 00153 "data and header lines\n", csv_header->length (), len); 00154 errors++; 00155 } 00156 // create dataset if possible 00157 if (!errors) { 00158 csv_create_dataset (len); 00159 } 00160 } 00161 00162 /* free temporary memory */ 00163 csv_finalize (); 00164 00165 return errors ? -1 : 0; 00166 } 00167 00168 00169 // Destroys data used by the CSV file lexer, parser and checker. 00170 void csv_destroy (void) { 00171 if (csv_result != NULL) { 00172 // delete associated dataset 00173 delete csv_result; 00174 csv_result = NULL; 00175 } 00176 if (csv_vector != NULL) { 00177 csv_finalize (); 00178 csv_vector = NULL; 00179 } 00180 } 00181 00182 // Initializes the CSV file checker. 00183 void csv_init (void) { 00184 csv_result = NULL; 00185 csv_vector = NULL; 00186 csv_header = NULL; 00187 } 00188