filemanager.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright 2017 Herik Lima de Castro and Marcelo Medeiros Eler
  3. Distributed under MIT license, or public domain if desired and
  4. recognized in your jurisdiction.
  5. See file LICENSE for detail.
  6. */
  7. #include "filemanager.h"
  8. #include <QDir>
  9. CWF_BEGIN_NAMESPACE
  10. QString FileManager::extract(QString &name, char ch)
  11. {
  12. QString fName;
  13. for (int i = (name.size() - 1); i >= 0; --i) {
  14. if (name[i] == ch)
  15. break;
  16. fName.push_front(name[i]);
  17. }
  18. return fName;
  19. }
  20. void FileManager::removeLastBar(QString &path)
  21. {
  22. if (path.endsWith("/"))
  23. path.remove(path.length() - 1, 1);
  24. }
  25. void FileManager::removeFirstBar(QString &path)
  26. {
  27. if (path.startsWith("/"))
  28. path.remove(0, 1);
  29. }
  30. void FileManager::putFirstBar(QString &path)
  31. {
  32. if (!path.startsWith("/"))
  33. path.push_front("/");
  34. }
  35. QByteArray FileManager::readAll(const QString &fileName, QFile::FileError &fileErro)
  36. {
  37. QFile file(fileName);
  38. if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
  39. fileErro = file.error();
  40. return file.errorString().toUtf8();
  41. }
  42. return file.readAll();
  43. }
  44. bool FileManager::copyDirectoryFiles(const QString &fromDir,
  45. const QString &toDir,
  46. bool coverFileIfExist)
  47. {
  48. QDir sourceDir(fromDir);
  49. QDir targetDir(toDir);
  50. if (!targetDir.exists()) {
  51. if (!targetDir.mkdir(targetDir.absolutePath()))
  52. return false;
  53. }
  54. QFileInfoList fileInfoList(sourceDir.entryInfoList());
  55. for (const QFileInfo &fileInfo : fileInfoList) {
  56. if (fileInfo.fileName() == "." || fileInfo.fileName() == "..") {
  57. continue;
  58. }
  59. if (fileInfo.isDir()) {
  60. if (!copyDirectoryFiles(fileInfo.filePath(),
  61. targetDir.filePath(fileInfo.fileName()),
  62. coverFileIfExist)) {
  63. return false;
  64. }
  65. } else {
  66. if (coverFileIfExist && targetDir.exists(fileInfo.fileName())) {
  67. targetDir.remove(fileInfo.fileName());
  68. }
  69. if (!QFile::copy(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()))) {
  70. return false;
  71. }
  72. }
  73. }
  74. return true;
  75. }
  76. CWF_END_NAMESPACE