E-MailRelay

gbatchfile.h
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 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
33 namespace 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///
49/// Batch files always use the OEM code page on Windows but at
50/// this interface it's all UTF-8.
51///
52 class G::BatchFile
53{
54 public:
55 G_EXCEPTION_CLASS( Error , tx("batch file error") )
56
57 explicit BatchFile( const Path & ) ;
58 ///< Constructor that reads from a file.
59
60 BatchFile( const Path & , std::nothrow_t ) ;
61 ///< Constructor that reads from a file that might be missing
62 ///< or empty. The line() will be empty if construction fails.
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 bool empty() const ;
69 ///< Returns true if line() is empty.
70
71 std::string name() const ;
72 ///< Returns the "start" window name, if any.
73
74 const StringArray & args() const ;
75 ///< Returns the startup command-line broken up into de-quoted pieces.
76 ///< The first item in the list will be the executable.
77
78 std::size_t lineArgsPos() const ;
79 ///< Returns the position in line() where the arguments start.
80
81 static void write( const Path & new_batch_file , const StringArray & args ,
82 const std::string & start_window_name = {} , bool do_backup = false ) ;
83 ///< Writes a startup batch file, including a "start" prefix.
84 ///< If the "start" window name is not supplied then it is
85 ///< derived from the command-line. The 'args' must not
86 ///< contain double-quote characters. The first 'args' item
87 ///< is the target executable.
88
89 private:
90 void init( const Path & ) ;
91 void clear() ;
92 static std::string quote( const std::string & ) ;
93 static std::string percents( const std::string & ) ;
94 static void dequote( std::string & ) ;
95 std::string readFrom( std::istream & , const std::string & , bool do_throw = true ) ;
96 struct ParseResult
97 {
98 std::string name ;
99 std::string line ;
100 std::string error ;
101 } ;
102 static ParseResult parse( const std::string & ) ;
103 static StringArray split( const std::string & ) ;
104 static bool ignorable( const std::string & line ) ;
105 static bool relevant( const std::string & line ) ;
106 static void backup( const Path & ) ;
107
108 private:
109 std::string m_raw_line ;
110 std::string m_line ;
111 std::string m_name ;
112 StringArray m_args ;
113} ;
114
115#endif
G::BatchFile
A class for reading and writing windows-style startup batch files containing a single command-line,...
Definition: gbatchfile.h:53
G::BatchFile::args
const StringArray & args() const
Returns the startup command-line broken up into de-quoted pieces.
Definition: gbatchfile.cpp:176
G::BatchFile::empty
bool empty() const
Returns true if line() is empty.
Definition: gbatchfile.cpp:72
G::BatchFile::lineArgsPos
std::size_t lineArgsPos() const
Returns the position in line() where the arguments start.
Definition: gbatchfile.cpp:181
G::BatchFile::name
std::string name() const
Returns the "start" window name, if any.
Definition: gbatchfile.cpp:171
G::BatchFile::BatchFile
BatchFile(const Path &)
Constructor that reads from a file.
Definition: gbatchfile.cpp:32
G::BatchFile::line
std::string line() const
Returns the main command-line from within the batchfile, with normalised spaces, without any "start" ...
Definition: gbatchfile.cpp:166
G::BatchFile::write
static void write(const Path &new_batch_file, const StringArray &args, const std::string &start_window_name={}, bool do_backup=false)
Writes a startup batch file, including a "start" prefix.
Definition: gbatchfile.cpp:204
G::Path
A Path object represents a file system path.
Definition: gpath.h:82
gexception.h
gpath.h
gstringarray.h
G
Low-level classes.
Definition: garg.h:36
G::StringArray
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30
G::tx
constexpr const char * tx(const char *p) noexcept
A briefer alternative to G::gettext_noop().
Definition: ggettext.h:84