Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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$
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 #include <list>
35 #include <iostream>
36 #include <fstream>
37 
38 #include "logging.h"
39 #include "precision.h"
40 #include "component.h"
41 #include "components.h"
42 #include "net.h"
43 #include "input.h"
44 #include "dataset.h"
45 #include "equation.h"
46 #include "environment.h"
47 #include "exceptionstack.h"
48 #include "check_netlist.h"
49 #include "module.h"
50 
51 #if HAVE_UNISTD_H
52 #include <unistd.h>
53 #endif
54 
55 using namespace qucs;
56 
57 int main (int argc, char ** argv) {
58 
59  char * infile = NULL;
60  char * outfile = NULL;
61  char * projPath = NULL;
62  net * subnet;
63  input * in;
64  circuit * gnd;
65  dataset * out;
66  environment * root;
67  int listing = 0;
68  int ret = 0;
69  int dynamicLoad = 0;
70 
71  std::list<std::string> vamodules;
72 
73  loginit ();
74  precinit ();
75  ::srand (::time (NULL));
76 
77  // check program arguments
78  for (int i = 1; i < argc; i++) {
79  if (!strcmp (argv[i], "-v") || !strcmp (argv[i], "--version")) {
80  fprintf (stdout,
81  "Qucsator " PACKAGE_VERSION "\n"
82  "Copyright (C) 2003-2009 "
83  "Stefan Jahn <stefan@lkcc.org>\n"
84  "Copyright (C) 2006 Helene Parruitte <parruit@enseirb.fr>\n"
85  "Copyright (C) 2006 Bastien Roucaries <roucaries.bastien@gmail.com>\n"
86  "\nThis is free software; see the source for copying "
87  "conditions. There is NO\n"
88  "warranty; not even for MERCHANTABILITY or FITNESS FOR A "
89  "PARTICULAR PURPOSE.\n");
90  return 0;
91  }
92  if (!strcmp (argv[i], "-h") || !strcmp (argv[i], "--help")) {
93  fprintf (stdout,
94  "Usage: %s [OPTION]...\n\n"
95  " -h, --help display this help and exit\n"
96  " -v, --version display version information and exit\n"
97  " -i FILENAME use file as input netlist (default stdin)\n"
98  " -o FILENAME use file as output dataset (default stdout)\n"
99  " -b, --bar enable textual progress bar\n"
100  " -g, --gui special progress bar used by gui\n"
101  " -c, --check check the input netlist and exit\n"
102 #if DEBUG
103  " -l, --listing emit C-code for available definitions\n"
104 #endif
105  " -p, --path project path (or location of dynamic modules)\n"
106  " -m, --module list of dynamic loaded modules (base names separated by space)\n"
107  "\nReport bugs to <" PACKAGE_BUGREPORT ">.\n", argv[0]);
108  return 0;
109  }
110  else if (!strcmp (argv[i], "-i")) {
111  infile = argv[++i];
112  }
113  else if (!strcmp (argv[i], "-o")) {
114  outfile = argv[++i];
115  file_status = stdout;
116  }
117  else if (!strcmp (argv[i], "-b") || !strcmp (argv[i], "--bar")) {
118  progressbar_enable = 1;
119  }
120  else if (!strcmp (argv[i], "-g") || !strcmp (argv[i], "--gui")) {
121  progressbar_gui = 1;
122  }
123  else if (!strcmp (argv[i], "-c") || !strcmp (argv[i], "--check")) {
124  netlist_check = 1;
125  }
126  else if (!strcmp (argv[i], "-l") || !strcmp (argv[i], "--listing")) {
127  listing = 1;
128  }
129  // \todo remove command arguments
130  else if (!strcmp (argv[i], "-p") || !strcmp (argv[i], "--path")) {
131  projPath = argv[++i];
132  }
133  else if (!strcmp (argv[i], "-m") || !strcmp (argv[i], "--module")) {
134  dynamicLoad = 1;
135  }
136  else {
137  if (dynamicLoad) {
138  vamodules.push_back(argv[i]);
139  }
140  }
141  }
142 
143  // create static modules
145 
146 #if DEBUG
147  // emit C-code for available definitions if requested and exit
148  if (listing) {
149  module::print ();
150  return ret;
151  }
152 #endif /* DEBUG */
153 
154  // look for dynamic libs, load and register them
155  // \todo, keep this way of loading or keep only annotated netlist?
156  if (dynamicLoad) {
157  module::registerDynamicModules (projPath, vamodules);
158  }
159 
160  else { //no argument, look into netlist
161 
162  std::string sLine = "";
163  std::ifstream file;
164 
165  std::string projPathNet ="";
166  std::string projVaMoules = "";
167 
168  file.open(infile);
169 
170  while (!file.eof()) {
171  getline(file, sLine);
172 
173  if (sLine.find("--path") != std::string::npos) {
174  std::cout << sLine << std::endl;
175 
176  size_t pos = 0;
177  pos = sLine.find("=");
178  sLine.erase(0, pos + 1);
179  std::cout << sLine << std::endl;
180 
181 // projPath = const_cast<char*>(sLine.c_str());
182 // projPath = (char*)sLine.c_str();
183 
184 // std::cout << "inside" << projPath << std::endl;
185 
186  projPathNet = sLine;
187 
188  }
189 
190  if (sLine.find("--module") != std::string::npos) {
191  std::cout << sLine << std::endl;
192 
193  size_t pos = 0;
194  pos = sLine.find("=");
195  sLine.erase(0, pos + 1);
196  //std::cout << sLine << std::endl;
197  projVaMoules = sLine;
198 
199  // put module names into list
200  std::istringstream ss(sLine);
201  std::string token;
202 
203  while(std::getline(ss, token, ' ')) {
204  std::cout << token << '\n';
205 
206  vamodules.push_back(token);
207  }
208  }
209  }
210 
211  module::registerDynamicModules ((char*)projPathNet.c_str(), vamodules);
212  file.close();
213  }
214 
215 
216  // create root environment
217  root = new environment ("root");
218 
219  // create netlist object and input
220  subnet = new net ("subnet");
221  in = infile ? new input (infile) : new input ();
222 
223  // pass root environment to netlist object and input
224  subnet->setEnv (root);
225  in->setEnv (root);
226 
227  // get input netlist
228  if (in->netlist (subnet) != 0) {
229  if (netlist_check) {
230  logprint (LOG_STATUS, "checker notice, netlist check FAILED\n");
231  }
232  return -1;
233  }
234  if (netlist_check) {
235  logprint (LOG_STATUS, "checker notice, netlist OK\n");
236  return 0;
237  }
238 
239  // attach a ground to the netlist
240  gnd = new ground ();
241  gnd->setNode (0, "gnd");
242  gnd->setName ("GND");
243  subnet->insertCircuit (gnd);
244 
245  // analyse the netlist
246  int err = 0;
247  out = subnet->runAnalysis (err);
248  ret |= err;
249 
250  // evaluate output dataset
251  ret |= root->equationSolver (out);
252  out->setFile (outfile);
253  out->print ();
254 
255  estack.print ("uncaught");
256 
257  delete subnet;
258  delete in;
259  delete out;
260  delete root;
261 
262  // delete static modules and dynamic modules
264 
265  // close all the dynamic libs if any opened
267 
269  return ret;
270 }
exceptionstack estack
static void registerDynamicModules(char *proj, std::list< std::string > modlist)
Definition: module.cpp:441
static void registerModules(void)
Definition: module.cpp:156
i
Definition: parse_mdl.y:516
int netlist_check
Definition: input.cpp:51
FILE * file_status
Definition: logging.c:56
int main(int argc, char **argv)
Definition: ucs.cpp:57
int progressbar_gui
Definition: logging.c:87
void print(const char *prefix=NULL)
static void closeDynamicLibs(void)
Definition: module.cpp:553
void loginit(void)
Definition: logging.c:51
void precinit(void)
Definition: precision.c:36
void netlist_destroy_env(void)
The environment class definition.
static void unregisterModules(void)
Definition: module.cpp:323
#define LOG_STATUS
Definition: logging.h:29
void logprint(int level, const char *format,...)
Definition: logging.c:37
Definition: ground.h:28
int progressbar_enable
Definition: logging.c:84
static void print(void)