%% 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_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_c8_exists(const char *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_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_c8_is_regular(const char *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_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_c8_is_directory(const char *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 DK4STAT8_H_INCLUDED
#include "dk4stat8.h"
#endif
#ifndef DK4STAT_H_INCLUDED
#include "dk4stat.h"
#endif
#endif
#include "dk4file8.h"
int
dk4file_c8_exists(const char *fn, dk4_er_t *erp)
{
#if DK4_ON_WINDOWS
DWORD dwattr;
HANDLE ha;
WIN32_FIND_DATAA ffdata;
#else
dk4_stat_t stb;
dk4_stat_t lstb;
#endif
int back = 0;
if (NULL != fn) {
#if DK4_ON_WINDOWS
dwattr = GetFileAttributesA(fn);
if (INVALID_FILE_ATTRIBUTES != dwattr) {
back = 1;
} else {
ha = FindFirstFileA(fn, &ffdata);
if (INVALID_HANDLE_VALUE != ha) {
back = 1;
FindClose(ha);
} else {
dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
}
}
#else
if (0 != dk4stat_c8(&stb, fn, NULL)) {
back = 1;
} else {
if (0 != dk4stat_l_c8(&lstb, fn, NULL)) {
back = 1;
} else {
dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
}
}
#endif
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
return back;
}
int
dk4file_c8_is_regular(const char *fn, dk4_er_t *erp)
{
#if DK4_ON_WINDOWS
DWORD dwattr;
HANDLE ha;
WIN32_FIND_DATAA ffdata;
#else
dk4_stat_t stb;
#endif
int back = 0;
if (NULL != fn) {
#if DK4_ON_WINDOWS
dwattr = GetFileAttributesA(fn);
if (INVALID_FILE_ATTRIBUTES != dwattr) {
if ((DWORD)0UL == (FILE_ATTRIBUTE_DIRECTORY & dwattr)) {
if ((DWORD)0UL == (FILE_ATTRIBUTE_DEVICE & dwattr)) {
back = 1;
}
}
} else {
ha = FindFirstFileA(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
if (0 != dk4stat_c8(&stb, fn, NULL)) {
if (0 != dk4stat_is_regular(&stb, erp)) {
back = 1;
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
}
#endif
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
return back;
}
int
dk4file_c8_is_directory(const char *fn, dk4_er_t *erp)
{
#if DK4_ON_WINDOWS
DWORD dwattr;
HANDLE ha;
WIN32_FIND_DATAA ffdata;
#else
dk4_stat_t stb;
#endif
int back = 0;
if (NULL != fn) {
#if DK4_ON_WINDOWS
dwattr = GetFileAttributesA(fn);
if (INVALID_FILE_ATTRIBUTES != dwattr) {
if ((DWORD)0UL != (FILE_ATTRIBUTE_DIRECTORY & dwattr)) {
back = 1;
}
} else {
ha = FindFirstFileA(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
if (0 != dk4stat_c8(&stb, fn, NULL)) {
if (0 != dk4stat_is_directory(&stb, erp)) {
back = 1;
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
}
#endif
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
return back;
}