FileReader.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //
  2. // Created by xcbosa on 2023/1/28.
  3. //
  4. #include "utils-private.h"
  5. #include "../webuiconf.h"
  6. using namespace std;
  7. namespace xc::utils {
  8. string contentsOfTextFile(string filePath) {
  9. ifstream fin(filePath);
  10. if (fin.fail()) {
  11. cerr << "[FileIOError]: Read Error " << ::strerror(errno) << endl;
  12. return "";
  13. }
  14. stringstream buffer;
  15. buffer << fin.rdbuf();
  16. string str(buffer.str());
  17. fin.close();
  18. return str;
  19. }
  20. void saveTextFile(string filePath, string content) {
  21. ofstream ofs(filePath);
  22. if (ofs.fail()) {
  23. cerr << "[FileIOError]: Write Error " << ::strerror(errno) << endl;
  24. return;
  25. }
  26. ofs << content;
  27. ofs.flush();
  28. ofs.close();
  29. }
  30. string mimeTypeOfFile(string filePath) {
  31. std::filesystem::path p(filePath);
  32. string ext = p.extension();
  33. auto cit = conf::fileExtensionToMimeTypes.find(ext);
  34. if (cit != conf::fileExtensionToMimeTypes.end()) {
  35. return cit->second;
  36. }
  37. cit = conf::fileExtensionToMimeTypes.find("default");
  38. if (cit != conf::fileExtensionToMimeTypes.end()) {
  39. return cit->second;
  40. }
  41. return "data";
  42. }
  43. }