Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

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.24  2003/09/17 01:18:01  csoutheren
00031  * Removed recursive include file system and removed all references
00032  * to deprecated coooperative threading support
00033  *
00034  * Revision 1.23  2003/03/27 07:27:07  robertj
00035  * Added function to get a bunch of arguments as a string array.
00036  *
00037  * Revision 1.22  2002/09/16 01:08:59  robertj
00038  * Added #define so can select if #pragma interface/implementation is used on
00039  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00040  *
00041  * Revision 1.21  2001/12/15 04:49:06  robertj
00042  * Added stream I/O functions for argument list.
00043  *
00044  * Revision 1.20  2000/05/25 11:05:31  robertj
00045  * Added PConfigArgs class so can save program arguments to config files.
00046  *
00047  * Revision 1.19  1999/03/09 02:59:49  robertj
00048  * Changed comments to doc++ compatible documentation.
00049  *
00050  * Revision 1.18  1999/02/16 08:07:10  robertj
00051  * MSVC 6.0 compatibility changes.
00052  *
00053  * Revision 1.17  1998/11/01 04:56:51  robertj
00054  * Added BOOl return value to Parse() to indicate there are parameters available.
00055  *
00056  * Revision 1.16  1998/10/30 11:22:14  robertj
00057  * Added constructors that take strings as well as const char *'s.
00058  *
00059  * Revision 1.15  1998/10/30 05:24:29  robertj
00060  * Added return value to << and >> operators for shifting arguments.
00061  *
00062  * Revision 1.14  1998/10/29 05:35:14  robertj
00063  * Fixed porblem with GetCount() == 0 if do not call Parse() function.
00064  *
00065  * Revision 1.13  1998/10/28 03:26:41  robertj
00066  * Added multi character arguments (-abc style) and options precede parameters mode.
00067  *
00068  * Revision 1.12  1998/10/28 00:59:46  robertj
00069  * New improved argument parsing.
00070  *
00071  * Revision 1.11  1998/09/23 06:20:14  robertj
00072  * Added open source copyright license.
00073  *
00074  * Revision 1.10  1995/12/10 11:26:38  robertj
00075  * Fixed signed/unsigned bug in shift count.
00076  *
00077  * Revision 1.9  1995/06/17 11:12:17  robertj
00078  * Documentation update.
00079  *
00080  * Revision 1.8  1995/03/14 12:40:58  robertj
00081  * Updated documentation to use HTML codes.
00082  *
00083  * Revision 1.7  1994/12/05  11:15:13  robertj
00084  * Documentation.
00085  *
00086  * Revision 1.6  1994/11/26  03:44:19  robertj
00087  * Documentation.
00088  *
00089  * Revision 1.6  1994/11/24  11:48:26  robertj
00090  * Documentation.
00091  *
00092  * Revision 1.5  1994/08/23  11:32:52  robertj
00093  * Oops
00094  *
00095  * Revision 1.4  1994/08/22  00:46:48  robertj
00096  * Added pragma fro GNU C++ compiler.
00097  *
00098  * Revision 1.3  1994/07/27  05:58:07  robertj
00099  * Synchronisation.
00100  *
00101  * Revision 1.2  1994/07/17  10:46:06  robertj
00102  * Changed to use container classes to plug memory leak.
00103  *
00104  * Revision 1.1  1994/04/01  14:08:52  robertj
00105  * Initial revision
00106  *
00107  */
00108 
00109 #ifndef _PARGLIST
00110 #define _PARGLIST
00111 
00112 #ifdef P_USE_PRAGMA
00113 #pragma interface
00114 #endif
00115 
00120 class PArgList : public PObject
00121 {
00122   PCLASSINFO(PArgList, PObject);
00123 
00124   public:
00137     PArgList(
00138       const char * theArgPtr = NULL,   
00139       const char * argumentSpecPtr = NULL,
00143       BOOL optionsBeforeParams = TRUE  
00144     );
00146     PArgList(
00147       const PString & theArgStr,   
00148       const char * argumentSpecPtr = NULL,
00152       BOOL optionsBeforeParams = TRUE  
00153     );
00155     PArgList(
00156       const PString & theArgStr,   
00157       const PString & argumentSpecStr,
00161       BOOL optionsBeforeParams = TRUE  
00162     );
00164     PArgList(
00165       int theArgc,     
00166       char ** theArgv, 
00167       const char * argumentSpecPtr = NULL,
00171       BOOL optionsBeforeParams = TRUE  
00172     );
00174     PArgList(
00175       int theArgc,     
00176       char ** theArgv, 
00177       const PString & argumentSpecStr,
00181       BOOL optionsBeforeParams = TRUE  
00182     );
00184 
00189     virtual void PrintOn(
00190       ostream & strm  
00191     ) const;
00192 
00196     virtual void ReadFrom(
00197       istream & strm  
00198     );
00200 
00205     void SetArgs(
00206       const PString & theArgStr 
00207     );
00209     void SetArgs(
00210       int theArgc,     
00211       char ** theArgv  
00212     );
00214     void SetArgs(
00215       const PStringArray & theArgs 
00216     );
00217 
00240     virtual BOOL Parse(
00241       const char * theArgumentSpec,
00245       BOOL optionsBeforeParams = TRUE  
00246     );
00248     virtual BOOL Parse(
00249       const PString & theArgumentStr,
00253       BOOL optionsBeforeParams = TRUE  
00254     );
00256 
00264     virtual PINDEX GetOptionCount(
00265       char optionChar  
00266     ) const;
00268     virtual PINDEX GetOptionCount(
00269       const char * optionStr 
00270     ) const;
00272     virtual PINDEX GetOptionCount(
00273       const PString & optionName 
00274     ) const;
00275 
00281     BOOL HasOption(
00282       char optionChar  
00283     ) const;
00285     BOOL HasOption(
00286       const char * optionStr 
00287     ) const;
00289     BOOL HasOption(
00290       const PString & optionName 
00291     ) const;
00292 
00301     virtual PString GetOptionString(
00302       char optionChar,          
00303       const char * dflt = NULL  
00304     ) const;
00306     virtual PString GetOptionString(
00307       const char * optionStr,   
00308       const char * dflt = NULL  
00309     ) const;
00311     virtual PString GetOptionString(
00312       const PString & optionName, 
00313       const char * dflt = NULL    
00314     ) const;
00315 
00323     PINDEX GetCount() const;
00324 
00329     PStringArray GetParameters(
00330       PINDEX first = 0,
00331       PINDEX last = P_MAX_INDEX
00332     ) const;
00333 
00338     PString GetParameter(
00339       PINDEX num   
00340     ) const;
00341 
00347     PString operator[](
00348       PINDEX num   
00349     ) const;
00350 
00354     void Shift(
00355       int sh 
00356     );
00357 
00361     PArgList & operator<<(
00362       int sh 
00363     );
00364 
00368     PArgList & operator>>(
00369       int sh 
00370     );
00372 
00379     virtual void IllegalArgumentIndex(
00380       PINDEX idx 
00381     ) const;
00382 
00387     virtual void UnknownOption(
00388       const PString & option   
00389     ) const;
00390 
00396     virtual void MissingArgument(
00397       const PString & option  
00398     ) const;
00400 
00401   protected:
00403     PStringArray argumentArray;
00405     PString      optionLetters;
00407     PStringArray optionNames;
00409     PIntArray    optionCount;
00411     PStringArray optionString;
00413     PIntArray    parameterIndex;
00415     int          shift;
00416 
00417   private:
00418     BOOL ParseOption(PINDEX idx, PINDEX offset, PINDEX & arg, const PIntArray & canHaveOptionString);
00419     PINDEX GetOptionCountByIndex(PINDEX idx) const;
00420     PString GetOptionStringByIndex(PINDEX idx, const char * dflt) const;
00421 };
00422 
00423 
00427 class PConfigArgs : public PArgList
00428 {
00429     PCLASSINFO(PConfigArgs, PArgList);
00430   public:
00433     PConfigArgs(
00434       const PArgList & args   
00435     );
00437 
00445     virtual PINDEX GetOptionCount(
00446       char optionChar  
00447     ) const;
00449     virtual PINDEX GetOptionCount(
00450       const char * optionStr 
00451     ) const;
00453     virtual PINDEX GetOptionCount(
00454       const PString & optionName 
00455     ) const;
00456 
00465     virtual PString GetOptionString(
00466       char optionChar,          
00467       const char * dflt = NULL  
00468     ) const;
00470     virtual PString GetOptionString(
00471       const char * optionStr,   
00472       const char * dflt = NULL  
00473     ) const;
00475     virtual PString GetOptionString(
00476       const PString & optionName, 
00477       const char * dflt = NULL    
00478     ) const;
00480 
00489     void Save(
00490       const PString & optionName   
00491     );
00492 
00495     void SetSectionName(
00496       const PString & section 
00497     ) { sectionName = section; }
00498 
00501     const PString & GetSectionName() const { return sectionName; }
00502 
00506     void SetNegationPrefix(
00507       const PString & prefix 
00508     ) { negationPrefix = prefix; }
00509 
00513     const PString & GetNegationPrefix() const { return negationPrefix; }
00515 
00516 
00517   protected:
00518     PString CharToString(char ch) const;
00519     PConfig config;
00520     PString sectionName;
00521     PString negationPrefix;
00522 };
00523 
00524 #endif
00525 
00526 // End Of File ///////////////////////////////////////////////////////////////

Generated on Wed Sep 28 10:27:32 2005 for PWLib by  doxygen 1.4.4