diff --git a/src/base/system.cpp b/src/base/system.cpp index ae7a251ba..a163d49e4 100644 --- a/src/base/system.cpp +++ b/src/base/system.cpp @@ -2333,6 +2333,21 @@ int fs_removedir(const char *path) #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) { #if defined(CONF_FAMILY_WINDOWS) diff --git a/src/base/system.h b/src/base/system.h index 056f5752e..f4cac7a0d 100644 --- a/src/base/system.h +++ b/src/base/system.h @@ -1878,6 +1878,19 @@ int fs_makedir_rec_for(const char *path); */ 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.