libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YCommandLine.h
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or 
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:         YCommandLine.h
00020 
00021   Author:       Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 #ifndef YCommandLine_h
00026 #define YCommandLine_h
00027 
00028 #include <string>
00029 #include "ImplPtr.h"
00030 
00031 class YCommandLinePrivate;
00032 
00033 
00034 /**
00035  * Utility class to access /proc/<pid>/cmdline to retrieve argc and argv
00036  **/
00037 class YCommandLine
00038 {
00039 public:
00040     /**
00041      * Constructor. This will read /proc/<pid>/cmdline of this process.
00042      **/
00043     YCommandLine();
00044 
00045     /**
00046      * Destructor.
00047      **/
00048     ~YCommandLine();
00049 
00050     /**
00051      * Return the number of arguments in the command line.
00052      * Remember that the command itself (the binary of the process) is
00053      * included, so a value of 1 (not 0!) means "no additional arguments".
00054      **/
00055     int argc() const;
00056 
00057     /**
00058      * Return the arguments in a C compatible fashion: An array of pointers to
00059      * characters. The data are copied with strdup(), so they are valid beyond
00060      * the life time of this object (but OTOH should be released with free() at
00061      * some point).
00062      **/
00063     char ** argv() const;
00064 
00065     /**
00066      * Alias for argc() for those who like a more C++ -like syntax.
00067      **/
00068     int size() const { return argc(); }
00069 
00070     /**
00071      * Return command line argument no. 'index' (from 0 on).
00072      *
00073      * This might throw an YUIIndexOutOfRangeException.
00074      **/
00075     std::string arg( int index ) const;
00076 
00077     /**
00078      * Return command line argument no. 'index' (from 0 on) as operator[]:
00079      *
00080      * for ( int i=0; i < cmdLine.argc(); i++ )
00081      *     cout << cmdLine[i]  << std::endl;
00082      *
00083      * This might throw an YUIIndexOutOfRangeException.
00084      **/
00085     std::string operator[]( int index ) const
00086         { return arg( index ); }
00087 
00088     /**
00089      * Add a command line argument (at the end of the existing ones).
00090      **/
00091     void add( const std::string & arg );
00092 
00093     /**
00094      * Remove command line argument no. 'index' (from 0 on).
00095      *
00096      * This might throw an YUIIndexOutOfRangeException.
00097      **/
00098     void remove( int index );
00099 
00100     /**
00101      * Replace command line argument no. 'index' (from 0 on) with 'arg'.
00102      *
00103      * This might throw an YUIIndexOutOfRangeException.
00104      **/
00105     void replace( int index, const std::string & arg );
00106 
00107     /**
00108      * Find a command line argument 'argName' ("-display" etc.).
00109      * Notice that leading minus signs must be specified in 'argName'.
00110      * Since argv[0] is the program name, the search starts from argv[1].
00111      *
00112      * Return the position of 'argName' (from 0 on) or -1 if not found.
00113      **/
00114     int find( const std::string & argName ) const;
00115 
00116 
00117 private:
00118 
00119     ImplPtr<YCommandLinePrivate> priv;
00120 };
00121 
00122 
00123 #endif // YCommandLine_h
 All Classes Functions Variables Enumerations Friends