noh.h: noh_file_exists and some documentation

This commit is contained in:
2026-06-17 17:34:07 +02:00
parent 998c0bd994
commit 0894243a43

View File

@@ -343,6 +343,9 @@ void noh_string_append_null(Noh_String *string);
// Resets a Noh_String, setting the count to 0. // Resets a Noh_String, setting the count to 0.
#define noh_string_reset(string) noh_da_reset(string) #define noh_string_reset(string) noh_da_reset(string)
// Returns true if a file or directory exists, false otherwise.
bool noh_file_exists(const char *filename);
// Reads the contents of a file into a Noh_String. // Reads the contents of a file into a Noh_String.
bool noh_string_read_file(Noh_String *string, const char *filename); bool noh_string_read_file(Noh_String *string, const char *filename);
@@ -368,12 +371,14 @@ void noh_string_append_sv(Noh_String *string, Noh_String_View sv);
void noh_sv_increase_position(Noh_String_View *sv, size_t distance); void noh_sv_increase_position(Noh_String_View *sv, size_t distance);
// Finds the first occurrence of the specified delimiter in a string view and returns the part of the string until // Finds the first occurrence of the specified delimiter in a string view and returns the part of the string until
// that delimiter. The string view itself is shrunk to start after the delimiter. // that delimiter. The input string view itself is shrunk to start after the delimiter.
// If the delimiter is not found, the result is the entire string and the input string view is empty.
Noh_String_View noh_sv_chop_by_delim(Noh_String_View *sv, char delim); Noh_String_View noh_sv_chop_by_delim(Noh_String_View *sv, char delim);
// Finds the first occurrence of a line separator in a string view and returns the part of the string until // Finds the first occurrence of a line separator in a string view and returns the part of the string until
// that separator. The string view itself is shrunk to start after the separator. // that separator. The string view itself is shrunk to start after the separator.
// Supports '/r', '/n' and '/r/n'. // Supports '/r', '/n' and '/r/n'.
// If no line separator is found, the entire string is returned and the input string is empty.
Noh_String_View noh_sv_chop_line(Noh_String_View *sv); Noh_String_View noh_sv_chop_line(Noh_String_View *sv);
// Chops a string while a predicate matches. // Chops a string while a predicate matches.
@@ -842,6 +847,11 @@ void noh_string_append_null(Noh_String *string) {
noh_da_append(string, '\0'); noh_da_append(string, '\0');
} }
bool noh_file_exists(const char *filename) {
struct stat st;
return (stat(filename, &st)) == 0;
}
bool noh_string_read_file(Noh_String *string, const char *filename) { bool noh_string_read_file(Noh_String *string, const char *filename) {
bool result = true; bool result = true;
size_t buf_size = 32*1024; size_t buf_size = 32*1024;