FileUtils.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <utils/FileUtils.h>
  2. // qt incl
  3. #include <QDir>
  4. #include <QFileInfo>
  5. // hyperion include
  6. #include <hyperion/Hyperion.h>
  7. namespace FileUtils {
  8. QString getBaseName(const QString& sourceFile)
  9. {
  10. QFileInfo fi(sourceFile);
  11. return fi.fileName();
  12. }
  13. QString getDirName(const QString& sourceFile)
  14. {
  15. QFileInfo fi(sourceFile);
  16. return fi.path();
  17. }
  18. bool removeDir(const QString& path, Logger* log)
  19. {
  20. if(!QDir(path).removeRecursively())
  21. {
  22. Error(log, "Failed to remove directory: %s", QSTRING_CSTR(path));
  23. return false;
  24. }
  25. return true;
  26. }
  27. bool fileExists(const QString& path, Logger* log, bool ignError)
  28. {
  29. if(!QFile::exists(path))
  30. {
  31. ErrorIf((!ignError), log,"File does not exist: %s",QSTRING_CSTR(path));
  32. return false;
  33. }
  34. return true;
  35. }
  36. bool readFile(const QString& path, QString& data, Logger* log, bool ignError)
  37. {
  38. QFile file(path);
  39. if(!fileExists(path, log, ignError))
  40. {
  41. return false;
  42. }
  43. if(!file.open(QFile::ReadOnly | QFile::Text))
  44. {
  45. if(!ignError)
  46. resolveFileError(file,log);
  47. return false;
  48. }
  49. data = QString(file.readAll());
  50. file.close();
  51. return true;
  52. }
  53. bool writeFile(const QString& path, const QByteArray& data, Logger* log)
  54. {
  55. QFile file(path);
  56. if (!file.open(QFile::WriteOnly | QFile::Truncate))
  57. {
  58. resolveFileError(file,log);
  59. return false;
  60. }
  61. if(file.write(data) == -1)
  62. {
  63. resolveFileError(file,log);
  64. return false;
  65. }
  66. file.close();
  67. return true;
  68. }
  69. bool removeFile(const QString& path, Logger* log, bool ignError)
  70. {
  71. QFile file(path);
  72. if(!file.remove())
  73. {
  74. if(!ignError)
  75. resolveFileError(file,log);
  76. return false;
  77. }
  78. return true;
  79. }
  80. void resolveFileError(const QFile& file, Logger* log)
  81. {
  82. QFile::FileError error = file.error();
  83. const char* fn = QSTRING_CSTR(file.fileName());
  84. switch(error)
  85. {
  86. case QFileDevice::NoError:
  87. Debug(log,"No error occurred while procesing file: %s",fn);
  88. break;
  89. case QFileDevice::ReadError:
  90. Error(log,"Can't read file: %s",fn);
  91. break;
  92. case QFileDevice::WriteError:
  93. Error(log,"Can't write file: %s",fn);
  94. break;
  95. case QFileDevice::FatalError:
  96. Error(log,"Fatal error while processing file: %s",fn);
  97. break;
  98. case QFileDevice::ResourceError:
  99. Error(log,"Resource Error while processing file: %s",fn);
  100. break;
  101. case QFileDevice::OpenError:
  102. Error(log,"Can't open file: %s",fn);
  103. break;
  104. case QFileDevice::AbortError:
  105. Error(log,"Abort Error while processing file: %s",fn);
  106. break;
  107. case QFileDevice::TimeOutError:
  108. Error(log,"Timeout Error while processing file: %s",fn);
  109. break;
  110. case QFileDevice::UnspecifiedError:
  111. Error(log,"Unspecified Error while processing file: %s",fn);
  112. break;
  113. case QFileDevice::RemoveError:
  114. Error(log,"Failed to remove file: %s",fn);
  115. break;
  116. case QFileDevice::RenameError:
  117. Error(log,"Failed to rename file: %s",fn);
  118. break;
  119. case QFileDevice::PositionError:
  120. Error(log,"Position Error while processing file: %s",fn);
  121. break;
  122. case QFileDevice::ResizeError:
  123. Error(log,"Resize Error while processing file: %s",fn);
  124. break;
  125. case QFileDevice::PermissionsError:
  126. Error(log,"Permission Error at file: %s",fn);
  127. break;
  128. case QFileDevice::CopyError:
  129. Error(log,"Error during file copy of file: %s",fn);
  130. break;
  131. default:
  132. break;
  133. }
  134. }
  135. };