args.h

Go to the documentation of this file.
00001 /*
00002  * args.h
00003  *
00004  * Program Argument Parsing class
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
00025  * All Rights Reserved.
00026  *
00027  * Contributor(s): ______________________________________.
00028  *
00029  * $Log: args.h,v $
00030  * Revision 1.28  2005/11/30 12:47:37  csoutheren
00031  * Removed tabs, reformatted some code, and changed tags for Doxygen
00032  *
00033  * Revision 1.27  2005/11/25 03:43:47  csoutheren
00034  * Fixed function argument comments to be compatible with Doxygen
00035  *
00036  * Revision 1.26  2005/11/20 22:02:59  dereksmithies
00037  * Fix tags so they work with doxygen.
00038  *
00039  * Revision 1.25  2005/01/26 05:37:43  csoutheren
00040  * Added ability to remove config file support
00041  *
00042  * Revision 1.24  2003/09/17 01:18:01  csoutheren
00043  * Removed recursive include file system and removed all references
00044  * to deprecated coooperative threading support
00045  *
00046  * Revision 1.23  2003/03/27 07:27:07  robertj
00047  * Added function to get a bunch of arguments as a string array.
00048  *
00049  * Revision 1.22  2002/09/16 01:08:59  robertj
00050  * Added #define so can select if #pragma interface/implementation is used on
00051  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00052  *
00053  * Revision 1.21  2001/12/15 04:49:06  robertj
00054  * Added stream I/O functions for argument list.
00055  *
00056  * Revision 1.20  2000/05/25 11:05:31  robertj
00057  * Added PConfigArgs class so can save program arguments to config files.
00058  *
00059  * Revision 1.19  1999/03/09 02:59:49  robertj
00060  * Changed comments to doc++ compatible documentation.
00061  *
00062  * Revision 1.18  1999/02/16 08:07:10  robertj
00063  * MSVC 6.0 compatibility changes.
00064  *
00065  * Revision 1.17  1998/11/01 04:56:51  robertj
00066  * Added BOOl return value to Parse() to indicate there are parameters available.
00067  *
00068  * Revision 1.16  1998/10/30 11:22:14  robertj
00069  * Added constructors that take strings as well as const char *'s.
00070  *
00071  * Revision 1.15  1998/10/30 05:24:29  robertj
00072  * Added return value to << and >> operators for shifting arguments.
00073  *
00074  * Revision 1.14  1998/10/29 05:35:14  robertj
00075  * Fixed porblem with GetCount() == 0 if do not call Parse() function.
00076  *
00077  * Revision 1.13  1998/10/28 03:26:41  robertj
00078  * Added multi character arguments (-abc style) and options precede parameters mode.
00079  *
00080  * Revision 1.12  1998/10/28 00:59:46  robertj
00081  * New improved argument parsing.
00082  *
00083  * Revision 1.11  1998/09/23 06:20:14  robertj
00084  * Added open source copyright license.
00085  *
00086  * Revision 1.10  1995/12/10 11:26:38  robertj
00087  * Fixed signed/unsigned bug in shift count.
00088  *
00089  * Revision 1.9  1995/06/17 11:12:17  robertj
00090  * Documentation update.
00091  *
00092  * Revision 1.8  1995/03/14 12:40:58  robertj
00093  * Updated documentation to use HTML codes.
00094  *
00095  * Revision 1.7  1994/12/05  11:15:13  robertj
00096  * Documentation.
00097  *
00098  * Revision 1.6  1994/11/26  03:44:19  robertj
00099  * Documentation.
00100  *
00101  * Revision 1.6  1994/11/24  11:48:26  robertj
00102  * Documentation.
00103  *
00104  * Revision 1.5  1994/08/23  11:32:52  robertj
00105  * Oops
00106  *
00107  * Revision 1.4  1994/08/22  00:46:48  robertj
00108  * Added pragma fro GNU C++ compiler.
00109  *
00110  * Revision 1.3  1994/07/27  05:58:07  robertj
00111  * Synchronisation.
00112  *
00113  * Revision 1.2  1994/07/17  10:46:06  robertj
00114  * Changed to use container classes to plug memory leak.
00115  *
00116  * Revision 1.1  1994/04/01  14:08:52  robertj
00117  * Initial revision
00118  *
00119  */
00120 
00121 #ifndef _PARGLIST
00122 #define _PARGLIST
00123 
00124 #ifdef P_USE_PRAGMA
00125 #pragma interface
00126 #endif
00127 
00132 class PArgList : public PObject
00133 {
00134   PCLASSINFO(PArgList, PObject);
00135 
00136   public:
00149     PArgList(
00150       const char * theArgPtr = NULL,        
00151       const char * argumentSpecPtr = NULL,  
00152       BOOL optionsBeforeParams = TRUE       
00153     );
00155     PArgList(
00156       const PString & theArgStr,             
00157       const char * argumentSpecPtr = NULL,   
00158       BOOL optionsBeforeParams = TRUE        
00159     );
00161     PArgList(
00162       const PString & theArgStr,             
00163       const PString & argumentSpecStr,       
00164       BOOL optionsBeforeParams = TRUE        
00165     );
00167     PArgList(
00168       int theArgc,                           
00169       char ** theArgv,                       
00170       const char * argumentSpecPtr = NULL,   
00171       BOOL optionsBeforeParams = TRUE        
00172     );
00174     PArgList(
00175       int theArgc,                           
00176       char ** theArgv,                       
00177       const PString & argumentSpecStr,       
00178       BOOL optionsBeforeParams = TRUE        
00179     );
00181 
00186     virtual void PrintOn(
00187       ostream & strm  
00188     ) const;
00189 
00193     virtual void ReadFrom(
00194       istream & strm  
00195     );
00197 
00202     void SetArgs(
00203       const PString & theArgStr 
00204     );
00206     void SetArgs(
00207       int theArgc,     
00208       char ** theArgv  
00209     );
00211     void SetArgs(
00212       const PStringArray & theArgs 
00213     );
00214 
00237     virtual BOOL Parse(
00238       const char * theArgumentSpec,    
00239       BOOL optionsBeforeParams = TRUE  
00240     );
00242     virtual BOOL Parse(
00243       const PString & theArgumentStr,  
00244       BOOL optionsBeforeParams = TRUE  
00245     );
00247 
00255     virtual PINDEX GetOptionCount(
00256       char optionChar        
00257     ) const;
00259     virtual PINDEX GetOptionCount(
00260       const char * optionStr 
00261     ) const;
00263     virtual PINDEX GetOptionCount(
00264       const PString & optionName 
00265     ) const;
00266 
00272     BOOL HasOption(
00273       char optionChar             
00274     ) const;
00276     BOOL HasOption(
00277       const char * optionStr     
00278     ) const;
00280     BOOL HasOption(
00281       const PString & optionName 
00282     ) const;
00283 
00292     virtual PString GetOptionString(
00293       char optionChar,          
00294       const char * dflt = NULL  
00295     ) const;
00297     virtual PString GetOptionString(
00298       const char * optionStr,   
00299       const char * dflt = NULL  
00300     ) const;
00302     virtual PString GetOptionString(
00303       const PString & optionName, 
00304       const char * dflt = NULL    
00305     ) const;
00306 
00314     PINDEX GetCount() const;
00315 
00320     PStringArray GetParameters(
00321       PINDEX first = 0,
00322       PINDEX last = P_MAX_INDEX
00323     ) const;
00324 
00329     PString GetParameter(
00330       PINDEX num   
00331     ) const;
00332 
00338     PString operator[](
00339       PINDEX num   
00340     ) const;
00341 
00345     void Shift(
00346       int sh 
00347     );
00348 
00352     PArgList & operator<<(
00353       int sh 
00354     );
00355 
00359     PArgList & operator>>(
00360       int sh 
00361     );
00363 
00370     virtual void IllegalArgumentIndex(
00371       PINDEX idx 
00372     ) const;
00373 
00378     virtual void UnknownOption(
00379       const PString & option   
00380     ) const;
00381 
00387     virtual void MissingArgument(
00388       const PString & option  
00389     ) const;
00391 
00392   protected:
00394     PStringArray argumentArray;
00396     PString      optionLetters;
00398     PStringArray optionNames;
00400     PIntArray    optionCount;
00402     PStringArray optionString;
00404     PIntArray    parameterIndex;
00406     int          shift;
00407 
00408   private:
00409     BOOL ParseOption(PINDEX idx, PINDEX offset, PINDEX & arg, const PIntArray & canHaveOptionString);
00410     PINDEX GetOptionCountByIndex(PINDEX idx) const;
00411     PString GetOptionStringByIndex(PINDEX idx, const char * dflt) const;
00412 };
00413 
00414 
00415 #ifdef P_CONFIG_FILE
00416 
00420 class PConfigArgs : public PArgList
00421 {
00422     PCLASSINFO(PConfigArgs, PArgList);
00423   public:
00426     PConfigArgs(
00427       const PArgList & args   
00428     );
00430 
00438     virtual PINDEX GetOptionCount(
00439       char optionChar  
00440     ) const;
00442     virtual PINDEX GetOptionCount(
00443       const char * optionStr 
00444     ) const;
00446     virtual PINDEX GetOptionCount(
00447       const PString & optionName 
00448     ) const;
00449 
00458     virtual PString GetOptionString(
00459       char optionChar,          
00460       const char * dflt = NULL  
00461     ) const;
00462 
00464     virtual PString GetOptionString(
00465       const char * optionStr,   
00466       const char * dflt = NULL  
00467     ) const;
00468 
00470     virtual PString GetOptionString(
00471       const PString & optionName, 
00472       const char * dflt = NULL    
00473     ) const;
00475 
00484     void Save(
00485       const PString & optionName   
00486     );
00487 
00490     void SetSectionName(
00491       const PString & section 
00492     ) { sectionName = section; }
00493 
00496     const PString & GetSectionName() const { return sectionName; }
00497 
00501     void SetNegationPrefix(
00502       const PString & prefix 
00503     ) { negationPrefix = prefix; }
00504 
00508     const PString & GetNegationPrefix() const { return negationPrefix; }
00510 
00511 
00512   protected:
00513     PString CharToString(char ch) const;
00514     PConfig config;
00515     PString sectionName;
00516     PString negationPrefix;
00517 };
00518 
00519 #endif // P_CONFIG_FILE
00520 
00521 #endif
00522 
00523 // End Of File ///////////////////////////////////////////////////////////////

Generated on Fri Mar 7 06:25:02 2008 for PTLib by  doxygen 1.5.1