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
27 G::ExecutableCommand::ExecutableCommand( const std::string & s )
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
53 G::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
63 G::Path G::ExecutableCommand::exe() const
64{
65 return m_exe ;
66}
67
68 G::StringArray G::ExecutableCommand::args() const
69{
70 return m_args ;
71}
72
73std::string G::ExecutableCommand::displayString() const
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
81 void G::ExecutableCommand::add( const std::string & arg )
82{
83 m_args.push_back( arg ) ;
84}
85
G::Arg
A class which holds a represention of the argc/argv command line array, and supports simple command-l...
Definition: garg.h:44
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::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::ExecutableCommand::ExecutableCommand
ExecutableCommand(const std::string &command_line=std::string())
Constructor taking a complete command-line.
Definition: gexecutablecommand.cpp:27
G::ExecutableCommand::add
void add(const std::string &arg)
Adds a command-line argument.
Definition: gexecutablecommand.cpp:81
G::ExecutableCommand::args
StringArray args() const
Returns the command-line arguments.
Definition: gexecutablecommand.cpp:68
G::ExecutableCommand::displayString
std::string displayString() const
Returns a printable representation for logging and diagnostics.
Definition: gexecutablecommand.cpp:73
G::ExecutableCommand::exe
Path exe() const
Returns the executable.
Definition: gexecutablecommand.cpp:63
G::Path
A Path object represents a file system path.
Definition: gpath.h:72
G::Path::empty
bool empty() const noexcept
Returns true if size() is zero.
Definition: gpath.h:203
G::Str::join
static std::string join(const std::string &sep, const StringArray &strings)
Concatenates an array of strings with separators.
Definition: gstr.cpp:1195
garg.h
gexecutablecommand.h
gstr.h
G::StringArray
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:31