libyui  3.3.2
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 
40 {
41  std::vector<std::string> args;
42 };
43 
44 
45 
46 
47 
49  : priv( new YCommandLinePrivate() )
50 {
51  YUI_CHECK_NEW( priv );
52 
53  std::ifstream cmdline( "/proc/self/cmdline", std::ifstream::in | std::ifstream::binary );
54 
55  while ( cmdline.good() )
56  {
57  std::string arg;
58  getline( cmdline, arg, '\0' );
59 
60  if ( ! arg.empty() )
61  {
62  yuiDebug() << "Arg #" << priv->args.size()
63  << ": \"" << arg << "\"" << std::endl;
64 
65  priv->args.push_back( arg );
66  }
67  }
68 }
69 
70 
72 {
73 
74 }
75 
76 
77 int
79 {
80  return priv->args.size();
81 }
82 
83 
84 char **
86 {
87  char ** argArray = (char **) ( malloc( argc() * sizeof( char * ) ) );
88 
89  if ( argArray )
90  {
91  for ( int i=0; i < argc(); i++ )
92  {
93  argArray[ i ] = strdup( priv->args[i].c_str() );
94  }
95  }
96 
97  return argArray;
98 }
99 
100 
101 void
102 YCommandLine::add( const std::string & arg )
103 {
104  priv->args.push_back( arg );
105 }
106 
107 
108 std::string
109 YCommandLine::arg( int index ) const
110 {
111  YUI_CHECK_INDEX( index, 0, (int) priv->args.size()-1 );
112 
113  return priv->args[ index ];
114 }
115 
116 
117 void
119 {
120  YUI_CHECK_INDEX( index, 0, (int) priv->args.size()-1 );
121 
122  priv->args.erase( priv->args.begin() + index );
123 }
124 
125 
126 void
127 YCommandLine::replace( int index, const std::string & newArg )
128 {
129  YUI_CHECK_INDEX( index, 0, (int) priv->args.size()-1 );
130 
131  priv->args[ index ] = newArg;
132 }
133 
134 
135 int
136 YCommandLine::find( const std::string & argName ) const
137 {
138  for ( int i=0; i < argc(); i++ )
139  {
140  if ( priv->args[i] == argName )
141  return i;
142  }
143 
144  return -1;
145 }
int find(const std::string &argName) const
Find a command line argument &#39;argName&#39; ("-display" etc.).
~YCommandLine()
Destructor.
Definition: YCommandLine.cc:71
YCommandLine()
Constructor.
Definition: YCommandLine.cc:48
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:85
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:78
std::string arg(int index) const
Return command line argument no.