File.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/xia-chu/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 <stdio.h>
  13. #include <stdlib.h>
  14. #include <string>
  15. #include "util.h"
  16. #include <functional>
  17. using namespace std;
  18. #if defined(__linux__)
  19. #include <limits.h>
  20. #endif
  21. #if defined(_WIN32)
  22. #ifndef PATH_MAX
  23. #define PATH_MAX 1024
  24. #endif // !PATH_MAX
  25. struct dirent{
  26. long d_ino; /* inode number*/
  27. off_t d_off; /* offset to this dirent*/
  28. unsigned short d_reclen; /* length of this d_name*/
  29. unsigned char d_type; /* the type of d_name*/
  30. char d_name[1]; /* file name (null-terminated)*/
  31. };
  32. typedef struct _dirdesc {
  33. int dd_fd; /** file descriptor associated with directory */
  34. long dd_loc; /** offset in current buffer */
  35. long dd_size; /** amount of data returned by getdirentries */
  36. char *dd_buf; /** data buffer */
  37. int dd_len; /** size of data buffer */
  38. long dd_seek; /** magic cookie returned by getdirentries */
  39. HANDLE handle;
  40. struct dirent *index;
  41. } DIR;
  42. # define __dirfd(dp) ((dp)->dd_fd)
  43. int mkdir(const char *path, int mode);
  44. DIR *opendir(const char *);
  45. int closedir(DIR *);
  46. struct dirent *readdir(DIR *);
  47. #endif // defined(_WIN32)
  48. namespace toolkit {
  49. class File {
  50. public:
  51. //创建路径
  52. static bool create_path(const char *file, unsigned int mod);
  53. //新建文件,目录文件夹自动生成
  54. static FILE *create_file(const char *file, const char *mode);
  55. //判断是否为目录
  56. static bool is_dir(const char *path) ;
  57. //判断是否为常规文件
  58. static bool is_file(const char *path) ;
  59. //判断是否是特殊目录(. or ..)
  60. static bool is_special_dir(const char *path);
  61. //删除目录或文件
  62. static int delete_file(const char *path) ;
  63. /**
  64. * 加载文件内容至string
  65. * @param path 加载的文件路径
  66. * @return 文件内容
  67. */
  68. static string loadFile(const char *path);
  69. /**
  70. * 保存内容至文件
  71. * @param data 文件内容
  72. * @param path 保存的文件路径
  73. * @return 是否保存成功
  74. */
  75. static bool saveFile(const string &data,const char *path);
  76. /**
  77. * 获取父文件夹
  78. * @param path 路径
  79. * @return 文件夹
  80. */
  81. static string parentDir(const string &path);
  82. /**
  83. * 替换"../",获取绝对路径
  84. * @param path 相对路径,里面可能包含 "../"
  85. * @param currentPath 当前目录
  86. * @param canAccessParent 能否访问父目录之外的目录
  87. * @return 替换"../"之后的路径
  88. */
  89. static string absolutePath(const string &path, const string &currentPath,bool canAccessParent = false);
  90. /**
  91. * 遍历文件夹下的所有文件
  92. * @param path 文件夹路径
  93. * @param cb 回调对象 ,path为绝对路径,isDir为该路径是否为文件夹,返回true代表继续扫描,否则中断
  94. * @param enterSubdirectory 是否进入子目录扫描
  95. */
  96. static void scanDir(const string &path,const function<bool(const string &path,bool isDir)> &cb, bool enterSubdirectory = false);
  97. private:
  98. File();
  99. ~File();
  100. };
  101. } /* namespace toolkit */
  102. #endif /* SRC_UTIL_FILE_H_ */