You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.5 KiB

5 months ago
  1. #include "fileutils.hpp"
  2. using namespace iflytop;
  3. using namespace core;
  4. bool FileUtils::exist(const string &path) {
  5. struct stat statInfo;
  6. if (stat(path.c_str(), &statInfo) == 0) {
  7. return true;
  8. }
  9. return false;
  10. }
  11. bool FileUtils::isDirectory(const string &path) {
  12. struct stat statInfo;
  13. if (stat(path.c_str(), &statInfo) == 0) {
  14. if (S_ISDIR(statInfo.st_mode)) {
  15. return true;
  16. } else {
  17. return false;
  18. }
  19. }
  20. return false;
  21. }
  22. // mkfile
  23. bool FileUtils::makeDirIfNoExist(const string &path) {
  24. string::size_type sepPos = path.find_last_of("/");
  25. if (sepPos == string::npos) {
  26. return false;
  27. }
  28. string dirPath = path.substr(0, sepPos);
  29. if (exist(dirPath)) {
  30. return true;
  31. }
  32. int ret = system(("mkdir -p " + dirPath).c_str());
  33. // mkdir(dirPath.c_str(), S_IRWXU | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  34. return 0 == ret ? true : false;
  35. }
  36. // readfile
  37. string FileUtils::readFileAsString(const string &filePath) {
  38. if (!exist(filePath)) {
  39. return "";
  40. }
  41. fstream file(filePath.c_str(), ios::in | ios::binary);
  42. stringstream sstream;
  43. sstream << file.rdbuf();
  44. file.close();
  45. string str(sstream.str());
  46. sstream.clear();
  47. return str;
  48. }
  49. std::vector<char> FileUtils::readFileAsBuffer(const string &filePath) {
  50. std::vector<char> ret;
  51. if (!exist(filePath)) {
  52. return std::move(ret);
  53. }
  54. fstream fs;
  55. fs.open(filePath.c_str(), ios::in | ios::binary);
  56. if (!fs.is_open()) {
  57. return std::move(ret);
  58. }
  59. fs.seekg(0, ios::end);
  60. int fileSize = fs.tellg();
  61. fs.seekg(0, ios::beg);
  62. ret.resize(fileSize);
  63. char *dataAddr = (char *)ret.data();
  64. fs.read(dataAddr, fileSize);
  65. fs.close();
  66. return std::move(ret);
  67. }
  68. // write file
  69. bool FileUtils::writeToFile(const string &fileName, const string &buf) {
  70. return writeToFile(fileName, buf.c_str(), buf.size());
  71. }
  72. shared_ptr<ofstream> FileUtils::openTrunc(const string &fileName) {
  73. makeDirIfNoExist(fileName);
  74. shared_ptr<ofstream> file(new ofstream(fileName, std::ios::trunc | std::ios::binary));
  75. return file;
  76. }
  77. bool FileUtils::writeToFile(const string &fileName, const char *buf, size_t size) {
  78. std::ofstream outfile1(fileName, std::ios::trunc | std::ios::binary);
  79. if (outfile1.is_open()) {
  80. outfile1.write((char *)buf, size);
  81. if (outfile1.good()) return true;
  82. return false;
  83. } else {
  84. return false;
  85. }
  86. }
  87. #if 0
  88. bool FileUtils::getRepetitionFileName(string fileName, string suffix, string &outfilename, int maxNum = -1) {}
  89. bool FileUtils::getRepetitionDirName(string fileName, string &outfilename, int maxNum = -1) {}
  90. // filename operat
  91. string FileUtils::getRepetitionFileName(string fileName, string suffix, int maxNum = -1) {
  92. string curName;
  93. if (maxNum >= 0) {
  94. for (int i = 0; i < maxNum; i++) {
  95. curName = fmt::format("{}{}.{}", fileName, i, suffix);
  96. if (!FileUtils::exist(curName)) return curName;
  97. }
  98. } else {
  99. for (int i = 0; i <= 65536; i++) {
  100. curName = fmt::format("{}{}.{}", fileName, i, suffix);
  101. if (!FileUtils::exist(curName)) return curName;
  102. }
  103. }
  104. return fmt::format("{}{}.{}", fileName, "reachMax", suffix);
  105. }
  106. string FileUtils::getRepetitionDirName(string fileName, int maxNum = -1) {
  107. string curName;
  108. if (maxNum >= 0) {
  109. for (int i = 0; i < maxNum; i++) {
  110. curName = fmt::format("{}{}", fileName, i);
  111. if (!FileUtils::exist(curName)) return curName + "/";
  112. }
  113. } else {
  114. for (int i = 0; i <= 65536; i++) {
  115. curName = fmt::format("{}{}", fileName, i);
  116. if (!FileUtils::exist(curName)) return curName + "/";
  117. }
  118. }
  119. return "";
  120. }
  121. #endif