VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gexecutable.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 // gexecutable.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gexecutable.h"
23 #include "gstr.h"
24 #include <algorithm>
25 
26 G::Executable::Executable( const std::string & s_ )
27 {
28  std::string s( s_ ) ;
29  if( s.find(' ') == std::string::npos ) // optimisation
30  {
31  m_exe = G::Path(s) ;
32  }
33  else
34  {
35  // mark escaped spaces using nul
36  const std::string null( 1U , '\0' ) ;
37  G::Str::replaceAll( s , "\\ " , null ) ;
38 
39  // split up on (unescaped) spaces
40  G::Str::splitIntoTokens( s , m_args , " " ) ;
41 
42  // replace the escaped spaces
43  for( G::StringArray::iterator p = m_args.begin() ; p != m_args.end() ; ++p )
44  {
45  G::Str::replaceAll( *p , null , " " ) ;
46  }
47 
48  // take the first part as the path to the exe
49  if( m_args.size() )
50  {
51  m_exe = G::Path( m_args.at(0U) ) ;
52  std::rotate( m_args.begin() , m_args.begin()+1U , m_args.end() ) ;
53  m_args.pop_back() ; // ie. pop_front()
54  }
55  }
56 
57  // do o/s-specific fixups
58  if( m_exe != G::Path() && !osNativelyRunnable() )
59  {
60  osAddWrapper() ;
61  }
62 }
63 
64 G::Executable::Executable( const G::Path & exe_ , const G::StringArray & args_ ) :
65  m_exe(exe_) ,
66  m_args(args_)
67 {
68 }
69 
71 {
72  return m_exe ;
73 }
74 
76 {
77  return m_args ;
78 }
79 
80 std::string G::Executable::displayString() const
81 {
82  return
83  m_args.size() ?
84  std::string("[") + m_exe.str() + "] [" + G::Str::join("] [",m_args) + "]" :
85  std::string("[") + m_exe.str() + "]" ;
86 }
87 
88 void G::Executable::add( const std::string & arg )
89 {
90  m_args.push_back( arg ) ;
91 }
92 
93 /// \file gexecutable.cpp
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:33
static void splitIntoTokens(const std::string &in, StringArray &out, const std::string &ws)
Splits the string into 'ws'-delimited tokens.
Definition: gstr.cpp:868
static unsigned int replaceAll(std::string &s, const std::string &from, const std::string &to)
Does a global replace on string 's', replacing all occurances of sub-string 'from' with 'to'...
Definition: gstr.cpp:157
StringArray args() const
Returns the command-line arguments.
Definition: gexecutable.cpp:75
static std::string join(const std::string &sep, const StringArray &strings)
Concatenates an array of strings.
Definition: gstr.cpp:972
Executable(const std::string &command_line=std::string())
Constructor taking a complete command-line.
Definition: gexecutable.cpp:26
std::string displayString() const
Returns a printable representation for logging and diagnostics.
Definition: gexecutable.cpp:80
void add(const std::string &arg)
Adds a command-line argument.
Definition: gexecutable.cpp:88
A Path object represents a file system path.
Definition: gpath.h:72
Path exe() const
Returns the executable.
Definition: gexecutable.cpp:70