File.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef SRC_UTIL_FILE_H_
  11. #define SRC_UTIL_FILE_H_
  12. #include <cstdio>
  13. #include <cstdlib>
  14. #include <string>
  15. #include "util.h"
  16. #include <functional>
  17. #if defined(__linux__)
  18. #include <limits.h>
  19. #endif
  20. #if defined(_WIN32)
  21. #ifndef PATH_MAX
  22. #define PATH_MAX 1024
  23. #endif // !PATH_MAX
  24. struct dirent{
  25. long d_ino; /* inode number*/
  26. off_t d_off; /* offset to this dirent*/
  27. unsigned short d_reclen; /* length of this d_name*/
  28. unsigned char d_type; /* the type of d_name*/
  29. char d_name[1]; /* file name (null-terminated)*/
  30. };
  31. typedef struct _dirdesc {
  32. int dd_fd; /** file descriptor associated with directory */
  33. long dd_loc; /** offset in current buffer */
  34. long dd_size; /** amount of data returned by getdirentries */
  35. char *dd_buf; /** data buffer */
  36. int dd_len; /** size of data buffer */
  37. long dd_seek; /** magic cookie returned by getdirentries */
  38. HANDLE handle;
  39. struct dirent *index;
  40. } DIR;
  41. # define __dirfd(dp) ((dp)->dd_fd)
  42. int mkdir(const char *path, int mode);
  43. DIR *opendir(const char *);
  44. int closedir(DIR *);
  45. struct dirent *readdir(DIR *);
  46. #endif // defined(_WIN32)
  47. #if defined(_WIN32) || defined(_WIN64)
  48. #define fseek64 _fseeki64
  49. #define ftell64 _ftelli64
  50. #else
  51. #define fseek64 fseek
  52. #define ftell64 ftell
  53. #endif
  54. namespace toolkit {
  55. class File {
  56. public:
  57. //创建路径
  58. static bool create_path(const char *file, unsigned int mod);
  59. //新建文件,目录文件夹自动生成
  60. static FILE *create_file(const char *file, const char *mode);
  61. //判断是否为目录
  62. static bool is_dir(const char *path);
  63. //判断是否是特殊目录(. or ..)
  64. static bool is_special_dir(const char *path);
  65. //删除目录或文件
  66. static int delete_file(const char *path);
  67. //判断文件是否存在
  68. static bool fileExist(const char *path);
  69. /**
  70. * 加载文件内容至string
  71. * @param path 加载的文件路径
  72. * @return 文件内容
  73. */
  74. static std::string loadFile(const char *path);
  75. /**
  76. * 保存内容至文件
  77. * @param data 文件内容
  78. * @param path 保存的文件路径
  79. * @return 是否保存成功
  80. */
  81. static bool saveFile(const std::string &data, const char *path);
  82. /**
  83. * 获取父文件夹
  84. * @param path 路径
  85. * @return 文件夹
  86. */
  87. static std::string parentDir(const std::string &path);
  88. /**
  89. * 替换"../",获取绝对路径
  90. * @param path 相对路径,里面可能包含 "../"
  91. * @param current_path 当前目录
  92. * @param can_access_parent 能否访问父目录之外的目录
  93. * @return 替换"../"之后的路径
  94. */
  95. static std::string absolutePath(const std::string &path, const std::string &current_path, bool can_access_parent = false);
  96. /**
  97. * 遍历文件夹下的所有文件
  98. * @param path 文件夹路径
  99. * @param cb 回调对象 ,path为绝对路径,isDir为该路径是否为文件夹,返回true代表继续扫描,否则中断
  100. * @param enter_subdirectory 是否进入子目录扫描
  101. */
  102. static void scanDir(const std::string &path, const std::function<bool(const std::string &path, bool isDir)> &cb, bool enter_subdirectory = false);
  103. /**
  104. * 获取文件大小
  105. * @param fp 文件句柄
  106. * @param remain_size true:获取文件剩余未读数据大小,false:获取文件总大小
  107. */
  108. static uint64_t fileSize(FILE *fp, bool remain_size = false);
  109. /**
  110. * 获取文件大小
  111. * @param path 文件路径
  112. * @return 文件大小
  113. * @warning 调用者应确保文件存在
  114. */
  115. static uint64_t fileSize(const char *path);
  116. private:
  117. File();
  118. ~File();
  119. };
  120. } /* namespace toolkit */
  121. #endif /* SRC_UTIL_FILE_H_ */