VirtualBox

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

Last change on this file since 90900 was 90828, checked in by vboxsync, 3 years ago

Main: bugref:1909: Added API localization

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