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 #ifndef __QGARAPP_H_INCLUDED__ 00029 #define __QGARAPP_H_INCLUDED__ 00030 00031 00032 /** 00033 * @file QgarApp.H 00034 * @brief Header file of class qgar::QgarApp. 00035 * 00036 * @author <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Yvan Norsa">Yvan Norsa</a> 00037 * @date July 25, 2002 12:52 00038 * @since Qgar 1.0 00039 */ 00040 00041 00042 00043 // STD 00044 #include <iostream> 00045 #include <string> 00046 // QGAR 00047 #include <qgarlib/QgarArgs.H> 00048 00049 00050 00051 namespace qgar 00052 { 00053 00054 /** 00055 * @ingroup APP 00056 * 00057 * @class QgarApp QgarApp.H "qgarlib/QgarApp.H" 00058 * 00059 * @brief To handle parameters and interactive mode of an application. 00060 * 00061 * It is based on class qgar::QgarArgs. 00062 * 00063 * Here is a minimal and typical example of use of the class 00064 * for text-graphic separation: 00065 @code 00066 #include <qgarlib/QgarApp.H> 00067 #include <qgarlib/QgarArgs.H> 00068 00069 using namespace std; 00070 using namespace qgar; 00071 00072 int main(int argc, char* argv[]) 00073 { 00074 QgarApp app; 00075 00076 // INITIALIZATIONS 00077 // =============== 00078 00079 // Description of the parameters 00080 00081 app.addParameter("-in", 00082 QgarArgs::REQPARAM, 00083 QgarArgs::FILEIN, 00084 "Source image:"); 00085 00086 app.addParameter("-t", 00087 QgarArgs::REQPARAM, 00088 QgarArgs::FILEOUTD, 00089 "Text image:", 00090 ".text.pbm"); 00091 00092 app.addParameter("-g", 00093 QgarArgs::REQPARAM, 00094 QgarArgs::FILEOUTD, 00095 "Graphic image:", 00096 ".graph.pbm"); 00097 00098 app.addParameter("-s", 00099 QgarArgs::REQPARAM, 00100 QgarArgs::INT, 00101 "Dimension threshold:", 00102 0, 00103 "20"); 00104 00105 app.addParameter("-thr", 00106 QgarArgs::OPTPARAM, 00107 QgarArgs::INT, 00108 "Threshold:", 00109 0, 00110 "3"); 00111 00112 app.addParameter("-d", 00113 QgarArgs::REQPARAM, 00114 QgarArgs::FLOAT, 00115 "Black pixel density:", 00116 0, 00117 ".6"); 00118 00119 // Description of the application 00120 app.setDescription("Text-graphic separation", QgarArgs::PBM); 00121 00122 // COMMAND LINE ANALYSIS 00123 // ===================== 00124 00125 app.analyzeLine(argc, argv); 00126 00127 // Error while parsing parameters? 00128 if (app.isError()) 00129 { 00130 return app._CODE_ERROR; 00131 } 00132 00133 // Application invoked with flag '-gui'? 00134 if (app.isExit()) 00135 { 00136 return app._CODE_GUI; 00137 } 00138 00139 // Set progress bar on 00140 app.showProgressBar(); 00141 app.setProgressBar(0); 00142 00143 // GET ARGUMENTS 00144 // ============= 00145 00146 // Get the value of parameter introduced by flag '-in' 00147 // (it must be given, as this parameter is required) 00148 const char* name = app.getStringOption("-in"); 00149 00150 // Get the threshold value if defined by the command line 00151 int threshold = 3; 00152 if (app.isOptionSet("-thr")) 00153 { 00154 threshold = atoi(app.getStringOption("-thr")); 00155 } 00156 00157 app.setProgressBar(5); 00158 00159 // AND SO ON... 00160 // ============ 00161 00162 ... 00163 00164 // NORMAL TERMINATION 00165 // ================== 00166 00167 return app._CODE_END; 00168 00169 } // END main() 00170 @endcode 00171 * 00172 * @todo Complete and update documentation 00173 * 00174 * @author <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Yvan Norsa">Yvan Norsa</a> 00175 * @date July 25, 2002 12:52 00176 * @since Qgar 1.0 00177 */ 00178 00179 class QgarApp 00180 { 00181 // ------------------------------------------------------------------- 00182 // P U B L I C M E M B E R S 00183 // ------------------------------------------------------------------- 00184 public: 00185 00186 /** @name Constructors */ 00187 // ============ 00188 //@{ 00189 00190 /** 00191 * @brief Default constructor. 00192 */ 00193 QgarApp(); 00194 00195 //@} 00196 00197 00198 /**@name Destructor */ 00199 // ========== 00200 //@{ 00201 00202 /** 00203 * @brief Virtual destructor. 00204 */ 00205 virtual ~QgarApp(); 00206 00207 //@} 00208 00209 00210 /**@name The application */ 00211 // =============== 00212 //@{ 00213 00214 /** 00215 * @brief Set application description. 00216 * 00217 * @param aDescription application description 00218 * @param aFileType type of the source file 00219 */ 00220 inline void setDescription(const char* aDescription, 00221 QgarArgs::QGEfileType aFileType); 00222 00223 /** 00224 * @brief Does the client have to exit the application? 00225 * 00226 * The client has to exit if the application has been launched 00227 * with either a <b>-h</b> flag (help) or a <b>-gui</b> flag 00228 * (get application parameters). 00229 */ 00230 inline bool isExit() const; 00231 00232 /** 00233 * @brief Is the application in interactive mode? 00234 */ 00235 inline bool isInteractive() const; 00236 00237 /** 00238 * @brief Does the application have to exit? 00239 */ 00240 inline bool exit() const; 00241 00242 //@} 00243 00244 00245 /**@name The command line */ 00246 // ================ 00247 //@{ 00248 00249 /** 00250 * @brief Analyze the command line. 00251 * 00252 * @param argc number of arguments 00253 * @param argv the full command line 00254 */ 00255 inline void analyzeLine(int argc, char* argv[]); 00256 00257 /** 00258 * @brief Get next word of command line (blank space as separator). 00259 * 00260 * @param aComLine number of arguments 00261 * @param anIdx current index in line 00262 */ 00263 char* nextWord(char* aComLine, int* anIdx); 00264 00265 /** 00266 * @brief Is there an error in the command line? 00267 */ 00268 inline bool isError() const; 00269 00270 //@} 00271 00272 00273 /**@name Termination codes */ 00274 // ================= 00275 //@{ 00276 00277 /** 00278 * @brief Value to be returned by an application 00279 * which normally terminates. 00280 */ 00281 static const unsigned char _CODE_END; 00282 00283 /** 00284 * @brief Value to be returned by an application invoked 00285 * with flag <b>-gui</b>. 00286 */ 00287 static const unsigned char _CODE_GUI; 00288 00289 /** 00290 * @brief Value to be returned by an application 00291 * which abnormally terminates. 00292 */ 00293 static const unsigned char _CODE_ERROR; 00294 00295 //@} 00296 00297 00298 /**@name Input */ 00299 // ===== 00300 //@{ 00301 00302 /** 00303 * @brief Read on standard input. 00304 */ 00305 void nextCommand(); 00306 00307 /** 00308 * @brief Has a <b>SET *name* *value*</b> command been read? 00309 * @param aName 00310 */ 00311 bool newValue(const char* aName); 00312 00313 //@} 00314 00315 00316 /**@name The parameters */ 00317 // ============== 00318 //@{ 00319 00320 /** 00321 * @brief Declare a parameter supported by an application. 00322 * 00323 * This method must be called for each supported parameter. 00324 * 00325 * If a parameter is an output file, its name is the concatenation 00326 * of the name of the input file (without its extension) and of 00327 * argument <b>anExtension</b>. 00328 * 00329 * If a parameter is not an output file, its default value may be given 00330 * by argument <b>aDefault</b>. 00331 * 00332 * If the parameter status is qgar::QgarArgs::SINGLEFLAG, the corresponding 00333 * type must be qgar::QgarArgs::BOOL. 00334 * 00335 * @param aParamName parameter name, first character is a minus (e.g. <b>-f</b>) 00336 * @param aParamStatus parameter status (see enum type qgar::QgarArgs::ParamStatus) 00337 * @param aParamType parameter type (see enum type qgar::QgarArgs::ParamType) 00338 * @param aDescription short description of the parameter 00339 * @param anExtension file extension, in case of an output file 00340 * @param aDefault default value (given as a string) for a parameter 00341 * which is not a file 00342 * 00343 * @see class qgar::QgarArgs 00344 */ 00345 void addParameter(const char* aParamName, 00346 QgarArgs::QGEparamStatus aParamStatus, 00347 QgarArgs::QGEparamType aParamType, 00348 const char* aDescription, 00349 const char* anExtension = 0, 00350 const char* aDefault = 0); 00351 00352 /** 00353 * @brief Is an optional parameter set? 00354 * 00355 * @param aFlag flag introducing the parameter 00356 */ 00357 bool isOptionSet(const char* aFlag) const; 00358 00359 /** 00360 * @brief Get the string associated with a parameter. 00361 * 00362 * @param aFlag flag introducing the parameter 00363 * 00364 * @warning If the parameter is optional, check first that 00365 * the parameter is set, using function qgar::QgarApp::isOptionSet. 00366 */ 00367 const char* getStringOption (const char* aFlag); 00368 00369 /** 00370 * @brief Set a parameter value. 00371 * 00372 * @param aFlag flag introducing the parameter 00373 * @param aVal value to assign 00374 */ 00375 inline void setValue(char* aFlag, char* aVal); 00376 00377 //@} 00378 00379 00380 /**@name The progress bar */ 00381 // ================ 00382 //@{ 00383 00384 /** 00385 * @brief Display the progress bar. 00386 */ 00387 inline void showProgressBar(); 00388 00389 /** 00390 * @brief Close the progress bar. 00391 */ 00392 inline void closeProgressBar(); 00393 00394 /** 00395 * @brief Set progress bar to a given percentage. 00396 * 00397 * @param aPercentage value between <b>0</b> and <b>100</b> 00398 */ 00399 inline void setProgressBar(int aPercentage); 00400 00401 //@} 00402 00403 00404 /**@name The dialog with the QgarGui user interface */ 00405 // ========================================== 00406 //@{ 00407 00408 /** 00409 * @brief Display an image. 00410 * 00411 * @param aFileName name of the file including the image to be displayed 00412 */ 00413 inline void showPicture(char* aFileName); 00414 00415 /** 00416 * @brief Display the superimpose file. 00417 * 00418 * @param aFileName a file name 00419 */ 00420 inline void showSuperImpose(char* aFileName); 00421 00422 /** 00423 * @brief Display a dialog to change parameter values. 00424 */ 00425 void showDialog(); 00426 00427 //@} 00428 00429 // ------------------------------------------------------------------- 00430 // P R O T E C T E D M E M B E R S 00431 // ------------------------------------------------------------------- 00432 protected: 00433 00434 /**@name Type definitions */ 00435 // ================ 00436 //@{ 00437 00438 /** 00439 * @brief Action types. 00440 */ 00441 enum QGEaction 00442 { 00443 QGE_ACTION_NONE, 00444 QGE_ACTION_SET 00445 }; 00446 00447 //@} 00448 00449 00450 /**@name Information about parameters */ 00451 // ============================ 00452 //@{ 00453 00454 /** 00455 * @brief Table of parameter names. 00456 */ 00457 std::list<std::string> _paramNameTab; 00458 00459 //@} 00460 00461 00462 /**@name Information about the application */ 00463 // ================================= 00464 //@{ 00465 00466 /** 00467 * @brief Application arguments. 00468 */ 00469 QgarArgs _args; 00470 00471 /** 00472 * @brief Flag set if the application is about to end. 00473 */ 00474 bool _exit; 00475 00476 //@} 00477 00478 00479 /**@name Information about actions */ 00480 // ========================= 00481 //@{ 00482 00483 /** 00484 * @brief Nature of the current command. 00485 */ 00486 QGEaction _action; 00487 00488 /** 00489 * @brief Parameter of the current command. 00490 */ 00491 char* _value; 00492 00493 /** 00494 * @brief Current number of processed actions. 00495 */ 00496 int _actionCnt; 00497 00498 //@} 00499 00500 // ------------------------------------------------------------------- 00501 }; // class QgarApp 00502 00503 00504 00505 00506 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 00507 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 00508 // I N L I N E F U N C T I O N S I M P L E M E N T A T I O N 00509 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 00510 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 00511 00512 00513 // =============== 00514 // THE APPLICATION 00515 // =============== 00516 00517 00518 // SET APPLICATION DESCRIPTION 00519 00520 inline void 00521 QgarApp::setDescription(const char* aDescription, 00522 QgarArgs::QGEfileType aFileType) 00523 { 00524 _args.setDescription(aDescription, aFileType); 00525 } 00526 00527 00528 // DOES THE CLIENT HAVE TO EXIT THE APPLICATION? 00529 00530 inline bool 00531 QgarApp::isExit() const 00532 { 00533 return _args.isExit(); 00534 } 00535 00536 00537 // IS THE APPLICATION IN INTERACTIVE MODE? 00538 inline bool 00539 QgarApp::isInteractive() const 00540 { 00541 return _args.isInteractive(); 00542 } 00543 00544 00545 // DOES THE APPLICATION HAVE TO EXIT? 00546 00547 inline bool 00548 QgarApp::exit() const 00549 { 00550 return _exit; 00551 } 00552 00553 00554 // ================ 00555 // THE COMMAND LINE 00556 // ================ 00557 00558 00559 // ANALYZE THE COMMAND LINE 00560 00561 inline void 00562 QgarApp::analyzeLine(int argc, char* argv[]) 00563 { 00564 _args.analyzeLine(argc, argv); 00565 } 00566 00567 00568 // IS THERE AN ERROR IN THE COMMAND LINE? 00569 00570 inline bool 00571 QgarApp::isError() const 00572 { 00573 return _args.isError(); 00574 } 00575 00576 00577 // ============== 00578 // THE PARAMETERS 00579 // ============== 00580 00581 00582 // IS AN OPTIONAL PARAMETER SET? 00583 00584 inline bool 00585 QgarApp::isOptionSet(const char* aFlag) const 00586 { 00587 return _args.isOptionSet(aFlag); 00588 } 00589 00590 00591 // SET A PARAMETER NAME 00592 00593 inline void 00594 QgarApp::setValue(char* aFlag, char* aVal) 00595 { 00596 _args.setParamValue(aFlag, aVal); 00597 } 00598 00599 00600 // ================ 00601 // THE PROGRESS BAR 00602 // ================ 00603 00604 00605 // DISPLAY THE PROGRESS BAR 00606 00607 void 00608 QgarApp::showProgressBar() 00609 { 00610 std::cout << "Set progress bar ON" << std::endl; 00611 } 00612 00613 00614 // CLOSE THE PROGRESS BAR 00615 00616 void 00617 QgarApp::closeProgressBar() 00618 { 00619 std::cout << "Set progress bar OFF" << std::endl; 00620 } 00621 00622 00623 // SET PROGRESS BAR TO A GIVEN PERCENTAGE 00624 00625 void 00626 QgarApp::setProgressBar(int aPercentage) 00627 { 00628 std::cout << "Set progress bar " << aPercentage << std::endl; 00629 } 00630 00631 00632 // DISPLAY AN IMAGE 00633 00634 void 00635 QgarApp::showPicture(char *aFileName) 00636 { 00637 std::cout << "Show picture " << aFileName << std::endl; 00638 } 00639 00640 00641 // DISPLAY THE SUPERIMPOSE FILE 00642 00643 void 00644 QgarApp::showSuperImpose(char *aFileName) 00645 { 00646 std::cout << "Show superimpose " << aFileName << std::endl; 00647 } 00648 00649 00650 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 00651 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII 00652 00653 } // namespace qgar 00654 00655 00656 #endif /* __QGARAPP_H_INCLUDED__ */