diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 14:19:18 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 14:19:18 +0000 |
commit | 4035b1bfb1e5843a539a8b624d21952b756974d1 (patch) | |
tree | f1e9cd5bf548cbc57ff2fddfb2b4aa9ae95587e2 /src/VBox/Main/include/TextScript.h | |
parent | Initial commit. (diff) | |
download | virtualbox-4035b1bfb1e5843a539a8b624d21952b756974d1.tar.xz virtualbox-4035b1bfb1e5843a539a8b624d21952b756974d1.zip |
Adding upstream version 6.1.22-dfsg.upstream/6.1.22-dfsgupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/VBox/Main/include/TextScript.h')
-rw-r--r-- | src/VBox/Main/include/TextScript.h | 231 |
1 files changed, 231 insertions, 0 deletions
diff --git a/src/VBox/Main/include/TextScript.h b/src/VBox/Main/include/TextScript.h new file mode 100644 index 00000000..5dad034c --- /dev/null +++ b/src/VBox/Main/include/TextScript.h @@ -0,0 +1,231 @@ +/* $Id: TextScript.h $ */ +/** @file + * Classes for reading/parsing/saving text scripts (unattended installation, ++). + */ + +/* + * Copyright (C) 2006-2020 Oracle Corporation + * + * This file is part of VirtualBox Open Source Edition (OSE), as + * available from http://www.virtualbox.org. This file is free software; + * you can redistribute it and/or modify it under the terms of the GNU + * General Public License (GPL) as published by the Free Software + * Foundation, in version 2 as it comes in the "COPYING" file of the + * VirtualBox OSE distribution. VirtualBox OSE is distributed in the + * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. + */ + +#ifndef MAIN_INCLUDED_TextScript_h +#define MAIN_INCLUDED_TextScript_h +#ifndef RT_WITHOUT_PRAGMA_ONCE +# pragma once +#endif + +#include "VirtualBoxBase.h" +#include <iprt/cpp/utils.h> +#include <vector> + + +/** + * Base class for all the script readers/editors. + * + * @todo get rid of this silly bugger. + */ +class AbstractScript : public RTCNonCopyable +{ +protected: + /** For setting errors. + * Yeah, class isn't entirely abstract now. */ + VirtualBoxBase *mpSetError; + +private: /* no default constructors for children. */ + AbstractScript() {} + +public: + AbstractScript(VirtualBoxBase *pSetError) : mpSetError(pSetError) {} + virtual ~AbstractScript() {} + + /** + * Read a script from a file + */ + virtual HRESULT read(const Utf8Str &rStrFilename) = 0; + + /** + * Read a script from a VFS file handle. + */ + virtual HRESULT readFromHandle(RTVFSFILE hVfsFile, const char *pszFilename) = 0; + + /** + * Parse the script + */ + virtual HRESULT parse() = 0; + + /** + * Save a script to a string. + * + * This is used by save() and later others to deloy the script. + */ + virtual HRESULT saveToString(Utf8Str &rStrDst) = 0; + + /** + * Save a script to a file. + * @param rStrPath Where to save the script. This normally points to a + * file, but in a number of child use cases it's + * actually giving a directory to put the script in + * using the default deloyment filename. One day we + * might make the caller do this path joining. + * @param fOverwrite Whether to overwrite the file or not. + */ + virtual HRESULT save(const Utf8Str &rStrPath, bool fOverwrite) = 0; + + /** + * Path where an actual script with user's data is located + */ + virtual const Utf8Str &getActualScriptPath() const = 0; +}; + +/** + * Base class for text based script readers/editors. + * + * This deals with reading the file into a string data member, writing it back + * out to a file, and remember the filenames. + */ +class BaseTextScript : public AbstractScript +{ +protected: + const char * const mpszDefaultTemplateFilename; /**< The default template filename. Can be empty. */ + const char * const mpszDefaultFilename; /**< Filename to use when someone calls save() with a directory path. Can be NULL. */ + RTCString mStrScriptFullContent; /**< Raw text file content. Produced by read() and typically only used by parse(). */ + Utf8Str mStrOriginalPath; /**< Path where an original script is located (set by read()). */ + Utf8Str mStrSavedPath; /**< Path where an saved script with user's data is located (set by save()). */ + +public: + BaseTextScript(VirtualBoxBase *pSetError, const char *pszDefaultTemplateFilename, const char *pszDefaultFilename) + : AbstractScript(pSetError) + , mpszDefaultTemplateFilename(pszDefaultTemplateFilename) + , mpszDefaultFilename(pszDefaultFilename) + { } + virtual ~BaseTextScript() {} + + HRESULT read(const Utf8Str &rStrFilename); + HRESULT readFromHandle(RTVFSFILE hVfsFile, const char *pszFilename); + HRESULT save(const Utf8Str &rStrFilename, bool fOverwrite); + + /** + * Gets the default filename for this class of scripts (empty if none). + * + * @note Just the filename, no path. + */ + const char *getDefaultFilename() const + { + return mpszDefaultFilename; + } + + /** + * Gets the default template filename for this class of scripts (empty if none). + * + * @note Just the filename, no path. + */ + const char *getDefaultTemplateFilename() const + { + return mpszDefaultTemplateFilename; + } + + /** + * Path to the file we last saved the script as. + */ + const Utf8Str &getActualScriptPath() const + { + return mStrSavedPath; + } + + /** + * Path where an original script is located + */ + const Utf8Str &getOriginalScriptPath() const + { + return mStrOriginalPath; + } +}; + + +/** + * Generic line based text script editor. + * + * This is used for editing isolinux configuratin files among other things. + */ +class GeneralTextScript : public BaseTextScript +{ +protected: + RTCList<RTCString> mScriptContentByLines; /**< Content index by line. This contains the edited version. */ + bool mfDataParsed; /**< Indicates whether the script has been parse() yet. */ + +public: + GeneralTextScript(VirtualBoxBase *pSetError, const char *pszDefaultTemplateFilename = NULL, const char *pszDefaultFilename = NULL) + : BaseTextScript(pSetError, pszDefaultTemplateFilename, pszDefaultFilename), mfDataParsed(false) + {} + virtual ~GeneralTextScript() {} + + HRESULT parse(); + HRESULT saveToString(Utf8Str &rStrDst); + + //////////////////New functions////////////////////////////// + + bool isDataParsed() const + { + return mfDataParsed; + } + + /** + * Returns the actual size of script in lines + */ + size_t getLineNumbersOfScript() const + { + return mScriptContentByLines.size(); + } + + /** + * Gets a read-only reference to the given line, returning Utf8Str::Empty if + * idxLine is out of range. + * + * @returns Line string reference or Utf8Str::Empty. + * @param idxLine The line number. + * + * @todo RTCList doesn't allow this method to be const. + */ + RTCString const &getContentOfLine(size_t idxLine); + + /** + * Set new content of line + */ + HRESULT setContentOfLine(size_t idxLine, const Utf8Str &newContent); + + /** + * Find a substring in the script + * Returns a list with the found lines + * @throws std::bad_alloc + */ + std::vector<size_t> findTemplate(const Utf8Str &rStrNeedle, RTCString::CaseSensitivity enmCase = RTCString::CaseSensitive); + + /** + * In line @a idxLine replace the first occurence of @a rStrNeedle with + * @a rStrRelacement. + */ + HRESULT findAndReplace(size_t idxLine, const Utf8Str &rStrNeedle, const Utf8Str &rStrReplacement); + + /** + * Append a string into the end of the given line. + */ + HRESULT appendToLine(size_t idxLine, const Utf8Str &rStrToAppend); + + /** + * Prepend a string in the beginning of the given line. + */ + HRESULT prependToLine(size_t idxLine, const Utf8Str &rStrToPrepend); + + //////////////////New functions////////////////////////////// +}; + + +#endif /* !MAIN_INCLUDED_TextScript_h */ + |