diff --git a/src/blackmisc/fileutilities.cpp b/src/blackmisc/fileutilities.cpp new file mode 100644 index 000000000..cfcf9e8bd --- /dev/null +++ b/src/blackmisc/fileutilities.cpp @@ -0,0 +1,57 @@ +/* Copyright (C) 2015 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "blackmisc/worker.h" +#include "fileutilities.h" + +#include +#include + +namespace BlackMisc +{ + bool CFileUtils::writeStringToFile(const QString &content, const QString &fileNameAndPath) + { + if (fileNameAndPath.isEmpty()) { return false; } + QFile file(fileNameAndPath); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { return false; } + QTextStream stream(&file); + stream << content; + file.close(); + return true; + } + + QString CFileUtils::readFileToString(const QString &fileNameAndPath) + { + QFile file(fileNameAndPath); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { return ""; } + QTextStream stream(&file); + QString content(stream.readAll()); + file.close(); + return content; + } + + bool CFileUtils::writeStringToFileInBackground(const QString &content, const QString &fileNameAndPath) + { + if (fileNameAndPath.isEmpty()) { return false; } + CWorker *worker = BlackMisc::CWorker::fromTask(QCoreApplication::instance(), "writeStringToFileInBackground", [content, fileNameAndPath]() + { + bool s = CFileUtils::writeStringToFile(content, fileNameAndPath); + Q_UNUSED(s); + }); + return worker ? true : false; + } + + QString CFileUtils::appendFilePaths(const QString &path1, const QString &path2) + { + if (path1.isEmpty()) { return QDir::cleanPath(path2); } + if (path2.isEmpty()) { return QDir::cleanPath(path1); } + return QDir::cleanPath(path1 + QDir::separator() + path2); + } + +} // ns diff --git a/src/blackmisc/fileutilities.h b/src/blackmisc/fileutilities.h new file mode 100644 index 000000000..19c32422d --- /dev/null +++ b/src/blackmisc/fileutilities.h @@ -0,0 +1,42 @@ +/* Copyright (C) 2015 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_CFILEUTILITIES_H +#define BLACKMISC_CFILEUTILITIES_H + +#include "blackmiscexport.h" +#include +#include + +namespace BlackMisc +{ + /*! + * Utility class for file operations + */ + class BLACKMISC_EXPORT CFileUtils + { + public: + //! Write string to text file + static bool writeStringToFile(const QString &content, const QString &fileNameAndPath); + + //! Read file into string + static QString readFileToString(const QString &fileNameAndPath); + + //! Write string to text file in background + static bool writeStringToFileInBackground(const QString &content, const QString &fileNameAndPath); + + //! Append file paths + //! \sa CNetworkUtils::buildUrl for URLs + static QString appendFilePaths(const QString &path1, const QString &path2); + }; +} // ns + +#endif // guard