File indexing completed on 2024-05-05 04:38:44

0001 /*
0002     SPDX-FileCopyrightText: 2020 Igor Kushnir <igorkuo@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KDEVPLATFORM_FILESYSTEMHELPERS_H
0008 #define KDEVPLATFORM_FILESYSTEMHELPERS_H
0009 
0010 #include "utilexport.h"
0011 
0012 #include <QByteArray>
0013 #include <QByteArrayList>
0014 
0015 class QString;
0016 class QStringList;
0017 
0018 namespace FilesystemHelpers {
0019 /**
0020  * @brief Creates a new file at a specified path.
0021  * @param filePath The path where the file will be created.
0022  * @param fileContents Data to be written into the new file.
0023  * @return true on success; false if the file already existed or another error occurred.
0024  */
0025 KDEVPLATFORMUTIL_EXPORT bool createNewFileAndWrite(const QString& filePath,
0026                                                    const QByteArray& fileContents = QByteArray{});
0027 
0028 /**
0029  * @brief Creates all necessary parent directories and a new file at a specified path.
0030  * @param dirPath An absolute path to a directory, the beginning of the path to the new file.
0031  * @param[in,out] filePath A relative path from @p dirPath to the location of the new file.
0032  *                         On return this path is made absolute by prepending @p dirPath to it.
0033  * @param fileContents Data to be written into the new file.
0034  * @return An empty string on success or the path to the directory or file where an error occurred.
0035  */
0036 KDEVPLATFORMUTIL_EXPORT QString makeAbsoluteCreateAndWrite(const QString& dirPath, QString& filePath,
0037                                                            const QByteArray& fileContents = QByteArray{});
0038 
0039 /**
0040  * @brief Calls the single-file overload for each element of @p filePaths.
0041  * @param fileContents The list of desired file contents. Must be of the same size as @p filePaths.
0042  * @return An empty string on success or the path to the directory or file where an error occurred.
0043  */
0044 KDEVPLATFORMUTIL_EXPORT QString makeAbsoluteCreateAndWrite(const QString& dirPath, QStringList& filePaths,
0045                                                            const QByteArrayList& fileContents);
0046 
0047 /**
0048  * @brief The difference from QByteArrayList fileContents overload is that the same data -
0049  *        @p commonFileContents - is written into each new file.
0050  */
0051 KDEVPLATFORMUTIL_EXPORT QString makeAbsoluteCreateAndWrite(const QString& dirPath, QStringList& filePaths,
0052                                                            const QByteArray& commonFileContents = QByteArray{});
0053 }
0054 
0055 #endif // KDEVPLATFORM_FILESYSTEMHELPERS_H