fileutil.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * category: [tool for file]
  3. * apply status: framework
  4. * edit status:
  5. * build status:
  6. * description:
  7. */
  8. #ifndef __FILEUTIL_H__
  9. #define __FILEUTIL_H__
  10. #pragma once
  11. #include "config.h"
  12. #include <winpr/file.h>
  13. #define SLASH '/'
  14. #define BACK_SLASH '\\'
  15. #define MB_SLASH_STR "/"
  16. #define MB_BACK_SLASH_STR "\\"
  17. #define W_SLASH_STR L"/"
  18. #define W_BACK_SLASH_STR L"\\"
  19. #if defined(UNICODE) || defined(_UNICODE)
  20. #define SLASH_STR W_SLASH_STR
  21. #define BACK_SLASH_STR W_BACK_SLASH_STR
  22. #else
  23. #define SLASH_STR MB_SLASH_STR
  24. #define BACK_SLASH_STR MB_BACK_SLASH_STR
  25. #endif
  26. #ifdef _WIN32
  27. #define SPLIT_SLASH BACK_SLASH
  28. #define SPLIT_SLASH_STR BACK_SLASH_STR
  29. #define MB_SPLIT_SLASH_STR MB_BACK_SLASH_STR
  30. #define W_SPLIT_SLASH_STR W_BACK_SLASH_STR
  31. #define LINE_BREAK_STR "\r\n"
  32. #else
  33. #define SPLIT_SLASH SLASH
  34. #define SPLIT_SLASH_STR SLASH_STR
  35. #define MB_SPLIT_SLASH_STR MB_SLASH_STR
  36. #define W_SPLIT_SLASH_STR W_SLASH_STR
  37. #define LINE_BREAK_STR "\n"
  38. #endif
  39. #ifndef _WIN32
  40. #ifndef _MAX_DRIVE
  41. #ifdef PATH_MAX
  42. #define _MAX_PATH PATH_MAX
  43. #else
  44. #define _MAX_PATH 260
  45. #endif //PATH_MAX
  46. #define _MAX_DRIVE 32 /*default is /opt at linux*/
  47. #define _MAX_DIR 256
  48. #define _MAX_FNAME 256
  49. #define _MAX_EXT 256
  50. #endif
  51. #endif //NOT _WIN32
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. static __inline BOOL ExistsDirA(LPCSTR lpDirPath)
  56. {
  57. DWORD dwRet = GetFileAttributesA(lpDirPath);
  58. return (dwRet != INVALID_FILE_ATTRIBUTES) && (dwRet & FILE_ATTRIBUTE_DIRECTORY);
  59. }
  60. static __inline BOOL ExistsDirW(LPCWSTR lpDirPath)
  61. {
  62. DWORD dwRet = GetFileAttributesW(lpDirPath);
  63. return (dwRet != INVALID_FILE_ATTRIBUTES) && (dwRet & FILE_ATTRIBUTE_DIRECTORY);
  64. }
  65. #if defined(UNICODE) || defined(_UNICODE)
  66. #define ExistsDir ExistsDirW
  67. #else
  68. #define ExistsDir ExistsDirA
  69. #endif
  70. static __inline BOOL ExistsFileA(LPCSTR lpFilePath)
  71. {
  72. DWORD dwRet = GetFileAttributesA(lpFilePath);
  73. return (dwRet != INVALID_FILE_ATTRIBUTES) && !(dwRet & FILE_ATTRIBUTE_DIRECTORY);
  74. }
  75. static __inline BOOL ExistsFileW(LPCWSTR lpFilePath)
  76. {
  77. DWORD dwRet = GetFileAttributesW(lpFilePath);
  78. return (dwRet != INVALID_FILE_ATTRIBUTES) && !(dwRet & FILE_ATTRIBUTE_DIRECTORY);
  79. }
  80. #if defined(UNICODE) || defined(_UNICODE)
  81. #define ExistsFile ExistsFileW
  82. #else
  83. #define ExistsFile ExistsFileA
  84. #endif
  85. TOOLKIT_API BOOL RemoveFileA(LPCSTR pszFile);
  86. TOOLKIT_API BOOL RemoveFileReadOnlyAttributeA(LPCSTR pszFile);
  87. TOOLKIT_API BOOL CopyDirA(LPCSTR pszSourceDir, LPCSTR pszDestDir);
  88. TOOLKIT_API BOOL CreateDirA(LPCSTR lpDirPath, BOOL bRecursive);
  89. TOOLKIT_API BOOL CreateDirW(LPCWSTR lpDirPath, BOOL bRecursive);
  90. #if defined(UNICODE) || defined(_UNICODE)
  91. #define CreateDir CreateDirW
  92. #else
  93. #define CreateDir CreateDirA
  94. #endif
  95. static __inline BOOL CreateDirRecursiveA(LPCSTR lpDirPath) { return CreateDirA(lpDirPath, TRUE);}
  96. static __inline BOOL CreateDirRecursiveW(LPCWSTR lpDirPath) { return CreateDirW(lpDirPath, TRUE);}
  97. #if defined(UNICODE) || defined(_UNICODE)
  98. #define CreateDirRecursive CreateDirRecursiveW
  99. #else
  100. #define CreateDirRecursive CreateDirRecursiveA
  101. #endif
  102. TOOLKIT_API BOOL ClearDirRecursiveA(LPCSTR lpDirPath);
  103. TOOLKIT_API BOOL ClearDirRecursiveW(LPCWSTR lpDirPath);
  104. #if defined(UNICODE) || defined(_UNICODE)
  105. #define ClearDirRecursive ClearDirRecursiveW
  106. #else
  107. #define ClearDirRecursive ClearDirRecursiveA
  108. #endif
  109. TOOLKIT_API BOOL RemoveDirRecursiveA(LPCSTR lpDirPath);
  110. TOOLKIT_API BOOL RemoveDirRecursiveW(LPCWSTR lpDirPath);
  111. #if defined(UNICODE) || defined(_UNICODE)
  112. #define RemoveDirRecursive RemoveDirRecursiveW
  113. #else
  114. #define RemoveDirRecursive RemoveDirRecursiveA
  115. #endif
  116. TOOLKIT_API BOOL CreateParentDirA(LPCSTR lpPath, BOOL bRecursive);
  117. TOOLKIT_API BOOL CreateParentDirW(LPCWSTR lpPath, BOOL bRecursive);
  118. #if defined(UNICODE) || defined(_UNICODE)
  119. #define CreateParentDir CreateParentDirW
  120. #else
  121. #define CreateParentDir CreateParentDirA
  122. #endif
  123. static __inline HANDLE ExtCreateFileA(LPCSTR lpFileName,
  124. DWORD dwDesiredAccess,
  125. DWORD dwShareMode,
  126. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  127. DWORD dwCreationDisposition,
  128. DWORD dwFlagsAndAttributes,
  129. HANDLE hTemplateFile)
  130. {
  131. if (!lpFileName)
  132. return INVALID_HANDLE_VALUE;
  133. if (lpFileName[0] != '\\')
  134. CreateParentDirA(lpFileName, TRUE);
  135. return CreateFileA(lpFileName,
  136. dwDesiredAccess,
  137. dwShareMode,
  138. lpSecurityAttributes,
  139. dwCreationDisposition,
  140. dwFlagsAndAttributes,
  141. hTemplateFile);
  142. }
  143. static __inline HANDLE ExtCreateFileW(LPCWSTR lpFileName,
  144. DWORD dwDesiredAccess,
  145. DWORD dwShareMode,
  146. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  147. DWORD dwCreationDisposition,
  148. DWORD dwFlagsAndAttributes,
  149. HANDLE hTemplateFile)
  150. {
  151. if (!lpFileName)
  152. return INVALID_HANDLE_VALUE;
  153. if (lpFileName[0] != '\\')
  154. CreateParentDirW(lpFileName, TRUE);
  155. return CreateFileW(lpFileName,
  156. dwDesiredAccess,
  157. dwShareMode,
  158. lpSecurityAttributes,
  159. dwCreationDisposition,
  160. dwFlagsAndAttributes,
  161. hTemplateFile);
  162. }
  163. #if defined(UNICODE) || defined(_UNICODE)
  164. #define ExtCreateFile ExtCreateFileW
  165. #else
  166. #define ExtCreateFile ExtCreateFileA
  167. #endif
  168. typedef struct array_header_t array_header_t;
  169. TOOLKIT_API array_header_t *fileutil_get_sub_files_a(const char *path);
  170. TOOLKIT_API array_header_t *fileutil_get_sub_dirs_a(const char *path);
  171. TOOLKIT_API array_header_t *fileutil_get_sub_files_w(const wchar_t *path);
  172. TOOLKIT_API array_header_t *fileutil_get_sub_dirs_w(const wchar_t *path);
  173. TOOLKIT_API array_header_t *fileutil_get_sub_files2_a(const char *path, int limitation);
  174. TOOLKIT_API array_header_t *fileutil_get_sub_dirs2_a(const char *path, int limitation);
  175. TOOLKIT_API array_header_t *fileutil_get_sub_files2_w(const wchar_t *path, int limitation);
  176. TOOLKIT_API array_header_t *fileutil_get_sub_dirs2_w(const wchar_t *path, int limitation);
  177. #if defined(UNICODE) || defined(_UNICODE)
  178. #define fileutil_get_sub_files fileutil_get_sub_files_w
  179. #define fileutil_get_sub_dirs fileutil_get_sub_dirs_w
  180. #else
  181. #define fileutil_get_sub_files fileutil_get_sub_files_a
  182. #define fileutil_get_sub_dirs fileutil_get_sub_dirs_a
  183. #endif
  184. TOOLKIT_API FILE *fileutil_transaction_fopen(const char *filename, const char *mode);
  185. TOOLKIT_API int fileutil_transaction_fclose(const char *filename, FILE *fp);
  186. #ifndef _WIN32
  187. TOOLKIT_API void _splitpath(const char* path, char* drive, char* dir, char* fname, char* ext);
  188. #endif //NOT _WIN32
  189. #ifdef __cplusplus
  190. } // extern "C" {
  191. #endif
  192. #endif //__FILEUTIL_H__