libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YCommandLine.cc
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.cc
00020 
00021   Author:       Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 
00026 #include <stdlib.h>     // malloc()
00027 #include <string.h>     // strdup()
00028 
00029 #include <vector>
00030 #include <fstream>
00031 
00032 #include "YCommandLine.h"
00033 #include "YUIException.h"
00034 
00035 #define YUILogComponent "ui"
00036 #include "YUILog.h"
00037 
00038 
00039 struct YCommandLinePrivate
00040 {
00041     std::vector<std::string> args;
00042 };
00043 
00044 
00045 
00046 
00047 
00048 YCommandLine::YCommandLine()
00049     : priv( new YCommandLinePrivate() )
00050 {
00051     YUI_CHECK_NEW( priv );
00052 
00053     std::ifstream cmdline( "/proc/self/cmdline", std::ifstream::in | std::ifstream::binary );
00054 
00055     while ( cmdline.good() )
00056     {
00057         std::string arg;
00058         getline( cmdline, arg, '\0' );
00059 
00060         if ( ! arg.empty() )
00061         {
00062             yuiDebug() << "Arg #" << priv->args.size()
00063                        << ": \"" << arg << "\"" << std::endl;
00064 
00065             priv->args.push_back( arg );
00066         }
00067     }
00068 }
00069 
00070 
00071 YCommandLine::~YCommandLine()
00072 {
00073 
00074 }
00075 
00076 
00077 int
00078 YCommandLine::argc() const
00079 {
00080     return priv->args.size();
00081 }
00082 
00083 
00084 char **
00085 YCommandLine::argv() const
00086 {
00087     char ** argArray = (char **) ( malloc( argc() * sizeof( char * ) ) );
00088 
00089     if ( argArray )
00090     {
00091         for ( int i=0; i < argc(); i++ )
00092         {
00093             argArray[ i ] = strdup( priv->args[i].c_str() );
00094         }
00095     }
00096 
00097     return argArray;
00098 }
00099 
00100 
00101 void
00102 YCommandLine::add( const std::string & arg )
00103 {
00104     priv->args.push_back( arg );
00105 }
00106 
00107 
00108 std::string
00109 YCommandLine::arg( int index ) const
00110 {
00111     YUI_CHECK_INDEX( index, 0, (int) priv->args.size()-1 );
00112 
00113     return priv->args[ index ];
00114 }
00115 
00116 
00117 void
00118 YCommandLine::remove( int index )
00119 {
00120     YUI_CHECK_INDEX( index, 0, (int) priv->args.size()-1 );
00121 
00122     priv->args.erase( priv->args.begin() + index );
00123 }
00124 
00125 
00126 void
00127 YCommandLine::replace( int index, const std::string & newArg )
00128 {
00129     YUI_CHECK_INDEX( index, 0, (int) priv->args.size()-1 );
00130 
00131     priv->args[ index ] = newArg;
00132 }
00133 
00134 
00135 int
00136 YCommandLine::find( const std::string & argName ) const
00137 {
00138     for ( int i=0; i < argc(); i++ )
00139     {
00140         if ( priv->args[i] == argName )
00141             return i;
00142     }
00143 
00144     return -1;
00145 }
 All Classes Functions Variables Enumerations Friends