1 | /* $Id: TextScript.h 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Classes for reading/parsing/saving text scripts (unattended installation, ++).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef MAIN_INCLUDED_TextScript_h
|
---|
19 | #define MAIN_INCLUDED_TextScript_h
|
---|
20 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
21 | # pragma once
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #include "VirtualBoxBase.h"
|
---|
25 | #include <iprt/cpp/utils.h>
|
---|
26 | #include <vector>
|
---|
27 |
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Base class for all the script readers/editors.
|
---|
31 | *
|
---|
32 | * @todo get rid of this silly bugger.
|
---|
33 | */
|
---|
34 | class AbstractScript
|
---|
35 | : public RTCNonCopyable
|
---|
36 | {
|
---|
37 | protected:
|
---|
38 | /** For setting errors.
|
---|
39 | * Yeah, class isn't entirely abstract now. */
|
---|
40 | VirtualBoxBase *mpSetError;
|
---|
41 |
|
---|
42 | private: /* no default constructors for children. */
|
---|
43 | AbstractScript() {}
|
---|
44 |
|
---|
45 | public:
|
---|
46 | DECLARE_TRANSLATE_METHODS(AbstractScript)
|
---|
47 |
|
---|
48 | AbstractScript(VirtualBoxBase *pSetError) : mpSetError(pSetError) {}
|
---|
49 | virtual ~AbstractScript() {}
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Read a script from a file
|
---|
53 | */
|
---|
54 | virtual HRESULT read(const Utf8Str &rStrFilename) = 0;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Read a script from a VFS file handle.
|
---|
58 | */
|
---|
59 | virtual HRESULT readFromHandle(RTVFSFILE hVfsFile, const char *pszFilename) = 0;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Parse the script
|
---|
63 | */
|
---|
64 | virtual HRESULT parse() = 0;
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Save a script to a string.
|
---|
68 | *
|
---|
69 | * This is used by save() and later others to deloy the script.
|
---|
70 | */
|
---|
71 | virtual HRESULT saveToString(Utf8Str &rStrDst) = 0;
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Save a script to a file.
|
---|
75 | * @param rStrPath Where to save the script. This normally points to a
|
---|
76 | * file, but in a number of child use cases it's
|
---|
77 | * actually giving a directory to put the script in
|
---|
78 | * using the default deloyment filename. One day we
|
---|
79 | * might make the caller do this path joining.
|
---|
80 | * @param fOverwrite Whether to overwrite the file or not.
|
---|
81 | */
|
---|
82 | virtual HRESULT save(const Utf8Str &rStrPath, bool fOverwrite) = 0;
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Path where an actual script with user's data is located
|
---|
86 | */
|
---|
87 | virtual const Utf8Str &getActualScriptPath() const = 0;
|
---|
88 | };
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Base class for text based script readers/editors.
|
---|
92 | *
|
---|
93 | * This deals with reading the file into a string data member, writing it back
|
---|
94 | * out to a file, and remember the filenames.
|
---|
95 | */
|
---|
96 | class BaseTextScript : public AbstractScript
|
---|
97 | {
|
---|
98 | protected:
|
---|
99 | const char * const mpszDefaultTemplateFilename; /**< The default template filename. Can be empty. */
|
---|
100 | const char * const mpszDefaultFilename; /**< Filename to use when someone calls save() with a directory path. Can be NULL. */
|
---|
101 | RTCString mStrScriptFullContent; /**< Raw text file content. Produced by read() and typically only used by parse(). */
|
---|
102 | Utf8Str mStrOriginalPath; /**< Path where an original script is located (set by read()). */
|
---|
103 | Utf8Str mStrSavedPath; /**< Path where an saved script with user's data is located (set by save()). */
|
---|
104 |
|
---|
105 | public:
|
---|
106 | DECLARE_TRANSLATE_METHODS(BaseTextScript)
|
---|
107 |
|
---|
108 | BaseTextScript(VirtualBoxBase *pSetError, const char *pszDefaultTemplateFilename, const char *pszDefaultFilename)
|
---|
109 | : AbstractScript(pSetError)
|
---|
110 | , mpszDefaultTemplateFilename(pszDefaultTemplateFilename)
|
---|
111 | , mpszDefaultFilename(pszDefaultFilename)
|
---|
112 | { }
|
---|
113 | virtual ~BaseTextScript() {}
|
---|
114 |
|
---|
115 | HRESULT read(const Utf8Str &rStrFilename);
|
---|
116 | HRESULT readFromHandle(RTVFSFILE hVfsFile, const char *pszFilename);
|
---|
117 | HRESULT save(const Utf8Str &rStrFilename, bool fOverwrite);
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Gets the default filename for this class of scripts (empty if none).
|
---|
121 | *
|
---|
122 | * @note Just the filename, no path.
|
---|
123 | */
|
---|
124 | const char *getDefaultFilename() const
|
---|
125 | {
|
---|
126 | return mpszDefaultFilename;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Gets the default template filename for this class of scripts (empty if none).
|
---|
131 | *
|
---|
132 | * @note Just the filename, no path.
|
---|
133 | */
|
---|
134 | const char *getDefaultTemplateFilename() const
|
---|
135 | {
|
---|
136 | return mpszDefaultTemplateFilename;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Path to the file we last saved the script as.
|
---|
141 | */
|
---|
142 | const Utf8Str &getActualScriptPath() const
|
---|
143 | {
|
---|
144 | return mStrSavedPath;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Path where an original script is located
|
---|
149 | */
|
---|
150 | const Utf8Str &getOriginalScriptPath() const
|
---|
151 | {
|
---|
152 | return mStrOriginalPath;
|
---|
153 | }
|
---|
154 | };
|
---|
155 |
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Generic line based text script editor.
|
---|
159 | *
|
---|
160 | * This is used for editing isolinux configuratin files among other things.
|
---|
161 | */
|
---|
162 | class GeneralTextScript : public BaseTextScript
|
---|
163 | {
|
---|
164 | protected:
|
---|
165 | RTCList<RTCString> mScriptContentByLines; /**< Content index by line. This contains the edited version. */
|
---|
166 | bool mfDataParsed; /**< Indicates whether the script has been parse() yet. */
|
---|
167 |
|
---|
168 | public:
|
---|
169 | DECLARE_TRANSLATE_METHODS(GeneralTextScript)
|
---|
170 |
|
---|
171 | GeneralTextScript(VirtualBoxBase *pSetError, const char *pszDefaultTemplateFilename = NULL, const char *pszDefaultFilename = NULL)
|
---|
172 | : BaseTextScript(pSetError, pszDefaultTemplateFilename, pszDefaultFilename), mfDataParsed(false)
|
---|
173 | {}
|
---|
174 | virtual ~GeneralTextScript() {}
|
---|
175 |
|
---|
176 | HRESULT parse();
|
---|
177 | HRESULT saveToString(Utf8Str &rStrDst);
|
---|
178 |
|
---|
179 | //////////////////New functions//////////////////////////////
|
---|
180 |
|
---|
181 | bool isDataParsed() const
|
---|
182 | {
|
---|
183 | return mfDataParsed;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Returns the actual size of script in lines
|
---|
188 | */
|
---|
189 | size_t getLineNumbersOfScript() const
|
---|
190 | {
|
---|
191 | return mScriptContentByLines.size();
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Gets a read-only reference to the given line, returning Utf8Str::Empty if
|
---|
196 | * idxLine is out of range.
|
---|
197 | *
|
---|
198 | * @returns Line string reference or Utf8Str::Empty.
|
---|
199 | * @param idxLine The line number.
|
---|
200 | *
|
---|
201 | * @todo RTCList doesn't allow this method to be const.
|
---|
202 | */
|
---|
203 | RTCString const &getContentOfLine(size_t idxLine);
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Set new content of line
|
---|
207 | */
|
---|
208 | HRESULT setContentOfLine(size_t idxLine, const Utf8Str &newContent);
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Find a substring in the script
|
---|
212 | * Returns a list with the found lines
|
---|
213 | * @throws std::bad_alloc
|
---|
214 | */
|
---|
215 | std::vector<size_t> findTemplate(const Utf8Str &rStrNeedle, RTCString::CaseSensitivity enmCase = RTCString::CaseSensitive);
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * In line @a idxLine replace the first occurence of @a rStrNeedle with
|
---|
219 | * @a rStrRelacement.
|
---|
220 | */
|
---|
221 | HRESULT findAndReplace(size_t idxLine, const Utf8Str &rStrNeedle, const Utf8Str &rStrReplacement);
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Append a string into the end of the given line.
|
---|
225 | */
|
---|
226 | HRESULT appendToLine(size_t idxLine, const Utf8Str &rStrToAppend);
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Prepend a string in the beginning of the given line.
|
---|
230 | */
|
---|
231 | HRESULT prependToLine(size_t idxLine, const Utf8Str &rStrToPrepend);
|
---|
232 |
|
---|
233 | //////////////////New functions//////////////////////////////
|
---|
234 | };
|
---|
235 |
|
---|
236 |
|
---|
237 | #endif /* !MAIN_INCLUDED_TextScript_h */
|
---|
238 |
|
---|