E-MailRelay
gexecutablecommand.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 gexecutablecommand.cpp
19///
20
21#include "gdef.h"
22#include "gexecutablecommand.h"
23#include "garg.h"
24#include "gstr.h"
25#include <algorithm>
26
28{
29 if( s.find(' ') == std::string::npos ) // optimisation
30 {
31 m_exe = s ;
32 }
33 else
34 {
35 G::Arg arg ;
36 arg.parse( s ) ;
37 m_args = arg.array() ;
38 if( !m_args.empty() )
39 {
40 m_exe = m_args.at(0U) ;
41 std::rotate( m_args.begin() , m_args.begin()+1U , m_args.end() ) ;
42 m_args.pop_back() ; // remove exe
43 }
44 }
45
46 // do o/s-specific fixups
47 if( !m_exe.empty() && !osNativelyRunnable() )
48 {
49 osAddWrapper() ;
50 }
51}
52
53G::ExecutableCommand::ExecutableCommand( const G::Path & exe_ , const G::StringArray & args_ , bool add_wrapper ) :
54 m_exe(exe_) ,
55 m_args(args_)
56{
57 if( add_wrapper && !m_exe.empty() && !osNativelyRunnable() )
58 {
59 osAddWrapper() ;
60 }
61}
62
64{
65 return m_exe ;
66}
67
69{
70 return m_args ;
71}
72
74{
75 return
76 m_args.empty() ?
77 std::string("[") + m_exe.str() + "]" :
78 std::string("[") + m_exe.str() + "] [" + Str::join("] [",m_args) + "]" ;
79}
80
81void G::ExecutableCommand::add( const std::string & arg )
82{
83 m_args.push_back( arg ) ;
84}
85
A class which holds a represention of the argc/argv command line array, and supports simple command-l...
Definition: garg.h:44
StringArray array(unsigned int shift=0U) const
Returns the arguments as a string array, with an optional shift.
Definition: garg.cpp:85
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
ExecutableCommand(const std::string &command_line=std::string())
Constructor taking a complete command-line.
void add(const std::string &arg)
Adds a command-line argument.
StringArray args() const
Returns the command-line arguments.
std::string displayString() const
Returns a printable representation for logging and diagnostics.
Path exe() const
Returns the executable.
A Path object represents a file system path.
Definition: gpath.h:72
bool empty() const noexcept
Returns true if size() is zero.
Definition: gpath.h:203
static std::string join(const std::string &sep, const StringArray &strings)
Concatenates an array of strings with separators.
Definition: gstr.cpp:1195
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:31