file.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #pragma once
  2. #include <string>
  3. #include <fstream>
  4. #include <map>
  5. #include "base/define.h"
  6. #include "base/error.h"
  7. #include "base/buffer.h"
  8. namespace ylib
  9. {
  10. namespace file
  11. {
  12. /**
  13. * @brief 删除文件
  14. * @param filepath
  15. * @return
  16. */
  17. bool remove(const std::string& filepath);
  18. /**
  19. * @brief 删除目录
  20. * @param dirpath
  21. * @param recycle
  22. * @return
  23. */
  24. bool remove_dir(const std::string& dirpath
  25. #if _WIN32
  26. , bool recycle = false
  27. #endif
  28. );
  29. /**
  30. * @brief 创建目录
  31. * @param dirpath
  32. * @return
  33. */
  34. bool create_dir(const std::string& dirpath, bool create_parent_dir = true);
  35. /**
  36. * @brief 读取文件
  37. * @param filepath
  38. * @return
  39. */
  40. ylib::buffer read(const std::string& filepath);
  41. bool read(const std::string& filepath, ylib::buffer& data);
  42. /**
  43. * @brief 写到文件
  44. * @param filepath
  45. * @param data
  46. * @return
  47. */
  48. bool write(const std::string& filepath, const ylib::buffer& data);
  49. bool write(const std::string& filepath, const char* data, size_t len);
  50. #ifdef _WIN32
  51. /**
  52. * @brief 列出目录文件
  53. * @param rootPath
  54. * @param list<std::string,bool[文件夹]>
  55. * @return
  56. */
  57. bool list(const std::string& rootPath, std::map<std::string, bool>& list);
  58. #endif
  59. /**
  60. * @brief 取扩展名
  61. * @param path
  62. * @return
  63. */
  64. std::string ext(const std::string& path);
  65. /**
  66. * @brief 是否存在文件
  67. * @param filepath
  68. * @return
  69. */
  70. bool exist(const std::string& filepath);
  71. /**
  72. * @brief 是否存在目录
  73. * @param filepath
  74. * @return
  75. */
  76. bool exist_dir(const std::string& dirpath);
  77. /**
  78. * @brief 取文件大小
  79. * @param filepath
  80. * @return
  81. * 成功>=0 失败=-1
  82. */
  83. int64 size(const std::string& filepath);
  84. /**
  85. * @brief 取上级目录
  86. * @param path
  87. * @return
  88. */
  89. std::string parent_dir(const std::string& path);
  90. /**
  91. * @brief 取文件名
  92. * @param path
  93. * @param have_ext
  94. * @return
  95. */
  96. std::string filename(const std::string& path, bool have_ext = true);
  97. /**
  98. * @brief 复制文件
  99. */
  100. bool copy(const std::string& src, const std::string& dst);
  101. /**
  102. * @brief 复制目录
  103. */
  104. void copy_dir(const std::string& src, const std::string& dst);
  105. // 遍历目录
  106. std::map<std::string, ylib::FileType> traverse(const std::string& dirpath, const std::string& regex_pattern = "(.*\\.txt$)");
  107. // 生成一个临时文件路径
  108. std::string temp_filepath();
  109. // 格式化目录
  110. std::string format_separator(const std::string& filepath);
  111. // 最后修改时间
  112. timestamp last_write_time(const std::string& filepath);
  113. }
  114. class file_io :public ylib::error_base
  115. {
  116. public:
  117. file_io();
  118. ~file_io();
  119. bool open(const std::string& filepath, bool only_read = false, bool auto_create = true);
  120. void close();
  121. bool appead(const char* data, int64 len);
  122. bool appead(const ylib::buffer& data);
  123. bool write(const char* data, int64 len);
  124. bool write(const ylib::buffer& data);
  125. ylib::buffer read(int64 size);
  126. bool read(int64 size, ylib::buffer& data);
  127. void jump(int64 offset, std::ios_base::seekdir way = std::ios::cur);
  128. std::streampos cur();
  129. bool clear();
  130. std::streamsize size();
  131. bool is_open();
  132. inline const std::string& filepath() { return m_filepath; }
  133. private:
  134. std::string m_filepath;
  135. std::fstream* m_stream = nullptr;
  136. bool m_only_read = false;
  137. };
  138. }