%% options copyright owner = Dirk Krause copyright year = 2015-xxxx license = bsd %% header /** @file File checks. */ #ifndef DK4CONF_H_INCLUDED #include "dk4conf.h" #endif #ifndef DK4TYPES_H_INCLUDED #include "dk4types.h" #endif #ifndef DK4ERROR_H_INCLUDED #include "dk4error.h" #endif #ifdef __cplusplus extern "C" { #endif /** Check whether a path exists. CRT on Windows: Not used. @param fn File name. @param erp Error report, may be NULL. @return 1 if file exists, 0 otherwise. Note: The function also returns 0 if it fails to obtain information about a file, i.e. due to missing permissions... Error codes: - DK4_E_NOT_SUPPORTED
on non-Windows systems, - DK4_E_INVALID_ARGUMENTS
if fn is NULL, - DK4_E_NOT_FOUND
if the specified file was not found or retrieving information failed for other reasons. */ int dk4file_wc_exists(const wchar_t *fn, dk4_er_t *erp); /** Check whether a path name specifies a regular file. CRT on Windows: Not used. @param fn File name. @param erp Error report, may be NULL. @return 1 for existing regular file, 0 otherwise. Error codes: - DK4_E_NOT_SUPPORTED
on non-Windows systems, - DK4_E_INVALID_ARGUMENTS
if fn is NULL, - DK4_E_NOT_FOUND
if the file does not exist or retrieving the information failed for some reason, - none
if the path exists but is not a regular file. */ int dk4file_wc_is_regular(const wchar_t *fn, dk4_er_t *erp); /** Check whether a path name specifies a directory. CRT on Windows: Not used. @param fn File name. @param erp Error report, may be NULL. @return 1 for existing directory, 0 otherwise. Error codes: - DK4_E_NOT_SUPPORTED
on non-Windows systems, - DK4_E_INVALID_ARGUMENTS
if fn is NULL, - DK4_E_NOT_FOUND
if the file does not exist or retrieving the information failed for some reason, - none if the path exists but is not a directory. */ int dk4file_wc_is_directory(const wchar_t *fn, dk4_er_t *erp); #ifdef __cplusplus } #endif %% module #include "dk4conf.h" #include "dk4types.h" #if DK4_ON_WINDOWS #ifndef WINDOWS_H_INCLUDED #include #define WINDOWS_H_INCLUDED 1 #endif #else #ifndef DK4STATW_H_INCLUDED #include "dk4statw.h" #endif #endif #include "dk4filew.h" int dk4file_wc_exists(const wchar_t *fn, dk4_er_t *erp) { #if DK4_ON_WINDOWS DWORD dwattr; HANDLE ha; WIN32_FIND_DATAW ffdata; #endif int back = 0; #if DK4_ON_WINDOWS if (NULL != fn) { dwattr = GetFileAttributesW(fn); if (INVALID_FILE_ATTRIBUTES != dwattr) { back = 1; } else { ha = FindFirstFileW(fn, &ffdata); if (INVALID_HANDLE_VALUE != ha) { back = 1; FindClose(ha); } else { dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND); } } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } #else dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED); #endif return back; } int dk4file_wc_is_regular(const wchar_t *fn, dk4_er_t *erp) { #if DK4_ON_WINDOWS DWORD dwattr; HANDLE ha; WIN32_FIND_DATAW ffdata; int back = 0; if (NULL != fn) { dwattr = GetFileAttributesW(fn); if (INVALID_FILE_ATTRIBUTES != dwattr) { if ((DWORD)0UL == (FILE_ATTRIBUTE_DIRECTORY & dwattr)) { if ((DWORD)0UL == (FILE_ATTRIBUTE_DEVICE & dwattr)) { back = 1; } } } else { ha = FindFirstFileW(fn, &ffdata); if (INVALID_HANDLE_VALUE != ha) { dwattr = ffdata.dwFileAttributes; if ((DWORD)0UL == (FILE_ATTRIBUTE_DIRECTORY & dwattr)) { if ((DWORD)0UL == (FILE_ATTRIBUTE_DEVICE & dwattr)) { back = 1; } } FindClose(ha); } else { dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND); } } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } return back; #else dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED); return 0; #endif } int dk4file_wc_is_directory(const wchar_t *fn, dk4_er_t *erp) { #if DK4_ON_WINDOWS WIN32_FIND_DATAW ffdata; DWORD dwattr; HANDLE ha; int back = 0; if (NULL != fn) { dwattr = GetFileAttributesW(fn); if (INVALID_FILE_ATTRIBUTES != dwattr) { if ((DWORD)0UL != (FILE_ATTRIBUTE_DIRECTORY & dwattr)) { back = 1; } } else { ha = FindFirstFileW(fn, &ffdata); if (INVALID_HANDLE_VALUE != ha) { dwattr = ffdata.dwFileAttributes; if ((DWORD)0UL != (FILE_ATTRIBUTE_DIRECTORY & dwattr)) { back = 1; } FindClose(ha); } else { dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND); } } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } return back; #else dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED); return 0; #endif }