My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ucs.cpp
Go to the documentation of this file.
1 /*
2  * ucs.cpp - main program implementation
3  *
4  * Copyright (C) 2003-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: ucs.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 <assert.h>
32 #include <string.h>
33 #include <time.h>
34 
35 #include "logging.h"
36 #include "precision.h"
37 #include "component.h"
38 #include "components.h"
39 #include "net.h"
40 #include "input.h"
41 #include "dataset.h"
42 #include "equation.h"
43 #include "environment.h"
44 #include "exceptionstack.h"
45 #include "check_netlist.h"
46 #include "module.h"
47 
48 #if HAVE_UNISTD_H
49 #include <unistd.h>
50 #endif
51 
52 using namespace qucs;
53 
54 int main (int argc, char ** argv) {
55 
56  char * infile = NULL, * outfile = NULL;
57  net * subnet;
58  input * in;
59  circuit * gnd;
60  dataset * out;
61  environment * root;
62  int listing = 0;
63  int ret = 0;
64 
65  loginit ();
66  precinit ();
67  ::srand (::time (NULL));
68 
69  // check program arguments
70  for (int i = 1; i < argc; i++) {
71  if (!strcmp (argv[i], "-v") || !strcmp (argv[i], "--version")) {
72  fprintf (stdout,
73  "Qucsator " PACKAGE_VERSION "\n"
74  "Copyright (C) 2003-2009 "
75  "Stefan Jahn <stefan@lkcc.org>\n"
76  "Copyright (C) 2006 Helene Parruitte <parruit@enseirb.fr>\n"
77  "Copyright (C) 2006 Bastien Roucaries <roucaries.bastien@gmail.com>\n"
78  "\nThis is free software; see the source for copying "
79  "conditions. There is NO\n"
80  "warranty; not even for MERCHANTABILITY or FITNESS FOR A "
81  "PARTICULAR PURPOSE.\n");
82  return 0;
83  }
84  if (!strcmp (argv[i], "-h") || !strcmp (argv[i], "--help")) {
85  fprintf (stdout,
86  "Usage: %s [OPTION]...\n\n"
87  " -h, --help display this help and exit\n"
88  " -v, --version display version information and exit\n"
89  " -i FILENAME use file as input netlist (default stdin)\n"
90  " -o FILENAME use file as output dataset (default stdout)\n"
91  " -b, --bar enable textual progress bar\n"
92  " -g, --gui special progress bar used by gui\n"
93  " -c, --check check the input netlist and exit\n"
94  "\nReport bugs to <" PACKAGE_BUGREPORT ">.\n", argv[0]);
95  return 0;
96  }
97  else if (!strcmp (argv[i], "-i")) {
98  infile = argv[++i];
99  }
100  else if (!strcmp (argv[i], "-o")) {
101  outfile = argv[++i];
102  file_status = stdout;
103  }
104  else if (!strcmp (argv[i], "-b") || !strcmp (argv[i], "--bar")) {
105  progressbar_enable = 1;
106  }
107  else if (!strcmp (argv[i], "-g") || !strcmp (argv[i], "--gui")) {
108  progressbar_gui = 1;
109  }
110  else if (!strcmp (argv[i], "-c") || !strcmp (argv[i], "--check")) {
111  netlist_check = 1;
112  }
113  else if (!strcmp (argv[i], "-l") || !strcmp (argv[i], "--listing")) {
114  listing = 1;
115  }
116  }
117 
118  // create static modules
120 
121 #if DEBUG
122  // emit C-code for available definitions if requested and exit
123  if (listing) {
124  module::print ();
125  return ret;
126  }
127 #endif /* DEBUG */
128 
129  // create root environment
130  root = new environment ("root");
131 
132  // create netlist object and input
133  subnet = new net ("subnet");
134  in = infile ? new input (infile) : new input ();
135 
136  // pass root environment to netlist object and input
137  subnet->setEnv (root);
138  in->setEnv (root);
139 
140  // get input netlist
141  if (in->netlist (subnet) != 0) {
142  if (netlist_check) {
143  logprint (LOG_STATUS, "checker notice, netlist check FAILED\n");
144  }
145  return -1;
146  }
147  if (netlist_check) {
148  logprint (LOG_STATUS, "checker notice, netlist OK\n");
149  return 0;
150  }
151 
152  // attach a ground to the netlist
153  gnd = new ground ();
154  gnd->setNode (0, "gnd");
155  gnd->setName ("GND");
156  subnet->insertCircuit (gnd);
157 
158  // analyse the netlist
159  int err = 0;
160  out = subnet->runAnalysis (err);
161  ret |= err;
162 
163  // evaluate output dataset
164  ret |= root->equationSolver (out);
165  out->setFile (outfile);
166  out->print ();
167 
168  estack.print ("uncaught");
169 
170  delete subnet;
171  delete in;
172  delete out;
173  delete root;
174 
175  // delete modules
177 
179  return ret;
180 }