E-MailRelay

gexecutablecommand.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 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#include <iterator>
27
28#ifndef G_LIB_SMALL
29 G::ExecutableCommand::ExecutableCommand( const std::string & s )
30{
31 if( !s.empty() )
32 {
33 m_args = Arg(s).array() ;
34 m_exe = m_args.at(0U) ;
35 std::rotate( m_args.begin() , m_args.begin()+1U , m_args.end() ) ;
36 m_args.pop_back() ; // remove exe
37 }
38}
39#endif
40
41 G::ExecutableCommand::ExecutableCommand( const G::Path & exe_ , const G::StringArray & args_ ) :
42 m_exe(exe_) ,
43 m_args(args_)
44{
45}
46
47 G::Path G::ExecutableCommand::exe() const
48{
49 return m_exe ;
50}
51
52 G::StringArray G::ExecutableCommand::args() const
53{
54 return m_args ;
55}
56
57std::string G::ExecutableCommand::displayString() const
58{
59 return
60 m_args.empty() ?
61 std::string("[") + m_exe.str() + "]" :
62 std::string("[") + m_exe.str() + "] [" + Str::join("] [",m_args) + "]" ;
63}
64
65 void G::ExecutableCommand::add( const std::string & arg )
66{
67 m_args.push_back( arg ) ;
68}
69
70#ifndef G_LIB_SMALL
71 void G::ExecutableCommand::insert( const G::StringArray & array )
72{
73 if( !array.empty() )
74 {
75 m_args.insert( m_args.begin() , m_exe.str() ) ;
76 m_args.insert( m_args.begin() , std::next(array.begin()) , array.end() ) ;
77 m_exe = m_args.at( 0U ) ;
78 }
79}
80#endif
81
G::Arg
A class which holds a represention of the argc/argv command line array, and supports simple command-l...
Definition: garg.h:50
G::Arg::array
StringArray array(unsigned int shift=0U) const
Returns the arguments as a string array, with an optional shift.
Definition: garg.cpp:88
G::ExecutableCommand::ExecutableCommand
ExecutableCommand(const std::string &command_line={})
Constructor taking a complete command-line.
Definition: gexecutablecommand.cpp:29
G::ExecutableCommand::add
void add(const std::string &arg)
Adds a command-line argument.
Definition: gexecutablecommand.cpp:65
G::ExecutableCommand::args
StringArray args() const
Returns the command-line arguments.
Definition: gexecutablecommand.cpp:52
G::ExecutableCommand::displayString
std::string displayString() const
Returns a printable representation for logging and diagnostics.
Definition: gexecutablecommand.cpp:57
G::ExecutableCommand::insert
void insert(const G::StringArray &)
Inserts at the front of the command-line.
Definition: gexecutablecommand.cpp:71
G::ExecutableCommand::exe
Path exe() const
Returns the executable.
Definition: gexecutablecommand.cpp:47
G::Path
A Path object represents a file system path.
Definition: gpath.h:82
G::Str::join
static std::string join(std::string_view sep, const StringArray &strings)
Concatenates an array of strings with separators.
Definition: gstr.cpp:1221
garg.h
gexecutablecommand.h
gstr.h
G::StringArray
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30