E-MailRelay
gbatchfile.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 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 gbatchfile.h
19///
20
21#ifndef G_BATCH_FILE_H
22#define G_BATCH_FILE_H
23
24#include "gdef.h"
25#include "gpath.h"
26#include "gstringarray.h"
27#include "gexception.h"
28#include <iostream>
29#include <string>
30#include <vector>
31#include <new>
32
33namespace G
34{
35 class BatchFile ;
36}
37
38//| \class G::BatchFile
39/// A class for reading and writing windows-style startup batch files
40/// containing a single command-line, optionally using "start".
41///
42/// Eg:
43/// \code
44/// @echo off
45/// rem a windows batch file
46/// start "my app" "c:\my app\run.exe" arg-one "arg two"
47/// \endcode
48///
50{
51public:
52 G_EXCEPTION( Error , tx("batch file error") ) ;
53
54 explicit BatchFile( const Path & ) ;
55 ///< Constructor that reads from a file.
56
57 BatchFile( const Path & , std::nothrow_t ) ;
58 ///< Constructor that reads from a file that might be missing
59 ///< or empty.
60
61 explicit BatchFile( std::istream & , const std::string & stream_name = {} ) ;
62 ///< Constructor that reads from a stream.
63
64 std::string line() const ;
65 ///< Returns the main command-line from within the batchfile, with normalised
66 ///< spaces, without any "start" prefix, and including quotes.
67
68 std::string name() const ;
69 ///< Returns the "start" window name, if any.
70
71 const StringArray & args() const ;
72 ///< Returns the startup command-line broken up into de-quoted pieces.
73 ///< The first item in the list will be the executable.
74
75 std::size_t lineArgsPos() const ;
76 ///< Returns the position in line() where the arguments start.
77
78 static void write( const Path & new_batch_file , const StringArray & args ,
79 const std::string & start_window_name = {} ) ;
80 ///< Writes a startup batch file, including a "start" prefix.
81 ///< If the "start" window name is not supplied then it is
82 ///< derived from the command-line. The 'args' must not
83 ///< contain double-quote characters. The first 'args' item
84 ///< is the target executable.
85
86private:
87 static std::string quote( const std::string & ) ;
88 static std::string percents( const std::string & ) ;
89 static void dequote( std::string & ) ;
90 std::string readFrom( std::istream & , const std::string & , bool ) ;
91 static StringArray split( const std::string & ) ;
92 static bool ignorable( const std::string & line ) ;
93 static bool relevant( const std::string & line ) ;
94 static std::string join( const std::string & file_name , unsigned int line_number ) ;
95
96private:
97 std::string m_line ;
98 std::string m_name ;
99 StringArray m_args ;
100} ;
101
102#endif
A class for reading and writing windows-style startup batch files containing a single command-line,...
Definition: gbatchfile.h:50
static void write(const Path &new_batch_file, const StringArray &args, const std::string &start_window_name={})
Writes a startup batch file, including a "start" prefix.
Definition: gbatchfile.cpp:190
const StringArray & args() const
Returns the startup command-line broken up into de-quoted pieces.
Definition: gbatchfile.cpp:150
std::size_t lineArgsPos() const
Returns the position in line() where the arguments start.
Definition: gbatchfile.cpp:155
std::string name() const
Returns the "start" window name, if any.
Definition: gbatchfile.cpp:145
BatchFile(const Path &)
Constructor that reads from a file.
Definition: gbatchfile.cpp:31
std::string line() const
Returns the main command-line from within the batchfile, with normalised spaces, without any "start" ...
Definition: gbatchfile.cpp:140
A Path object represents a file system path.
Definition: gpath.h:73
Low-level classes.
Definition: garg.h:30
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30
constexpr const char * tx(const char *p)
A briefer alternative to G::gettext_noop().
Definition: ggettext.h:84