Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

QgarApp.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------+
00002  | Library QgarLib, graphics analysis and recognition                  |
00003  | Copyright (C) 2002  Qgar Project, LORIA                             |
00004  |                                                                     |
00005  | This library is free software; you can redistribute it and/or       |
00006  | modify it under the terms of the GNU Lesser General Public          |
00007  | License version 2.1, as published by the Free Software Foundation.  |
00008  |                                                                     |
00009  | This library is distributed in the hope that it will be useful,     |
00010  | but WITHOUT ANY WARRANTY; without even the implied warranty of      |
00011  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                |
00012  | See the GNU Lesser General Public License for more details.         |
00013  |                                                                     |
00014  | The GNU Lesser General Public License is included in the file       |
00015  | LICENSE.LGPL, in the root directory of the Qgar packaging. See      |
00016  | http://www.gnu.org/licenses/lgpl.html for the terms of the licence. |
00017  | To receive a paper copy, write to the Free Software Foundation,     |
00018  | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.       |
00019  |                                                                     |
00020  | Contact Project Qgar for any information:                           |
00021  |   LORIA - équipe Qgar                                               |
00022  |   B.P. 239, 54506 Vandoeuvre-lès-Nancy Cedex, France                |
00023  |   email: qgar-contact@loria.fr                                      |
00024  |   http://www.qgar.org/                                              |
00025  *---------------------------------------------------------------------*/
00026 
00027 
00028 /**
00029  * @file    QgarApp.C
00030  * @brief   Implementation of class qgar::QgarApp.
00031  *
00032  * See file QgarApp.H for the interface.
00033  *
00034  * @author   <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Yvan Norsa">Yvan Norsa</a>
00035  * @date     July 25, 2002  11:34
00036  * @since    Qgar 2.0
00037  */
00038 
00039 
00040 
00041 // STD
00042 #include <iostream>
00043 // Qgar
00044 #include <qgarlib/QgarApp.H>
00045 #include <qgarlib/QgarArgs.H>
00046 
00047 
00048 
00049 using namespace std;
00050 
00051 
00052 namespace qgar
00053 {
00054 
00055 
00056 // -------------------------------------------------------------------
00057 // T E R M I N A T I O N    C O D E S    ( S T A T I C )
00058 // -------------------------------------------------------------------
00059 
00060 
00061 // VALUE TO BE RETURNED BY AN APPLICATION WHICH NORMALLY TERMINATES
00062 
00063 const unsigned char
00064 QgarApp::_CODE_END   = 100;
00065 
00066 
00067 // VALUE TO BE RETURNED BY AN APPLICATION INVOKED WITH FLAG '-gui'
00068 
00069 const unsigned char
00070 QgarApp::_CODE_GUI   = 101;
00071     
00072 
00073 // VALUE TO BE RETURNED BY AN APPLICATION WHICH ABNORMALLY TERMINATES
00074 
00075 const unsigned char
00076 QgarApp::_CODE_ERROR = 200;
00077 
00078 
00079 // -------------------------------------------------------------------
00080 // C O N S T R U C T O R S
00081 // -------------------------------------------------------------------
00082 
00083 
00084 // DEFAULT CONSTRUCTOR
00085 
00086 QgarApp::QgarApp()
00087   
00088   : _exit(false),
00089     _action(QGE_ACTION_NONE),
00090     _actionCnt(0)
00091   
00092 {
00093   // VOID
00094 }
00095 
00096 
00097 // -------------------------------------------------------------------
00098 // D E S T R U C T O R
00099 // -------------------------------------------------------------------
00100 
00101 
00102 // VIRTUAL DESTRUCTOR
00103 
00104 QgarApp::~QgarApp()
00105 {
00106   // VOID
00107 }
00108 
00109 
00110 // -------------------------------------------------------------------
00111 // T H E   C O M M A N D   L I N E
00112 // -------------------------------------------------------------------
00113 
00114 
00115 // GET NEXT WORD OF COMMAND LINE (BLANK SPACE AS SEPARATOR)
00116 
00117 char*
00118 QgarApp::nextWord(char* aComLine, int* anIdx)
00119 {
00120   int idx;
00121 
00122   for(int idx = *anIdx ; idx < (int)strlen(aComLine) ; ++idx)
00123     {
00124       if(aComLine[idx] == ' ')
00125         {
00126           break;
00127         }
00128     }
00129 
00130   char* res = new char[idx - (*anIdx) + 1];
00131 
00132   int kdx = 0;
00133 
00134   for(int jdx = (*anIdx); jdx < idx; ++jdx, ++kdx)
00135     {
00136       res[kdx] = aComLine[jdx];
00137     }
00138   res[kdx + 1] = '\0';
00139 
00140   (*anIdx) = idx + 1;
00141 
00142   return(res);
00143 }
00144 
00145 
00146 // -------------------------------------------------------------------
00147 // I N P U T
00148 // -------------------------------------------------------------------
00149 
00150 
00151 // READ ON STANDARD INPUT
00152 
00153 void 
00154 QgarApp::nextCommand()
00155 {
00156   char command[64];  
00157   cin.getline(command, 64, '\n');
00158 
00159   if(strcmp(command, "EXIT") == 0)
00160     {
00161       _exit = true;
00162       return;
00163     }
00164 
00165   int * pos2 = new int();
00166   *pos2 = 0;
00167   
00168   char * cmd = nextWord(command, pos2);
00169 
00170   if(strcmp(cmd, "SET") == 0)
00171     {
00172       _action = QGE_ACTION_SET;
00173     }
00174   else
00175     {
00176       return;
00177     }
00178 
00179   // Looking for a variable name
00180   while(*pos2 < (int)strlen(command))
00181     {
00182       char * target = nextWord(command, pos2);
00183 
00184       list<string>::iterator it;
00185 
00186       bool ok = false;
00187 
00188       for (it = _paramNameTab.begin(); it != _paramNameTab.end(); ++it)
00189         {
00190           const char * tmp2 = (*it).c_str();
00191           char * tmp = new char[strlen(tmp2) + 1];
00192       
00193           int i = 1;
00194           int j;
00195       
00196           for(j = 0; j < (int)strlen(tmp2); ++j)
00197             {
00198               tmp[j] = tmp2[i];
00199               ++i;
00200             }
00201 
00202           tmp[j] = '\0';
00203       
00204           if(strcmp(tmp, target) == 0)
00205             {
00206               ok = true;
00207               break;
00208             }
00209           delete[] tmp;
00210         }
00211             
00212       if(!ok)
00213         {
00214           return;
00215         }
00216 
00217       char* value = nextWord(command, pos2);
00218 
00219       _value = value;
00220       setValue(target, value);
00221 
00222       if(_action == QGE_ACTION_SET)
00223         {
00224           ++_actionCnt;
00225         }
00226     } // END while
00227 }
00228 
00229 
00230 // HAS a "SET <name> <value>" COMMAND BEEN READ?
00231 
00232 bool 
00233 QgarApp::newValue(const char* aName)
00234 {
00235   if(_action == QGE_ACTION_SET)
00236     {
00237       string tmp("-");
00238       tmp += aName;
00239     
00240       QgarArgs::QgarParam* param = _args.findParam(tmp.c_str());
00241       
00242       if(param != 0)
00243         {
00244           return(param->newValue());
00245         }
00246     }
00247   
00248   return false;
00249 }
00250 
00251 
00252 // -------------------------------------------------------------------
00253 // T H E   P A R A M E T E R S
00254 // -------------------------------------------------------------------
00255 
00256 
00257 // DECLARE A PARAMETER SUPPORTED BY AN APPLICATION
00258 
00259 void
00260 QgarApp::addParameter(const char* aParamName,
00261                       QgarArgs::QGEparamStatus aParamStatus,
00262                       QgarArgs::QGEparamType aParamType,
00263                       const char* aDescription,
00264                       const char* anExtension,
00265                       const char* aDefault)
00266 {
00267   if((aParamType >= QgarArgs::INT) && (aParamType <= QgarArgs::BOOL))
00268     {
00269       _paramNameTab.push_back(string(aParamName));
00270     }
00271 
00272   _args.addParameter(aParamName,
00273                      aParamStatus,
00274                      aParamType,
00275                      aDescription,
00276                      anExtension,
00277                      aDefault);
00278 }
00279 
00280 
00281 // GET THE STRING ASSOCIATED WITH A PARAMETER
00282 
00283 const char*
00284 QgarApp::getStringOption (const char* aFlag)
00285 {
00286   bool special = false;
00287     
00288   int size = (int)strlen(aFlag);
00289   char tmp[size + 1];
00290 
00291   int idx;
00292 
00293   for(idx = 0; idx < size; ++idx)
00294     {
00295       tmp[idx] = aFlag[idx];
00296     }
00297 
00298   tmp[idx] = '\0';
00299 
00300   char _flag[strlen(tmp)];
00301 
00302   int jdx = 0;
00303 
00304   for(idx = 1; idx < (int)strlen(tmp); ++idx)
00305     {
00306       _flag[jdx] = tmp[idx];
00307       ++jdx;
00308     }
00309   _flag[jdx] = '\0';
00310     
00311   if(_action == QGE_ACTION_SET)
00312     {
00313       if(_actionCnt > 0)
00314         {
00315           --_actionCnt;
00316         }
00317       else
00318         {
00319           _action = QGE_ACTION_NONE;
00320         }
00321       
00322       special = true;           
00323     }
00324     
00325   const char* res = _args.getStringOption(aFlag, special);
00326   
00327   return(res);
00328 }
00329    
00330 
00331 // -------------------------------------------------------------------
00332 // DIALOG WITH THE QgarGui USER INTERFACE
00333 // -------------------------------------------------------------------
00334 
00335 
00336 // DISPLAY A DIALOG TO CHANGE PARAMETER VALUES
00337 
00338 void
00339 QgarApp::showDialog()
00340 {
00341   cout << "Show slider";
00342 
00343   list<string>::iterator it;
00344 
00345   // Display the list of every modifiable parameter
00346 
00347   for (it = _paramNameTab.begin(); it != _paramNameTab.end(); ++it)
00348     {
00349       cout << ' ';
00350       QgarArgs::QgarParam* param = _args.findParam(it->c_str());
00351       param->printGUI(false);
00352     }
00353 
00354   cout << std::endl;
00355 }
00356 
00357 
00358 // -------------------------------------------------------------------
00359 
00360 
00361 } // namespace qgar