E-MailRelay

garg.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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/// \file garg.cpp
19///
20
21#include "gdef.h"
22#include "garg.h"
23#include "gprocess.h"
24#include "gpath.h"
25#include "gstr.h"
26#include "glog.h"
27#include "gassert.h"
28#include <cstring>
29
30 bool G::Arg::m_first = true ;
31std::string G::Arg::m_v0 ;
32std::string G::Arg::m_cwd ;
33
34 G::Arg::Arg( int argc , char **argv )
35{
36 G_ASSERT( argc > 0 ) ;
37 G_ASSERT( argv != nullptr ) ;
38 for( int i = 0 ; i < argc ; i++ )
39 m_array.push_back( argv[i] ) ;
40
41 if( m_first )
42 {
43 m_v0 = std::string( argv[0] ) ;
44 m_cwd = Process::cwd(true/*nothrow*/) ; // don't throw yet - we may "cd /" to deamonise
45 m_first = false ;
46 }
47}
48
49 G::Arg::Arg( const StringArray & args ) :
50 m_array(args)
51{
52}
53
54 G::Arg::Arg()
55= default ; // now use parse()
56
57 void G::Arg::parse( HINSTANCE , const std::string & command_line_tail )
58{
59 std::string proc_exe = Process::exe() ;
60 if( proc_exe.empty() ) throw Exception( "cannot determine the path of this executable" ) ;
61 m_array.clear() ;
62 m_array.push_back( proc_exe ) ;
63 parseImp( command_line_tail ) ;
64}
65
66 void G::Arg::parse( const std::string & command_line )
67{
68 G_ASSERT( !command_line.empty() ) ;
69 m_array.clear() ;
70 parseImp( command_line ) ;
71}
72
73 void G::Arg::reparse( const std::string & command_line_tail )
74{
75 auto p = m_array.begin() ;
76 m_array.erase( ++p , m_array.end() ) ;
77 parseImp( command_line_tail ) ;
78}
79
80std::string G::Arg::v0()
81{
82 return m_v0 ;
83}
84
85 G::StringArray G::Arg::array( unsigned int shift ) const
86{
87 StringArray result = m_array ;
88 while( !result.empty() && shift-- )
89 result.erase( result.begin() ) ;
90 return result ;
91}
92
93 bool G::Arg::contains( const std::string & option , std::size_t option_args , bool cs ) const
94{
95 return find( cs , option , option_args , nullptr ) != 0U ;
96}
97
98std::size_t G::Arg::count( const std::string & option )
99{
100 return find( true , option , 0U , nullptr ) ;
101}
102
103std::size_t G::Arg::find( bool cs , const std::string & option , std::size_t option_args ,
104 std::size_t * index_p ) const
105{
106 std::size_t count = 0U ;
107 for( std::size_t i = 1U ; i < m_array.size() ; i++ ) // start from v[1]
108 {
109 if( strmatch(cs,option,m_array[i]) && (i+option_args) < m_array.size() )
110 {
111 count++ ;
112 if( index_p != nullptr )
113 *index_p = i ;
114 i += option_args ;
115 }
116 }
117 return count ;
118}
119
120std::size_t G::Arg::match( const std::string & prefix ) const
121{
122 for( std::size_t i = 1U ; i < m_array.size() ; i++ )
123 {
124 if( Str::headMatch(m_array[i],prefix) )
125 {
126 return i ;
127 }
128 }
129 return 0U ;
130}
131
132 bool G::Arg::strmatch( bool cs , const std::string & s1 , const std::string & s2 )
133{
134 return cs ? (s1==s2) : (Str::upper(s1)==Str::upper(s2)) ;
135}
136
137 bool G::Arg::remove( const std::string & option , std::size_t option_args )
138{
139 std::size_t i = 0U ;
140 const bool found = find( true , option , option_args , &i ) ;
141 if( found )
142 removeAt( i , option_args ) ;
143 return found ;
144}
145
146std::string G::Arg::removeAt( std::size_t option_index , std::size_t option_args )
147{
148 std::string value ;
149 if( option_index > 0U && (option_index+option_args) < m_array.size() )
150 {
151 value = v( option_index + (option_args?1U:0U) , std::string() ) ;
152 auto p = m_array.begin() ;
153 for( std::size_t i = 0U ; i < option_index ; i++ ) ++p ; // (rather than cast)
154 p = m_array.erase( p ) ;
155 for( std::size_t i = 0U ; i < option_args && p != m_array.end() ; i++ )
156 p = m_array.erase( p ) ;
157 }
158 return value ;
159}
160
161std::size_t G::Arg::index( const std::string & option , std::size_t option_args ,
162 std::size_t default_ ) const
163{
164 std::size_t i = 0U ;
165 const bool found = find( true , option , option_args , &i ) ;
166 return found ? i : default_ ;
167}
168
169std::size_t G::Arg::c() const
170{
171 return m_array.size() ;
172}
173
174std::string G::Arg::v( std::size_t i ) const
175{
176 G_ASSERT( i < m_array.size() ) ;
177 return m_array.at(i) ;
178}
179
180std::string G::Arg::v( std::size_t i , const std::string & default_ ) const
181{
182 return i < m_array.size() ? m_array.at(i) : default_ ;
183}
184
185std::string G::Arg::prefix() const
186{
187 G_ASSERT( !m_array.empty() ) ;
188 Path path( m_array.at(0U) ) ;
189 return path.withoutExtension().basename() ;
190}
191
192 const char * G::Arg::prefix( char ** argv ) noexcept
193{
194 const char * exe = argv[0] ;
195 const char * p1 = std::strrchr( exe , '/' ) ;
196 const char * p2 = std::strrchr( exe , '\\' ) ;
197 p1 = p1 ? (p1+1U) : exe ;
198 p2 = p2 ? (p2+1U) : exe ;
199 return p1 > p2 ? p1 : p2 ;
200}
201
202 void G::Arg::parseImp( const std::string & command_line )
203{
204 string_view ws( " \t" ) ;
205 string_view nbws( "\0\0" , 2U ) ;
206 const char esc = '\\' ;
207 const char qq = '\"' ;
208 Str::splitIntoTokens( Str::dequote(command_line,qq,esc,ws,nbws) , m_array , ws , esc ) ;
209 Str::replace( m_array , '\0' , ' ' ) ;
210}
211
212std::string G::Arg::exe( bool do_throw )
213{
214 std::string proc_exe = Process::exe() ;
215 if( proc_exe.empty() && ( m_v0.empty() || ( m_cwd.empty() && Path(m_v0).isRelative() ) ) )
216 {
217 if( do_throw )
218 {
219 throw Exception( "cannot determine the absolute path of the current executable" ,
220 G::is_windows() ? "" : "try mounting procfs" ) ;
221 }
222 return std::string() ;
223 }
224 else if( proc_exe.empty() && Path(m_v0).isRelative() )
225 {
226 return Path::join(m_cwd,m_v0).collapsed().str() ;
227 }
228 else if( proc_exe.empty() )
229 {
230 return m_v0 ;
231 }
232 else
233 {
234 return proc_exe ;
235 }
236}
237
G::Arg::c
std::size_t c() const
Returns the number of tokens in the command line, including the program name.
Definition: garg.cpp:169
G::Arg::exe
static std::string exe(bool do_throw=true)
Returns Process::exe() or an absolute path constructed from v0() and possibly using the cwd.
Definition: garg.cpp:212
G::Arg::match
std::size_t match(const std::string &prefix) const
Returns the index of the first argument that matches the given prefix.
Definition: garg.cpp:120
G::Arg::v
std::string v(std::size_t i) const
Returns the i'th argument.
Definition: garg.cpp:174
G::Arg::index
std::size_t index(const std::string &option, std::size_t option_args=0U, std::size_t default_=0U) const
Returns the index of the given option.
Definition: garg.cpp:161
G::Arg::removeAt
std::string removeAt(std::size_t option_index, std::size_t option_args=0U)
Removes the given argument and the following 'option_args' ones.
Definition: garg.cpp:146
G::Arg::prefix
std::string prefix() const
Returns the basename of v(0) without any extension.
Definition: garg.cpp:185
G::Arg::contains
bool contains(const std::string &option, std::size_t option_args=0U, bool case_sensitive=true) const
Returns true if the command line contains the given option with enough command line arguments left to...
Definition: garg.cpp:93
G::Arg::array
StringArray array(unsigned int shift=0U) const
Returns the arguments as a string array, with an optional shift.
Definition: garg.cpp:85
G::Arg::remove
bool remove(const std::string &option, std::size_t option_args=0U)
Removes the given option and its arguments.
Definition: garg.cpp:137
G::Arg::parse
void parse(HINSTANCE hinstance, const std::string &command_line_tail)
Parses the given command-line tail, splitting it up into an array of tokens.
Definition: garg.cpp:57
G::Arg::v0
static std::string v0()
Returns a copy of argv[0] from the first call to the constructor that takes argc/argv.
Definition: garg.cpp:80
G::Arg::count
std::size_t count(const std::string &option)
Returns the number of times the given string appears in the list of arguments.
Definition: garg.cpp:98
G::Arg::Arg
Arg()
Default constructor. Initialise with parse().
G::Arg::reparse
void reparse(const std::string &command_line_tail)
Reinitialises the object with the given command-line tail.
Definition: garg.cpp:73
G::Exception
A general-purpose exception class derived from std::exception and containing an error message.
Definition: gexception.h:45
G::Path
A Path object represents a file system path.
Definition: gpath.h:72
G::Path::join
static Path join(const StringArray &parts)
Builds a path from a set of parts.
Definition: gpath.cpp:408
G::Path::basename
std::string basename() const
Returns the rightmost part of the path, ignoring "." parts.
Definition: gpath.cpp:328
G::Path::withoutExtension
Path withoutExtension() const
Returns a path without the basename extension, if any.
Definition: gpath.cpp:346
G::Path::collapsed
Path collapsed() const
Returns the path with "foo/.." and "." parts removed, so far as is possible without changing the mean...
Definition: gpath.cpp:433
G::Path::str
std::string str() const
Returns the path string.
Definition: gpath.h:215
G::Process::cwd
static std::string cwd(bool no_throw=false)
Returns the current working directory.
Definition: gprocess_unix.cpp:180
G::Process::exe
static std::string exe()
Returns the absolute path of the current executable, independent of the argv array passed to main().
Definition: gprocess_unix.cpp:225
G::Str::splitIntoTokens
static void splitIntoTokens(const std::string &in, StringArray &out, string_view ws, char esc='\0')
Splits the string into 'ws'-delimited tokens.
Definition: gstr.cpp:1073
G::Str::upper
static std::string upper(const std::string &s)
Returns a copy of 's' in which all Latin-1 lower-case characters have been replaced by upper-case cha...
Definition: gstr.cpp:754
G::Str::dequote
static std::string dequote(const std::string &, char qq='\"' , char esc = '\\' , string_view ws = Str::ws() , string_view nbws = Str::ws() )
Dequotes a string by removing unescaped quotes and escaping quoted whitespace, so "qq-aaa-esc-qq-bbb-...
Definition: gstr.cpp:167
G::Str::headMatch
static bool headMatch(const std::string &in, const std::string &head)
Returns true if the string has the given start (or head is empty).
Definition: gstr.cpp:1322
G::Str::replace
static bool replace(std::string &s, const std::string &from, const std::string &to, std::size_t *pos_p=nullptr)
Replaces 'from' with 'to', starting at offset '*pos_p'.
Definition: gstr.cpp:264
G::basic_string_view
A class template like c++17's std::basic_string_view.
Definition: gstringview.h:73
garg.h
gassert.h
glog.h
gpath.h
gprocess.h
gstr.h
G::StringArray
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:31