- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

garg.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // garg.cpp
19 //
20
21 #include "gdef.h"
22 #include "garg.h"
23 #include "gpath.h"
24 #include "gstr.h"
25 #include "gdebug.h"
26 #include "gassert.h"
27 #include <cstring>
28
29 G::Arg::Arg( int argc , char *argv[] )
30 {
31 G_ASSERT( argc > 0 ) ;
32 G_ASSERT( argv != NULL ) ;
33 for( int i = 0 ; i < argc ; i++ )
34 m_array.push_back( argv[i] ) ;
35
36 setExe() ;
37 setPrefix() ;
38 }
39
40 G::Arg::~Arg()
41 {
42 }
43
44 G::Arg::Arg()
45 {
46 setExe() ;
47 setPrefix() ;
48 // now use parse()
49 }
50
51 G::Arg::Arg( const Arg & other ) :
52 m_array(other.m_array) ,
53 m_prefix(other.m_prefix)
54 {
55 }
56
57 G::Arg & G::Arg::operator=( const Arg & rhs )
58 {
59 if( this != &rhs )
60 {
61 m_array = rhs.m_array ;
62 m_prefix = rhs.m_prefix ;
63 }
64 return *this ;
65 }
66
67 void G::Arg::setPrefix()
68 {
69 G_ASSERT( m_array.size() > 0U ) ;
70 Path path( m_array[0U] ) ;
71 path.removeExtension() ;
72 m_prefix = path.basename() ;
73 }
74
75 bool G::Arg::contains( const std::string & sw , size_type sw_args , bool cs ) const
76 {
77 return find( cs , sw , sw_args , NULL ) ;
78 }
79
80 bool G::Arg::find( bool cs , const std::string & sw , size_type sw_args , size_type * index_p ) const
81 {
82 for( size_type i = 1 ; i < m_array.size() ; i++ ) // start from v[1]
83 {
84 if( match(cs,sw,m_array[i]) && (i+sw_args) < m_array.size() )
85 {
86 if( index_p != NULL )
87 *index_p = i ;
88 return true ;
89 }
90 }
91 return false ;
92 }
93
94 bool G::Arg::match( bool cs , const std::string & s1 , const std::string & s2 )
95 {
96 return
97 cs ?
98 s1 == s2 :
99 Str::upper(s1) == Str::upper(s2) ;
100 }
101
102 bool G::Arg::remove( const std::string & sw , size_type sw_args )
103 {
104 size_type i = 0U ;
105 const bool found = find( true , sw , sw_args , &i ) ;
106 if( found )
107 removeAt( i , sw_args ) ;
108 return found ;
109 }
110
111 void G::Arg::removeAt( size_type sw_index , size_type sw_args )
112 {
113 G_ASSERT( sw_index > 0U && sw_index < m_array.size() ) ;
114 if( sw_index > 0U && sw_index < m_array.size() )
115 {
116 StringArray::iterator p = m_array.begin() ;
117 for( size_type i = 0U ; i < sw_index ; i++ ) ++p ; // (rather than cast)
118 p = m_array.erase( p ) ;
119 for( size_type i = 0U ; i < sw_args && p != m_array.end() ; i++ )
120 p = m_array.erase( p ) ;
121 }
122 }
123
124 G::Arg::size_type G::Arg::index( const std::string & sw , size_type sw_args ) const
125 {
126 size_type i = 0U ;
127 const bool found = find( true , sw , sw_args , &i ) ;
128 return found ? i : 0U ;
129 }
130
131 G::Arg::size_type G::Arg::c() const
132 {
133 return m_array.size() ;
134 }
135
136 std::string G::Arg::v( size_type i ) const
137 {
138 G_ASSERT( i < m_array.size() ) ;
139 return m_array[i] ;
140 }
141
142 std::string G::Arg::prefix() const
143 {
144 return m_prefix ;
145 }
146
147 const char * G::Arg::prefix( char * argv [] ) // throw()
148 {
149 const char * exe = argv[0] ;
150 const char * p1 = std::strrchr( exe , '/' ) ;
151 const char * p2 = std::strrchr( exe , '\\' ) ;
152 p1 = p1 ? (p1+1U) : exe ;
153 p2 = p2 ? (p2+1U) : exe ;
154 return p1 > p2 ? p1 : p2 ;
155 }
156
G::Arg::prefix
std::string prefix() const
Returns the basename of v(0) without any extension.
Definition: garg.cpp:142
G::Arg::~Arg
~Arg()
Destructor.
Definition: garg.cpp:40
G::Arg::contains
bool contains(const std::string &sw, size_type sw_args=0U, bool case_sensitive=true) const
< An exception-free version of prefix() which can be used in main() outside of the outermost try bloc...
Definition: garg.cpp:75
G::Arg::size_type
size_t size_type
Definition: garg.h:49
G::Arg::Arg
Arg()
Default constructor for Windows.
Definition: garg.cpp:44
md5::size_type
std::string::size_type size_type
A std::size_t type.
Definition: md5.h:43
G::Arg::index
size_type index(const std::string &sw, size_type sw_args=0U) const
Returns the index of the given switch.
Definition: garg.cpp:124
garg.h
G::Arg::operator=
Arg & operator=(const Arg &)
Assignment operator.
Definition: garg.cpp:57
G::Arg::v
std::string v(size_type i) const
Returns the i'th argument.
Definition: garg.cpp:136
G_ASSERT
#define G_ASSERT(test)
Definition: gassert.h:30
gstr.h
gassert.h
G::Arg::c
size_type c() const
Returns the number of tokens in the command line, including the program name.
Definition: garg.cpp:131
G::Arg::remove
bool remove(const std::string &sw, size_type sw_args=0U)
Removes the given switch and its arguments.
Definition: garg.cpp:102
G::Str::upper
static std::string upper(const std::string &s)
Returns a copy of 's' in which all lowercase characters have been replaced by uppercase characters...
Definition: gstr.cpp:426
gpath.h
G::Arg::removeAt
void removeAt(size_type sw_index, size_type sw_args=0U)
Removes the given argument and the following 'sw_args' ones.
Definition: garg.cpp:111
G::Arg
A class which holds a represention of the argc/argv command line array, and supports simple command-l...
Definition: garg.h:46
G::Path
A Path object represents a file system path.
Definition: gpath.h:44
gdebug.h