libyui  3.9.3
YCommandLine.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YCommandLine.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #include <stdlib.h> // malloc()
27 #include <string.h> // strdup()
28 
29 #include <vector>
30 #include <fstream>
31 
32 #include "YCommandLine.h"
33 #include "YUIException.h"
34 
35 #define YUILogComponent "ui"
36 #include "YUILog.h"
37 
38 using std::string;
39 
40 
42 {
43  std::vector<string> args;
44 };
45 
46 
47 
48 
49 
51  : priv( new YCommandLinePrivate() )
52 {
53  YUI_CHECK_NEW( priv );
54 
55  std::ifstream cmdline( "/proc/self/cmdline", std::ifstream::in | std::ifstream::binary );
56 
57  while ( cmdline.good() )
58  {
59  string arg;
60  getline( cmdline, arg, '\0' );
61 
62  if ( ! arg.empty() )
63  {
64  yuiDebug() << "Arg #" << priv->args.size()
65  << ": \"" << arg << "\"" << endl;
66 
67  priv->args.push_back( arg );
68  }
69  }
70 }
71 
72 
74 {
75 
76 }
77 
78 
79 int
81 {
82  return priv->args.size();
83 }
84 
85 
86 char **
88 {
89  char ** argArray = (char **) ( malloc( argc() * sizeof( char * ) ) );
90 
91  if ( argArray )
92  {
93  for ( int i=0; i < argc(); i++ )
94  {
95  argArray[ i ] = strdup( priv->args[i].c_str() );
96  }
97  }
98 
99  return argArray;
100 }
101 
102 
103 void
104 YCommandLine::add( const string & arg )
105 {
106  priv->args.push_back( arg );
107 }
108 
109 
110 string
111 YCommandLine::arg( int index ) const
112 {
113  YUI_CHECK_INDEX( index, 0, (int) priv->args.size()-1 );
114 
115  return priv->args[ index ];
116 }
117 
118 
119 void
121 {
122  YUI_CHECK_INDEX( index, 0, (int) priv->args.size()-1 );
123 
124  priv->args.erase( priv->args.begin() + index );
125 }
126 
127 
128 void
129 YCommandLine::replace( int index, const string & newArg )
130 {
131  YUI_CHECK_INDEX( index, 0, (int) priv->args.size()-1 );
132 
133  priv->args[ index ] = newArg;
134 }
135 
136 
137 int
138 YCommandLine::find( const string & argName ) const
139 {
140  for ( int i=0; i < argc(); i++ )
141  {
142  if ( priv->args[i] == argName )
143  return i;
144  }
145 
146  return -1;
147 }
int find(const std::string &argName) const
Find a command line argument &#39;argName&#39; ("-display" etc.).
~YCommandLine()
Destructor.
Definition: YCommandLine.cc:73
YCommandLine()
Constructor.
Definition: YCommandLine.cc:50
std::string arg(int index) const
Return command line argument no.
void replace(int index, const std::string &arg)
Replace command line argument no.
char ** argv() const
Return the arguments in a C compatible fashion: An array of pointers to characters.
Definition: YCommandLine.cc:87
void remove(int index)
Remove command line argument no.
void add(const std::string &arg)
Add a command line argument (at the end of the existing ones).
int argc() const
Return the number of arguments in the command line.
Definition: YCommandLine.cc:80