gexecutable.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 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 // gexecutable.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gexecutable.h"
23 #include "gstr.h"
24 
25 G::Executable::Executable( const std::string & s_ )
26 {
27  std::string s( s_ ) ;
28  if( s.find(' ') == std::string::npos ) // optimisation
29  {
30  m_exe = G::Path(s) ;
31  }
32  else
33  {
34  // mark escaped spaces using nul
35  const std::string null( 1U , '\0' ) ;
36  G::Str::replaceAll( s , "\\ " , null ) ;
37 
38  // split up on (unescaped) spaces
39  G::Str::splitIntoTokens( s , m_args , " " ) ;
40 
41  // replace the escaped spaces
42  for( G::Strings::iterator p = m_args.begin() ; p != m_args.end() ; ++p )
43  {
44  G::Str::replaceAll( *p , null , " " ) ;
45  }
46 
47  // take the first part as the path to the exe
48  if( m_args.size() )
49  {
50  m_exe = G::Path( m_args.front() ) ;
51  m_args.pop_front() ;
52  }
53  }
54 
55  // do o/s-specific fixups
56  if( 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 
72 std::string G::Executable::displayString() const
73 {
74  return m_exe.str() + std::string(m_args.size()?1U:0U,' ') + G::Str::join( m_args , " " ) ;
75 }
76 
std::list< std::string > Strings
A std::list of std::strings.
Definition: gstrings.h:39
static void splitIntoTokens(const std::string &in, Strings &out, const std::string &ws)
Splits the string into 'ws'-delimited tokens.
Definition: gstr.cpp:714
Strings args() const
Returns the command-line arguments.
Definition: gexecutable.cpp:67
static unsigned int replaceAll(std::string &s, const std::string &from, const std::string &to)
Does a global replace on string 's', replacing all occurences of sub-string 'from' with 'to'...
Definition: gstr.cpp:78
Executable(const std::string &command_line=std::string())
Constructor taking a complete command-line.
Definition: gexecutable.cpp:25
std::string displayString() const
Returns a printable representation for logging and diagnostics.
Definition: gexecutable.cpp:72
A Path object represents a file system path.
Definition: gpath.h:44
static std::string join(const Strings &strings, const std::string &sep)
Concatenates a set of strings.
Definition: gstr.cpp:799
Path exe() const
Returns the executable.
Definition: gexecutable.cpp:62