/* * category: [tool for file] * apply status: framework * edit status: * build status: * description: */ #ifndef __FILEUTIL_H__ #define __FILEUTIL_H__ #pragma once #include "config.h" #include #define SLASH '/' #define BACK_SLASH '\\' #define MB_SLASH_STR "/" #define MB_BACK_SLASH_STR "\\" #define W_SLASH_STR L"/" #define W_BACK_SLASH_STR L"\\" #if defined(UNICODE) || defined(_UNICODE) #define SLASH_STR W_SLASH_STR #define BACK_SLASH_STR W_BACK_SLASH_STR #else #define SLASH_STR MB_SLASH_STR #define BACK_SLASH_STR MB_BACK_SLASH_STR #endif #ifdef _WIN32 #define SPLIT_SLASH BACK_SLASH #define SPLIT_SLASH_STR BACK_SLASH_STR #define MB_SPLIT_SLASH_STR MB_BACK_SLASH_STR #define W_SPLIT_SLASH_STR W_BACK_SLASH_STR #define LINE_BREAK_STR "\r\n" #else #define SPLIT_SLASH SLASH #define SPLIT_SLASH_STR SLASH_STR #define MB_SPLIT_SLASH_STR MB_SLASH_STR #define W_SPLIT_SLASH_STR W_SLASH_STR #define LINE_BREAK_STR "\n" #endif #ifndef _WIN32 #ifndef _MAX_DRIVE #ifdef PATH_MAX #define _MAX_PATH PATH_MAX #else #define _MAX_PATH 260 #endif //PATH_MAX #define _MAX_DRIVE 32 /*default is /opt at linux*/ #define _MAX_DIR 256 #define _MAX_FNAME 256 #define _MAX_EXT 256 #endif #endif //NOT _WIN32 #ifdef __cplusplus extern "C" { #endif static __inline BOOL ExistsDirA(LPCSTR lpDirPath) { DWORD dwRet = GetFileAttributesA(lpDirPath); return (dwRet != INVALID_FILE_ATTRIBUTES) && (dwRet & FILE_ATTRIBUTE_DIRECTORY); } static __inline BOOL ExistsDirW(LPCWSTR lpDirPath) { DWORD dwRet = GetFileAttributesW(lpDirPath); return (dwRet != INVALID_FILE_ATTRIBUTES) && (dwRet & FILE_ATTRIBUTE_DIRECTORY); } #if defined(UNICODE) || defined(_UNICODE) #define ExistsDir ExistsDirW #else #define ExistsDir ExistsDirA #endif static __inline BOOL ExistsFileA(LPCSTR lpFilePath) { DWORD dwRet = GetFileAttributesA(lpFilePath); return (dwRet != INVALID_FILE_ATTRIBUTES) && !(dwRet & FILE_ATTRIBUTE_DIRECTORY); } static __inline BOOL ExistsFileW(LPCWSTR lpFilePath) { DWORD dwRet = GetFileAttributesW(lpFilePath); return (dwRet != INVALID_FILE_ATTRIBUTES) && !(dwRet & FILE_ATTRIBUTE_DIRECTORY); } #if defined(UNICODE) || defined(_UNICODE) #define ExistsFile ExistsFileW #else #define ExistsFile ExistsFileA #endif TOOLKIT_API BOOL RemoveFileA(LPCSTR pszFile); TOOLKIT_API BOOL RemoveFileReadOnlyAttributeA(LPCSTR pszFile); TOOLKIT_API BOOL CopyDirA(LPCSTR pszSourceDir, LPCSTR pszDestDir); TOOLKIT_API BOOL CreateDirA(LPCSTR lpDirPath, BOOL bRecursive); TOOLKIT_API BOOL CreateDirW(LPCWSTR lpDirPath, BOOL bRecursive); #if defined(UNICODE) || defined(_UNICODE) #define CreateDir CreateDirW #else #define CreateDir CreateDirA #endif static __inline BOOL CreateDirRecursiveA(LPCSTR lpDirPath) { return CreateDirA(lpDirPath, TRUE);} static __inline BOOL CreateDirRecursiveW(LPCWSTR lpDirPath) { return CreateDirW(lpDirPath, TRUE);} #if defined(UNICODE) || defined(_UNICODE) #define CreateDirRecursive CreateDirRecursiveW #else #define CreateDirRecursive CreateDirRecursiveA #endif TOOLKIT_API BOOL ClearDirRecursiveA(LPCSTR lpDirPath); TOOLKIT_API BOOL ClearDirRecursiveW(LPCWSTR lpDirPath); #if defined(UNICODE) || defined(_UNICODE) #define ClearDirRecursive ClearDirRecursiveW #else #define ClearDirRecursive ClearDirRecursiveA #endif TOOLKIT_API BOOL RemoveDirRecursiveA(LPCSTR lpDirPath); TOOLKIT_API BOOL RemoveDirRecursiveW(LPCWSTR lpDirPath); #if defined(UNICODE) || defined(_UNICODE) #define RemoveDirRecursive RemoveDirRecursiveW #else #define RemoveDirRecursive RemoveDirRecursiveA #endif TOOLKIT_API BOOL CreateParentDirA(LPCSTR lpPath, BOOL bRecursive); TOOLKIT_API BOOL CreateParentDirW(LPCWSTR lpPath, BOOL bRecursive); #if defined(UNICODE) || defined(_UNICODE) #define CreateParentDir CreateParentDirW #else #define CreateParentDir CreateParentDirA #endif static __inline HANDLE ExtCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) { if (!lpFileName) return INVALID_HANDLE_VALUE; if (lpFileName[0] != '\\') CreateParentDirA(lpFileName, TRUE); return CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); } static __inline HANDLE ExtCreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) { if (!lpFileName) return INVALID_HANDLE_VALUE; if (lpFileName[0] != '\\') CreateParentDirW(lpFileName, TRUE); return CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); } #if defined(UNICODE) || defined(_UNICODE) #define ExtCreateFile ExtCreateFileW #else #define ExtCreateFile ExtCreateFileA #endif typedef struct array_header_t array_header_t; TOOLKIT_API array_header_t *fileutil_get_sub_files_a(const char *path); TOOLKIT_API array_header_t *fileutil_get_sub_dirs_a(const char *path); TOOLKIT_API array_header_t *fileutil_get_sub_files_w(const wchar_t *path); TOOLKIT_API array_header_t *fileutil_get_sub_dirs_w(const wchar_t *path); TOOLKIT_API array_header_t *fileutil_get_sub_files2_a(const char *path, int limitation); TOOLKIT_API array_header_t *fileutil_get_sub_dirs2_a(const char *path, int limitation); TOOLKIT_API array_header_t *fileutil_get_sub_files2_w(const wchar_t *path, int limitation); TOOLKIT_API array_header_t *fileutil_get_sub_dirs2_w(const wchar_t *path, int limitation); #if defined(UNICODE) || defined(_UNICODE) #define fileutil_get_sub_files fileutil_get_sub_files_w #define fileutil_get_sub_dirs fileutil_get_sub_dirs_w #else #define fileutil_get_sub_files fileutil_get_sub_files_a #define fileutil_get_sub_dirs fileutil_get_sub_dirs_a #endif TOOLKIT_API FILE *fileutil_transaction_fopen(const char *filename, const char *mode); TOOLKIT_API int fileutil_transaction_fclose(const char *filename, FILE *fp); #ifndef _WIN32 TOOLKIT_API void _splitpath(const char* path, char* drive, char* dir, char* fname, char* ext); #endif //NOT _WIN32 #ifdef __cplusplus } // extern "C" { #endif #endif //__FILEUTIL_H__