VirtualBox

source: vbox/trunk/src/VBox/Main/include/TextScript.h@ 76454

Last change on this file since 76454 was 76167, checked in by vboxsync, 6 years ago

Main: Split out the two text script classes from UnattendedScript since they are now used separately from unattended installation. bugref:9152

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette