1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-27 17:28:41 +00:00

Add nodiscard attribute to helper functions where it makes sense

This commit is contained in:
sfan5 2025-03-26 21:56:09 +01:00
parent e6acc4e7ed
commit ae0f955a0e
10 changed files with 71 additions and 29 deletions

View file

@ -36,22 +36,23 @@ struct DirListNode
bool dir;
};
[[nodiscard]]
std::vector<DirListNode> GetDirListing(const std::string &path);
// Returns true if already exists
bool CreateDir(const std::string &path);
bool PathExists(const std::string &path);
[[nodiscard]] bool PathExists(const std::string &path);
bool IsPathAbsolute(const std::string &path);
[[nodiscard]] bool IsPathAbsolute(const std::string &path);
bool IsDir(const std::string &path);
[[nodiscard]] bool IsDir(const std::string &path);
bool IsExecutable(const std::string &path);
[[nodiscard]] bool IsExecutable(const std::string &path);
bool IsFile(const std::string &path);
[[nodiscard]] bool IsFile(const std::string &path);
inline bool IsDirDelimiter(char c)
[[nodiscard]] inline bool IsDirDelimiter(char c)
{
return c == '/' || c == DIR_DELIM_CHAR;
}
@ -64,20 +65,21 @@ bool DeleteSingleFileOrEmptyDirectory(const std::string &path);
/// Returns path to temp directory.
/// You probably don't want to use this directly, see `CreateTempFile` or `CreateTempDir`.
/// @return path or "" on error
std::string TempPath();
[[nodiscard]] std::string TempPath();
/// Returns path to securely-created temporary file (will already exist when this function returns).
/// @return path or "" on error
std::string CreateTempFile();
[[nodiscard]] std::string CreateTempFile();
/// Returns path to securely-created temporary directory (will already exist when this function returns).
/// @return path or "" on error
std::string CreateTempDir();
[[nodiscard]] std::string CreateTempDir();
/* Returns a list of subdirectories, including the path itself, but excluding
hidden directories (whose names start with . or _)
*/
void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir);
[[nodiscard]]
std::vector<std::string> GetRecursiveDirs(const std::string &dir);
/* Multiplatform */
@ -128,16 +130,19 @@ std::string RemoveRelativePathComponents(std::string path);
// Returns the absolute path for the passed path, with "." and ".." path
// components and symlinks removed. Returns "" on error.
[[nodiscard]]
std::string AbsolutePath(const std::string &path);
// This is a combination of RemoveRelativePathComponents() and AbsolutePath()
// It will resolve symlinks for the leading path components that exist and
// still remove "." and ".." in the rest of the path.
// Returns "" on error.
[[nodiscard]]
std::string AbsolutePathPartial(const std::string &path);
// Returns the filename from a path or the entire path if no directory
// delimiter is found.
[[nodiscard]]
const char *GetFilenameFromPath(const char *path);
// Replace the content of a file on disk in a way that is safe from
@ -180,6 +185,7 @@ bool OpenStream(std::filebuf &stream, const char *filename,
* @param mode additional mode bits (e.g. std::ios::app)
* @return file stream, will be !good in case of error
*/
[[nodiscard]]
inline std::ofstream open_ofstream(const char *name, bool log,
std::ios::openmode mode = std::ios::openmode())
{
@ -202,6 +208,7 @@ inline std::ofstream open_ofstream(const char *name, bool log,
* @param mode additional mode bits (e.g. std::ios::ate)
* @return file stream, will be !good in case of error
*/
[[nodiscard]]
inline std::ifstream open_ifstream(const char *name, bool log,
std::ios::openmode mode = std::ios::openmode())
{