file.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. /**
  51. * @brief 列出目录文件
  52. * @param rootPath
  53. * @param list<std::string,bool[文件夹]>
  54. * @return
  55. */
  56. bool list(const std::string& rootPath, std::map<std::string, bool>& list);
  57. /**
  58. * @brief 取扩展名
  59. * @param path
  60. * @return
  61. */
  62. std::string ext(const std::string& path);
  63. /**
  64. * @brief 是否存在文件
  65. * @param filepath
  66. * @return
  67. */
  68. bool exist(const std::string& filepath);
  69. /**
  70. * @brief 是否存在目录
  71. * @param filepath
  72. * @return
  73. */
  74. bool exist_dir(const std::string& dirpath);
  75. /**
  76. * @brief 取文件大小
  77. * @param filepath
  78. * @return
  79. * 成功>=0 失败=-1
  80. */
  81. int64 size(const std::string& filepath);
  82. /**
  83. * @brief 取上级目录
  84. * @param path
  85. * @return
  86. */
  87. std::string parent_dir(const std::string& path);
  88. /**
  89. * @brief 取文件名
  90. * @param path
  91. * @param have_ext
  92. * @return
  93. */
  94. std::string filename(const std::string& path, bool have_ext = true);
  95. /**
  96. * @brief 复制文件
  97. */
  98. bool copy(const std::string& src, const std::string& dst);
  99. /**
  100. * @brief 复制目录
  101. */
  102. void copy_dir(const std::string& src, const std::string& dst);
  103. // 遍历目录
  104. std::map<std::string, ylib::FileType> traverse(const std::string& dirpath, const std::string& regex_pattern = "(.*\\.txt$)");
  105. // 生成一个临时文件路径
  106. std::string temp_filepath();
  107. // 格式化目录
  108. std::string format_separator(const std::string& filepath);
  109. // 最后修改时间
  110. timestamp last_write_time(const std::string& filepath);
  111. }
  112. class file_io :public ylib::error_base
  113. {
  114. public:
  115. file_io();
  116. ~file_io();
  117. bool open(const std::string& filepath, bool only_read = false, bool auto_create = true);
  118. void close();
  119. bool appead(const char* data, int64 len);
  120. bool appead(const ylib::buffer& data);
  121. bool write(const char* data, int64 len);
  122. bool write(const ylib::buffer& data);
  123. ylib::buffer read(int64 size);
  124. bool read(int64 size, ylib::buffer& data);
  125. void jump(int64 offset, std::ios_base::seekdir way = std::ios::cur);
  126. std::streampos cur();
  127. bool clear();
  128. std::streamsize size();
  129. bool is_open();
  130. inline const std::string& filepath() { return m_filepath; }
  131. private:
  132. std::string m_filepath;
  133. std::fstream* m_stream = nullptr;
  134. bool m_only_read = false;
  135. };
  136. }