main.cpp
1 /*
2  name: tools/engine/main.cpp
3 
4  This file is part of ARCS - Augmented Reality Component System
5  (version 2-current), written by Jean-Yves Didier
6  for IBISC Laboratory (http://www.ibisc.univ-evry.fr)
7 
8  Copyright (C) 2013 Université d'Evry-Val d'Essonne
9 
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Please send bugreports with examples or suggestions to
25  jean-yves.didier__at__ibisc.univ-evry.fr
26 */
27 
28 
29 #include <arcs/arcsapplicationcomponent.h>
30 #include <arcs/arcsxmlhandler.h>
31 #include <arcs/arcslog.h>
32 
33 
185 void usage(char* name)
186 {
187  std::cout << name << " [OPTION]... [XML_FILE]..." << std::endl
188  << std::endl;
189  std::cout << " -h, --help : print this help" << std::endl << std::endl;
190  std::cout << "Overriding application mode : " << std::endl << std::endl;
191  std::cout << " -b, --mode-base : simple loop based applications."
192  << std::endl;
193  std::cout << " -e, --mode-event : event loop based console applications."
194  << std::endl;
195  std::cout << " -g, --mode-gui : event loop based GUI applications."
196  << std::endl;
197  std::cout << " -t, --mode-thread : threaded application." << std::endl;
198  std::cout << " -te, --mode-thread-event: threaded event based application." << std::endl <<std::endl;
199  std::cout << "Defining options:" << std::endl;
200  std::cout << " -d, --define vars : define constants" << std::endl;
201  std::cout << " -p, --profile file : define a profile" << std::endl;
202  std::cout << " -o, --profile-out : define a file where to dump profile" << std::endl;
203  std::cout << " -l [0-2] : define output log mode - 0, none; 1, console (default); 2, html" << std::endl;
204 }
205 
206 
207 void parseOptions(int argc, char* argv[], ARCS::ARCSAppFlag & flag, QString & constantList, QString & xmlfile, QString & profile, QString& profileOut)
208 {
209  int i=1;
210  flag = ARCS::ARCS_APP_NONE;
211 
212  for(i=1; i < argc; i++)
213  {
214  QString current(argv[i]);
215 
216  if (current == "-h" || current == "--help")
217  usage(argv[0]);
218  else
219  if (current == "-b" || current == "--mode-base")
220  flag = ARCS::ARCS_APP_BASE;
221  else
222  if (current == "-e" || current == "--mode-event")
223  flag = ARCS::ARCS_APP_EVENT;
224  else
225  if (current == "-g" || current == "--mode-gui")
226  flag = ARCS::ARCS_APP_GUI;
227  else
228  if (current == "-t" || current == "--mode-thread")
229  flag = ARCS::ARCS_APP_THREAD;
230  else
231  if (current == "-te" || current == "--mode-thread-event")
233  else
234  if (current == "-l")
235  {
236  if (i+1 < argc)
237  {
238  ARCSLog::getInstance()->setLogMode((ARCSLog::LogModes)QString(argv[i+1]).toInt());
239  i++;
240  }
241  }
242 
243  else
244  if (current == "-d" || current == "--define")
245  {
246  if (i+1 < argc)
247  {
248  constantList = argv[i+1];
249  i++;
250  }
251  }
252  else
253  {
254  if (current == "-p" || current == "--profile")
255  {
256  if ( i+1 < argc)
257  {
258  profile = argv[i+1];
259  i++;
260  }
261  }
262  else
263  {
264  if (current == "-o" || current == "--profile-out")
265  {
266  if (i+1 < argc)
267  {
268  profileOut = argv[i+1];
269  i++;
270  }
271 
272  }
273  else
274  xmlfile = argv[i];
275  }
276  }
277  }
278 }
279 
280 
281 void parseConstants(QString arglist, ARCSApplicationComponent* app)
282 {
283  int i;
284  QStringList lst = arglist.split(":");
285  for (i=0; i < lst.count(); i++)
286  {
287  QStringList lst_2 = lst[i].split("="); // silly windows naming !
288  if (lst_2.count() == 2)
289  {
290  std::cout << "Overriding constant " << qPrintable(lst_2[0])
291  << " with value " << qPrintable(lst_2[1]) << std::endl;
292  app->changeConstant(lst_2[0], lst_2[1]);
293  }
294  }
295 }
296 
297 
298 
299 
300 
301 int main(int argc, char* argv[])
302 {
304  QString xmlfile ;
305  QString profileFile;
306  QString profileOut;
307  ARCS::ARCSAppFlag flag;
308  QString constantList;
309 
310  parseOptions(argc,argv, flag, constantList, xmlfile,profileFile,profileOut);
311 
312  if (xmlfile.isEmpty())
313  {
314  usage(argv[0]);
315  return 0;
316  }
317 
318  if (flag != ARCS::ARCS_APP_NONE)
319  app->setCurrentMode(flag);
320 
321  if (!app->loadFile(xmlfile)) //parseString(xmlfile))
322  {
323  std::cerr << "An error occured when trying to load the application" << std::endl;
324  return 0;
325  }
326 
327  std::cout << "Application " << qPrintable(xmlfile) << " loaded." << std::endl
328  << "==================================================" << std::endl;
329 
330  if (!profileFile.isEmpty())
331  {
332  ARCSXMLHandler* xmlHandler= new ARCSXMLHandler();
333  if (xmlHandler->openFile(profileFile))
334  if (! xmlHandler->parseProfile(app->getContext()))
335  std::cerr << "Could not read profile file." << std::endl;
336  delete xmlHandler;
337  }
338 
339 
340  if (!constantList.isEmpty())
341  parseConstants(constantList, app);
342 
343 
344  app->startApplication();
345  app->wait();
346  if (!profileOut.isEmpty())
347  {
348  ARCSXMLHandler* xmlHandler= new ARCSXMLHandler();
349  xmlHandler->storeProfile(app->getContext());
350  if (!xmlHandler->saveFile(profileOut))
351  std::cerr << "Could not write profile file." << std::endl;
352  delete xmlHandler;
353  }
354 
355  return 0;
356 }
ARCSAppFlag
Flags to specify in which mode applications should start.
Definition: arcs.h:41
For applications running with Qt Widgets.
Definition: arcs.h:48
void changeConstant(QString name, QString representation)
This hack allows to change the value of a constant in the constant pool.
This class represents an application.
void setCurrentMode(ARCS::ARCSAppFlag flag)
bool openFile(QString s)
Opens a file given its path. The file should describe either an application or a composite component...
None of the application mode has been selected so far.
Definition: arcs.h:43
For applications nested in a thread.
Definition: arcs.h:46
static ARCSLog * getInstance()
returns the only instance of the logging system that is available
Definition: arcslog.cpp:188
LogModes
logging modes
Definition: arcslog.h:212
bool saveFile(QString s)
Saves a file given its path. The file will describe either an application or a composite component...
virtual bool loadFile(QString fn)
Defines a way to load a component description from a file.
For applications nested in a thread running with an event loop.
Definition: arcs.h:47
For applications requiring event loops.
Definition: arcs.h:45
This is an XML Handler for all ARCS native XML formats.
For basic applications.
Definition: arcs.h:44