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