Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
qucsconv.cpp
Go to the documentation of this file.
1 /*
2  * qucsconv.cpp - main converter program implementation
3  *
4  * Copyright (C) 2004, 2005, 2006, 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 <assert.h>
31 #include <string.h>
32 #include <errno.h>
33 
34 #include "logging.h"
35 #include "precision.h"
36 #include "check_spice.h"
37 #include "check_vcd.h"
38 #include "check_citi.h"
39 #include "check_touchstone.h"
40 #include "check_csv.h"
41 #include "check_zvr.h"
42 #include "check_mdl.h"
43 #include "check_dataset.h"
44 #include "qucs_producer.h"
45 #include "csv_producer.h"
46 #include "touchstone_producer.h"
47 #include "matlab_producer.h"
48 #include "dataset.h"
49 
50 using namespace qucs;
51 
52 /* structure defining a conversion */
53 struct actionset_t {
54  const char * in; /* -if parameter */
55  const char * out; /* -of parameter */
56 
57  /* callback for the logic, return error code of application */
58  int (* execute) (struct actionset_t *, char * infile, char * outfile);
59 };
60 
61 /* data variable specification */
62 char * data_var = NULL;
63 
64 /* required forward declarations */
65 int spice2qucs (struct actionset_t *, char *, char *);
66 int vcd2qucs (struct actionset_t *, char *, char *);
67 int qucs2csv (struct actionset_t *, char *, char *);
68 int qucs2touch (struct actionset_t *, char *, char *);
69 int citi2qucs (struct actionset_t *, char *, char *);
70 int touch2qucs (struct actionset_t *, char *, char *);
71 int csv2qucs (struct actionset_t *, char *, char *);
72 int zvr2qucs (struct actionset_t *, char *, char *);
73 int mdl2qucs (struct actionset_t *, char *, char *);
74 int qucs2mat (struct actionset_t *, char *, char *);
75 
76 /* conversion definitions */
77 struct actionset_t actionset[] = {
78  { "spice", "qucs", spice2qucs },
79  { "spice", "qucslib", spice2qucs },
80  { "vcd", "qucsdata", vcd2qucs },
81  { "qucsdata", "csv", qucs2csv },
82  { "qucsdata", "touchstone", qucs2touch },
83  { "citi", "qucsdata", citi2qucs },
84  { "touchstone", "qucsdata", touch2qucs },
85  { "csv", "qucsdata", csv2qucs },
86  { "zvr", "qucsdata", zvr2qucs },
87  { "mdl", "qucsdata", mdl2qucs },
88  { "qucsdata", "matlab", qucs2mat },
89  { NULL, NULL, NULL}
90 };
91 
92 /* opens the given file, fallback to stdin/stdout */
93 FILE * open_file (char * file, const char * flag) {
94  FILE * fd = NULL;
95  if (file) {
96  if ((fd = fopen (file, flag)) == NULL) {
97  fprintf (stderr, "cannot open file `%s': %s, using %s instead\n",
98  file, strerror (errno), flag[0] == 'r' ? "stdin" : "stdout");
99  fd = flag[0] == 'r' ? stdin : stdout;
100  }
101  }
102  else {
103  fd = flag[0] == 'r' ? stdin : stdout;
104  }
105  return fd;
106 }
107 
108 /* main entry point */
109 int main (int argc, char ** argv) {
110 
111  char * infile = NULL, * outfile = NULL, * input = NULL, * output = NULL;
112 
113  loginit ();
114  precinit ();
115 
116  // check program arguments
117  for (int i = 1; i < argc; i++) {
118  if (!strcmp (argv[i], "-v") || !strcmp (argv[i], "--version")) {
119  fprintf (stdout,
120  "QucsConverter " PACKAGE_VERSION "\n"
121  "Copyright (C) 2004, 2005, 2006, 2007 Stefan Jahn <stefan@lkcc.org>\n"
122  "\nThis is free software; see the source for copying "
123  "conditions. There is NO\n"
124  "warranty; not even for MERCHANTABILITY or FITNESS FOR A "
125  "PARTICULAR PURPOSE.\n");
126  return 0;
127  }
128  if (!strcmp (argv[i], "-h") || !strcmp (argv[i], "--help")) {
129  fprintf (stdout,
130  "Usage: %s [OPTION]...\n\n"
131  " -h, --help display this help and exit\n"
132  " -v, --version display version information and exit\n"
133  " -i FILENAME use file as input file (default stdin)\n"
134  " -o FILENAME use file as output file (default stdout)\n"
135  " -if FORMAT input data specification (e.g. spice)\n"
136  " -of FORMAT output data specification (e.g. qucs)\n"
137  " -a, --noaction do not include netlist actions in the output\n"
138  " -g GNDNODE replace ground node\n"
139  " -d DATANAME data variable specification\n"
140  " -c, --correct enable node correction\n"
141  "\nReport bugs to <" PACKAGE_BUGREPORT ">.\n", argv[0]);
142  return 0;
143  }
144  else if (!strcmp (argv[i], "-i")) {
145  infile = argv[++i];
146  }
147  else if (!strcmp (argv[i], "-o")) {
148  outfile = argv[++i];
149  }
150  else if (!strcmp (argv[i], "-if")) {
151  input = argv[++i];
152  }
153  else if (!strcmp (argv[i], "-of")) {
154  output = argv[++i];
155  }
156  else if (!strcmp (argv[i], "-a") || !strcmp (argv[i], "--noaction")) {
157  qucs_actions = 0;
158  }
159  else if (!strcmp (argv[i], "-g")) {
160  if (argv[++i]) qucs_gnd = argv[i];
161  }
162  else if (!strcmp (argv[i], "-d")) {
163  if (argv[++i]) data_var = argv[i];
164  }
165  else if (!strcmp (argv[i], "-c") || !strcmp (argv[i], "--correct")) {
166  vcd_correct = 1;
167  }
168  }
169 
170  // check input/output formats
171  int infound = 0;
172  int outfound = 0;
173  for (int j = 0; actionset[j].in != NULL; j++) {
174  int in = 0, out = 0;
175  if (input && !strcmp (input, actionset[j].in)) {
176  in = infound = 1;
177  }
178  if (output && !strcmp (output, actionset[j].out)) {
179  out = outfound = 1;
180  }
181  if (in && out) {
182  return actionset[j].execute (&actionset[j], infile, outfile);
183  }
184  }
185 
186  // no appropriate conversion found
187  if (!infound) {
188  fprintf (stderr, "invalid input data specification `%s'\n",
189  input ? input : "not given");
190  }
191  if (!outfound) {
192  fprintf (stderr, "invalid output data specification `%s'\n",
193  output ? output : "not given");
194  }
195  fprintf (stderr, "invalid input/output data specification `%s->%s'\n",
196  input ? input : "not given", output ? output : "not given");
197  return -1;
198 }
199 
200 // SPICE to Qucs conversion.
201 int spice2qucs (struct actionset_t * action, char * infile, char * outfile) {
202  int ret = 0;
203  if ((spice_in = open_file (infile, "r")) == NULL) {
204  ret = -1;
205  } else if (spice_parse () != 0) {
206  ret = -1;
207  } else if (spice_checker () != 0) {
208  ret = -1;
209  }
211  if (spice_in)
212  fclose (spice_in);
213  if (ret) {
214  spice_destroy ();
215  return -1;
216  }
217 
218  if ((qucs_out = open_file (outfile, "w")) == NULL)
219  return -1;
220  if (!strcmp (action->out, "qucs"))
221  qucs_producer ();
222  else /* "qucslib" */
223  qucslib_producer ();
224  fclose (qucs_out);
225  spice_destroy ();
226  return 0;
227 }
228 
229 // VCD to Qucs conversion.
230 int vcd2qucs (struct actionset_t * action, char * infile, char * outfile) {
231  int ret = 0;
232  vcd_init ();
233  if ((vcd_in = open_file (infile, "r")) == NULL) {
234  ret = -1;
235  } else if (vcd_parse () != 0) {
236  ret = -1;
237  } else if (vcd_checker () != 0) {
238  ret = -1;
239  }
240  vcd_lex_destroy ();
241  if (vcd_in)
242  fclose (vcd_in);
243  if (ret) {
244  vcd_destroy ();
245  return -1;
246  }
247 
248  if ((qucs_out = open_file (outfile, "w")) == NULL)
249  return -1;
250  if (!strcmp (action->out, "qucsdata"))
252  fclose (qucs_out);
253  vcd_destroy ();
254  return 0;
255 }
256 
257 // Qucs dataset to CSV conversion.
258 int qucs2csv (struct actionset_t * action, char * infile, char * outfile) {
259  int ret = 0;
260  if ((dataset_in = open_file (infile, "r")) == NULL) {
261  ret = -1;
262  } else if (dataset_parse () != 0) {
263  ret = -1;
264  } else if (dataset_result == NULL) {
265  ret = -1;
266  } else if (dataset_check (dataset_result) != 0) {
267  delete dataset_result;
268  dataset_result = NULL;
269  ret = -1;
270  }
272  dataset_result = NULL;
274  if (dataset_in)
275  fclose (dataset_in);
276  if (ret)
277  return -1;
278 
279  if ((csv_out = open_file (outfile, "w")) == NULL)
280  return -1;
281  if (!strcmp (action->out, "csv")) {
282  if (data_var != NULL)
283  csv_producer (data_var, ";");
284  else {
285  fprintf (stderr, "no data variable given (passed by -d option)\n");
286  ret = -1;
287  }
288  fclose (csv_out);
289  return ret;
290  }
291  return -1;
292 }
293 
294 // Qucs dataset to Touchstone conversion.
295 int qucs2touch (struct actionset_t * action, char * infile, char * outfile) {
296  int ret = 0;
297  if ((dataset_in = open_file (infile, "r")) == NULL) {
298  ret = -1;
299  } else if (dataset_parse () != 0) {
300  ret = -1;
301  } else if (dataset_result == NULL) {
302  ret = -1;
303  } else if (dataset_check (dataset_result) != 0) {
304  delete dataset_result;
305  dataset_result = NULL;
306  ret = -1;
307  }
309  dataset_result = NULL;
311  if (dataset_in)
312  fclose (dataset_in);
313  if (ret)
314  return -1;
315 
316  if ((touchstone_out = open_file (outfile, "w")) == NULL)
317  return -1;
318  if (!strcmp (action->out, "touchstone")) {
320  fclose (touchstone_out);
321  return ret;
322  }
323  return -1;
324 }
325 
326 // Qucs dataset to Matlab conversion.
327 int qucs2mat (struct actionset_t * action, char * infile, char * outfile) {
328  int ret = 0;
329  if ((dataset_in = open_file (infile, "r")) == NULL) {
330  ret = -1;
331  } else if (dataset_parse () != 0) {
332  ret = -1;
333  } else if (dataset_result == NULL) {
334  ret = -1;
335  } else if (dataset_check (dataset_result) != 0) {
336  delete dataset_result;
337  dataset_result = NULL;
338  ret = -1;
339  }
341  dataset_result = NULL;
343  if (dataset_in)
344  fclose (dataset_in);
345  if (ret)
346  return -1;
347 
348  if ((matlab_out = open_file (outfile, "wb")) == NULL)
349  return -1;
350  if (!strcmp (action->out, "matlab")) {
351  matlab_producer ();
352  fclose (matlab_out);
353  return ret;
354  }
355  return -1;
356 }
357 
358 // CITIfile to Qucs conversion.
359 int citi2qucs (struct actionset_t * action, char * infile, char * outfile) {
360  int ret = 0;
361  citi_init ();
362  if ((citi_in = open_file (infile, "r")) == NULL) {
363  ret = -1;
364  } else if (citi_parse () != 0) {
365  ret = -1;
366  } else if (citi_check () != 0) {
367  ret = -1;
368  }
369  citi_lex_destroy ();
370  if (citi_in)
371  fclose (citi_in);
372  if (ret) {
373  citi_destroy ();
374  return -1;
375  }
376 
377  if (!strcmp (action->out, "qucsdata")) {
378  citi_result->setFile (outfile);
380  }
381  citi_destroy ();
382  return 0;
383 }
384 
385 // Touchstone to Qucs conversion.
386 int touch2qucs (struct actionset_t * action, char * infile, char * outfile) {
387  int ret = 0;
388  touchstone_init ();
389  if ((touchstone_in = open_file (infile, "r")) == NULL) {
390  ret = -1;
391  } else if (touchstone_parse () != 0) {
392  ret = -1;
393  } else if (touchstone_check () != 0) {
394  ret = -1;
395  }
397  if (touchstone_in)
398  fclose (touchstone_in);
399  if (ret) {
401  return -1;
402  }
403 
404  if (!strcmp (action->out, "qucsdata")) {
405  touchstone_result->setFile (outfile);
407  }
409  return 0;
410 }
411 
412 // CSV to Qucs conversion.
413 int csv2qucs (struct actionset_t * action, char * infile, char * outfile) {
414  int ret = 0;
415  csv_init ();
416  if ((csv_in = open_file (infile, "r")) == NULL) {
417  ret = -1;
418  } else if (csv_parse () != 0) {
419  ret = -1;
420  } else if (csv_check () != 0) {
421  ret = -1;
422  }
423  csv_lex_destroy ();
424  if (csv_in)
425  fclose (csv_in);
426  if (ret) {
427  csv_destroy ();
428  return -1;
429  }
430 
431  if (!strcmp (action->out, "qucsdata")) {
432  csv_result->setFile (outfile);
434  }
435  csv_destroy ();
436  return 0;
437 }
438 
439 // ZVR to Qucs conversion.
440 int zvr2qucs (struct actionset_t * action, char * infile, char * outfile) {
441  int ret = 0;
442  zvr_init ();
443  if ((zvr_in = open_file (infile, "r")) == NULL) {
444  ret = -1;
445  } else if (zvr_parse () != 0) {
446  ret = -1;
447  } else if (zvr_check () != 0) {
448  ret = -1;
449  }
450  zvr_lex_destroy ();
451  if (zvr_in)
452  fclose (zvr_in);
453  if (ret) {
454  zvr_destroy ();
455  return -1;
456  }
457  if (!strcmp (action->out, "qucsdata")) {
458  zvr_result->setFile (outfile);
460  }
461  zvr_destroy ();
462  return 0;
463 }
464 
465 // MDL to Qucs conversion.
466 int mdl2qucs (struct actionset_t * action, char * infile, char * outfile) {
467  int ret = 0;
468  mdl_init ();
469  if ((mdl_in = open_file (infile, "r")) == NULL) {
470  ret = -1;
471  } else if (mdl_parse () != 0) {
472  ret = -1;
473  } else if (mdl_check () != 0) {
474  ret = -1;
475  }
476  mdl_lex_destroy ();
477  if (mdl_in)
478  fclose (mdl_in);
479  if (ret) {
480  mdl_destroy ();
481  return -1;
482  }
483  if (!strcmp (action->out, "qucsdata")) {
484  mdl_result->setFile (outfile);
486  }
487  mdl_destroy ();
488  return 0;
489 }
490 
FILE * citi_in
Definition: parse_citi.y:54
FILE * zvr_in
FILE * csv_out
FILE * touchstone_in
__BEGIN_DECLS int dataset_parse(void)
__BEGIN_DECLS int csv_parse(void)
void citi_init(void)
Definition: check_citi.cpp:283
void vcd_init(void)
Definition: check_vcd.cpp:605
int dataset_check(dataset *data)
int spice_parse(void)
void touchstone_init(void)
void qucsdata_producer(dataset *data)
char * data_var
Definition: qucsconv.cpp:62
FILE * mdl_in
void spice_destroy(void)
FILE * spice_in
Definition: parse_spice.y:41
void citi_destroy(void)
Definition: check_citi.cpp:270
const char * out
Definition: qucsconv.cpp:55
int mdl_parse(void)
void mdl_init(void)
Definition: check_mdl.cpp:695
void qucsdata_producer_vcd(void)
FILE * csv_in
int citi_check(void)
Definition: check_citi.cpp:175
ActionLine action
int zvr_parse(void)
void matlab_producer(void)
int zvr_check(void)
Definition: check_zvr.cpp:220
int citi2qucs(struct actionset_t *, char *, char *)
Definition: qucsconv.cpp:359
int mdl_lex_destroy(void)
int qucs2mat(struct actionset_t *, char *, char *)
Definition: qucsconv.cpp:327
i
Definition: parse_mdl.y:516
FILE * open_file(char *file, const char *flag)
Definition: qucsconv.cpp:93
int citi_parse(void)
int touchstone_lex_destroy(void)
dataset * dataset_result
dataset * mdl_result
Definition: check_mdl.cpp:52
int zvr_lex_destroy(void)
int touch2qucs(struct actionset_t *, char *, char *)
Definition: qucsconv.cpp:386
void touchstone_destroy(void)
dataset * zvr_result
Definition: check_zvr.cpp:48
int main(int argc, char **argv)
Definition: qucsconv.cpp:109
int mdl2qucs(struct actionset_t *, char *, char *)
Definition: qucsconv.cpp:466
struct actionset_t actionset[]
Definition: qucsconv.cpp:77
void csv_init(void)
Definition: check_csv.cpp:183
int csv2qucs(struct actionset_t *, char *, char *)
Definition: qucsconv.cpp:413
FILE * qucs_out
int vcd_checker(void)
Definition: check_vcd.cpp:500
void csv_destroy(void)
Definition: check_csv.cpp:170
int spice2qucs(struct actionset_t *, char *, char *)
Definition: qucsconv.cpp:201
FILE * matlab_out
void zvr_init(void)
Definition: check_zvr.cpp:268
void qucs_producer(void)
FILE * touchstone_out
int touchstone_parse(void)
FILE * vcd_in
dataset * csv_result
Definition: check_csv.cpp:50
const char * qucs_gnd
int vcd_lex_destroy(void)
int csv_lex_destroy(void)
int citi_lex_destroy(void)
qucs::dataset * citi_result
Definition: check_citi.cpp:48
List int
Definition: parse_citi.y:183
int qucs2csv(struct actionset_t *, char *, char *)
Definition: qucsconv.cpp:258
void loginit(void)
Definition: logging.c:51
void precinit(void)
Definition: precision.c:36
const char * in
Definition: qucsconv.cpp:54
void zvr_destroy(void)
Definition: check_zvr.cpp:255
int touchstone_check(void)
int spice_checker(void)
void vcd_destroy(void)
Definition: check_vcd.cpp:594
int csv_check(void)
Definition: check_csv.cpp:128
int vcd_parse(void)
int zvr2qucs(struct actionset_t *, char *, char *)
Definition: qucsconv.cpp:440
int qucs_actions
int vcd2qucs(struct actionset_t *, char *, char *)
Definition: qucsconv.cpp:230
qucs::dataset * qucs_data
int mdl_check(void)
Definition: check_mdl.cpp:658
void qucslib_producer(void)
void mdl_destroy(void)
Definition: check_mdl.cpp:672
int(* execute)(struct actionset_t *, char *infile, char *outfile)
Definition: qucsconv.cpp:58
int vcd_correct
Definition: check_vcd.cpp:52
int spice_lex_destroy(void)
void csv_producer(char *variable, const char *sep)
dataset * touchstone_result
void touchstone_producer(const char *variable)
FILE * dataset_in
int qucs2touch(struct actionset_t *, char *, char *)
Definition: qucsconv.cpp:295
void setFile(const char *)
Definition: dataset.cpp:323
int dataset_lex_destroy(void)