Files
pilotclient/src/blackmisc/fileutils.cpp
Klaus Basan 1fc7300982 refs #507, split up blackmiscfreefunctions
* single register metadata functions
* string utils
* metadata utils
* some formatting / license header where applicable
2015-11-22 02:42:29 +01:00

91 lines
3.1 KiB
C++

/* 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 "fileutils.h"
#include <QFile>
#include <QCoreApplication>
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;
}
QString CFileUtils::readFileToString(const QString &filePath, const QString &fileName)
{
return readFileToString(appendFilePaths(filePath, fileName));
}
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);
}
bool CFileUtils::copyRecursively(const QString &sourceDir, const QString &destinationDir)
{
QFileInfo sourceFileInfo(sourceDir);
if (sourceFileInfo.isDir())
{
QDir targetDir(destinationDir);
if (!targetDir.mkpath("."))
{
return false;
}
QDir originDir(sourceFileInfo.absoluteFilePath());
auto fileNames = originDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
for (const QString &fileName: fileNames)
{
if (!copyRecursively(originDir.absoluteFilePath(fileName), targetDir.absoluteFilePath(fileName)))
return false;
}
}
else
{
if (!QFile::copy(sourceDir, destinationDir))
return false;
}
return true;
}
} // ns