FileIO.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "FileIO.h"
  2. #include <QDebug>
  3. #include <QFile>
  4. #include <QDir>
  5. #include <QQmlEngine>
  6. #include <QCryptographicHash>
  7. FileIO::FileIO(QObject *parent) : QObject(parent)
  8. {
  9. }
  10. QString FileIO::userHomePath()
  11. {
  12. return qEnvironmentVariable("HOME");
  13. }
  14. bool FileIO::isFileExists(const QString &fileName)
  15. {
  16. return QFile(fileName).exists();
  17. }
  18. QStringList FileIO::matchByWildcard(const QString &fileWildcard)
  19. {
  20. QStringList result;
  21. QString fileName = fileWildcard;
  22. int end = 0;
  23. int pos = fileWildcard.indexOf('*', end);
  24. if (pos != -1) {
  25. int prev = fileWildcard.lastIndexOf('/', pos);
  26. int next = fileWildcard.indexOf('/', pos);
  27. if (prev != -1 && next != -1) {
  28. const QString &prefix = fileWildcard.left(prev + 1);
  29. const QString &mask = fileWildcard.mid(prev + 1, next - prev - 1);
  30. const QString &suffix = fileWildcard.right(fileWildcard.size() - next);
  31. QDir dir(prefix);
  32. const QStringList &entries = dir.entryList(QStringList() << mask,
  33. QDir::Dirs | QDir::Hidden | QDir::NoDotAndDotDot);
  34. for (const QString &entry : entries) {
  35. const QString &fileName = prefix + entry + suffix;
  36. if (isFileExists(fileName)) {
  37. result += fileName;
  38. }
  39. }
  40. }
  41. } else {
  42. if (isFileExists(fileName)) {
  43. result += fileName;
  44. }
  45. }
  46. return result;
  47. }
  48. QStringList FileIO::list(const QString &path, const QString &mask)
  49. {
  50. QStringList result;
  51. QDir dir(path);
  52. const QStringList &entries = dir.entryList(QStringList() << mask, QDir::Files);
  53. for (const QString &entry : entries) {
  54. result << (path + entry);
  55. }
  56. return result;
  57. }
  58. QString FileIO::readTextFile(const QString &fileName)
  59. {
  60. QFile file(fileName);
  61. if (!file.exists()) {
  62. qWarning() << "FileIO::readTextFile file doesn't exist: " + fileName;
  63. return QString();
  64. }
  65. if (!file.open(QFile::ReadOnly)) {
  66. qCritical() << "FileIO::readTextFile failed to open file for reading: " << fileName << " : " << file.errorString();
  67. return QString();
  68. }
  69. return QString(file.readAll());
  70. }
  71. bool FileIO::writeTextFile(const QString &fileName, const QString &text)
  72. {
  73. QFile file(fileName);
  74. QDir().mkpath(QFileInfo(file).absolutePath());
  75. if (!file.open(QFile::ReadWrite | QFile::Truncate)) {
  76. qCritical() << "FileIO::readTextFile failed to open file for writing: " << fileName << " : " << file.errorString();
  77. return false;
  78. }
  79. qint64 res = file.write(text.toUtf8());
  80. if (res == -1) {
  81. qCritical() << "FileIO::writeTextFile failed to write to file: " << fileName << " : " << file.errorString();
  82. return false;
  83. }
  84. return true;
  85. }
  86. bool FileIO::rename(const QString &oldPath, const QString &newPath)
  87. {
  88. return QFile::rename(oldPath, newPath);
  89. }
  90. bool FileIO::removeFile(const QString &fileName)
  91. {
  92. return QFile::remove(fileName);
  93. }
  94. bool FileIO::removeDirectory(const QString &path)
  95. {
  96. return QDir(path).removeRecursively();
  97. }
  98. bool FileIO::createDirectory(const QString &path)
  99. {
  100. return QDir().mkpath(path);
  101. }
  102. qint64 FileIO::size(const QString &fileName)
  103. {
  104. return QFileInfo(QFile(fileName)).size();
  105. }
  106. QString FileIO::md5(const QString &fileName)
  107. {
  108. QFile f(fileName);
  109. if (f.open(QFile::ReadOnly)) {
  110. QCryptographicHash hash(QCryptographicHash::Md5);
  111. if (hash.addData(&f)) {
  112. return QString(hash.result().toHex());
  113. }
  114. }
  115. return QString();
  116. }
  117. QString FileIO::localStoragePath()
  118. {
  119. return qobject_cast<QQmlEngine *>(parent())->offlineStoragePath();
  120. }