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