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