Add fs_is_file to check if a given path is file

This commit is contained in:
Robert Müller 2023-01-22 16:50:10 +01:00
parent 7e9129cb44
commit b67a1f5ce5
2 changed files with 28 additions and 0 deletions

View file

@ -2333,6 +2333,21 @@ int fs_removedir(const char *path)
#endif #endif
} }
int fs_is_file(const char *path)
{
#if defined(CONF_FAMILY_WINDOWS)
WCHAR wPath[IO_MAX_PATH_LENGTH];
dbg_assert(MultiByteToWideChar(CP_UTF8, 0, path, -1, wPath, std::size(wPath)) > 0, "MultiByteToWideChar failure");
DWORD attributes = GetFileAttributesW(wPath);
return attributes != INVALID_FILE_ATTRIBUTES && !(attributes & FILE_ATTRIBUTE_DIRECTORY) ? 1 : 0;
#else
struct stat sb;
if(stat(path, &sb) == -1)
return 0;
return S_ISREG(sb.st_mode) ? 1 : 0;
#endif
}
int fs_is_dir(const char *path) int fs_is_dir(const char *path)
{ {
#if defined(CONF_FAMILY_WINDOWS) #if defined(CONF_FAMILY_WINDOWS)

View file

@ -1878,6 +1878,19 @@ int fs_makedir_rec_for(const char *path);
*/ */
int fs_storage_path(const char *appname, char *path, int max); int fs_storage_path(const char *appname, char *path, int max);
/**
* Checks if a file exists.
*
* @ingroup Filesystem
*
* @param path the path to check.
*
* @return 1 if a file with the given path exists,
* 0 on failure or if the file does not exist.
*
* @remark The strings are treated as zero-terminated strings.
*/
int fs_is_file(const char *path);
/** /**
* Checks if a folder exists. * Checks if a folder exists.